瀏覽代碼

feat(Builder): support concurrent building

evshiron 8 年之前
父節點
當前提交
4553108e0f
共有 3 個文件被更改,包括 84 次插入24 次删除
  1. 16 10
      src/bin/build.ts
  2. 48 12
      src/lib/Builder.ts
  3. 20 2
      test/Builder.js

+ 16 - 10
src/bin/build.ts

@@ -7,16 +7,6 @@ const debug = require('debug')('build:commandline:build');
 import { Builder } from '../lib';
 
 const argv = require('yargs')
-.option('x86', {
-    type: 'boolean',
-    describe: 'Build for x86 arch',
-    default: Builder.DEFAULT_OPTIONS.x86,
-})
-.option('x64', {
-    type: 'boolean',
-    describe: 'Build for x64 arch',
-    default: Builder.DEFAULT_OPTIONS.x64,
-})
 .option('win', {
     type: 'boolean',
     describe: 'Build for Windows platform',
@@ -35,10 +25,25 @@ const argv = require('yargs')
     default: Builder.DEFAULT_OPTIONS.linux,
     alias: 'l',
 })
+.option('x86', {
+    type: 'boolean',
+    describe: 'Build for x86 arch',
+    default: Builder.DEFAULT_OPTIONS.x86,
+})
+.option('x64', {
+    type: 'boolean',
+    describe: 'Build for x64 arch',
+    default: Builder.DEFAULT_OPTIONS.x64,
+})
 .option('mirror', {
     describe: 'Modify NW.js mirror',
     default: Builder.DEFAULT_OPTIONS.mirror,
 })
+.option('concurrent', {
+    type: 'boolean',
+    describe: 'Build concurrently',
+    default: Builder.DEFAULT_OPTIONS.concurrent,
+})
 .help()
 .argv;
 
@@ -53,6 +58,7 @@ const argv = require('yargs')
         x86: argv.x86,
         x64: argv.x64,
         mirror: argv.mirror,
+        concurrent: argv.concurrent,
         mute: false,
     }, argv._.shift());
 

+ 48 - 12
src/lib/Builder.ts

@@ -3,6 +3,7 @@ import { dirname, basename, join, resolve } from 'path';
 
 import * as semver from 'semver';
 import { ensureDirAsync, emptyDir, readFileAsync, readJsonAsync, writeFileAsync, copyAsync, removeAsync, createReadStream, createWriteStream, renameAsync } from 'fs-extra-promise';
+import * as Bluebird from 'bluebird';
 
 const debug = require('debug')('build:builder');
 const globby = require('globby');
@@ -23,6 +24,7 @@ interface IBuilderOptions {
     x86?: boolean;
     x64?: boolean;
     mirror?: string;
+    concurrent?: boolean;
     mute?: boolean;
 }
 
@@ -35,6 +37,7 @@ export class Builder {
         x86: false,
         x64: false,
         mirror: Downloader.DEFAULT_OPTIONS.mirror,
+        concurrent: false,
         mute: true,
     };
 
@@ -64,6 +67,7 @@ export class Builder {
         if(!this.options.mute) {
             console.info('Starting building tasks...', {
                 tasks,
+                concurrent: this.options.concurrent,
             });
         }
 
@@ -71,14 +75,48 @@ export class Builder {
             throw new Error('ERROR_NO_TASK');
         }
 
-        const pkg: any = await readJsonAsync(join(this.dir, 'package.json'));
-        const config = new BuildConfig(pkg);
+        if(this.options.concurrent) {
 
-        debug('in build', 'config', config);
+            await Bluebird.map(tasks, async ([ platform, arch ], idx) => {
 
-        for(const [ platform, arch ] of tasks) {
+                const options: any = {};
+                options[platform] = true;
+                options[arch] = true;
+                options.mirror = this.options.mirror;
+                options.concurrent = false;
+                options.mute = true;
 
-            await this.buildTask(platform, arch, pkg, config);
+                const builder = new Builder(options, this.dir);
+
+                const started = Date.now();
+
+                await builder.build();
+
+                if(!this.options.mute) {
+                    console.info(`Building for ${ platform }, ${ arch } ends within ${ ((Date.now() - started) / 1000).toFixed(2) }ms.`);
+                }
+
+            });
+
+        }
+        else {
+
+            const pkg: any = await readJsonAsync(join(this.dir, 'package.json'));
+            const config = new BuildConfig(pkg);
+
+            debug('in build', 'config', config);
+
+            for(const [ platform, arch ] of tasks) {
+
+                const started = Date.now();
+
+                await this.buildTask(platform, arch, pkg, config);
+
+                if(!this.options.mute) {
+                    console.info(`Building for ${ platform }, ${ arch } ends within ${ ((Date.now() - started) / 1000).toFixed(2) }ms.`);
+                }
+
+            }
 
         }
 
@@ -367,7 +405,7 @@ export class Builder {
         await writeFileAsync(script, data);
 
         await nsisBuild(toDir, script, {
-            mute: false,
+            mute: this.options.mute,
         });
 
         await removeAsync(script);
@@ -455,7 +493,9 @@ export class Builder {
     protected async buildNsisTarget(platform: string, arch: string, targetDir: string, pkg: any, config: BuildConfig) {
 
         if(platform != 'win') {
-            console.info(`Skip building nsis target for ${ platform }.`);
+            if(!this.options.mute) {
+                console.info(`Skip building nsis target for ${ platform }.`);
+            }
             return;
         }
 
@@ -487,7 +527,7 @@ export class Builder {
         await writeFileAsync(script, data);
 
         await nsisBuild(targetDir, script, {
-            mute: false,
+            mute: this.options.mute,
         });
 
         await removeAsync(script);
@@ -557,10 +597,6 @@ export class Builder {
             }
         }
 
-        if(!this.options.mute) {
-            console.info(`Building for ${ platform }, ${ arch } ends.`);
-        }
-
     }
 
 }

+ 20 - 2
test/Builder.js

@@ -6,7 +6,7 @@ import { spawnAsync } from '../dist/lib/util';
 
 const dir = './assets/project/';
 
-test('commandline', async (t) => {
+test.skip('commandline', async (t) => {
 
     const mirror = process.env.CI ? '' : '--mirror https://npm.taobao.org/mirrors/nwjs/';
 
@@ -17,7 +17,7 @@ test('commandline', async (t) => {
 
 });
 
-test('module', async (t) => {
+test.skip('module', async (t) => {
 
     const mirror = process.env.CI ? undefined : 'https://npm.taobao.org/mirrors/nwjs/';
 
@@ -30,3 +30,21 @@ test('module', async (t) => {
     await builder.build();
 
 });
+
+test('concurrent', async (t) => {
+
+    const mirror = process.env.CI ? undefined : 'https://npm.taobao.org/mirrors/nwjs/';
+
+    const builder = new Builder({
+        win: true,
+        mac: true,
+        linux: true,
+        x64: true,
+        mirror,
+        concurrent: true,
+        mute: false,
+    }, dir);
+
+    await builder.build();
+
+});