-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathpackageIos.ts
More file actions
179 lines (156 loc) · 6.29 KB
/
packageIos.ts
File metadata and controls
179 lines (156 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import fs from 'node:fs';
import path from 'node:path';
import {
getBuildOptions,
mergeFrameworks,
type BuildFlags as AppleBuildFlags,
} from '@rock-js/platform-apple-helpers';
import { packageIosAction } from '@rock-js/plugin-brownfield-ios';
import {
colorLink,
getReactNativeVersion,
logger,
relativeToCwd,
} from '@rock-js/tools';
import { Command } from 'commander';
import { runExpoPrebuildIfNeeded } from '../utils/expo.js';
import { getProjectInfo } from '../utils/project.js';
import {
actionRunner,
curryOptions,
ExampleUsage,
} from '../../shared/index.js';
import { runBrownieCodegenIfApplicable } from '../../brownie/helpers/runBrownieCodegenIfApplicable.js';
import { runNavigationCodegenIfApplicable } from '../../navigation/helpers/runNavigationCodegenIfApplicable.js';
import { stripFrameworkBinary } from '../utils/stripFrameworkBinary.js';
export const packageIosCommand = curryOptions(
new Command('package:ios').description('Build iOS XCFramework'),
getBuildOptions({ platformName: 'ios' }).map((option) =>
option.name.startsWith('--build-folder')
? {
...option,
description:
option.description +
" By default, the '.brownfield/build' path will be used.",
}
: option
)
).action(
actionRunner(async (options: AppleBuildFlags) => {
const { projectRoot, platformConfig, userConfig } = getProjectInfo('ios');
await runExpoPrebuildIfNeeded({ projectRoot, platform: 'ios' });
if (!userConfig.project.ios) {
throw new Error('iOS project not found.');
}
if (!userConfig.project.ios.xcodeProject) {
throw new Error('iOS Xcode project not found in the configuration.');
}
let dotBrownfieldDir = path.join(
// for Expo projects, platformConfig?.sourceDir == "", but for non-Expo projects, it's "ios"
...(userConfig.project.ios.sourceDir.trim().length > 0
? [userConfig.project.ios.sourceDir]
: [projectRoot, 'ios']),
'.brownfield'
);
// non-Expo projects have a relative sourceDir path, so we need to make it absolute
if (!path.isAbsolute(dotBrownfieldDir)) {
dotBrownfieldDir = path.join(projectRoot, dotBrownfieldDir);
}
options.buildFolder ??= path.join(dotBrownfieldDir, 'build');
// The new_architecture.rb script scans Info.plist and fails on binary plist files,
// which is the case for our XCFrameworks.
// We're reusing the "build" directory which is excluded from the scan.
// Reference: https://github.com/facebook/react-native/blob/490c5e8dcc6cdb19c334cc39e93a39a48ba71e96/packages/react-native/scripts/cocoapods/new_architecture.rb#L171
const packageDir = path.join(dotBrownfieldDir, 'package', 'build');
const configuration = options.configuration ?? 'Debug';
const { hasBrownie } = await runBrownieCodegenIfApplicable(
projectRoot,
'swift'
);
const { hasNavigation } = await runNavigationCodegenIfApplicable(projectRoot);
await packageIosAction(
options,
{
projectRoot,
reactNativePath: userConfig.reactNativePath,
// below: the userConfig.reactNativeVersion may be a non-semver-format string,
// e.g. '0.82' (note the missing patch component),
// therefore we resolve it manually from RN's package.json using Rock's utils
reactNativeVersion: getReactNativeVersion(projectRoot),
usePrebuiltRNCore: false, // for brownfield, it is required to build RN from source
packageDir, // the output directory for artifacts
skipCache: true, // cache is dependent on existence of Rock config file
},
platformConfig
);
const reactBrownfieldXcframeworkPath = path.join(
packageDir,
'ReactBrownfield.xcframework'
);
if (fs.existsSync(reactBrownfieldXcframeworkPath)) {
// Strip the binary from ReactBrownfield.xcframework to make it interface-only.
// This avoids duplicate symbols when consumer apps embed both BrownfieldLib
// (which contains ReactBrownfield symbols) and ReactBrownfield.xcframework.
stripFrameworkBinary(reactBrownfieldXcframeworkPath);
}
if (hasBrownie) {
const productsPath = path.join(options.buildFolder, 'Build', 'Products');
const brownieOutputPath = path.join(packageDir, 'Brownie.xcframework');
await mergeFrameworks({
sourceDir: userConfig.project.ios.sourceDir,
frameworkPaths: [
path.join(
productsPath,
`${configuration}-iphoneos`,
'Brownie',
'Brownie.framework'
),
path.join(
productsPath,
`${configuration}-iphonesimulator`,
'Brownie',
'Brownie.framework'
),
],
outputPath: brownieOutputPath,
});
// Strip the binary from Brownie.xcframework to make it interface-only.
// This avoids duplicate symbols when consumer apps embed both BrownfieldLib
// (which contains Brownie symbols) and Brownie.xcframework.
stripFrameworkBinary(brownieOutputPath);
logger.success(
`Brownie.xcframework created at ${colorLink(relativeToCwd(brownieOutputPath))}`
);
}
if (hasNavigation) {
const productsPath = path.join(options.buildFolder, 'Build', 'Products');
const brownfieldNavigationOutputPath = path.join(packageDir, 'BrownfieldNavigation.xcframework');
await mergeFrameworks({
sourceDir: userConfig.project.ios.sourceDir,
frameworkPaths: [
path.join(
productsPath,
`${configuration}-iphoneos`,
'BrownfieldNavigation',
'BrownfieldNavigation.framework'
),
path.join(
productsPath,
`${configuration}-iphonesimulator`,
'BrownfieldNavigation',
'BrownfieldNavigation.framework'
),
],
outputPath: brownfieldNavigationOutputPath,
});
stripFrameworkBinary(brownfieldNavigationOutputPath);
logger.success(
`BrownfieldNavigation.xcframework created at ${colorLink(relativeToCwd(brownfieldNavigationOutputPath))}`
);
}
})
);
export const packageIosExample = new ExampleUsage(
'package:ios --scheme BrownfieldLib --configuration Release',
"Build iOS XCFramework for 'BrownfieldLib' scheme in Release configuration"
);