Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
neon build accepts an optional list of modules to build.
Browse files Browse the repository at this point in the history
Also use some utility packages to make the CLI more polished.
  • Loading branch information
Dave Herman committed Apr 1, 2017
1 parent 62c47ed commit 42726bd
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 63 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
"license": "MIT",
"dependencies": {
"chalk": "^1.1.1",
"command-line-args": "^4.0.2",
"command-line-commands": "^2.0.0",
"command-line-usage": "^4.0.0",
"fs-extra": "^0.26.3",
"git-config": "0.0.7",
"handlebars": "^4.0.3",
"in-publish": "^2.0.0",
"inquirer": "^0.11.0",
"minimist": "^1.2.0",
"rsvp": "^3.1.0",
"semver": "^5.1.0",
"shallow-copy": "0.0.1",
Expand Down
228 changes: 176 additions & 52 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,208 @@
import path from 'path';
import metadata from '../package.json';
import minimist from 'minimist';
import neon_new from './ops/neon_new';
import neon_build from './ops/neon_build';
import * as style from './ops/style';
import parseCommands from 'command-line-commands';
import parseArgs from 'command-line-args';
import parseUsage from 'command-line-usage';

function printUsage() {
console.log();
console.log("Usage:");
console.log();
console.log(" neon new [@<scope>/]<name> [--rust|-r nightly|stable|default]");
console.log(" create a new Neon project");
console.log();
console.log(" neon build [--rust|-r nightly|stable|default] [--debug|-d]");
console.log(" rebuild the project");
console.log();
console.log(" neon version");
console.log(" print neon-cli version");
console.log();
console.log(" neon help");
console.log(" print this usage information");
console.log();
console.log("neon-cli@" + metadata.version + " " + path.dirname(__dirname));
function channel(value) {
if (!['default', 'nightly', 'beta', 'stable'].indexOf(value) > -1) {
throw new Error("Expected one of 'default', 'nightly', 'beta', or 'stable', got '" + value + "'");
}
return value;
}

function commandUsage(command) {
if (!spec[command]) {
let e = new Error();
e.command = command;
e.name = 'INVALID_COMMAND';
throw e;
}
console.error(parseUsage(spec[command].usage));
}

const SUBCOMMANDS = {
'version': function() {
console.log(metadata.version);
const spec = {

null: {
args: [{ name: "version", alias: "v", type: Boolean },
{ name: "help", alias: "h", type: Boolean }],
usage: [{
header: "Neon",
content: "Neon is a tool for building native Node.js modules with Rust."
}, {
header: "Synopsis",
content: "$ neon [options] <command>"
}, {
header: "Command List",
content: [{ name: "new", summary: "Create a new Neon project." },
{ name: "build", summary: "(Re)build a Neon project." },
{ name: "version", summary: "Display the Neon version." },
{ name: "help", summary: "Display help information about Neon." }]
}],
action: function(options, usage) {
if (options.version && !options.help) {
spec.version.action.call(this, options);
} else {
console.error(usage);
}
}
},

help: {
args: [{ name: "command", type: String, defaultOption: true },
{ name: "help", alias: "h", type: Boolean }],
usage: [{
header: "neon help",
content: "Get help about a Neon command"
}, {
header: "Synopsis",
content: "$ neon help [command]"
}],
action: function(options) {
if (options && options.command) {
commandUsage(options.command);
} else {
console.error(parseUsage(spec.null.usage));
}
}
},

'help': function() {
printUsage();
new: {
args: [{ name: "rust", alias: "r", type: channel, defaultValue: "default" },
{ name: "name", type: String, defaultOption: true },
{ name: "help", alias: "h", type: Boolean }],
usage: [{
header: "neon new",
content: "Create a new Neon project."
}, {
header: "Synopsis",
content: "$ neon new [options] [@<scope>/]<name>"
}, {
header: "Options",
optionList: [{
name: "rust",
alias: "r",
type: channel,
description: "Rust channel (default, nightly, beta, or stable). [default: default]"
}]
}],
action: function(options) {
if (options.help) {
commandUsage('new');
return;
}

return neon_new(this.cwd, options.name, options.rust);
}
},

'new': function() {
if (this.args._.length !== 1) {
printUsage();
console.log();
throw new Error(this.args._.length === 0 ? "You must specify a project name." : "Too many arguments.");
build: {
args: [{ name: "debug", alias: "d", type: Boolean },
{ name: "path", alias: "p", type: Boolean },
{ name: "rust", alias: "r", type: channel, defaultValue: "default" },
{ name: "modules", type: String, multiple: true, defaultOption: true },
{ name: "node_module_version", type: Number },
{ name: "help", alias: "h", type: Boolean }],
usage: [{
header: "neon build",
content: "(Re)build a Neon project."
}, {
header: "Synopsis",
content: ["$ neon build [options]",
"$ neon build [options] [underline]{module} ..."]
}, {
header: "Options",
optionList: [{
name: "rust",
alias: "r",
type: channel,
description: "Rust channel (default, nightly, beta, or stable). [default: default]"
}, {
name: "debug",
alias: "d",
type: Boolean,
description: "Debug build."
}, {
name: "path",
alias: "p",
type: Boolean,
description: "Specify modules by path instead of name."
}]
}],
action: async function(options) {
if (options.help) {
commandUsage('build');
return;
}

let modules = options.modules
? options.modules.map(m => options.path ? path.resolve(this.cwd, m)
: path.resolve(this.cwd, 'node_modules', m))
: [this.cwd];

let info = modules.length > 1;

for (let mod of modules) {
if (info) {
console.log(style.info("building Neon package at " + (path.relative(this.cwd, mod) || ".")));
}

await neon_build(mod, options.rust, options.debug ? 'debug' : 'release', options.node_module_version);
}
}
return neon_new(this.cwd, this.args._[0], this.args.rust || this.args.r || 'default');
},

'build': function() {
if (this.args._.length > 0) {
printUsage();
console.log();
throw new Error("Too many arguments.");
version: {
args: [{ name: "help", alias: "h", type: Boolean }],
usage: [{
header: "neon version",
content: "Display the Neon version."
}, {
header: "Synopsis",
content: "$ neon version"
}],
action: function(options) {
if (options.help) {
commandUsage('version');
return;
}

console.log(metadata.version);
}
return neon_build(this.cwd,
this.args.rust || this.args.r || 'default',
this.args.debug || this.args.d ? 'debug' : 'release',
this.args.node_module_version);
}

};

export default class CLI {
constructor(argv, cwd) {
this.command = argv[2];
this.args = minimist(argv.slice(3));
this.argv = argv;
this.cwd = cwd;
}

async exec() {
try {
if (!this.command) {
printUsage();
throw null;
}
if (!SUBCOMMANDS.hasOwnProperty(this.command)) {
printUsage();
console.log();
throw new Error("'" + this.command + "' is not a neon command.");
}
await SUBCOMMANDS[this.command].call(this);
// FIXME: use this.argv
let { command, argv } = parseCommands([ null, 'help', 'new', 'build', 'version' ]);

await spec[command].action.call(this,
parseArgs(spec[command].args, { argv }),
parseUsage(spec[command].usage));
} catch (e) {
if (e) {
console.log(style.error(e.message));
spec.help.action.call(this);

switch (e.name) {
case 'INVALID_COMMAND':
console.error(style.error("No manual entry for `neon " + e.command + "`"));
break;

default:
console.error(style.error(e.message));
break;
}
throw e;
}
}
}
20 changes: 10 additions & 10 deletions src/ops/neon_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function explicit_cargo_target() {
}
}

function cargo(toolchain, configuration, nodeModuleVersion, target) {
function cargo(root, toolchain, configuration, nodeModuleVersion, target) {
let macos = process.platform === 'darwin';

let [command, prefix] = toolchain === 'default'
Expand All @@ -57,26 +57,26 @@ function cargo(toolchain, configuration, nodeModuleVersion, target) {

console.log(style.info([command].concat(args).join(" ")));

return spawn(command, args, { cwd: 'native', stdio: 'inherit', env: env });
return spawn(command, args, { cwd: path.resolve(root, 'native'), stdio: 'inherit', env: env });
}

async function main(name, configuration, target) {
async function main(root, name, configuration, target) {
let pp = process.platform;
let output_directory = target ?
path.resolve('native', 'target', target, configuration) :
path.resolve('native', 'target', configuration);
path.resolve(root, 'native', 'target', target, configuration) :
path.resolve(root, 'native', 'target', configuration);
let dylib = path.resolve(output_directory, LIB_PREFIX[pp] + name + LIB_SUFFIX[pp]);
let index = path.resolve('native', 'index.node');
let index = path.resolve(root, 'native', 'index.node');

console.log(style.info("generating native" + path.sep + "index.node"));

await remove(index);
await copy(dylib, index);
}

export default async function neon_build(pwd, toolchain, configuration, nodeModuleVersion) {
export default async function neon_build(root, toolchain, configuration, nodeModuleVersion) {
// 1. Read the Cargo metadata.
let metadata = TOML.parse(await readFile(path.resolve('native', 'Cargo.toml'), 'utf8'));
let metadata = TOML.parse(await readFile(path.resolve(root, 'native', 'Cargo.toml'), 'utf8'));

if (!metadata.lib.name) {
throw new Error("Cargo.toml does not contain a [lib] section with a 'name' field");
Expand All @@ -87,10 +87,10 @@ export default async function neon_build(pwd, toolchain, configuration, nodeModu
console.log(style.info("running cargo"));

// 2. Build the binary.
if ((await cargo(toolchain, configuration, nodeModuleVersion, target)) !== 0) {
if ((await cargo(root, toolchain, configuration, nodeModuleVersion, target)) !== 0) {
throw new Error("cargo build failed");
}

// 3. Copy the dylib into the main index.node file.
await main(metadata.lib.name, configuration, target);
await main(root, metadata.lib.name, configuration, target);
}

0 comments on commit 42726bd

Please sign in to comment.