WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit fc0d1a2

Browse files
authored
fix(create): Copy config.xml data during creation (#1502)
This ensures that by the time plugin installation happens, the preferences from the root application config.xml can be read as expected. Closes GH-1422.
1 parent 18a80d2 commit fc0d1a2

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

lib/Api.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ class Api {
107107
* @param {Object} [options] An options object. The most common options are:
108108
* @param {String} [options.customTemplate] A path to custom template, that
109109
* should override the default one from platform.
110-
* @param {Boolean} [options.link] Flag that indicates that platform's
111-
* sources will be linked to installed platform instead of copying.
112110
* @param {EventEmitter} [events] An EventEmitter instance that will be used for
113111
* logging purposes. If no EventEmitter provided, all events will be logged to
114112
* console

lib/create.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
const path = require('node:path');
2121
const fs = require('node:fs');
22-
const { CordovaError, events } = require('cordova-common');
22+
const { ConfigParser, CordovaError, events, xmlHelpers } = require('cordova-common');
2323
const xcode = require('xcode');
2424
const pkg = require('../package');
2525

@@ -31,10 +31,11 @@ const ROOT = path.join(__dirname, '..');
3131
* @param {string} project_path Path to your new Cordova iOS project
3232
* @param {string} package_name Package name, following reverse-domain style convention
3333
* @param {string} project_name Project name
34-
* @param {{ link: boolean, customTemplate: string }} opts Project creation options
34+
* @param {{ customTemplate: string }} opts Project creation options
35+
* @param {ConfigParser} root_config The application config.xml
3536
* @returns {Promise<void>} resolves when the project has been created
3637
*/
37-
exports.createProject = async (project_path, package_name, project_name, opts) => {
38+
exports.createProject = async (project_path, package_name, project_name, opts, root_config) => {
3839
package_name = package_name || 'my.cordova.project';
3940
project_name = project_name || 'CordovaExample';
4041

@@ -56,7 +57,7 @@ exports.createProject = async (project_path, package_name, project_name, opts) =
5657
},
5758
options: {
5859
templatePath: opts.customTemplate || path.join(ROOT, 'templates', 'project'),
59-
linkLib: !!opts.link
60+
rootConfig: root_config
6061
}
6162
}).create();
6263

@@ -73,6 +74,7 @@ class ProjectCreator {
7374
this.provideCordovaJs();
7475
this.provideBuildScripts();
7576
this.updateProjectSettings();
77+
this.updatePlatformConfigFile();
7678
}
7779

7880
provideProjectTemplate () {
@@ -97,6 +99,17 @@ class ProjectCreator {
9799
fs.cpSync(srcScriptsDir, destScriptsDir, { recursive: true });
98100
}
99101

102+
updatePlatformConfigFile () {
103+
const defaultXmlPath = this.projectPath('cordova', 'defaults.xml');
104+
const configXmlPath = this.projectPath('App', 'config.xml');
105+
106+
fs.cpSync(defaultXmlPath, configXmlPath);
107+
108+
const config = new ConfigParser(configXmlPath);
109+
xmlHelpers.mergeXml(this.options.rootConfig.doc.getroot(), config.doc.getroot(), 'ios', /* clobber= */true);
110+
config.write();
111+
}
112+
100113
updateProjectSettings () {
101114
const projectPath = this.projectPath('App.xcodeproj', 'project.pbxproj');
102115
const xcodeproj = xcode.project(projectPath);
@@ -105,6 +118,11 @@ class ProjectCreator {
105118
xcodeproj.updateBuildProperty('PRODUCT_NAME', `"${this.project.name}"`, null, 'App');
106119
xcodeproj.updateBuildProperty('PRODUCT_BUNDLE_IDENTIFIER', `"${this.project.id}"`, null, 'App');
107120

121+
const deploymentTarget = this.options.rootConfig.getPreference('deployment-target', 'ios');
122+
if (deploymentTarget) {
123+
xcodeproj.updateBuildProperty('IPHONEOS_DEPLOYMENT_TARGET', deploymentTarget);
124+
}
125+
108126
// Update the CordovaLib Swift package reference path
109127
const pkgRefs = xcodeproj.hash.project.objects.XCLocalSwiftPackageReference;
110128
if (pkgRefs) {

tests/spec/createAndBuild.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ const fs = require('node:fs');
2121
const os = require('node:os');
2222
const path = require('node:path');
2323
const xcode = require('xcode');
24+
const { ConfigParser } = require('cordova-common');
2425
const create = require('../../lib/create');
2526

2627
const makeTempDir = () => path.join(
2728
fs.realpathSync(os.tmpdir()),
2829
`cordova-ios-create-test-${Date.now()}`
2930
);
3031

32+
const templateConfigXmlPath = path.join(__dirname, '..', '..', 'templates', 'project', 'App', 'config.xml');
33+
3134
/**
3235
* Verifies that some of the project file exists. Not all will be tested.
3336
* E.g. App's resource directory, xcodeproj, xcworkspace, and CordovaLib.
@@ -90,7 +93,9 @@ function verifyBuild (tmpDir) {
9093
* @returns {Promise}
9194
*/
9295
async function verifyCreateAndBuild (tmpDir, packageName, projectName) {
93-
await create.createProject(tmpDir, packageName, projectName, {}, undefined)
96+
const configXml = new ConfigParser(templateConfigXmlPath);
97+
98+
await create.createProject(tmpDir, packageName, projectName, {}, configXml)
9499
.then(() => verifyProjectFiles(tmpDir, projectName))
95100
.then(() => verifyProjectBundleIdentifier(tmpDir, projectName, packageName))
96101
.then(() => verifyBuild(tmpDir));

tests/spec/unit/create.spec.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ const fs = require('node:fs');
2121
const os = require('node:os');
2222
const path = require('node:path');
2323
const xcode = require('xcode');
24+
const { ConfigParser } = require('cordova-common');
2425
const create = require('../../../lib/create');
2526

2627
const makeTempDir = () => path.join(
2728
fs.realpathSync(os.tmpdir()),
2829
`cordova-ios-create-test-${Date.now()}`
2930
);
3031

32+
const templateConfigXmlPath = path.join(__dirname, '..', '..', '..', 'templates', 'project', 'App', 'config.xml');
33+
3134
/**
3235
* Verifies that some of the project file exists. Not all will be tested.
3336
* E.g. App's resource directory, xcodeproj, xcworkspace, and CordovaLib.
@@ -82,6 +85,15 @@ function verifyProjectBundleIdentifier (tmpDir, projectName, expectedBundleIdent
8285
expect(actualBundleName).toBe(`"${projectName}"`);
8386
}
8487

88+
function verifyProjectDeploymentTarget (tmpDir, expectedTarget) {
89+
const pbxproj = path.join(tmpDir, 'App.xcodeproj', 'project.pbxproj');
90+
const xcodeproj = xcode.project(pbxproj);
91+
xcodeproj.parseSync();
92+
93+
const actualDeploymentTarget = xcodeproj.getBuildProperty('IPHONEOS_DEPLOYMENT_TARGET');
94+
expect(actualDeploymentTarget).toBe(expectedTarget);
95+
}
96+
8597
/**
8698
* Runs various project creation checks.
8799
*
@@ -90,8 +102,10 @@ function verifyProjectBundleIdentifier (tmpDir, projectName, expectedBundleIdent
90102
* @param {String} projectName
91103
* @returns {Promise}
92104
*/
93-
async function verifyCreatedProject (tmpDir, packageName, projectName) {
94-
await create.createProject(tmpDir, packageName, projectName, {}, undefined)
105+
async function verifyCreatedProject (tmpDir, packageName, projectName, configFile = templateConfigXmlPath) {
106+
const configXml = new ConfigParser(configFile);
107+
108+
await create.createProject(tmpDir, packageName, projectName, {}, configXml)
95109
.then(() => verifyProjectFiles(tmpDir, projectName))
96110
.then(() => verifyProjectBundleIdentifier(tmpDir, projectName, packageName));
97111
}
@@ -118,4 +132,13 @@ describe('create', () => {
118132
const projectName = '応応応応 hello & إثرا 用用用用';
119133
return verifyCreatedProject(tmpDir, packageName, projectName);
120134
});
135+
136+
it('should copy config.xml into the newly created project', () => {
137+
const configPath = path.join(__dirname, 'fixtures', 'test-config-3.xml');
138+
const packageName = 'io.cordova.hellocordova.ios';
139+
const projectName = 'Hello Cordova';
140+
141+
return verifyCreatedProject(tmpDir, packageName, projectName, configPath)
142+
.then(() => verifyProjectDeploymentTarget(tmpDir, '15.0'));
143+
});
121144
});

tests/spec/unit/fixtures/test-config-3.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<platform name="ios">
1313
<preference name="orientation" value="all" />
1414
<preference name="target-device" value="handset" />
15-
<preference name="deployment-target" value="13.0" />
15+
<preference name="deployment-target" value="15.0" />
1616
<preference name="SwiftVersion" value="4.1" />
1717
</platform>
1818

0 commit comments

Comments
 (0)