|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | +const xcode = require('xcode'); |
| 4 | + |
| 5 | +async function initIos() { |
| 6 | + console.log('log: Running iOS setup...'); |
| 7 | + const projectDir = path.join(process.cwd(), 'ios'); |
| 8 | + const files = fs.readdirSync(projectDir); |
| 9 | + const xcodeprojFile = files.find(file => file.endsWith('.xcodeproj')); |
| 10 | + if (!xcodeprojFile) { |
| 11 | + console.log('log: Could not find .xcodeproj file in ios directory'); |
| 12 | + return; |
| 13 | + } |
| 14 | + const projectName = xcodeprojFile.replace('.xcodeproj', ''); |
| 15 | + const appDelegatePath = findAppDelegate(path.join(projectDir, projectName)); |
| 16 | + |
| 17 | + if (!appDelegatePath) { |
| 18 | + console.log('log: Could not find AppDelegate file'); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + if (appDelegatePath.endsWith('.swift')) { |
| 23 | + await setupSwift(appDelegatePath, projectDir, projectName); |
| 24 | + } else { |
| 25 | + await setupObjectiveC(appDelegatePath); |
| 26 | + } |
| 27 | + |
| 28 | + console.log('log: Please run `cd ios && pod install` to complete the setup.'); |
| 29 | +} |
| 30 | + |
| 31 | +function findAppDelegate(searchPath) { |
| 32 | + if (!fs.existsSync(searchPath)) return null; |
| 33 | + const files = fs.readdirSync(searchPath); |
| 34 | + const appDelegateFile = files.find(file => file.startsWith('AppDelegate') && (file.endsWith('.m') || file.endsWith('.mm') || file.endsWith('.swift'))); |
| 35 | + return appDelegateFile ? path.join(searchPath, appDelegateFile) : null; |
| 36 | +} |
| 37 | + |
| 38 | +function modifyObjectiveCAppDelegate(appDelegateContent) { |
| 39 | + const IMPORT_STATEMENT = '#import <CodePush/CodePush.h>'; |
| 40 | + if (appDelegateContent.includes(IMPORT_STATEMENT)) { |
| 41 | + console.log('log: AppDelegate already has CodePush imported.'); |
| 42 | + return appDelegateContent; |
| 43 | + } |
| 44 | + |
| 45 | + return appDelegateContent |
| 46 | + .replace('#import "AppDelegate.h"\n', `#import "AppDelegate.h"\n${IMPORT_STATEMENT}\n`) |
| 47 | + .replace('[[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];', '[CodePush bundleURL];'); |
| 48 | +} |
| 49 | + |
| 50 | +function modifySwiftAppDelegate(appDelegateContent) { |
| 51 | + const CODEPUSH_CALL_STATEMENT = 'CodePush.bundleURL()'; |
| 52 | + if (appDelegateContent.includes(CODEPUSH_CALL_STATEMENT)) { |
| 53 | + console.log('log: AppDelegate.swift already configured for CodePush.'); |
| 54 | + return appDelegateContent; |
| 55 | + } |
| 56 | + |
| 57 | + return appDelegateContent |
| 58 | + .replace('Bundle.main.url(forResource: "main", withExtension: "jsbundle")', CODEPUSH_CALL_STATEMENT); |
| 59 | +} |
| 60 | + |
| 61 | +async function setupObjectiveC(appDelegatePath) { |
| 62 | + const appDelegateContent = fs.readFileSync(appDelegatePath, 'utf-8'); |
| 63 | + const newContent = modifyObjectiveCAppDelegate(appDelegateContent); |
| 64 | + fs.writeFileSync(appDelegatePath, newContent); |
| 65 | + console.log('log: Successfully updated AppDelegate.m/mm.'); |
| 66 | +} |
| 67 | + |
| 68 | +async function setupSwift(appDelegatePath, projectDir, projectName) { |
| 69 | + const bridgingHeaderPath = await ensureBridgingHeader(projectDir, projectName); |
| 70 | + if (!bridgingHeaderPath) { |
| 71 | + console.log('log: Failed to create or find bridging header.'); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const appDelegateContent = fs.readFileSync(appDelegatePath, 'utf-8'); |
| 76 | + const newContent = modifySwiftAppDelegate(appDelegateContent); |
| 77 | + fs.writeFileSync(appDelegatePath, newContent); |
| 78 | + console.log('log: Successfully updated AppDelegate.swift.'); |
| 79 | +} |
| 80 | + |
| 81 | +async function ensureBridgingHeader(projectDir, projectName) { |
| 82 | + const projectPath = path.join(projectDir, `${projectName}.xcodeproj`, 'project.pbxproj'); |
| 83 | + const myProj = xcode.project(projectPath); |
| 84 | + |
| 85 | + return new Promise((resolve, reject) => { |
| 86 | + myProj.parse(function (err) { |
| 87 | + if (err) { |
| 88 | + console.error(`Error parsing Xcode project: ${err}`); |
| 89 | + return reject(err); |
| 90 | + } |
| 91 | + |
| 92 | + const bridgingHeaderRelativePath = `${projectName}/${projectName}-Bridging-Header.h`; |
| 93 | + const bridgingHeaderAbsolutePath = path.join(projectDir, bridgingHeaderRelativePath); |
| 94 | + |
| 95 | + const configurations = myProj.pbxXCBuildConfigurationSection(); |
| 96 | + for (const name in configurations) { |
| 97 | + const config = configurations[name]; |
| 98 | + if (config.buildSettings) { |
| 99 | + config.buildSettings.SWIFT_OBJC_BRIDGING_HEADER = `"${bridgingHeaderRelativePath}"`; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (!fs.existsSync(bridgingHeaderAbsolutePath)) { |
| 104 | + fs.mkdirSync(path.dirname(bridgingHeaderAbsolutePath), { recursive: true }); |
| 105 | + fs.writeFileSync(bridgingHeaderAbsolutePath, '#import <CodePush/CodePush.h>\n'); |
| 106 | + console.log(`log: Created bridging header at ${bridgingHeaderAbsolutePath}`); |
| 107 | + const groupKey = myProj.findPBXGroupKey({ name: projectName }); |
| 108 | + myProj.addHeaderFile(bridgingHeaderRelativePath, { public: true }, groupKey); |
| 109 | + } else { |
| 110 | + const headerContent = fs.readFileSync(bridgingHeaderAbsolutePath, 'utf-8'); |
| 111 | + if (!headerContent.includes('#import <CodePush/CodePush.h>')) { |
| 112 | + fs.appendFileSync(bridgingHeaderAbsolutePath, '\n#import <CodePush/CodePush.h>\n'); |
| 113 | + console.log(`log: Updated bridging header at ${bridgingHeaderAbsolutePath}`); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + fs.writeFileSync(projectPath, myProj.writeSync()); |
| 118 | + console.log('log: Updated Xcode project with bridging header path.'); |
| 119 | + resolve(bridgingHeaderAbsolutePath); |
| 120 | + }); |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = { |
| 125 | + initIos: initIos, |
| 126 | + modifyObjectiveCAppDelegate: modifyObjectiveCAppDelegate, |
| 127 | + modifySwiftAppDelegate: modifySwiftAppDelegate, |
| 128 | +} |
0 commit comments