Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/community-cli-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ npx @react-native-community/cli spm [action] [options]
| `--yes` | Skip the dirty-pbxproj confirmation prompt. |
| `--xcodeproj <path>` | **[add]** Path to the `.xcodeproj` to inject SPM packages into (disambiguates when several exist). |
| `--productName <string>` | **[add]** App target to inject into (disambiguates when several exist). |
| `--deintegrate` | **[add]** Run `pod deintegrate` and strip React Native from the Podfile before injecting (CocoaPods → SwiftPM migration). |
| `--deintegrate` | **[add]** Remove the CocoaPods integration from the `.xcodeproj` before injecting (CocoaPods → SwiftPM migration). Uses `pod deintegrate` when CocoaPods is installed; CocoaPods is not required. The Podfile is kept. |
| `--artifacts <path>` | **[advanced]** Local artifact root containing complete `debug/` and `release/` slots. |
| `--download <string>` | **[advanced]** Artifact download policy: `auto` (default), `skip`, or `force`. |
| `--skipCodegen` | **[advanced]** Skip the react-native codegen step. |
Expand Down
6 changes: 4 additions & 2 deletions packages/community-cli-plugin/src/commands/spm.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ const spmCommand: Command = {
{
name: '--deintegrate',
description:
'[add] Run `pod deintegrate` and strip React Native from the Podfile ' +
'before injecting (CocoaPods → SwiftPM migration).',
'[add] Remove the CocoaPods integration from the .xcodeproj before ' +
'injecting (CocoaPods → SwiftPM migration). Uses `pod deintegrate` ' +
'when CocoaPods is installed; CocoaPods is not required. The Podfile ' +
'is kept.',
},
{
name: '--artifacts <path>',
Expand Down
78 changes: 54 additions & 24 deletions packages/react-native/scripts/setup-apple-spm.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
* settings, the Sync build phase) into your
* existing .xcodeproj, in place. Idempotent.
* Default on first run. `--deintegrate` first
* runs `pod deintegrate` and keeps the Podfile
* (CocoaPods → SPM migration).
* removes the CocoaPods integration from the
* .xcodeproj and keeps the Podfile (CocoaPods →
* SPM migration).
* update Re-run the pipeline and refresh the existing
* injection. Default once a project is injected.
* deinit The exact inverse of `add`: surgically remove
Expand All @@ -56,8 +57,11 @@
* --yes Skip the dirty-pbxproj confirmation prompt.
* [add] --xcodeproj <path> Which .xcodeproj to inject into (when several).
* [add] --product-name <name> Which app target to inject into (when several).
* [add] --deintegrate Run `pod deintegrate` before injecting. The
* Podfile is kept.
* [add] --deintegrate Remove the CocoaPods integration from the
* .xcodeproj before injecting: `pod deintegrate`
* when CocoaPods is installed, otherwise the
* script edits the project itself. The Podfile
* is kept.
* [advanced] --artifacts <path> Local two-flavor artifact root;
* must contain debug/ and release/ cache slots.
* [advanced] --download <auto|skip|force> Artifact policy (default: auto).
Expand Down Expand Up @@ -126,6 +130,7 @@ const {
refreshScaffoldedPlatformFloors,
scaffoldAll,
} = require('./spm/scaffold-package-swift');
const {removeCocoaPodsIntegration} = require('./spm/spm-pbxproj');
const {
CXX_API_DEFINES,
RemoteVersionError,
Expand Down Expand Up @@ -201,7 +206,7 @@ function parseArgs(argv /*: Array<string> */) /*: SetupArgs */ {
type: 'boolean',
default: false,
describe:
'[add] Run `pod deintegrate` before injecting (CocoaPods → SPM migration). The Podfile is kept.',
'[add] Remove the CocoaPods integration from the .xcodeproj before injecting (CocoaPods → SPM migration). Uses `pod deintegrate` when CocoaPods is installed; CocoaPods is not required. The Podfile is kept.',
})
.option('artifacts', {
type: 'string',
Expand Down Expand Up @@ -733,15 +738,17 @@ function generateXcframeworksPackage(
generatePackage(packageArgs);
}

// True when the chosen pbxproj is still CocoaPods-integrated (its build configs
// layer a `Pods-*.xcconfig`) — the real blocker for SPM injection.
// True when a pbxproj is still CocoaPods-integrated (its build configs layer a
// `Pods-*.xcconfig`) — the real blocker for SPM injection.
function usesCocoaPods(pbxproj /*: string */) /*: boolean */ {
return /\bPods[-/][^\n]*\.xcconfig\b/.test(pbxproj);
}

function pbxprojUsesCocoaPods(xcodeprojPath /*: string */) /*: boolean */ {
try {
const t = fs.readFileSync(
path.join(xcodeprojPath, 'project.pbxproj'),
'utf8',
return usesCocoaPods(
fs.readFileSync(path.join(xcodeprojPath, 'project.pbxproj'), 'utf8'),
);
return /\bPods[-/][^\n]*\.xcconfig\b/.test(t);
} catch {
return false;
}
Expand All @@ -750,8 +757,10 @@ function pbxprojUsesCocoaPods(xcodeprojPath /*: string */) /*: boolean */ {
function cocoaPodsIntegratedMessage(xcodeprojPath /*: string */) /*: string */ {
return (
`${path.basename(xcodeprojPath)} is CocoaPods-integrated. To move it to ` +
'SwiftPM, run `npx react-native spm add --deintegrate`. It runs ' +
'`pod deintegrate` and keeps the Podfile, so ' +
'SwiftPM, run `npx react-native spm add --deintegrate`. It removes the ' +
'CocoaPods integration from the project, with `pod deintegrate` when ' +
'CocoaPods is installed; CocoaPods is not required. It keeps the ' +
'Podfile, so ' +
'`npx react-native spm deinit` and then `pod install` go back to ' +
'CocoaPods. Pods other than React Native are not installed while the app ' +
'uses SwiftPM, unless SwiftPM provides them.'
Expand Down Expand Up @@ -797,18 +806,39 @@ function podfilePodNames(appRoot /*: string */) /*: Array<string> */ {
}

// Run `pod deintegrate` and leave the Podfile as it is, so `spm deinit` and
// then `pod install` go back to CocoaPods. Requires CocoaPods on PATH
// (fail-loud otherwise). Does NOT touch the .xcworkspace.
function runDeintegrate(appRoot /*: string */) /*: void */ {
// then `pod install` go back to CocoaPods. Without CocoaPods on PATH (e.g. a
// fresh template app on a Mac that never installed it), strip the CocoaPods
// references from the pbxproj directly instead — all or nothing, so a project
// it cannot fully clean is left as it was. Does NOT touch the .xcworkspace.
function runDeintegrate(
appRoot /*: string */,
xcodeprojPath /*: string */,
) /*: void */ {
try {
execFileSync('pod', ['--version'], {stdio: 'ignore'});
} catch {
logError(
'`--deintegrate` needs CocoaPods (`pod`) on PATH. Remove the React ' +
'Native integration from your project manually, then run `spm add`.',
);
process.exitCode = 1;
throw new Error('pod not found');
const projectName = path.basename(xcodeprojPath);
const pbxprojPath = path.join(xcodeprojPath, 'project.pbxproj');
const original = fs.readFileSync(pbxprojPath, 'utf8');
const deintegrated = removeCocoaPodsIntegration(original);
if (usesCocoaPods(deintegrated)) {
logError(
`CocoaPods is not installed, and ${projectName} has CocoaPods ` +
'references this script cannot remove, so it was left unchanged. ' +
'Install CocoaPods and run the same command again, or remove the ' +
'remaining CocoaPods references from the project by hand.',
);
process.exitCode = 1;
throw new Error('CocoaPods references remain');
}
if (deintegrated !== original) {
fs.writeFileSync(pbxprojPath, deintegrated, 'utf8');
log(
'CocoaPods is not installed, so the CocoaPods references were ' +
`removed from ${projectName} directly.`,
);
}
return;
}
log('Running `pod deintegrate`...');
execFileSync('pod', ['deintegrate'], {cwd: appRoot, stdio: 'inherit'});
Expand Down Expand Up @@ -907,7 +937,7 @@ async function setupXcodeproj(

// No backup is made — git is the safety net. Refuse on a dirty/untracked
// pbxproj unless --yes, so a bad inject is always `git checkout`-able. Asked
// before `pod deintegrate` edits the pbxproj, so "no" changes nothing.
// before `--deintegrate` edits the pbxproj, so "no" changes nothing.
const clean = gitTrackedAndClean(appRoot, pbxprojPath);
if (clean === false && !args.yes) {
const proceed = await promptYesNo(
Expand All @@ -928,7 +958,7 @@ async function setupXcodeproj(
}

if (args.deintegrate) {
runDeintegrate(appRoot);
runDeintegrate(appRoot, xcodeprojPath);
// `pod deintegrate` strips the build integration but can leave an empty
// `Pods` group in the navigator — remove it so the converted project is
// visually clean.
Expand Down
9 changes: 6 additions & 3 deletions packages/react-native/scripts/spm/__docs__/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ npx react-native spm # add on first run, update thereafter
```

On a CocoaPods app, this stops and changes nothing. To convert the app, run
`npx react-native spm add --deintegrate`. It runs `pod deintegrate` and keeps
the `Podfile`. It warns about pods that are not part of React Native, because
they are not installed while the app uses SwiftPM.
`npx react-native spm add --deintegrate`. It removes the CocoaPods integration
from the `.xcodeproj` and keeps the `Podfile`. CocoaPods does not need to be
installed: the command uses `pod deintegrate` when it is, and otherwise removes
the CocoaPods references from the `.xcodeproj` itself. It warns about pods that
are not part of React Native, because they are not installed while the app uses
SwiftPM.

`pod install` on a converted app stops with an error. To go back to CocoaPods,
run `npx react-native spm deinit`, then `pod install` in `ios/`. If your Podfile
Expand Down
Loading
Loading