From 8edd6ad5d5a60fa3ae5159fcfa4c3be779f388fa Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 14 Oct 2013 14:18:04 +0800 Subject: [PATCH 1/8] Rewrite bootstrap script in js. --- script/bootstrap | 51 +++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/script/bootstrap b/script/bootstrap index 784d63035..a42070eb9 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -1,22 +1,37 @@ -#!/bin/sh +#!/usr/bin/env node +var exec = require('child_process').exec; +var path = require('path'); -# exit on subprocess errors -set -o errexit - -exit_unless_npm_exists() { - if ! hash npm 2> /dev/null; then - echo "ERROR: Atom requires npm" - exit 1 - fi +function safeSpawn(command, options, callback) { + if (!callback) { + callback = options; + options = {}; + } + var child = exec(command, options, function(error, stdout, stderr) { + if (error) + process.exit(error.code); + else + callback(); + }); + child.stderr.pipe(process.stderr); + child.stdout.pipe(process.stdout); } -exit_unless_npm_exists +var apmDir = path.join('vendor', 'apm'); +var commands = [ + 'git submodule --quiet sync', + 'git submodule --quiet update --recursive --init', + 'cd vendor/apm; npm install --silent .', + 'npm install --silent vendor/apm', + 'echo', + 'node node_modules/.bin/apm install --silent', +]; -git submodule --quiet sync -git submodule --quiet update --recursive --init - -(cd vendor/apm && npm install --silent .) - -npm install --silent vendor/apm -echo "" -./node_modules/.bin/apm install --silent +var i = 0; +var startCommands = function() { + if (i < commands.length) + safeSpawn(commands[i++], startCommands); + else + process.exit(0); +} +startCommands(); From e17c9ced57224dbd85e2669f7ac4e7cd2324112d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 14 Oct 2013 14:37:16 +0800 Subject: [PATCH 2/8] Rewrite script/build in js. --- script/bootstrap | 2 ++ script/build | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/script/bootstrap b/script/bootstrap index a42070eb9..871039788 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -27,6 +27,8 @@ var commands = [ 'node node_modules/.bin/apm install --silent', ]; +process.chdir(path.dirname(__dirname)); + var i = 0; var startCommands = function() { if (i < commands.length) diff --git a/script/build b/script/build index 4ca3456ca..65614f59b 100755 --- a/script/build +++ b/script/build @@ -1,8 +1,18 @@ -#!/bin/sh +#!/usr/bin/env node +var spawn = require('child_process').spawn; +var path = require('path'); -set -e +process.chdir(path.dirname(__dirname)); -cd "$(dirname "$0")/.." +var child = spawn(process.execPath, [path.join('script', 'bootstrap')]); +child.stderr.pipe(process.stderr); +child.stdout.pipe(process.stdout); +child.on('exit', function(code) { + if (code != 0) process.exit(code); -./script/bootstrap -./node_modules/.bin/grunt "$@" + var gruntPath = path.join('node_modules', '.bin', 'grunt'); + var grunt = spawn(process.execPath, [gruntPath].concat(process.argv.slice(2))); + grunt.stderr.pipe(process.stderr); + grunt.stdout.pipe(process.stdout); + grunt.on('exit', process.exit); +}); From 1d343dd483739da940165bc8d43888338f62786c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 14 Oct 2013 15:09:17 +0800 Subject: [PATCH 3/8] Make the safeSpawn function a util module. --- script/bootstrap | 19 ++------------- script/build | 16 ++++-------- script/utils/child-process-wrapper.js | 35 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 script/utils/child-process-wrapper.js diff --git a/script/bootstrap b/script/bootstrap index 871039788..c6464147e 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -1,22 +1,7 @@ #!/usr/bin/env node -var exec = require('child_process').exec; +var safeExec = require('./utils/child-process-wrapper.js').safeExec; var path = require('path'); -function safeSpawn(command, options, callback) { - if (!callback) { - callback = options; - options = {}; - } - var child = exec(command, options, function(error, stdout, stderr) { - if (error) - process.exit(error.code); - else - callback(); - }); - child.stderr.pipe(process.stderr); - child.stdout.pipe(process.stdout); -} - var apmDir = path.join('vendor', 'apm'); var commands = [ 'git submodule --quiet sync', @@ -32,7 +17,7 @@ process.chdir(path.dirname(__dirname)); var i = 0; var startCommands = function() { if (i < commands.length) - safeSpawn(commands[i++], startCommands); + safeExec(commands[i++], startCommands); else process.exit(0); } diff --git a/script/build b/script/build index 65614f59b..00cb82e44 100755 --- a/script/build +++ b/script/build @@ -1,18 +1,12 @@ #!/usr/bin/env node -var spawn = require('child_process').spawn; +var cp = require('./utils/child-process-wrapper.js'); var path = require('path'); process.chdir(path.dirname(__dirname)); -var child = spawn(process.execPath, [path.join('script', 'bootstrap')]); -child.stderr.pipe(process.stderr); -child.stdout.pipe(process.stdout); -child.on('exit', function(code) { - if (code != 0) process.exit(code); - +cp.safeExec('node script/bootstrap', function() { + // ./node_modules/.bin/grunt "$@" var gruntPath = path.join('node_modules', '.bin', 'grunt'); - var grunt = spawn(process.execPath, [gruntPath].concat(process.argv.slice(2))); - grunt.stderr.pipe(process.stderr); - grunt.stdout.pipe(process.stdout); - grunt.on('exit', process.exit); + var args = [gruntPath].concat(process.argv.slice(2)); + cp.safeSpawn(process.execPath, args, process.exit); }); diff --git a/script/utils/child-process-wrapper.js b/script/utils/child-process-wrapper.js new file mode 100644 index 000000000..16eef8bb2 --- /dev/null +++ b/script/utils/child-process-wrapper.js @@ -0,0 +1,35 @@ +var childProcess = require('child_process'); + +// Exit the process if the command failed and only call the callback if the +// command succeed, output of the command would also be piped. +exports.safeExec = function(command, options, callback) { + if (!callback) { + callback = options; + options = {}; + } + var child = childProcess.exec(command, options, function(error, stdout, stderr) { + if (error) + process.exit(error.code); + else + callback(0); + }); + child.stderr.pipe(process.stderr); + child.stdout.pipe(process.stdout); +} + +// Same with safeExec but call child_process.spawn instead. +exports.safeSpawn = function(command, args, options, callback) { + if (!callback) { + callback = options; + options = {}; + } + var child = childProcess.spawn(command, args, options); + child.stderr.pipe(process.stderr); + child.stdout.pipe(process.stdout); + child.on('exit', function(code) { + if (code != 0) + process.exit(code); + else + callback(code); + }); +} From 4308e428d37ce7f8dfda0206a326a744c3451afb Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 14 Oct 2013 16:29:26 +0800 Subject: [PATCH 4/8] Rewrite script/cibuild in js. --- script/cibuild | 49 ++++++++++++++++++++------- script/utils/child-process-wrapper.js | 4 +-- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/script/cibuild b/script/cibuild index 8742fe7c6..9a749de51 100755 --- a/script/cibuild +++ b/script/cibuild @@ -1,18 +1,41 @@ -#!/bin/sh +#!/usr/bin/env node +var cp = require('./utils/child-process-wrapper.js'); +var fs = require('fs'); +var path = require('path'); -set -e +process.chdir(path.dirname(__dirname)); -cd "$(dirname "$0")/.." +if (process.platform != 'darwin') + throw new Error('cibuild can not run on ' + process.platform + ' yet!'); -rm -rf ~/.atom -git clean -dff +var homeDir = process.platform == 'win32' ? process.env.USERPROFILE : process.env.HOME; -ATOM_CREDENTIALS_FILE=/var/lib/jenkins/config/atomcredentials -if [ -f $ATOM_CREDENTIALS_FILE ]; then - . $ATOM_CREDENTIALS_FILE - export ATOM_ACCESS_TOKEN=$ATOM_ACCESS_TOKEN # make it visibile to grunt. -fi +function readEnvironmentVariables(callback) { + var credenticalsPath = '/var/lib/jenkins/config/atomcredentials'; + fs.readFile(credenticalsPath, function(error, data) { + if (!error) { + var lines = String(data).trim().split('\n'); + for (i in lines) { + var parts = lines[i].split('='); + var key = parts[0].trim(); + var value = parts[1].trim().substr(1, parts[1].length - 2); + process.env[key] = value; + } + } + // Do not quit when got error. + callback(null); + }); +} -./script/bootstrap -./node_modules/.bin/apm clean -./node_modules/.bin/grunt ci --stack --no-color +cp.safeExec('node script/bootstrap', function() { + var async = require('async'); + async.series([ + readEnvironmentVariables, + require('rimraf').bind(global, path.join(homeDir, '.atom')), + cp.safeExec.bind(global, 'git clean -dff'), + cp.safeExec.bind(global, 'node node_modules/.bin/apm clean'), + cp.safeExec.bind(global, 'node node_modules/.bin/grunt ci --stack --no-color'), + ], function(error) { + process.exit(error ? 1 : 0); + }); +}); diff --git a/script/utils/child-process-wrapper.js b/script/utils/child-process-wrapper.js index 16eef8bb2..c31f80b10 100644 --- a/script/utils/child-process-wrapper.js +++ b/script/utils/child-process-wrapper.js @@ -11,7 +11,7 @@ exports.safeExec = function(command, options, callback) { if (error) process.exit(error.code); else - callback(0); + callback(null); }); child.stderr.pipe(process.stderr); child.stdout.pipe(process.stdout); @@ -30,6 +30,6 @@ exports.safeSpawn = function(command, args, options, callback) { if (code != 0) process.exit(code); else - callback(code); + callback(null); }); } From fae1f500ebb76bca4713d77b8f644f9960196e3a Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 14 Oct 2013 16:52:52 +0800 Subject: [PATCH 5/8] Rewrite script/test in js. --- script/test | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/script/test b/script/test index deec68c42..708a7b5ec 100755 --- a/script/test +++ b/script/test @@ -1,8 +1,9 @@ -#!/bin/sh +#!/usr/bin/env node +var safeExec = require('./utils/child-process-wrapper.js').safeExec; +var path = require('path'); -set -e +process.chdir(path.dirname(__dirname)); -cd "$(dirname "$0")/.." - -./script/bootstrap -./node_modules/.bin/grunt ci --stack --no-color +safeExec('node script/bootstrap', function() { + safeExec('node node_modules/.bin/grunt ci --stack --no-color', process.exit); +}); From 53b4b1c349d160188827643f00d5f2d17f317734 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 14 Oct 2013 16:59:31 +0800 Subject: [PATCH 6/8] :lipstick: Remove unsed variable. --- script/bootstrap | 1 - 1 file changed, 1 deletion(-) diff --git a/script/bootstrap b/script/bootstrap index c6464147e..59d49a2a5 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -2,7 +2,6 @@ var safeExec = require('./utils/child-process-wrapper.js').safeExec; var path = require('path'); -var apmDir = path.join('vendor', 'apm'); var commands = [ 'git submodule --quiet sync', 'git submodule --quiet update --recursive --init', From 082686bba8192352784d9a1143315fc112d51f71 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 19 Oct 2013 22:11:03 +0800 Subject: [PATCH 7/8] Fix bootstrap script on Windows. --- script/bootstrap | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/script/bootstrap b/script/bootstrap index 59d49a2a5..f87a8c8b1 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -2,22 +2,30 @@ var safeExec = require('./utils/child-process-wrapper.js').safeExec; var path = require('path'); +// Executes an array of commands one by one. +function executeCommands(commands, done, index) { + index = (index == undefined ? 0 : index); + if (index < commands.length) + safeExec(commands[index], executeCommands.bind(this, commands, done, index + 1)); + else + done(null); +} + +// Join multiple commands into one line. +function joinCommands() { + var commandSeparator = process.platform == 'win32' ? '&' : ';'; + return Array.prototype.slice.call(arguments, 0).join(commandSeparator); +} + +var echoNewLine = process.platform == 'win32' ? 'echo.' : 'echo'; var commands = [ 'git submodule --quiet sync', 'git submodule --quiet update --recursive --init', - 'cd vendor/apm; npm install --silent .', + joinCommands('cd vendor/apm', 'npm install --silent .'), 'npm install --silent vendor/apm', - 'echo', - 'node node_modules/.bin/apm install --silent', + echoNewLine, + 'node vendor/apm/bin/apm install --silent', ]; process.chdir(path.dirname(__dirname)); - -var i = 0; -var startCommands = function() { - if (i < commands.length) - safeExec(commands[i++], startCommands); - else - process.exit(0); -} -startCommands(); +executeCommands(commands, process.exit); From dbd1438cf7eac538edb6a9a874386bf0c428259f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 19 Oct 2013 22:32:26 +0800 Subject: [PATCH 8/8] Add script/bootstrap.cmd. --- script/bootstrap.cmd | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 script/bootstrap.cmd diff --git a/script/bootstrap.cmd b/script/bootstrap.cmd new file mode 100644 index 000000000..c9cf66238 --- /dev/null +++ b/script/bootstrap.cmd @@ -0,0 +1,6 @@ +@IF EXIST "%~dp0\node.exe" ( + "%~dp0\node.exe" "%~dp0\bootstrap" %* +) ELSE ( + node "%~dp0\bootstrap" %* +) +