Skip to content
Browse files

builder & titanium functions

  • Loading branch information...
1 parent 0fb6b73 commit cca8b8149a4dd3ee5090c8f6f8c05f19b939df36 @rf committed
Showing with 229 additions and 68 deletions.
  1. 0 fixtures/2/.gitkeep
  2. BIN fixtures/sdk.zip
  3. +148 −65 index.js
  4. +2 −0 package.json
  5. +79 −3 test.js
View
0 fixtures/2/.gitkeep
No changes.
View
BIN fixtures/sdk.zip
Binary file not shown.
View
213 index.js
@@ -12,6 +12,152 @@ var path = require("path");
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var exec = require('child_process').exec;
+var spawn = require('child_process').spawn;
+var rimraf = require('rimraf');
+
+var tsm = {};
+module.exports = tsm;
+
+// ## Public API
+
+// ### install
+// * `options` object
+// * `output` output directory
+// * `input` git hash or version to match
+// * `os` os to match, should be 'osx' 'win32' 'linux'
+// * `done` callback, called with `(error)`
+//
+// Installs a matching SDK to the provided directory. Returns an emitter which
+// will emit `progress` events like `{left: 38413, done: 5893, percent: 34}`,
+// `debug` events and `log` events.
+tsm.install = function (options, done) {
+ var emitter = options.emitter || new EventEmitter();
+
+ tsm.getAllBuilds(options.input, options.os, function (error, builds) {
+ if (error) return done(error);
+ if (builds.length === 0) return done(new Error("no matching SDK versions"));
+ var build = builds.pop();
+ var total = build.size;
+ var left = total;
+ var dest = path.join(options.output, build.filename);
+
+ var req = request(build.zip);
+ req.pipe(fs.createWriteStream(dest));
+
+ req.on('error', done);
+
+ req.on('data', function (buffer) {
+ left -= buffer.length;
+ emitter.emit('progress', {
+ left: left,
+ done: total - left,
+ percent: (left / total) * 100
+ });
+ });
+
+ req.on('end', function () {
+ emitter.emit('debug', "complete");
+ emitter.emit('downloaded');
+ tsm.unzip(dest, options.output, function (er) {
+ if (er) return done(er);
+
+ fs.unlink(options.output + "/" + build.filename, function (er) {
+ if (er) return done(er);
+
+ done(null);
+ emitter.emit('done');
+ });
+ });
+ });
+ }, emitter);
+};
+
+// ### remove
+// * `options` object
+// * `dir` directory to find sdks
+// * `input` git hash or version to match
+// * `done` callback called with `(error)`
+//
+// Removes sdks matching `input`. Returns an emitter which may emit `debug`
+// and/or `log` events.
+
+tsm.remove = function (options, done) {
+ var emitter = options.emitter || new EventEmitter();
+
+ tsm.findInstalled(options.dir, options.input, function (error, builds) {
+ if (error) return done(error);
+ if (builds.length === 0) return done(new Error('no matched builds'));
+ var dirs = builds.map(function (item) { return item.dir; });
+ async.forEach(dirs, rimraf, done);
+ }, emitter);
+
+ return emitter;
+};
+
+// ### builder
+// * `options` object
+// * `dir` directory to find sdks
+// * `input` git hash or version to match
+// * `os` 'android' or 'iphone'
+// * `args` array of arguments to pass to builder.py
+// * `python` string, optional, path of python
+// * `done` callback to call when completed or upon error
+//
+// Runs the builder.py script for the specified `os`.
+
+tsm.builder = function (options, done) {
+ var emitter = options.emitter || new EventEmitter();
+ options.python = options.python || 'python';
+
+ tsm.findInstalled(options.dir, options.input, function (error, builds) {
+ if (error) return done(error);
+ if (builds.length === 0) return done (new Error('no matched builds'));
+ var build = builds.pop();
+
+ var args = [path.join(build.dir, options.os, 'builder.py')];
+ var child = spawn('python', args.concat(options.args || []));
+ emitter.emit('spawned', child);
+
+ child.on('exit', function (code) {
+ done(code === 0? null : new Error("process exited with code: " + code));
+ });
+ }, emitter);
+
+ return emitter;
+};
+
+// ### titanium
+// * `options` object
+// * `dir` directory to find sdks
+// * `input` git hash or version to match
+// * `args` array of arguments to pass to titanium.py
+// * `python` string, optional, path of python
+// * `done` callback to call when completed or upon error
+//
+// Runs the titanium.py script
+
+tsm.titanium = function (options, done) {
+ var emitter = options.emitter || new EventEmitter();
+ options.python = options.python || 'python';
+
+ tsm.findInstalled(options.dir, options.input, function (error, builds) {
+ if (error) return done(error);
+ if (builds.length === 0) return done (new Error('no matched builds'));
+ var build = builds.pop();
+
+ var args = [path.join(build.dir, 'titanium.py')];
+ var child = spawn('python', args.concat(options.args || []));
+ emitter.emit('spawned', child);
+
+ child.on('exit', function (code) {
+ done(code === 0? null : new Error("process exited with code: " + code));
+ });
+ }, emitter);
+
+ return emitter;
+};
+
+// ## Helper functions
var branchesURL =
'http://builds.appcelerator.com.s3.amazonaws.com/mobile/branches.json';
@@ -21,9 +167,6 @@ var branchURL =
// zipURL needs branch/zipname added to it
var zipURL = 'http://builds.appcelerator.com.s3.amazonaws.com/mobile/';
-var tsm = {};
-module.exports = tsm;
-
// ### gitCheck
// * `input` some partial hash
// * `revision` some git revision hash
@@ -248,7 +391,8 @@ tsm.findInstalled = function (dir, input, done, emitter) {
memo.push({
githash: build.githash,
version: build.version,
- date: new Date(build.timestamp)
+ date: new Date(build.timestamp),
+ dir: path.join(dir, version)
});
}
@@ -362,64 +506,3 @@ tsm.unzip = function (zip, output, done) {
else
exec("unzip -oqq '" + zip + "' -d '" + output + "'", done);
};
-
-// ### install
-// * `output` output directory
-// * `input` git hash or version to match
-// * `os` os to match, should be 'osx' 'win32' 'linux'
-//
-// Installs a matching SDK to the provided directory.
-tsm.install = function (options, done) {
- var emitter = options.emitter || new EventEmitter();
-
- tsm.getAllBuilds(options.input, options.os, function (error, builds) {
- if (error) return done(error);
- if (builds.length === 0) return done(new Error("no matching SDK versions"));
- var build = builds.pop();
- var total = build.size;
- var left = total;
- var dest = path.join(options.output, build.filename);
-
- var req = request(build.zip);
- req.pipe(fs.createWriteStream(dest));
-
- req.on('error', done);
-
- req.on('data', function (buffer) {
- left -= buffer.length;
- emitter.emit('progress', {
- left: left,
- done: total - left,
- percent: (left / total) * 100
- });
- });
-
- req.on('end', function () {
- emitter.emit('debug', "complete");
- emitter.emit('downloaded');
- tsm.unzip(dest, options.output, function (er) {
- if (er) return done(er);
-
- fs.unlink(options.output + "/" + build.filename, function (er) {
- if (er) return done(er);
-
- done(null);
- emitter.emit('done');
- });
- });
- });
- }, emitter);
-};
-
-tsm.remove = function (options, done) {
-
-};
-
-tsm.builder = function (options, done) {
-
-};
-
-tsm.titanium = function (options, done) {
-
-};
-
View
2 package.json
@@ -11,10 +11,12 @@
, "dependencies":
{ "flatiron": "0.1.x"
, "shelljs": "0.0.x"
+ , "rimraf": "2.0.x"
, "coffee-script": "1.3.x"
, "semver": "1.0.x"
, "d8": "0.2.x"
, "cliff": "0.1.x"
+ , "ncp": "0.2.x"
, "request": "2.9.x"
, "async": "0.1.x"
, "colors": "0.6.x"
View
82 test.js
@@ -1,3 +1,20 @@
+var EventEmitter = require("events").EventEmitter;
+
+// simple child process mocking
+var oldspawn = require('child_process').spawn;
+var spawned;
+var spawnerror = false;
+require('child_process').spawn = function (name, args) {
+ spawned = {name: name, args: args};
+ var emitter = new EventEmitter();
+
+ process.nextTick(function () {
+ if (spawnerror) emitter.emit('end', 127);
+ else emitter.emit('exit', 0);
+ });
+ return emitter;
+};
+
var assert = require("assert");
var tsm;
if (process.env.TSM_COV) tsm = require("./lib-cov/index");
@@ -7,6 +24,8 @@ var EventEmitter = require('events').EventEmitter;
var nock = require('nock');
var fs = require('fs');
if (!fs.exists) fs.exists = path.exists;
+var ncp = require('ncp').ncp;
+var rimraf = require('rimraf');
suite('gitCheck', function () {
var hash0 = "e721f1820f65368794aee29e2da8ebc03de804fd";
@@ -492,11 +511,10 @@ suite('unzip', function () {
suite('install', function () {
test('functional', function (done) {
- this.timeout(10000);
var zip = __dirname + "/fixtures/test.zip";
var output = __dirname + "/fixtures/2/";
var zipurl = "/mobile/master/mobilesdk-2.2.0.v20120828153312-osx.zip";
- var path = __dirname + "/fixtures/2/index.js";
+ var path = __dirname + "/fixtures/2/1.8.2/version.txt";
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/branches.json')
@@ -506,7 +524,7 @@ suite('install', function () {
.replyWithFile(200, __dirname + '/fixtures/master-index.json')
.get(zipurl)
- .replyWithFile(200, __dirname + '/fixtures/test.zip');
+ .replyWithFile(200, __dirname + '/fixtures/sdk.zip');
var emitter = tsm.install({
output: output,
@@ -525,3 +543,61 @@ suite('install', function () {
});
});
});
+
+suite('delete', function () {
+ test('functional', function (done) {
+ var src = __dirname + "/fixtures/1/";
+ var sdkdir = __dirname + "/fixtures/2";
+ ncp(src, sdkdir, function (err) {
+ if (err) return done(err);
+
+ tsm.remove({dir: sdkdir, input: "1.8.x"}, function (error) {
+ if (error) return done(error);
+
+ fs.exists(sdkdir + "/1.8.2", function (exists) {
+ if (exists) return done(new Error("sdk wasn't deleted"));
+
+ fs.exists(sdkdir + "/2.1.0", function (exists) {
+ if (!exists) return done(new Error("wrong sdk deleted"));
+ rimraf(sdkdir, function () {
+ fs.mkdir(sdkdir, done);
+ });
+ });
+ });
+ });
+ });
+ });
+});
+
+suite('builder', function () {
+ test('functional: iphone', function (done) {
+ var sdkdir = __dirname + "/fixtures/1/";
+ var options = {dir: sdkdir, input: '1.8', os: 'iphone', args: ['foo', 'bar']};
+ tsm.builder(options, function (error) {
+ try {
+ assert(spawned.name === "python");
+ assert(spawned.args[0] === "/Users/rfranknj/code/tsm/fixtures/1/1.8.2/iphone/builder.py");
+ assert(spawned.args[1] === "foo");
+ assert(spawned.args[2] === "bar");
+ done();
+ } catch (e) { done(e); }
+ });
+ });
+});
+
+suite('titanium', function () {
+ test('functional', function (done) {
+ var sdkdir = __dirname + "/fixtures/1/";
+ var options = {dir: sdkdir, input: '1.8', args: ['foo', 'bar']};
+ tsm.titanium(options, function (error) {
+ try {
+ assert(spawned.name === "python");
+ assert(spawned.args[0] === "/Users/rfranknj/code/tsm/fixtures/1/1.8.2/titanium.py");
+ assert(spawned.args[1] === "foo");
+ assert(spawned.args[2] === "bar");
+ done();
+ } catch (e) { done(e); }
+ });
+ });
+});
+

0 comments on commit cca8b81

Please sign in to comment.
Something went wrong with that request. Please try again.