Skip to content

Commit

Permalink
Addresses review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcr authored and rwaldron committed Oct 13, 2016
1 parent 69e1bfe commit b49841e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 65 deletions.
5 changes: 4 additions & 1 deletion bin/tessel-2.js
Expand Up @@ -569,14 +569,17 @@ parser.command('sdk')
} else if (options.subcommand === 'remove') {
options.operation = 'removeSdk';
callControllerWith('installer', options);
} else {
// Unreachable: this case should not be allowed by nomnom.
throw new Error('Expected subcommand "t2 sdk install" or "t2 sdk remove".');
}
})
.option('subcommand', {
position: 1,
required: true,
choices: ['install', 'remove']
})
.help('SDK management');
.help('Installs the Tessel SDK for binary cross-compilation.');

module.exports = function(args) {
var sIndexOfSA = -1;
Expand Down
109 changes: 54 additions & 55 deletions lib/sdk.js → lib/install/rust.js
@@ -1,20 +1,24 @@
// System Objects
var path = require('path');
var spawn = require('child_process').spawn;
var Transform = require('stream').Transform;
var zlib = require('zlib');

// Third Party Dependencies
var blocks = require('block-stream2');
var bz2 = require('unbzip2-stream');
var createHash = require('sha.js');
var fs = require('fs-extra-promise');
var fsTemp = require('fs-temp');
var osenv = require('osenv');
var path = require('path');
var Progress = require('progress');
var request = require('request');
var spawn = require('child_process').spawn;
var tar = require('tar-fs');
var tmp = require('tmp');
var Transform = require('stream').Transform;
var zlib = require('zlib');

var log = require('./log');
// Internal
var log = require('../log');

var getPlatform = exports.getPlatform = () => {
function getPlatform() {
switch (process.platform) {
case 'darwin':
return 'macos';
Expand All @@ -25,17 +29,17 @@ var getPlatform = exports.getPlatform = () => {
}
};

var paths = {
var SDK_PATHS = {
sdk: path.join(osenv.home(), '.tessel/sdk'),
rustlib: path.join(osenv.home(), '.tessel/rust'),
};

var sdkPath = {
'macos': 'https://builds.tessel.io/t2/sdk/t2-sdk-macos-x86_64.tar.bz2',
'linux': 'https://builds.tessel.io/t2/sdk/t2-sdk-linux-x86_64.tar.bz2',
var SDK_URLS = {
macos: 'https://builds.tessel.io/t2/sdk/t2-sdk-macos-x86_64.tar.bz2',
linux: 'https://builds.tessel.io/t2/sdk/t2-sdk-linux-x86_64.tar.bz2',
};

var rustlibUrl = 'https://builds.tessel.io/t2/sdk/t2-rustlib-VERSION.tar.gz';
var RUST_LIB_TGZ_URL = 'https://builds.tessel.io/t2/sdk/t2-rustlib-VERSION.tar.gz';

function sha256stream() {
var sha256 = createHash('sha256');
Expand All @@ -52,7 +56,7 @@ function sha256stream() {
}

function sha256file(hash, name) {
return hash + ' ' + name + '\n';
return `${hash} ${name}\n`;
}

function download(url) {
Expand Down Expand Up @@ -85,7 +89,7 @@ function download(url) {
function downloadString(url) {
return new Promise((resolve, reject) => {
request({
url: url,
url,
// We want to force Cloudfront to serve us the latest file.
headers: {
'Accept-Encoding': 'gzip, deflate',
Expand All @@ -100,27 +104,28 @@ function downloadString(url) {
});
}

var tmpdir = exports.tmpdir = () => {
function tmpdir() {
return new Promise((resolve, reject) => {
tmp.dir(function(err, tmppath, cleanup) {
if (err) {
reject(err);
} else {
resolve({
path: tmppath,
cleanup: cleanup,
});
var dir = fsTemp.template('t2-sdk-%s').mkdirSync();
resolve({
path: dir,
cleanup: () => {
try {
fs.removeSync(dir);
} catch (e) {
// Swappow errors in removing temporary folder.
}
}
});
});
};

exports.toolchainPath = () => {
return fs.readdirAsync(path.join(paths.sdk, getPlatform()))
module.exports.toolchainPath = () => {
return fs.readdirAsync(path.join(SDK_PATHS.sdk, getPlatform()))
.then((values) => new Promise((resolve, reject) => {
for (var i = 0; i < values.length; i++) {
if (values[i].match(/^toolchain\-/)) {
return resolve(path.join(paths.sdk, getPlatform(), values[i]));
return resolve(path.join(SDK_PATHS.sdk, getPlatform(), values[i]));
}
}
return reject(new Error('No toolchain found.'));
Expand All @@ -129,8 +134,8 @@ exports.toolchainPath = () => {

// Checks is CHECKSUM file in our SDK equals our expected checksum.
// This will resolve with checking that the SDK exists and matches the checksum.
exports.checkSdk = (checksumVerify) => {
var dir = path.join(paths.sdk, getPlatform());
module.exports.checkSdk = (checksumVerify) => {
var dir = path.join(SDK_PATHS.sdk, getPlatform());
return fs.readFileAsync(path.join(dir, 'CHECKSUM'), 'utf-8')
.then(checksum => ({
exists: true,
Expand All @@ -143,8 +148,8 @@ exports.checkSdk = (checksumVerify) => {
}));
};

exports.checkRustlib = (rustv, checksumVerify) => {
var dir = path.join(paths.rustlib, rustv);
module.exports.checkRustlib = (rustv, checksumVerify) => {
var dir = path.join(SDK_PATHS.rustlib, rustv);
return fs.readFileAsync(path.join(dir, 'CHECKSUM'), 'utf-8')
.then(checksum => ({
exists: true,
Expand All @@ -157,8 +162,8 @@ exports.checkRustlib = (rustv, checksumVerify) => {
}));
};

exports.installSdk = () => {
var url = sdkPath[getPlatform()];
module.exports.installSdk = () => {
var url = SDK_URLS[getPlatform()];
var checksumVerify = null;

return downloadString(url + '.sha256')
Expand All @@ -181,7 +186,7 @@ exports.installSdk = () => {
});
};

exports.installRustlib = () => {
module.exports.installRustlib = () => {
var url = null;
var checksumVerify = null;
var rustv = null;
Expand All @@ -191,7 +196,7 @@ exports.installRustlib = () => {
.then(_rustv => {
rustv = _rustv;
pkgname = 'MIPS libstd v' + rustv;
url = rustlibUrl.replace('VERSION', rustv);
url = RUST_LIB_TGZ_URL.replace('VERSION', rustv);

return downloadString(url + '.sha256');
})
Expand All @@ -212,7 +217,7 @@ exports.installRustlib = () => {
log.info(`Updating ${pkgname}...`);
}

return fs.mkdirpAsync(paths.rustlib)
return fs.mkdirpAsync(SDK_PATHS.rustlib)
.then(() => extractRustlib(checksumVerify, path.basename(url), download(url), rustv));
});
};
Expand All @@ -231,12 +236,6 @@ function extract(checksumVerify, filename, sdkStream, root, strip, name, decompr
}
});

function tryCleanup() {
try {
destdir.cleanup();
} catch (e) {}
}

return new Promise((resolve, reject) => {
var checksum = '';
sdkStream
Expand Down Expand Up @@ -269,10 +268,10 @@ function extract(checksumVerify, filename, sdkStream, root, strip, name, decompr
return fs.moveAsync(destdir.path, root);
})
.then(() => {
tryCleanup();
destdir.cleanup();
resolve();
}, e => {
tryCleanup();
destdir.cleanup();
reject(e);
});
})
Expand All @@ -285,16 +284,16 @@ function extract(checksumVerify, filename, sdkStream, root, strip, name, decompr
}

function extractSdk(checksumVerify, filename, sdkStream) {
var root = path.join(paths.sdk, 'macos');
var root = path.join(SDK_PATHS.sdk, 'macos');
return extract(checksumVerify, filename, sdkStream, root, 2, 'SDK', bz2());
}

function extractRustlib(checksumVerify, filename, sdkStream, rustVersion) {
var root = path.join(paths.rustlib, rustVersion);
var root = path.join(SDK_PATHS.rustlib, rustVersion);
return extract(checksumVerify, filename, sdkStream, root, 0, 'MIPS libstd', zlib.createGunzip());
}

exports.getBuildConfig = () => {
module.exports.getBuildConfig = () => {
var config = {
rustv: null,
toolchainPath: null,
Expand Down Expand Up @@ -332,7 +331,7 @@ exports.getBuildConfig = () => {
});
};

exports.rustVersion = () => {
module.exports.rustVersion = () => {
return new Promise((resolve, reject) => {
var rustc = spawn('rustc', ['-V']);
var stdout = [];
Expand All @@ -352,8 +351,8 @@ exports.rustVersion = () => {
});
};

exports.cargoMetadata = () => {
return new Promise((resolve, reject) => { // jshint ignore:line
module.exports.cargoMetadata = () => {
return new Promise((resolve) => {
var cargo = spawn('cargo', ['metadata', '--no-deps'], {
stdio: ['ignore', 'pipe', 'inherit'],
});
Expand All @@ -371,16 +370,16 @@ exports.cargoMetadata = () => {
});
};

exports.buildTessel = (config) => {
module.exports.buildTessel = (config) => {
var env = Object.assign({}, process.env);
Object.assign(env, {
STAGING_DIR: config.stagingDir,
RUST_TARGET_PATH: config.rustlibPath,
PATH: path.join(config.toolchainPath, 'bin') + ':' + env.PATH,
RUSTFLAGS: '-L ' + config.rustlibPath,
PATH: `${path.join(config.toolchainPath, 'bin')}:${env.PATH}`,
RUSTFLAGS: `-L ${config.rustlibPath}`,
});

return new Promise((resolve, reject) => { // jshint ignore:line
return new Promise((resolve) => {
var cargo = spawn('cargo', ['build', '--target=tessel2', '--bin', config.name, '--release'], {
env: env,
stdio: ['ignore', 'inherit', 'inherit'],
Expand All @@ -400,8 +399,8 @@ exports.buildTessel = (config) => {
});
};

exports.bundleTessel = (config) => {
return new Promise((resolve, reject) => { // jshint ignore:line
module.exports.bundleTessel = (config) => {
return new Promise((resolve) => {
var tarball = path.join(path.dirname(config.path), 'tessel-bundle.tar');
tar.pack(path.dirname(config.path), {
entries: [path.basename(config.path)]
Expand Down
4 changes: 1 addition & 3 deletions lib/installer.js
Expand Up @@ -7,12 +7,10 @@ var path = require('path');
// Third Party Dependencies
var fs = require('fs-extra');
var osenv = require('osenv');
var path = require('path');
var osenv = require('osenv');

// Internal
var log = require('./log');
var sdk = require('./sdk');
var rust = require('./install/rust');

module.exports.drivers = function() {
return new Promise((resolve, reject) => {
Expand Down
10 changes: 5 additions & 5 deletions lib/tessel/deployment/rust.js
Expand Up @@ -16,11 +16,11 @@ var commands = require('../commands');
var lists = require('./lists/rust');
var log = require('../../log');
var Tessel = require('../tessel');
var sdk = require('../../sdk');
var rust = require('../../install/rust');

function bundle() {
var config;
return sdk.getBuildConfig()
return rust.getBuildConfig()
.catch(e => {
console.error('Could not find all the components for cross-compiling Rust.');
console.error(e.message);
Expand All @@ -29,7 +29,7 @@ function bundle() {
process.exit(1);
})
.then(_config => config = _config)
.then(() => sdk.cargoMetadata())
.then(() => rust.cargoMetadata())
.then(metadata => {
// Get first package.
var pkg = metadata.packages.pop();
Expand Down Expand Up @@ -69,8 +69,8 @@ function bundle() {
config.name = out.name;
config.path = dest;
})
.then(() => sdk.buildTessel(config))
.then(() => sdk.bundleTessel(config))
.then(() => rust.buildTessel(config))
.then(() => rust.bundleTessel(config))
.then(tarball => {
return {
name: config.name,
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -80,7 +80,6 @@
"tar": "^2.1.1",
"tar-fs": "^1.13.2",
"tar-stream": "^1.3.0",
"tmp": "0.0.29",
"toml": "^2.3.0",
"uglify-js": "^2.6.2",
"unbzip2-stream": "^1.0.10",
Expand Down

0 comments on commit b49841e

Please sign in to comment.