Skip to content

Commit 048c1ca

Browse files
authored
Set $PROJECT_NAME properly when installing plugins (#910)
1 parent 32b21e1 commit 048c1ca

File tree

5 files changed

+82
-21
lines changed

5 files changed

+82
-21
lines changed

bin/templates/scripts/cordova/lib/build.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,6 @@ function createProjectObject (projectPath, projectName) {
6161
return projectFile.parse(locations);
6262
}
6363

64-
/**
65-
* Gets the resolved bundle identifier from a project.
66-
* Resolves the variable set in INFO.plist, if any (simple case)
67-
*
68-
* @param {*} projectObject
69-
*/
70-
function getBundleIdentifier (projectObject) {
71-
const packageName = projectObject.getPackageName();
72-
let bundleIdentifier = packageName;
73-
74-
const variables = packageName.match(/\$\((\w+)\)/); // match $(VARIABLE), if any
75-
if (variables && variables.length >= 2) {
76-
bundleIdentifier = projectObject.xcode.getBuildProperty(variables[1]);
77-
}
78-
79-
return bundleIdentifier;
80-
}
81-
8264
/**
8365
* Returns a promise that resolves to the default simulator target; the logic here
8466
* matches what `cordova emulate ios` does.
@@ -233,7 +215,7 @@ module.exports.run = buildOpts => {
233215
}
234216

235217
const project = createProjectObject(projectPath, projectName);
236-
const bundleIdentifier = getBundleIdentifier(project);
218+
const bundleIdentifier = project.getPackageName();
237219
const exportOptions = { compileBitcode: false, method: 'development' };
238220

239221
if (buildOpts.packageType) {

bin/templates/scripts/cordova/lib/projectFile.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ function parseProjectFile (locations) {
7777
fs.writeFileSync(frameworks_file, JSON.stringify(this.frameworks, null, 4));
7878
},
7979
getPackageName: function () {
80-
return plist.parse(fs.readFileSync(plist_file, 'utf8')).CFBundleIdentifier;
80+
const packageName = plist.parse(fs.readFileSync(plist_file, 'utf8')).CFBundleIdentifier;
81+
let bundleIdentifier = packageName;
82+
83+
const variables = packageName.match(/\$\((\w+)\)/); // match $(VARIABLE), if any
84+
if (variables && variables.length >= 2) {
85+
bundleIdentifier = xcodeproj.getBuildProperty(variables[1]);
86+
}
87+
88+
return bundleIdentifier.replace(/^"/, '').replace(/"$/, '');
8189
},
8290
getInstaller: function (name) {
8391
return pluginHandlers.getInstaller(name);

tests/spec/unit/fixtures/ios-config-xml/SampleApp/SampleApp-Info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<key>CFBundleIcons~ipad</key>
1414
<dict/>
1515
<key>CFBundleIdentifier</key>
16-
<string>com.example.friendstring</string>
16+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1717
<key>CFBundleInfoDictionaryVersion</key>
1818
<string>6.0</string>
1919
<key>CFBundleName</key>

tests/spec/unit/fixtures/org.test.plugins.dummyplugin/plugin.xml

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
<access origin="s3.amazonaws.com" />
4141
</config-file>
4242

43+
44+
<preference name="NAME_SPACE" default="" />
45+
4346
<!-- ios -->
4447
<platform name="ios">
4548
<!-- CDV 2.5+ -->
@@ -48,6 +51,11 @@
4851
value="DummyPluginCommand"/>
4952
</config-file>
5053

54+
<config-file parent="/*" target="config.xml">
55+
<preference name="PluginNameSpace" value="$NAME_SPACE" />
56+
<preference name="PluginPackageName" value="$PACKAGE_NAME" />
57+
</config-file>
58+
5159
<resource-file src="src/ios/DummyPlugin.bundle" />
5260

5361
<header-file src="src/ios/DummyPluginCommand.h" />

tests/spec/unit/pluginAdd.spec.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
const path = require('path');
21+
const fs = require('fs-extra');
22+
const EventEmitter = require('events').EventEmitter;
23+
const ConfigParser = require('cordova-common').ConfigParser;
24+
const PluginInfo = require('cordova-common').PluginInfo;
25+
const Api = require('../../../bin/templates/scripts/cordova/Api');
26+
27+
const FIXTURES = path.join(__dirname, 'fixtures');
28+
const DUMMY_PLUGIN = 'org.test.plugins.dummyplugin';
29+
30+
const iosProjectFixture = path.join(FIXTURES, 'ios-config-xml');
31+
const iosProject = path.join(FIXTURES, 'dummyProj');
32+
const iosPlatform = path.join(iosProject, 'platforms/ios');
33+
const dummyPlugin = path.join(FIXTURES, DUMMY_PLUGIN);
34+
35+
describe('plugin add', () => {
36+
let api;
37+
38+
beforeEach(() => {
39+
fs.ensureDirSync(iosPlatform);
40+
fs.copySync(iosProjectFixture, iosPlatform);
41+
api = new Api('ios', iosPlatform, new EventEmitter());
42+
});
43+
44+
afterEach(() => {
45+
fs.removeSync(iosPlatform);
46+
});
47+
48+
it('should handle plugin preference default values', () => {
49+
return api.addPlugin(new PluginInfo(dummyPlugin))
50+
.then(() => {
51+
const cfg = new ConfigParser(api.locations.configXml);
52+
expect(cfg.getPreference('PluginPackageName', 'ios')).toEqual('com.example.friendstring');
53+
});
54+
});
55+
56+
it('should handle plugin preference provided values', () => {
57+
return api.addPlugin(new PluginInfo(dummyPlugin), { variables: { NAME_SPACE: 'com.mycompany.myapp' } })
58+
.then(() => {
59+
const cfg = new ConfigParser(api.locations.configXml);
60+
expect(cfg.getPreference('PluginNameSpace', 'ios')).toEqual('com.mycompany.myapp');
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)