"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const ios_plugins_1 = require("../plugins/ios-plugins"); const Paths_1 = require("./Paths"); const Xcodeproj_1 = require("./utils/Xcodeproj"); const templateBridgingHeader = `// // Use this file to import your target's public headers that you would like to expose to Swift. // `; /** * Ensure a Swift bridging header is created for the project. * This helps fix problems related to using modules that are written in Swift (lottie, FBSDK). * * 1. Ensures the file exists given the project path. * 2. Writes the file and links to Xcode as a resource file. * 3. Sets the build configuration `SWIFT_OBJC_BRIDGING_HEADER = [PROJECT_NAME]-Bridging-Header.h` */ exports.withSwiftBridgingHeader = config => { return ios_plugins_1.withXcodeProject(config, config => { config.modResults = ensureSwiftBridgingHeaderSetup({ project: config.modResults, projectRoot: config.modRequest.projectRoot, }); return config; }); }; function ensureSwiftBridgingHeaderSetup({ projectRoot, project, }) { // Only create a bridging header if using objective-c if (shouldCreateSwiftBridgingHeader({ projectRoot, project })) { const projectName = Xcodeproj_1.getProjectName(projectRoot); const bridgingHeader = createBridgingHeaderFileName(projectName); // Ensure a bridging header is created in the Xcode project. project = createBridgingHeaderFile({ project, projectName, projectRoot, bridgingHeader, }); // Designate the newly created file as the Swift bridging header in the Xcode project. project = linkBridgingHeaderFile({ project, bridgingHeader: path_1.default.join(projectName, bridgingHeader), }); } return project; } exports.ensureSwiftBridgingHeaderSetup = ensureSwiftBridgingHeaderSetup; function shouldCreateSwiftBridgingHeader({ projectRoot, project, }) { // Only create a bridging header if the project is using in Objective C (AppDelegate is written in Objc). const isObjc = Paths_1.getAppDelegate(projectRoot).language === 'objc'; return isObjc && !getDesignatedSwiftBridgingHeaderFileReference({ project }); } /** * @returns String matching the default name used when Xcode automatically creates a bridging header file. */ function createBridgingHeaderFileName(projectName) { return `${projectName}-Bridging-Header.h`; } function getDesignatedSwiftBridgingHeaderFileReference({ project, }) { const configurations = project.pbxXCBuildConfigurationSection(); // @ts-ignore for (const { buildSettings } of Object.values(configurations || {})) { // Guessing that this is the best way to emulate Xcode. // Using `project.addToBuildSettings` modifies too many targets. if (typeof (buildSettings === null || buildSettings === void 0 ? void 0 : buildSettings.PRODUCT_NAME) !== 'undefined') { if (typeof buildSettings.SWIFT_OBJC_BRIDGING_HEADER === 'string' && buildSettings.SWIFT_OBJC_BRIDGING_HEADER) { return buildSettings.SWIFT_OBJC_BRIDGING_HEADER; } } } return null; } exports.getDesignatedSwiftBridgingHeaderFileReference = getDesignatedSwiftBridgingHeaderFileReference; /** * * @param bridgingHeader The bridging header filename ex: `ExpoAPIs-Bridging-Header.h` * @returns */ function linkBridgingHeaderFile({ project, bridgingHeader, }) { const configurations = project.pbxXCBuildConfigurationSection(); // @ts-ignore for (const { buildSettings } of Object.values(configurations || {})) { // Guessing that this is the best way to emulate Xcode. // Using `project.addToBuildSettings` modifies too many targets. if (typeof (buildSettings === null || buildSettings === void 0 ? void 0 : buildSettings.PRODUCT_NAME) !== 'undefined') { buildSettings.SWIFT_OBJC_BRIDGING_HEADER = bridgingHeader; } } return project; } exports.linkBridgingHeaderFile = linkBridgingHeaderFile; function createBridgingHeaderFile({ projectRoot, projectName, project, bridgingHeader, }) { const bridgingHeaderProjectPath = path_1.default.join(Paths_1.getSourceRoot(projectRoot), bridgingHeader); if (!fs_extra_1.default.existsSync(bridgingHeaderProjectPath)) { // Create the file fs_extra_1.default.writeFileSync(bridgingHeaderProjectPath, templateBridgingHeader, 'utf8'); } // This is non-standard, Xcode generates the bridging header in `/ios` which is kinda annoying. // Instead, this'll generate the default header in the application code folder `/ios/myproject/`. const filePath = `${projectName}/${bridgingHeader}`; // Ensure the file is linked with Xcode resource files if (!project.hasFile(filePath)) { project = Xcodeproj_1.addResourceFileToGroup({ filepath: filePath, groupName: projectName, project, // Not sure why, but this is how Xcode generates it. isBuildFile: false, }); } return project; } exports.createBridgingHeaderFile = createBridgingHeaderFile; //# sourceMappingURL=SwiftBridgingHeader.js.map