Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer --app-version from package.json #449

Merged
merged 14 commits into from
Aug 19, 2016
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

* The `package.json` `version` property is the default app version if `--app-version` is unspecified (#449)

### Fixed

* [CLI] ensure --out has either a string or null value (#442)
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ The human-readable copyright line for the app. Maps to the `LegalCopyright` meta

*String*

The release version of the application. Maps to the `ProductVersion` metadata property on Windows, and `CFBundleShortVersionString` on OS X.
The release version of the application. By default the `version` property in the `package.json` is used but it can be overridden with this argument. If neither are provided, the version of Electron will be used. Maps to the `ProductVersion` metadata property on Windows, and `CFBundleShortVersionString` on OS X.

##### `asar`

Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ function validateList (list, supported, name) {
return list
}

function getNameAndVersion (opts, dir, cb) {
function getMetadata (opts, dir, cb) {
var props = []
if (!opts.name) props.push(['productName', 'name'])
if (!opts['app-version']) props.push('version')
if (!opts.version) props.push(['dependencies.electron', 'devDependencies.electron'])

// Name and version provided, no need to infer
Expand All @@ -77,6 +78,12 @@ function inferNameAndVersionFromInstalled (packageName, opts, result, cb) {
debug('Inferring application name from productName or name in package.json')
opts.name = result.values.productName
}

if (result.values.version) {
debug('Inferring app-version from version in package.json')
opts['app-version'] = result.values.version
}

if (result.values[`dependencies.${packageName}`]) {
resolve(packageName, {
basedir: path.dirname(result.source[`dependencies.${packageName}`].src)
Expand Down Expand Up @@ -223,7 +230,7 @@ module.exports = function packager (opts, cb) {
debug(`Target Platforms: ${platforms.join(', ')}`)
debug(`Target Architectures: ${archs.join(', ')}`)

getNameAndVersion(opts, path.resolve(process.cwd(), opts.dir) || process.cwd(), function (err) {
getMetadata(opts, path.resolve(process.cwd(), opts.dir) || process.cwd(), function (err) {
if (err) {
err.message = 'Unable to determine application name or Electron version. ' +
'Please specify an application name and Electron version.\n\n' +
Expand Down
1 change: 1 addition & 0 deletions test/ci/before_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ case "$TRAVIS_OS_NAME" in
-in codesign.csr -out codesign.cer
openssl pkcs12 -export -in codesign.cer -inkey codesign.key -out codesign.p12 -password pass:12345
security import codesign.p12 -k ~/Library/Keychains/login.keychain -P 12345 -T /usr/bin/codesign
brew install wine
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the last time I tried this I had to do some weird things so that the OSX CI builds didn't take forever to run and used a relatively recent version of Wine: https://github.com/electron-userland/electron-packager/blob/v7.1.0/test/ci/before_install.sh#L6-L11

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you end up taking the brew installation out?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests involving rcedit kept timing out.

;;
esac
1 change: 1 addition & 0 deletions test/fixtures/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"main": "main.js",
"version": "4.99.101",
"productName": "MainJS",
"dependencies": {
"run-series": "^1.1.1"
Expand Down
31 changes: 31 additions & 0 deletions test/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ function createAppVersionTest (baseOpts, appVersion, buildVersion) {
}
}

function createAppVersionInferenceTest (baseOpts) {
return function (t) {
t.timeoutAfter(config.timeout)

var plistPath

waterfall([
function (cb) {
packager(baseOpts, cb)
}, function (paths, cb) {
plistPath = path.join(paths[0], baseOpts.name + '.app', 'Contents', 'Info.plist')
fs.stat(plistPath, cb)
}, function (stats, cb) {
t.true(stats.isFile(), 'The expected Info.plist file should exist')
fs.readFile(plistPath, 'utf8', cb)
}, function (file, cb) {
var obj = plist.parse(file)
t.equal(obj.CFBundleVersion, '4.99.101', 'CFBundleVersion should reflect package.json version')
t.equal(obj.CFBundleShortVersionString, '4.99.101', 'CFBundleShortVersionString should reflect package.json version')
cb()
}
], function (err) {
t.end(err)
})
}
}

function createAppCategoryTypeTest (baseOpts, appCategoryType) {
return function (t) {
t.timeoutAfter(config.timeout)
Expand Down Expand Up @@ -602,6 +629,10 @@ module.exports = function (baseOpts) {
test('app and build version test', createAppVersionTest(baseOpts, '1.1.0', '1.1.0.1234'))
util.teardown()

util.setup()
test('infer app version from package.json test', createAppVersionInferenceTest(baseOpts))
util.teardown()

util.setup()
test('app version test', createAppVersionTest(baseOpts, '1.1.0'))
util.teardown()
Expand Down