Skip to content

Commit

Permalink
Reworked plist logic
Browse files Browse the repository at this point in the history
Removed auxiliary properties appName, appVersion and copyright
Added checks for mandatory plist properties
  • Loading branch information
bastimeyer committed Nov 4, 2014
1 parent 214f3c6 commit c8bdf1e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ Default value: `false`
MAC ONLY: Use a `app.nw` folder instead of `ZIP` file, this significantly improves the startup speed of applications on `mac`, since no decompressing is needed. Builds on other platforms will still use `ZIP` files.

#### options.macPlist
Type: `String` or `Object`
Default value: `false`
Type: `String` or `Object`
Default value: `false`

MAC ONLY: If you supply a string to a Plist file it will use it. Otherwise it will generate something useful from your package.json

Expand Down
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,13 @@ NwBuilder.prototype.handleMacApp = function () {
allDone.push(Utils.copyFile(self.options.macPlist, PlistPath));
} else {
// Setup the Plst
var defaultPlist = {
appName: self.options.appName,
appVersion: self.options.appVersion,
copyright: self._appPkg.copyright || false
};
var plistOptions = Utils.getPlistOptions(
self.options.appName,
self.options.appVersion,
self._appPkg.copyright || false,
self.options.macPlist
);

var plistOptions = (self.options.macPlist ? _.defaults(self.options.macPlist, defaultPlist) : defaultPlist);
allDone.push(Utils.editPlist(PlistPath, PlistPath, plistOptions));
}

Expand Down
113 changes: 66 additions & 47 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
var fs = require('fs-extra');
var path = require('path');
var _ = require('lodash');
var Promise = require('bluebird');
var plist = require('plist');
var Glob = require('simple-glob');
var temp = require('temp');
var archiver = require('archiver');

var readFile = Promise.promisify(fs.readFile);
var writeFile = Promise.promisify(fs.writeFile);

// Automatically track and cleanup files at exit
temp.track();

Expand Down Expand Up @@ -136,59 +140,74 @@ module.exports = {

});
},
editPlist: function (plistInput, plistOutput, options) {
options = options || {};
if(!options.appName || !options.appVersion) {
return Promise.reject('You have to set the appName and appVersion in the Plist options');
getPlistOptions: function(appName, appVersion, copyright, custom) {
var obj = {};
if(appName) {
obj.CFBundleName = appName;
obj.CFBundleDisplayName = appName;
}
if(appVersion) {
obj.CFBundleVersion = appVersion;
obj.CFBundleShortVersionString = 'Version ' + appVersion;
}
if(copyright) {
obj.NSHumanReadableCopyright = copyright;
}

return new Promise(function(resolve, reject) {

// Handle the Info.plist file
var info = plist.parse(fs.readFileSync(plistInput, 'utf8') );
var keys = Object.keys(options);
return _.merge(obj, custom);
},
editPlist: function(plistInput, plistOutput, options) {
options = options || {};

if(!options.mac_document_types) {
info.CFBundleDocumentTypes = [];
// Make sure all required properties are set
[
'CFBundleName',
'CFBundleDisplayName',
'CFBundleVersion',
'CFBundleShortVersionString',
'NSHumanReadableCopyright'
].forEach(function(prop) {
if(!options.hasOwnProperty(prop)) {
throw new Error('Missing macPlist property \'' + prop + '\'');
}
info.UTExportedTypeDeclarations = [];

keys.forEach(function(key) {
var value = options[key];
switch(key) {
case 'appName':
info.CFBundleName = value;
info.CFBundleDisplayName = value;
break;
case 'appVersion':
info.CFBundleVersion = value;
info.CFBundleShortVersionString = 'Version ' + value;
break;
case 'copyright':
info.NSHumanReadableCopyright = value;
break;
case 'mac_bundle_id':
info.CFBundleIdentifier = value;
break;
case 'mac_document_types':
info.CFBundleDocumentTypes = value.map(function(type) {
return {
CFBundleTypeName: type.name,
CFBundleTypeExtensions: type.extensions,
CFBundleTypeRole: type.role,
LSIsAppleDefaultForType: type.isDefault
};
});
break;
default:
info[key] = value;
});

// Read the input file
return readFile(plistInput, 'utf8')
// Parse it
.then(plist.parse)
// Then overwrite the properties with custom values
.then(function(info) {
// Keep backwards compatibility and handle aliases
Object.keys(options).forEach(function(key) {
var value = options[key];
switch(key) {
case 'mac_bundle_id':
info.CFBundleIdentifier = value;
break;
case 'mac_document_types':
info.CFBundleDocumentTypes = value.map(function(type) {
return {
CFBundleTypeName: type.name,
CFBundleTypeExtensions: type.extensions,
CFBundleTypeRole: type.role,
LSIsAppleDefaultForType: type.isDefault
};
});
break;
default:
info[key] = value;
}
});

// Remove some unwanted properties
if(!options.hasOwnProperty('mac_document_types')) {
info.CFBundleDocumentTypes = [];
}
});
info.UTExportedTypeDeclarations = [];

fs.writeFile(plistOutput, plist.build(info), function(err) {
if(err) { reject(err); }
else { resolve(); }
// Write output file
return writeFile(plistOutput, plist.build(info));
});
});
}
};
2 changes: 1 addition & 1 deletion test/expected/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>TestApp</string>
<string>My cool TestApp</string>
<key>CFBundleDocumentTypes</key>
<array/>
<key>CFBundleExecutable</key>
Expand Down
17 changes: 10 additions & 7 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ test('getPackageInfo valid', function (t) {
test('editPlist', function (t) {
t.plan(1);
temp.open('plstest', function(err, info) {
utils.editPlist('./test/fixtures/Info.plist', info.path, {
appName: 'TestApp',
appVersion: '1.3.3.7',
copyright: '(c) by me',
LSEnvironment: {
PATH: '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
utils.editPlist('./test/fixtures/Info.plist', info.path, utils.getPlistOptions(
'TestApp',
'1.3.3.7',
'(c) by me',
{
CFBundleDisplayName: "My cool TestApp",
LSEnvironment: {
PATH: '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
}
}
}).then(function () {
)).then(function () {
var actual = fs.readFileSync(info.path).toString().replace(/\r|\n/gm, '');
var expected = fs.readFileSync('./test/expected/Info.plist').toString().replace(/\r|\n/gm, '');
t.equal(actual, expected, 'generate and write a valid plist file');
Expand Down

0 comments on commit c8bdf1e

Please sign in to comment.