Skip to content

Commit

Permalink
fix: Try updating Podfile deployment target on prepare (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed Jun 29, 2023
1 parent 6db1b6e commit ad57677
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 40 deletions.
43 changes: 3 additions & 40 deletions lib/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const {
PluginManager
} = require('cordova-common');
const util = require('util');
const xcode = require('xcode');

function setupEvents (externalEventEmitter) {
if (externalEventEmitter) {
Expand Down Expand Up @@ -417,7 +416,7 @@ class Api {
projectFile.purgeProjectFileCache(this.locations.root);

return podfileFile.install(check_reqs.check_cocoapods)
.then(() => this.setSwiftVersionForCocoaPodsLibraries(podsjsonFile));
.then(() => podsjsonFile.setSwiftVersionForCocoaPodsLibraries(this.root));
} else {
events.emit('verbose', 'Podfile unchanged, skipping `pod install`');
}
Expand Down Expand Up @@ -506,7 +505,7 @@ class Api {
events.emit('verbose', 'Running `pod install` (to uninstall pods)');

return podfileFile.install(check_reqs.check_cocoapods)
.then(() => this.setSwiftVersionForCocoaPodsLibraries(podsjsonFile));
.then(() => podsjsonFile.setSwiftVersionForCocoaPodsLibraries(this.root));
} else {
events.emit('verbose', 'Podfile unchanged, skipping `pod install`');
}
Expand All @@ -520,43 +519,7 @@ class Api {
* @param {PodsJson} podsjsonFile A PodsJson instance that represents pods.json
*/
setSwiftVersionForCocoaPodsLibraries (podsjsonFile) {
let __dirty = false;
return check_reqs.check_cocoapods().then(toolOptions => {
if (toolOptions.ignore) {
events.emit('verbose', '=== skip Swift Version Settings For Cocoapods Libraries');
} else {
const podPbxPath = path.join(this.root, 'Pods', 'Pods.xcodeproj', 'project.pbxproj');
const podXcodeproj = xcode.project(podPbxPath);
podXcodeproj.parseSync();
const podTargets = podXcodeproj.pbxNativeTargetSection();
const podConfigurationList = podXcodeproj.pbxXCConfigurationList();
const podConfigs = podXcodeproj.pbxXCBuildConfigurationSection();

const libraries = podsjsonFile.getLibraries();
Object.keys(libraries).forEach(key => {
const podJson = libraries[key];
const name = podJson.name;
const swiftVersion = podJson['swift-version'];
if (swiftVersion) {
__dirty = true;
Object.keys(podTargets)
.filter(targetKey => podTargets[targetKey].productName === name)
.map(targetKey => podTargets[targetKey].buildConfigurationList)
.map(buildConfigurationListId => podConfigurationList[buildConfigurationListId])
.map(buildConfigurationList => buildConfigurationList.buildConfigurations)
.reduce((acc, buildConfigurations) => acc.concat(buildConfigurations), [])
.map(buildConfiguration => buildConfiguration.value)
.forEach(buildId => {
__dirty = true;
podConfigs[buildId].buildSettings.SWIFT_VERSION = swiftVersion;
});
}
});
if (__dirty) {
fs.writeFileSync(podPbxPath, podXcodeproj.writeSync(), 'utf-8');
}
}
});
return podsjsonFile.setSwiftVersionForCocoaPodsLibraries(this.root);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions lib/PodsJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
const fs = require('fs-extra');
const path = require('path');
const util = require('util');
const xcode = require('xcode');
const check_reqs = require('./check_reqs');
const events = require('cordova-common').events;
const CordovaError = require('cordova-common').CordovaError;

Expand Down Expand Up @@ -206,4 +208,48 @@ PodsJson.prototype.isDirty = function () {
return this.__dirty;
};

/**
* set Swift Version for all CocoaPods libraries
*/
PodsJson.prototype.setSwiftVersionForCocoaPodsLibraries = function (projectRoot) {
let __dirty = false;
return check_reqs.check_cocoapods().then(toolOptions => {
if (toolOptions.ignore) {
events.emit('verbose', '=== skip Swift Version Settings For Cocoapods Libraries');
} else {
const podPbxPath = path.join(projectRoot, 'Pods', 'Pods.xcodeproj', 'project.pbxproj');
const podXcodeproj = xcode.project(podPbxPath);
podXcodeproj.parseSync();

const podTargets = podXcodeproj.pbxNativeTargetSection();
const podConfigurationList = podXcodeproj.pbxXCConfigurationList();
const podConfigs = podXcodeproj.pbxXCBuildConfigurationSection();

const libraries = this.getLibraries();
Object.keys(libraries).forEach(key => {
const podJson = libraries[key];
const name = podJson.name;
const swiftVersion = podJson['swift-version'];
if (swiftVersion) {
__dirty = true;
Object.keys(podTargets)
.filter(targetKey => podTargets[targetKey].productName === name)
.map(targetKey => podTargets[targetKey].buildConfigurationList)
.map(buildConfigurationListId => podConfigurationList[buildConfigurationListId])
.map(buildConfigurationList => buildConfigurationList.buildConfigurations)
.reduce((acc, buildConfigurations) => acc.concat(buildConfigurations), [])
.map(buildConfiguration => buildConfiguration.value)
.forEach(buildId => {
__dirty = true;
podConfigs[buildId].buildSettings.SWIFT_VERSION = swiftVersion;
});
}
});
if (__dirty) {
fs.writeFileSync(podPbxPath, podXcodeproj.writeSync(), 'utf-8');
}
}
});
};

module.exports.PodsJson = PodsJson;
18 changes: 18 additions & 0 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const PlatformMunger = require('cordova-common').ConfigChanges.PlatformMunger;
const PluginInfoProvider = require('cordova-common').PluginInfoProvider;
const FileUpdater = require('cordova-common').FileUpdater;
const projectFile = require('./projectFile');
const Podfile = require('./Podfile').Podfile;
const check_reqs = require('./check_reqs');

// launch storyboard and related constants
const IMAGESET_COMPACT_SIZE_CLASS = 'compact';
Expand Down Expand Up @@ -309,6 +311,22 @@ function handleBuildSettings (platformConfig, locations, infoPlist) {

project.write();

// If we have a Podfile, we want to update the deployment target there too
const podPath = path.join(locations.root, Podfile.FILENAME);
if (deploymentTarget && fs.existsSync(podPath)) {
const project_name = locations.xcodeCordovaProj.split(path.sep).pop();

const PodsJson = require('./PodsJson').PodsJson;
const podsjsonFile = new PodsJson(path.join(locations.root, PodsJson.FILENAME));
const podfileFile = new Podfile(podPath, project_name, deploymentTarget);
podfileFile.write();

events.emit('verbose', 'Running `pod install` (to install plugins)');
projectFile.purgeProjectFileCache(locations.root);
return podfileFile.install(check_reqs.check_cocoapods)
.then(() => podsjsonFile.setSwiftVersionForCocoaPodsLibraries(locations.root));
}

return Promise.resolve();
}

Expand Down

0 comments on commit ad57677

Please sign in to comment.