Skip to content

Commit

Permalink
Set $PROJECT_NAME properly when installing plugins (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue authored Jun 19, 2020
1 parent 32b21e1 commit 048c1ca
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 21 deletions.
20 changes: 1 addition & 19 deletions bin/templates/scripts/cordova/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,6 @@ function createProjectObject (projectPath, projectName) {
return projectFile.parse(locations);
}

/**
* Gets the resolved bundle identifier from a project.
* Resolves the variable set in INFO.plist, if any (simple case)
*
* @param {*} projectObject
*/
function getBundleIdentifier (projectObject) {
const packageName = projectObject.getPackageName();
let bundleIdentifier = packageName;

const variables = packageName.match(/\$\((\w+)\)/); // match $(VARIABLE), if any
if (variables && variables.length >= 2) {
bundleIdentifier = projectObject.xcode.getBuildProperty(variables[1]);
}

return bundleIdentifier;
}

/**
* Returns a promise that resolves to the default simulator target; the logic here
* matches what `cordova emulate ios` does.
Expand Down Expand Up @@ -233,7 +215,7 @@ module.exports.run = buildOpts => {
}

const project = createProjectObject(projectPath, projectName);
const bundleIdentifier = getBundleIdentifier(project);
const bundleIdentifier = project.getPackageName();
const exportOptions = { compileBitcode: false, method: 'development' };

if (buildOpts.packageType) {
Expand Down
10 changes: 9 additions & 1 deletion bin/templates/scripts/cordova/lib/projectFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ function parseProjectFile (locations) {
fs.writeFileSync(frameworks_file, JSON.stringify(this.frameworks, null, 4));
},
getPackageName: function () {
return plist.parse(fs.readFileSync(plist_file, 'utf8')).CFBundleIdentifier;
const packageName = plist.parse(fs.readFileSync(plist_file, 'utf8')).CFBundleIdentifier;
let bundleIdentifier = packageName;

const variables = packageName.match(/\$\((\w+)\)/); // match $(VARIABLE), if any
if (variables && variables.length >= 2) {
bundleIdentifier = xcodeproj.getBuildProperty(variables[1]);
}

return bundleIdentifier.replace(/^"/, '').replace(/"$/, '');
},
getInstaller: function (name) {
return pluginHandlers.getInstaller(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<key>CFBundleIcons~ipad</key>
<dict/>
<key>CFBundleIdentifier</key>
<string>com.example.friendstring</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<access origin="s3.amazonaws.com" />
</config-file>


<preference name="NAME_SPACE" default="" />

<!-- ios -->
<platform name="ios">
<!-- CDV 2.5+ -->
Expand All @@ -48,6 +51,11 @@
value="DummyPluginCommand"/>
</config-file>

<config-file parent="/*" target="config.xml">
<preference name="PluginNameSpace" value="$NAME_SPACE" />
<preference name="PluginPackageName" value="$PACKAGE_NAME" />
</config-file>

<resource-file src="src/ios/DummyPlugin.bundle" />

<header-file src="src/ios/DummyPluginCommand.h" />
Expand Down
63 changes: 63 additions & 0 deletions tests/spec/unit/pluginAdd.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

const path = require('path');
const fs = require('fs-extra');
const EventEmitter = require('events').EventEmitter;
const ConfigParser = require('cordova-common').ConfigParser;
const PluginInfo = require('cordova-common').PluginInfo;
const Api = require('../../../bin/templates/scripts/cordova/Api');

const FIXTURES = path.join(__dirname, 'fixtures');
const DUMMY_PLUGIN = 'org.test.plugins.dummyplugin';

const iosProjectFixture = path.join(FIXTURES, 'ios-config-xml');
const iosProject = path.join(FIXTURES, 'dummyProj');
const iosPlatform = path.join(iosProject, 'platforms/ios');
const dummyPlugin = path.join(FIXTURES, DUMMY_PLUGIN);

describe('plugin add', () => {
let api;

beforeEach(() => {
fs.ensureDirSync(iosPlatform);
fs.copySync(iosProjectFixture, iosPlatform);
api = new Api('ios', iosPlatform, new EventEmitter());
});

afterEach(() => {
fs.removeSync(iosPlatform);
});

it('should handle plugin preference default values', () => {
return api.addPlugin(new PluginInfo(dummyPlugin))
.then(() => {
const cfg = new ConfigParser(api.locations.configXml);
expect(cfg.getPreference('PluginPackageName', 'ios')).toEqual('com.example.friendstring');
});
});

it('should handle plugin preference provided values', () => {
return api.addPlugin(new PluginInfo(dummyPlugin), { variables: { NAME_SPACE: 'com.mycompany.myapp' } })
.then(() => {
const cfg = new ConfigParser(api.locations.configXml);
expect(cfg.getPreference('PluginNameSpace', 'ios')).toEqual('com.mycompany.myapp');
});
});
});

0 comments on commit 048c1ca

Please sign in to comment.