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