|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const { withDangerousMod, withXcodeProject } = require('@expo/config-plugins'); |
| 5 | + |
| 6 | +const { isExpo53OrLater } = require('../expo-version'); |
| 7 | + |
| 8 | +const BRIDGING_HEADER_NAME = 'AppDelegate+RNAppAuth.h'; |
| 9 | +const BRIDGING_HEADER_CONTENT = '#import "RNAppAuthAuthorizationFlowManager.h"\n'; |
| 10 | + |
| 11 | +const findBridgingHeader = dir => { |
| 12 | + const files = fs.readdirSync(dir); |
| 13 | + |
| 14 | + // First check current directory |
| 15 | + const headerInCurrentDir = files.find(f => f.endsWith('-Bridging-Header.h') || f.endsWith('.h')); |
| 16 | + if (headerInCurrentDir) { |
| 17 | + return path.join(dir, headerInCurrentDir); |
| 18 | + } |
| 19 | + |
| 20 | + // Then check subdirectories |
| 21 | + for (const file of files) { |
| 22 | + const fullPath = path.join(dir, file); |
| 23 | + if (fs.statSync(fullPath).isDirectory()) { |
| 24 | + const found = findBridgingHeader(fullPath); |
| 25 | + if (found) { |
| 26 | + return found; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return null; |
| 32 | +}; |
| 33 | + |
| 34 | +const withBridgingHeader = rootConfig => { |
| 35 | + if (!isExpo53OrLater(rootConfig)) { |
| 36 | + return rootConfig; |
| 37 | + } |
| 38 | + |
| 39 | + return withDangerousMod(rootConfig, [ |
| 40 | + 'ios', |
| 41 | + config => { |
| 42 | + const iosPath = path.join(config.modRequest.projectRoot, 'ios'); |
| 43 | + |
| 44 | + // Search for existing bridging header in the project and subfolders |
| 45 | + const existingHeaderPath = findBridgingHeader(iosPath); |
| 46 | + const importLine = BRIDGING_HEADER_CONTENT; |
| 47 | + let headerPath; |
| 48 | + |
| 49 | + if (existingHeaderPath) { |
| 50 | + headerPath = existingHeaderPath; |
| 51 | + const content = fs.readFileSync(headerPath, 'utf8'); |
| 52 | + |
| 53 | + if (!content.includes(importLine)) { |
| 54 | + fs.writeFileSync(headerPath, `${importLine}\n${content}`); |
| 55 | + } |
| 56 | + } else { |
| 57 | + // Default to new file if none found |
| 58 | + headerPath = path.join(iosPath, BRIDGING_HEADER_NAME); |
| 59 | + fs.writeFileSync(headerPath, `${importLine}\n`); |
| 60 | + config._createdBridgingHeader = BRIDGING_HEADER_NAME; |
| 61 | + } |
| 62 | + |
| 63 | + return config; |
| 64 | + }, |
| 65 | + ]); |
| 66 | +}; |
| 67 | + |
| 68 | +const withXcodeBuildSettings = rootConfig => |
| 69 | + withXcodeProject(rootConfig, config => { |
| 70 | + const project = config.modResults; |
| 71 | + const target = project.getFirstTarget().uuid; |
| 72 | + |
| 73 | + const currentSetting = project.getBuildProperty('SWIFT_OBJC_BRIDGING_HEADER', target); |
| 74 | + |
| 75 | + if (!currentSetting && config._createdBridgingHeader) { |
| 76 | + project.addBuildProperty( |
| 77 | + 'SWIFT_OBJC_BRIDGING_HEADER', |
| 78 | + `$(SRCROOT)/${config._createdBridgingHeader}`, |
| 79 | + target |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + return config; |
| 84 | + }); |
| 85 | + |
| 86 | +module.exports = { |
| 87 | + withBridgingHeader, |
| 88 | + withXcodeBuildSettings, |
| 89 | +}; |
0 commit comments