Skip to content

Commit 576f443

Browse files
chrfalchthymikee
andauthored
feat(init): set up iOS with Swift Package Manager by default (#2881)
* feat(init): set up iOS with Swift Package Manager by default On macOS, for React Native 0.87 and later, `init` now runs React Native's Swift Package Manager setup (`setup-apple-spm add --deintegrate --yes`) after dependencies are installed, instead of installing CocoaPods. `--skip-spm` keeps the CocoaPods setup. `--install-pods true` without `--skip-spm` is an error where the setup would run. A failed setup prints a warning and a retry command; init still finishes. * fix(init): prepare CocoaPods before the Swift Package Manager setup * test(init): ignore colors when matching the SwiftPM conflict error * test(e2e): keep CocoaPods in e2e projects with --skip-spm --------- Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
1 parent 2574a3a commit 576f443

14 files changed

Lines changed: 1154 additions & 26 deletions

File tree

‎__e2e__/config.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ beforeEach(() => {
4848
writeFiles(DIR, {});
4949

5050
// Initialise React Native project
51-
runCLI(DIR, ['init', 'TestProject', '--install-pods']);
51+
runCLI(DIR, ['init', 'TestProject', '--install-pods', '--skip-spm']);
5252

5353
// Link CLI to the project
5454
spawnScript('yarn', ['link', __dirname, '--all'], {

‎__e2e__/root.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ beforeAll(() => {
1616
writeFiles(DIR, {});
1717

1818
// Initialise React Native project
19-
runCLI(DIR, ['init', 'TestProject', `--pm`, 'npm', `--install-pods`]);
19+
runCLI(DIR, [
20+
'init',
21+
'TestProject',
22+
`--pm`,
23+
'npm',
24+
`--install-pods`,
25+
'--skip-spm',
26+
]);
2027

2128
// Link CLI to the project
2229
spawnScript('yarn', ['link', __dirname, '--all'], {

‎docs/commands.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ Skip dependencies installation
9898

9999
#### `--install-pods [boolean]`
100100

101-
Determine if CocoaPods should be installed when initializing a project. If set to `true` it will install pods, if set to `false`, it will skip the step entirely. If not used, prompt will be displayed
101+
Determine if CocoaPods should be installed when initializing a project. If set to `true` it will install pods, if set to `false`, it will skip the step entirely. If not used, prompt will be displayed. On macOS with React Native 0.87+, `init` uses Swift Package Manager instead; add `--skip-spm` to use CocoaPods. With `--template` or an out-of-tree `--platform-name`, `--install-pods true` keeps CocoaPods.
102+
103+
#### `--skip-spm`
104+
105+
Use CocoaPods instead of Swift Package Manager for iOS dependencies. On macOS, starting with React Native 0.87, `init` sets up iOS dependencies with Swift Package Manager, the same as `npx react-native spm add --deintegrate`. With this flag, `init` keeps the CocoaPods setup, and `--install-pods` decides whether pods are installed. Passing `--install-pods true` without this flag is an error for React Native 0.87 and later on macOS. The error applies only when `init` can look up the React Native version; if the lookup fails, for example offline, there is no error and `init` uses CocoaPods. It also applies only without `--skip-install` and with the default template. It does not apply with `--template` or with an out-of-tree `--platform-name`.
102106

103107
#### `--pm <string>`
104108

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import execa from 'execa';
2+
import {execaPod, findPodRunner} from '../tools/pods';
3+
4+
jest.mock('execa', () => jest.fn());
5+
6+
const mockExeca = execa as unknown as jest.Mock;
7+
const OPTIONS = {cwd: '/project'};
8+
9+
function failWhen(predicate: (command: string, args: string[]) => boolean) {
10+
mockExeca.mockImplementation(async (command: string, args: string[]) => {
11+
if (predicate(command, args)) {
12+
throw new Error(`${command} ${args.join(' ')} failed`);
13+
}
14+
return {exitCode: 0};
15+
});
16+
}
17+
18+
const isBundledPod = (command: string, args: string[]) =>
19+
command === 'bundle' && args[0] === 'exec';
20+
const isBundleInstall = (command: string, args: string[]) =>
21+
command === 'bundle' && args[0] === 'install';
22+
23+
beforeEach(() => {
24+
mockExeca.mockReset();
25+
});
26+
27+
describe('findPodRunner', () => {
28+
it('prefers the bundled CocoaPods', async () => {
29+
failWhen(() => false);
30+
31+
await expect(findPodRunner(OPTIONS)).resolves.toBe('bundle');
32+
expect(mockExeca).toHaveBeenCalledTimes(1);
33+
expect(mockExeca).toHaveBeenCalledWith(
34+
'bundle',
35+
['exec', 'pod', '--version'],
36+
OPTIONS,
37+
);
38+
});
39+
40+
it('runs bundle install with the given options when the bundled CocoaPods is missing', async () => {
41+
let installed = false;
42+
mockExeca.mockImplementation(async (command: string, args: string[]) => {
43+
if (isBundleInstall(command, args)) {
44+
installed = true;
45+
} else if (isBundledPod(command, args) && !installed) {
46+
throw new Error('not installed');
47+
}
48+
return {exitCode: 0};
49+
});
50+
51+
await expect(findPodRunner(OPTIONS)).resolves.toBe('bundle');
52+
expect(mockExeca).toHaveBeenCalledWith('bundle', ['install'], OPTIONS);
53+
});
54+
55+
it('falls back to the system CocoaPods', async () => {
56+
failWhen((command) => command === 'bundle');
57+
58+
await expect(findPodRunner(OPTIONS)).resolves.toBe('system');
59+
expect(mockExeca).toHaveBeenLastCalledWith('pod', ['--version'], OPTIONS);
60+
});
61+
62+
it('throws when CocoaPods is not installed', async () => {
63+
failWhen(() => true);
64+
65+
await expect(findPodRunner(OPTIONS)).rejects.toThrow(
66+
'cocoapods not installed',
67+
);
68+
});
69+
});
70+
71+
describe('execaPod', () => {
72+
it('runs pod through bundle exec when the bundled CocoaPods is available', async () => {
73+
failWhen(() => false);
74+
75+
await execaPod(['install'], OPTIONS);
76+
77+
expect(mockExeca).toHaveBeenLastCalledWith(
78+
'bundle',
79+
['exec', 'pod', 'install'],
80+
OPTIONS,
81+
);
82+
});
83+
84+
it('runs the system pod otherwise', async () => {
85+
failWhen((command) => command === 'bundle');
86+
87+
await execaPod(['install'], OPTIONS);
88+
89+
expect(mockExeca).toHaveBeenLastCalledWith('pod', ['install'], OPTIONS);
90+
});
91+
});

‎packages/cli-config-apple/src/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ export {
44
findPodfilePaths,
55
} from './config';
66
export {default as runCodegen} from './tools/runCodegen';
7-
export {default as installPods} from './tools/installPods';
8-
export {default as resolvePods} from './tools/pods';
7+
export {default as installPods, installCocoaPods} from './tools/installPods';
8+
export {default as resolvePods, findPodRunner} from './tools/pods';
9+
export type {PodRunner} from './tools/pods';
910
export {default as findXcodeProject} from './config/findXcodeProject';
1011
export {default as findPbxprojFile} from './config/findPbxprojFile';
1112
export {supportedPlatforms} from './config/supportedPlatforms';

‎packages/cli-config-apple/src/tools/pods.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,32 +251,43 @@ export default async function resolvePods(
251251
return true;
252252
}
253253

254-
export async function execaPod(args: string[], options?: execa.Options) {
255-
let podType: 'system' | 'bundle' = 'system';
254+
export type PodRunner = 'bundle' | 'system';
256255

256+
/**
257+
* Finds a working `pod`: the project's bundled CocoaPods first (running
258+
* `bundle install` if needed), then the system one.
259+
*/
260+
export async function findPodRunner(
261+
options?: execa.Options,
262+
): Promise<PodRunner> {
257263
try {
258264
// try bundle pod first
259265
await execa('bundle', ['exec', 'pod', '--version'], options);
260-
podType = 'bundle';
266+
return 'bundle';
261267
} catch (bundlePodError) {
262268
// try to install bundle pod
263269
try {
264-
await execa('bundle', ['install']);
270+
await execa('bundle', ['install'], options);
265271
await execa('bundle', ['exec', 'pod', '--version'], options);
266-
podType = 'bundle';
272+
return 'bundle';
267273
} catch (bundleInstallError) {
268274
// fall back to system pod
269275
try {
270276
await execa('pod', ['--version'], options);
271-
podType = 'system';
277+
return 'system';
272278
} catch (systemPodError) {
273279
throw new Error('cocoapods not installed');
274280
}
275281
}
276282
}
283+
}
277284

278-
if (podType === 'bundle') {
285+
export async function execaPod(args: string[], options?: execa.Options) {
286+
const podRunner = await findPodRunner(options);
287+
288+
if (podRunner === 'bundle') {
279289
return execa('bundle', ['exec', 'pod', ...args], options);
280290
}
291+
281292
return execa('pod', args, options);
282293
}

0 commit comments

Comments
 (0)