1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.createBridgingHeaderFile = createBridgingHeaderFile;
7exports.ensureSwiftBridgingHeaderSetup = ensureSwiftBridgingHeaderSetup;
8exports.getDesignatedSwiftBridgingHeaderFileReference = getDesignatedSwiftBridgingHeaderFileReference;
9exports.linkBridgingHeaderFile = linkBridgingHeaderFile;
10exports.withSwiftBridgingHeader = exports.withNoopSwiftFile = void 0;
11function _fs() {
12  const data = _interopRequireDefault(require("fs"));
13  _fs = function () {
14    return data;
15  };
16  return data;
17}
18function _path() {
19  const data = _interopRequireDefault(require("path"));
20  _path = function () {
21    return data;
22  };
23  return data;
24}
25function _Paths() {
26  const data = require("./Paths");
27  _Paths = function () {
28    return data;
29  };
30  return data;
31}
32function _XcodeProjectFile() {
33  const data = require("./XcodeProjectFile");
34  _XcodeProjectFile = function () {
35    return data;
36  };
37  return data;
38}
39function _Xcodeproj() {
40  const data = require("./utils/Xcodeproj");
41  _Xcodeproj = function () {
42    return data;
43  };
44  return data;
45}
46function _iosPlugins() {
47  const data = require("../plugins/ios-plugins");
48  _iosPlugins = function () {
49    return data;
50  };
51  return data;
52}
53function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
54const templateBridgingHeader = `//
55//  Use this file to import your target's public headers that you would like to expose to Swift.
56//
57`;
58
59/**
60 * Ensure a Swift bridging header is created for the project.
61 * This helps fix problems related to using modules that are written in Swift (lottie, FBSDK).
62 *
63 * 1. Ensures the file exists given the project path.
64 * 2. Writes the file and links to Xcode as a resource file.
65 * 3. Sets the build configuration `SWIFT_OBJC_BRIDGING_HEADER = [PROJECT_NAME]-Bridging-Header.h`
66 */
67const withSwiftBridgingHeader = config => {
68  return (0, _iosPlugins().withXcodeProject)(config, config => {
69    config.modResults = ensureSwiftBridgingHeaderSetup({
70      project: config.modResults,
71      projectRoot: config.modRequest.projectRoot
72    });
73    return config;
74  });
75};
76exports.withSwiftBridgingHeader = withSwiftBridgingHeader;
77function ensureSwiftBridgingHeaderSetup({
78  projectRoot,
79  project
80}) {
81  // Only create a bridging header if using objective-c
82  if (shouldCreateSwiftBridgingHeader({
83    projectRoot,
84    project
85  })) {
86    const projectName = (0, _Xcodeproj().getProjectName)(projectRoot);
87    const bridgingHeader = createBridgingHeaderFileName(projectName);
88    // Ensure a bridging header is created in the Xcode project.
89    project = createBridgingHeaderFile({
90      project,
91      projectName,
92      projectRoot,
93      bridgingHeader
94    });
95    // Designate the newly created file as the Swift bridging header in the Xcode project.
96    project = linkBridgingHeaderFile({
97      project,
98      bridgingHeader: _path().default.join(projectName, bridgingHeader)
99    });
100  }
101  return project;
102}
103function shouldCreateSwiftBridgingHeader({
104  projectRoot,
105  project
106}) {
107  // Only create a bridging header if the project is using in Objective C (AppDelegate is written in Objc).
108  const isObjc = (0, _Paths().getAppDelegate)(projectRoot).language !== 'swift';
109  return isObjc && !getDesignatedSwiftBridgingHeaderFileReference({
110    project
111  });
112}
113
114/**
115 * @returns String matching the default name used when Xcode automatically creates a bridging header file.
116 */
117function createBridgingHeaderFileName(projectName) {
118  return `${projectName}-Bridging-Header.h`;
119}
120function getDesignatedSwiftBridgingHeaderFileReference({
121  project
122}) {
123  const configurations = project.pbxXCBuildConfigurationSection();
124  // @ts-ignore
125  for (const {
126    buildSettings
127  } of Object.values(configurations || {})) {
128    // Guessing that this is the best way to emulate Xcode.
129    // Using `project.addToBuildSettings` modifies too many targets.
130    if (typeof (buildSettings === null || buildSettings === void 0 ? void 0 : buildSettings.PRODUCT_NAME) !== 'undefined') {
131      if (typeof buildSettings.SWIFT_OBJC_BRIDGING_HEADER === 'string' && buildSettings.SWIFT_OBJC_BRIDGING_HEADER) {
132        return buildSettings.SWIFT_OBJC_BRIDGING_HEADER;
133      }
134    }
135  }
136  return null;
137}
138
139/**
140 *
141 * @param bridgingHeader The bridging header filename ex: `ExpoAPIs-Bridging-Header.h`
142 * @returns
143 */
144function linkBridgingHeaderFile({
145  project,
146  bridgingHeader
147}) {
148  const configurations = project.pbxXCBuildConfigurationSection();
149  // @ts-ignore
150  for (const {
151    buildSettings
152  } of Object.values(configurations || {})) {
153    // Guessing that this is the best way to emulate Xcode.
154    // Using `project.addToBuildSettings` modifies too many targets.
155    if (typeof (buildSettings === null || buildSettings === void 0 ? void 0 : buildSettings.PRODUCT_NAME) !== 'undefined') {
156      buildSettings.SWIFT_OBJC_BRIDGING_HEADER = bridgingHeader;
157    }
158  }
159  return project;
160}
161function createBridgingHeaderFile({
162  projectRoot,
163  projectName,
164  project,
165  bridgingHeader
166}) {
167  const bridgingHeaderProjectPath = _path().default.join((0, _Paths().getSourceRoot)(projectRoot), bridgingHeader);
168  if (!_fs().default.existsSync(bridgingHeaderProjectPath)) {
169    // Create the file
170    _fs().default.writeFileSync(bridgingHeaderProjectPath, templateBridgingHeader, 'utf8');
171  }
172
173  // This is non-standard, Xcode generates the bridging header in `/ios` which is kinda annoying.
174  // Instead, this'll generate the default header in the application code folder `/ios/myproject/`.
175  const filePath = `${projectName}/${bridgingHeader}`;
176  // Ensure the file is linked with Xcode resource files
177  if (!project.hasFile(filePath)) {
178    project = (0, _Xcodeproj().addResourceFileToGroup)({
179      filepath: filePath,
180      groupName: projectName,
181      project,
182      // Not sure why, but this is how Xcode generates it.
183      isBuildFile: false,
184      verbose: false
185    });
186  }
187  return project;
188}
189const withNoopSwiftFile = config => {
190  return (0, _XcodeProjectFile().withBuildSourceFile)(config, {
191    filePath: 'noop-file.swift',
192    contents: ['//', '// @generated', '// A blank Swift file must be created for native modules with Swift files to work correctly.', '//', ''].join('\n')
193  });
194};
195exports.withNoopSwiftFile = withNoopSwiftFile;
196//# sourceMappingURL=Swift.js.map