UNPKG

expo-plugin-ios-static-libraries

Version:

Expo Config Plugin to set specific iOS libraries to use static build type

129 lines (128 loc) 4.98 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.patchPodfile = patchPodfile; const config_plugins_1 = require("@expo/config-plugins"); const fs = __importStar(require("fs")); const path_1 = __importDefault(require("path")); const pluginComment = "# Added by expo-plugin-ios-static-libraries"; /** * Patches a Podfile string to add static library configuration for specified libraries * This function is exported for testing purposes */ function patchPodfile(podfileContent, libraries = []) { // If there is already a match for this plugins comment, skip adding. if (podfileContent.indexOf(pluginComment) !== -1) { return podfileContent; } // Create condition for libraries let libraryConditions = ''; if (libraries.length > 0) { libraryConditions = libraries .map(lib => `pod.name.eql?('${lib}')`) .join(' || '); } else { // If no libraries specified, use a condition that's always false // This ensures the code is syntactically valid but won't affect any libraries libraryConditions = 'false'; } // Check if pre_install block already exists const preInstallRegex = /pre_install\s+do\s+\|installer\|([\s\S]*?)end/; const preInstallMatch = podfileContent.match(preInstallRegex); let newPodfileContent = podfileContent; if (preInstallMatch) { // There's an existing pre_install block, add our code to the end of it const existingContent = preInstallMatch[0]; const insertPoint = existingContent.lastIndexOf('end'); const codeToAdd = ` ${pluginComment} installer.pod_targets.each do |pod| if ${libraryConditions} def pod.build_type Pod::BuildType.static_library end end end `; // Insert our code before the last 'end' const newContent = existingContent.slice(0, insertPoint) + codeToAdd + existingContent.slice(insertPoint); newPodfileContent = podfileContent.replace(existingContent, newContent); } else { // No existing pre_install block, create a new one const preInstallBlock = ` ${pluginComment} pre_install do |installer| installer.pod_targets.each do |pod| if ${libraryConditions} def pod.build_type Pod::BuildType.static_library end end end end `; // Append the pre_install hook to the end of the Podfile newPodfileContent += `\n${preInstallBlock}\n`; } return newPodfileContent; } /** * Config plugin that adds pre_install hook to the iOS Podfile to set * specific libraries to use static build type */ const withIosStaticLibraries = (config, { libraries = [] }) => { return (0, config_plugins_1.withDangerousMod)(config, [ 'ios', async (config) => { const { platformProjectRoot } = config.modRequest; const podfilePath = path_1.default.join(platformProjectRoot, 'Podfile'); if (fs.existsSync(podfilePath)) { const podfileContent = fs.readFileSync(podfilePath, 'utf-8'); const newPodfileContent = patchPodfile(podfileContent, libraries); // Only write the file if changes were made if (newPodfileContent !== podfileContent) { fs.writeFileSync(podfilePath, newPodfileContent); } } return config; }, ]); }; exports.default = withIosStaticLibraries;