Skip to content

Commit

Permalink
Ability to select any flavor of NW.js, not just sdk
Browse files Browse the repository at this point in the history
* Implementation of flavor feature based on new master

It implements a new parameter "flavor" which is by default "sdk" that
allow to choose the flavor of the build (useful for dev and production
application).

* Fix variable declaration

* Move variable declaration

Move the flavor declaration to be placed after the promise.

* Update readme for feature flavor

Update documentation for flavor

* fixed tests

Resolves #340
  • Loading branch information
langco authored and adam-lynch committed Sep 14, 2016
1 parent 23f1314 commit 8d66522
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 114 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ Default value: `'latest'`

The version of NW.js you want to use. Per default it looks up the latest version. [Here is a list](https://github.com/nwjs/nw.js/wiki/Downloads-of-old-versions) of all available releases

#### options.flavor
Type: `String`
Default value: `'sdk'`

The flavor of NW.js you want to use. Per default it will be `sdk`. [Here is a list](https://github.com/nwjs/nw.js/wiki/Build-Flavors) of all flavor available.

The value `sdk` is most used for development whereas `normal` for production.

#### options.platforms
Type: `Array`
Default value: `['osx32', 'osx64', 'win32', 'win64']`
Expand Down Expand Up @@ -257,6 +265,7 @@ To get around it, run `ulimit -n 1024` (or add it to your `~/.bash_profile`). Fo
See [CONTRIBUTING.md](CONTRIBUTING.md).

## Release History
- 2016-09-13 `3.1.0` Implementation of flavor feature.
- 2016-08-28 `3.0.0` bumping graceful-fs-extra dependency to 2.0.0.
- 2016-08-14 `2.2.7` fix for macIcns option when using NW.js 0.12.3
- 2016-07-31 `2.2.6` fix for OS X caching
Expand Down Expand Up @@ -296,3 +305,5 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).

[depstat-url]: https://david-dm.org/nwjs/nw-builder
[depstat-image]: https://david-dm.org/nwjs/nw-builder.svg?style=flat


16 changes: 10 additions & 6 deletions lib/Version.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ module.exports = function Version(args){

// 0.12.3 is an exception that is in the manifest but is pretty much a legacy version
if(result.isLegacy || args.version === '0.12.3'){
generatePlatformUrls = function(version, supportedPlatforms){
generatePlatformUrls = function(version,flavors, supportedPlatforms){
var platformUrls = {};
supportedPlatforms.forEach(function (supportedPlatform) {
platformUrls[supportedPlatform] = args.downloadUrl + _.template(platforms[supportedPlatform].versionNameTemplate, result);
flavors.forEach(function(flavor) {
platformUrls[supportedPlatform + '-' + flavor] = args.downloadUrl + _.template(platforms[supportedPlatform].versionNameTemplate, result);
});
});
return platformUrls;
};
Expand All @@ -40,16 +42,18 @@ module.exports = function Version(args){
return 'zip'
};

generatePlatformUrls = function(version, supportedPlatforms){
generatePlatformUrls = function(version, flavors, supportedPlatforms){
var platformUrls = {};
supportedPlatforms.forEach(function(platform){
platformUrls[platform] = args.downloadUrl + 'v' + version + '/nwjs-sdk-v' + version + '-' + fixPlatformName(platform)
+ '.' + mapPlatformToExtension(platform);
flavors.forEach(function(flavor) {
platformUrls[platform + '-' + flavor] = args.downloadUrl + 'v' + version + '/nwjs' + (flavor === 'normal' ? '' : '-' + flavor) + '-v' + version + '-' + fixPlatformName(platform)
+ '.' + mapPlatformToExtension(platform);
});
});
return platformUrls;
};
}

result.platforms = generatePlatformUrls(args.version, args.supportedPlatforms);
result.platforms = generatePlatformUrls(args.version, args.flavors, args.supportedPlatforms);
return result;
};
31 changes: 18 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function NwBuilder(options) {
buildDir: './build',
cacheDir: './cache',
downloadUrl: 'http://dl.nwjs.io/',
flavor: 'sdk',
buildType: 'default',
forceDownload: false,
macCredits: false,
Expand Down Expand Up @@ -176,7 +177,7 @@ NwBuilder.prototype.resolveLatestVersion = function () {

if(self.options.version !== 'latest') return Promise.resolve();

return NwVersions.getLatestVersion(self.options.downloadUrl).then(function(latestVersion){
return NwVersions.getLatestVersion(self.options.downloadUrl, self.options.flavor).then(function(latestVersion){
self.emit('log', 'Latest Version: v' + latestVersion.version);
self.options.version = latestVersion.version;
return latestVersion;
Expand All @@ -185,6 +186,7 @@ NwBuilder.prototype.resolveLatestVersion = function () {

NwBuilder.prototype.checkVersion = function () {
var version = this.options.version,
flavor = semver.valid(version) && semver.satisfies(version, '<0.12.3') ? 'sdk' : this.options.flavor,
self = this;

if(!semver.valid(version)){
Expand All @@ -195,6 +197,7 @@ NwBuilder.prototype.checkVersion = function () {
var getVersionFromManifest = function(){
return NwVersions.getVersion({
desiredVersion: version,
flavor: flavor,
downloadUrl: self.options.downloadUrl
});
};
Expand All @@ -219,6 +222,7 @@ NwBuilder.prototype.checkVersion = function () {
if(areAllPlatformsCached){
getVersion = Promise.resolve(new Version({
version: version,
flavor: flavor,
downloadUrl: self.options.downloadUrl,
supportedPlatforms: Object.keys(this._platforms)
}));
Expand All @@ -237,7 +241,8 @@ NwBuilder.prototype.checkVersion = function () {
return getVersion
.then(function(version){
self._version = version;
self.emit('log', 'Using v' + self._version.version);
self._version.flavor = flavor;
self.emit('log', 'Using v' + self._version.version + ' (' + ((self._version.flavor === '') ? 'normal' : self._version.flavor + ')'));
if(self._version.isLegacy) {
deprecate('NW.js / node-webkit versions <0.12.3 are deprecated.');
}
Expand All @@ -251,8 +256,8 @@ NwBuilder.prototype.platformFilesForVersion = function () {
var satisfied = self.preparePlatformFiles(name, platform);

// need the second condition for newer NW.js versions
if (!(satisfied && !!self._version.platforms[name])) {
throw new Error("Unsupported NW.js version '" + self._version.version + "' for platform '" + name + "'");
if (!(satisfied && !!self._version.platforms[name + "-" + self._version.flavor])) {
throw new Error("Unsupported NW.js version '" + self._version.version + " (" + self._version.flavor + ")' for platform '" + name + "'");
}
});

Expand All @@ -264,23 +269,23 @@ NwBuilder.prototype.downloadNwjs = function () {
downloads = [];

this._forEachPlatform(function (name, platform) {
self.setPlatformCacheDirectory(name, platform, self._version.version);
platform.url = self._version.platforms[name];
self.setPlatformCacheDirectory(name, platform, self._version.version, self._version.flavor);
platform.url = self._version.platforms[name + '-' + self._version.flavor];

// Ensure that there is a cache folder
if(self.options.forceDownload) {
fs.removeSync(platform.cache);
}

fs.mkdirpSync(platform.cache);
self.emit('log', 'Create cache folder in ' + path.resolve(self.options.cacheDir, self._version.version));
self.emit('log', 'Create cache folder in ' + path.resolve(self.options.cacheDir, self._version.version + '-' + self._version.flavor));

if(!self.isPlatformCached(name, platform, self._version.version)) {
if(!self.isPlatformCached(name, platform, self._version.version, self._version.flavor)) {
downloads.push(
Downloader.downloadAndUnpack(platform.cache, platform.url)
.catch(function(err){
if(err.statusCode === 404){
self.emit('log', 'ERROR: The version '+self._version.version+' does not have a corresponding build posted at ' + self.options.downloadUrl + '. Please choose a version from that list.');
self.emit('log', 'ERROR: The version '+self._version.version+ ' (' + self._version.flavor + ') does not have a corresponding build posted at ' + self.options.downloadUrl + '. Please choose a version from that list.');
} else {
self.emit('log', err.msg);
}
Expand Down Expand Up @@ -771,15 +776,15 @@ NwBuilder.prototype.getExecutableName = function (platform) {
return this.options.appName + executableExtension;
};

NwBuilder.prototype.setPlatformCacheDirectory = function (platformName, platform, version) {
NwBuilder.prototype.setPlatformCacheDirectory = function (platformName, platform, version, flavor) {
if(!platform.cache) {
platform.cache = path.resolve(this.options.cacheDir, version, platformName);
platform.cache = path.resolve(this.options.cacheDir, version + "-" + flavor, platformName);
}
};


NwBuilder.prototype.isPlatformCached = function (platformName, platform, version) {
this.setPlatformCacheDirectory(platformName, platform, version);
NwBuilder.prototype.isPlatformCached = function (platformName, platform, version, flavor) {
this.setPlatformCacheDirectory(platformName, platform, version, flavor);
if (this.options.forceDownload) {
return false;
}
Expand Down
29 changes: 19 additions & 10 deletions lib/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function get(url, options){
* @param {string} downloadUrl
* @returns {promise} which resolves to an array of {Version}s
*/
function getLegacyVersions(downloadUrl){
function getLegacyVersions(downloadUrl, flavor){
var scrapePtrn = /href="v?([0-9]+\.[0-9]+\.[0-9]+[^"]*)\/"/ig,
searchRes,
versions = [];
Expand All @@ -56,13 +56,15 @@ function getLegacyVersions(downloadUrl){
// check if windows 64-bit ZIP exists
var win32Url = new Version({
version: version,
flavors: ['sdk'],
supportedPlatforms: ['win32'],
downloadUrl: downloadUrl
}).platforms.win32;
}).platforms['win32-sdk'];
request.head(win32Url, function(err, res){
// note: this takes a version string and replaces it with an object (will be converted back later)
resolve({
version: version,
flavor: ['sdk'],
valid: !err && res.statusCode === 200
});
});
Expand All @@ -81,6 +83,7 @@ function getLegacyVersions(downloadUrl){
.map(function(versionObj){
return new Version({
version: versionObj.version,
flavors: versionObj.flavor,
supportedPlatforms: allPlatforms,
downloadUrl: downloadUrl
});
Expand All @@ -102,7 +105,7 @@ function getManifest(){
* @param {string} downloadUrl
* @returns {promise} which resolves to an array of {Version}s
*/
function getVersionsFromManifest(downloadUrl){
function getVersionsFromManifest(downloadUrl, flavor){
var mapFilesToPlatforms = function(files){
return files.map(function(file){
// convert win-x64 to win64, linux-ia32 to linux 32, etc.
Expand All @@ -114,11 +117,12 @@ function getVersionsFromManifest(downloadUrl){
return manifest.versions
.filter(function(versionFromManifest){
// 0.12.3 is an exception that is in the manifest but is kind of a legacy version
return versionFromManifest.flavors.indexOf('sdk') !== -1 || versionFromManifest.version === 'v0.12.3';
return (versionFromManifest.flavors !== undefined) && ( versionFromManifest.flavors.indexOf('sdk') !== -1 || versionFromManifest.version === 'v0.12.3');
})
.map(function(versionFromManifest){
return new Version({
version: versionFromManifest.version.replace('v', ''),
flavors: versionFromManifest.flavors,
supportedPlatforms: mapFilesToPlatforms(versionFromManifest.files),
downloadUrl: downloadUrl
});
Expand All @@ -138,13 +142,15 @@ module.exports = {
/**
* Gets the latest stable version
* @param {string} downloadUrl
* @param {string} flavor
* @returns {promise} which resolves to a {Version}
*/
getLatestVersion: function(downloadUrl) {
getLatestVersion: function(downloadUrl, flavor) {
return getManifest()
.then(function(manifest) {
return {
desiredVersion: manifest.stable.replace('v', ''),
flavor: flavor,
downloadUrl: downloadUrl
}
})
Expand All @@ -153,10 +159,11 @@ module.exports = {
/**
* @param {string} args.desiredVersion
* @param {string} args.downloadUrl
* @param {string} args.flavor
* @returns {promise} which resolves to a {Version}
*/
getVersion: function(args){
return (isLegacyVersion(args.desiredVersion) ? getLegacyVersions : getVersionsFromManifest)(args.downloadUrl)
getVersion: function(args){
return (isLegacyVersion(args.desiredVersion) ? getLegacyVersions : getVersionsFromManifest)(args.downloadUrl)
.then(function(versions) {
var version = versions.findIndex(function(version){
return version.version === args.desiredVersion;
Expand All @@ -166,13 +173,15 @@ module.exports = {
? Promise.resolve(versions[version])
: Promise.reject('Version ' + args.desiredVersion + ' not found.');
});
},
},
/**
* @param {string} downloadUrl
* @param {string} flavor
* @returns {promise} which resolves to an array of {Version}s
*/
getVersions: function(downloadUrl){
return Promise.all([getVersionsFromManifest(downloadUrl), getLegacyVersions(downloadUrl)])
getVersions: function(downloadUrl, flavor){

return Promise.all([getVersionsFromManifest(downloadUrl, flavor), getLegacyVersions(downloadUrl, flavor)])
.then(function(versionLists){
return versionLists[0].concat(versionLists[1]);
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nw-builder",
"version": "3.0.0",
"version": "3.1.0",
"description": "nw-builder",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -65,3 +65,4 @@
"winresourcer": "^0.9.0"
}
}

Loading

0 comments on commit 8d66522

Please sign in to comment.