Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

WIP: Migrations #143

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4ae4d40
Expect `truffle exec` files to be modules instead of scripts. This re…
Apr 20, 2016
35fac07
Basis for migrations. Lots of refactoring across the board. Warning: …
May 13, 2016
6fc2f80
Various fixes. Namely, you're not allowed to add deployer steps once …
May 13, 2016
4bc82d7
Refactoring of configuration and related items to drastically reduce …
May 16, 2016
4c3191b
Full migration functionality and recording it on the blockchain.
May 18, 2016
55c8afc
Make init work. Also, migration numbers need to be >= 1
May 18, 2016
8d71347
Fix modified time vs. built time detection, and remove a bunch of dep…
May 18, 2016
8e0fd11
Continue work on managing networks through the config. Currently unfi…
May 23, 2016
31c9bd0
Rewrite test runner to new migrations systems. In doing so, create a …
May 23, 2016
bd4f567
Detect when abstract contracts are passed to autolink(), and don't do…
May 23, 2016
851feaf
Allow autolink() to autolink all libraries and contracts if none are …
May 23, 2016
5533489
Get truffle build/serve working.
May 24, 2016
8e036e9
Cleanup solc garbage output.
May 24, 2016
69d3a75
Make example deploy a bit simpler.
May 24, 2016
b7d8f8a
Upgrade to newest release of EtherPudding.
May 24, 2016
e0d46c4
Rewrite console. This paves the way for a completely interactive truf…
May 24, 2016
ff560c0
Get truffle exec to work.
May 25, 2016
5ffe00a
Start work on managing network information. Still very buggy. Added `…
May 25, 2016
068e5a6
Truffle is no longer a Truffle project (I'm taking Truffle out of you…
May 26, 2016
ece9350
Move networks functionality to profiler.
May 26, 2016
b670f27
Move Config.sandbox() to Init.sanbox() (feels right). Start on migrat…
May 26, 2016
ec61b33
Add more migration tests. Refactor cli tasks and command runner into …
May 27, 2016
985acb6
Reprovision after each command in the console so you can work with ne…
May 27, 2016
68fd4ab
Deployer: Move actions into individual functions that can be queued o…
Jun 3, 2016
f06df57
Whoops, I made one untested change before committing last time. :(
Jun 3, 2016
f0d8e19
Clean up API a bit.
Jun 6, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
353 changes: 41 additions & 312 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,332 +1,61 @@
#!/usr/bin/env node
require("babel-register");

var web3 = require("web3");
var path = require("path");
var fs = require("fs");
var chokidar = require('chokidar');
var deasync = require("deasync");
var colors = require('colors/safe');
var Truffle = require('./index.js');

var ConfigurationError = require("./lib/errors/configurationerror");
var Command = require("./lib/command");
var Tasks = require("./lib/tasks");
var TaskError = require("./lib/errors/taskerror");
var ExtendableError = require("./lib/errors/extendableerror");

var argv = require('yargs').argv;

var truffle_dir = process.env.TRUFFLE_NPM_LOCATION || argv.n || argv.npm_directory || __dirname;
var working_dir = process.env.TRUFFLE_WORKING_DIRECTORY || argv.w || argv.working_directory || process.cwd();

if (working_dir[working_dir.length - 1] != "/") {
working_dir += "/";
}

var pkg = JSON.parse(fs.readFileSync(path.join(truffle_dir, "package.json"), {encoding: "utf8"}));

var tasks = {};
var registerTask = function(name, description, fn) {
tasks[name] = {
name: name,
description: description,
fn: fn
};
}

var printSuccess = function() {
console.log(colors.green("Completed without errors on " + new Date().toString()));
};

var printFailure = function() {
console.log(colors.red("Completed with errors on " + new Date().toString()));
};

var runTask = function(name) {
try {
var fn = deasync(tasks[name].fn);
return fn() || 0;
} catch (e) {
if (e instanceof ExtendableError) {
console.log(e.message);

if (argv.stack != null) {
console.log(e.stack);
}

var command = new Command(Tasks);
command.run(process.argv.slice(2), function(err) {
if (err) {
if (err instanceof TaskError) {
command.run("list", function() {});
} else {
// Bubble up all other unexpected errors.
console.log(e.stack || e.toString());
}
return 1;
}
};

registerTask('watch', "Watch filesystem for changes and rebuild the project automatically", function(done) {
var needs_rebuild = true;
var needs_redeploy = false;

chokidar.watch(["app/**/*", "environments/*/contracts/**/*", "contracts/**/*", "truffle.json", "truffle.js"], {
ignored: /[\/\\]\./, // Ignore files prefixed with "."
cwd: working_dir,
ignoreInitial: true
}).on('all', function(event, filePath) {
// On changed/added/deleted
var display_path = path.join("./", filePath.replace(working_dir, ""));
console.log(colors.cyan(">> File " + display_path + " changed."));

needs_rebuild = true;

if (display_path.indexOf("contracts/") == 0) {
needs_redeploy = true;
} else {
needs_rebuild = true;
}
});

var check_rebuild = function() {
if (needs_redeploy == true) {
needs_redeploy = false;
needs_rebuild = false;
console.log("Redeploying...");
if (runTask("deploy") != 0) {
printFailure();
}
}

if (needs_rebuild == true) {
needs_rebuild = false;
console.log("Rebuilding...");
if (runTask("build") != 0) {
printFailure();
if (err instanceof ExtendableError) {
console.log(err.message);
} else {
// Bubble up all other unexpected errors.
console.log(err.stack || err.toString());
}
}

setTimeout(check_rebuild, 200);
};

check_rebuild();
});

registerTask('list', "List all available tasks", function(done) {
console.log("Truffle v" + pkg.version + " - a development framework for Ethereum");
console.log("");
console.log("Usage: truffle [command] [options]");
console.log("");
console.log("Commands:");
console.log("");

var sorted = Object.keys(tasks).sort();

var longestTask = sorted.reduce(function(a, b) {
var first = typeof a == "string" ? a.length : a;
return Math.max(first, b.length);
});

for (var i = 0; i < sorted.length; i++) {
var task = tasks[sorted[i]];
var heading = task.name;
while (heading.length < longestTask) {
heading += " ";
}
console.log(" " + heading + " => " + task.description)
}

console.log("");
done();
});

registerTask('version', "Show version number and exit", function(done) {
console.log("Truffle v" + pkg.version);
done();
});

registerTask('init', "Initialize new Ethereum project, including example contracts and tests", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv);
Truffle.init.all(config, done);
});

registerTask('create:contract', "Create a basic contract", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv);

var name = argv.name;

if (name == null && argv._.length > 1) {
name = argv._[1];
}

if (name == null) {
throw new ConfigurationError("Please specify a name. Example: truffle create:contract MyContract");
} else {
Truffle.create.contract(config, name, done);
}
});

registerTask('create:test', "Create a basic test", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv);

var name = argv.name;

if (name == null && argv._.length > 1) {
name = argv._[1];
}

if (name == null) {
throw new ConfigurationError("Please specify a name. Example: truffle create:test MyTest");
} else {
Truffle.create.test(config, name, done);
}
});

registerTask('compile', "Compile contracts", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv, "development");
Truffle.contracts.compile(config, done);
});

registerTask('deploy', "Deploy contracts to the network, compiling if needed", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv, "development");

console.log("Using environment " + config.environment + ".");

var compile = true;
var link = true;

if (argv.compile === false) {
compile = false;
}

// Compile and deploy.
Truffle.contracts.deploy(config, compile, function(err) {
if (err != null) {
done(err);
} else {
// console.log("Rebuilding app with new contracts...");
// runTask("build");
done();
}
});
});

registerTask('build', "Build development version of app", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv, "development");
Truffle.build.build(config, function(err) {
done(err);
if (err == null) {
printSuccess();
}
});
});

registerTask('dist', "Create distributable version of app (minified)", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv, "production");
console.log("Using environment " + config.environment + ".");
Truffle.build.dist(config, function(err) {
done(err);
if (err == null) {
printSuccess();
}
});
});

registerTask('exec', "Execute a JS file within truffle environment. Script *must* call process.exit() when finished.", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv, "development");

var file = argv.file;

if (file == null && argv._.length > 1) {
file = argv._[1];
}

if (file == null) {
console.log("Please specify a file, passing the path of the script you'd like the run. Note that all scripts *must* call process.exit() when finished.");
done();
return;
}

Truffle.exec.file(config, file, done);
});

// Supported options:
// --no-color: Disable color
// More to come.
registerTask('test', "Run tests", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv, "test");

console.log("Using environment " + config.environment + ".");

// Ensure we're quiet about deploys during tests.
config.argv.quietDeploy = true;

var file = argv.file;

if (file == null && argv._.length > 1) {
file = argv._[1];
}

if (file == null) {
Truffle.test.run(config, done);
} else {
Truffle.test.run(config, file, done);
}
});

registerTask('console', "Run a console with deployed contracts instantiated and available (REPL)", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv, "development");
Truffle.console.run(config, done);
});

registerTask('serve', "Serve app on localhost and rebuild changes as needed", function(done) {
var config = Truffle.config.gather(truffle_dir, working_dir, argv, "development");
console.log("Using environment " + config.environment + ".");
Truffle.serve.start(config, argv.port || argv.p || "8080", function() {
runTask("watch");
});
});



// registerTask('watch:tests', "Watch filesystem for changes and rerun tests automatically", function(done) {
//var environment = argv.e || argv.environment || process.env.NODE_ENV || "default";
//
// gaze(["app/**/*", "config/**/*", "contracts/**/*", "test/**/*"], {cwd: working_dir, interval: 1000, debounceDelay: 500}, function() {
// // On changed/added/deleted
// this.on('all', function(event, filePath) {
// if (filePath.match(/\/config\/.*?\/.*?\.sol\.js$/)) {
// // ignore changes to /config/*/*.sol.js since these changes every time
// // tests are run (when contracts are compiled)
// return;
// }
// process.stdout.write("\u001b[2J\u001b[0;0H"); // clear screen
// var display_path = "./" + filePath.replace(working_dir, "");
// console.log(colors.cyan(">> File " + display_path + " changed."));
// run_tests();
// });
// });
// if (working_dir[working_dir.length - 1] != "/") {
// working_dir += "/";
// }
//
// var run_tests = function() {
// console.log("Running tests...");
// var printNetwork = function() {
// console.log("Using network " + environment + ".");
// };
//
// process.chdir(working_dir);
// var config = Truffle.config.gather(truffle_dir, working_dir, argv, "test");
// config.argv.quietDeploy = true; // Ensure we're quiet about deploys during tests
// var printSuccess = function() {
// console.log(colors.green("Completed without errors on " + new Date().toString()));
// };
//
// var printFailure = function() {
// console.log(colors.red("Completed with errors on " + new Date().toString()));
// };
//
// Test.run(config, function() { console.log("> test run complete; watching for changes..."); });
// };
// run_tests(); // run once immediately
//
// });


// Default to listing available commands.
var current_task = argv._[0];

if (current_task == null) {
current_task = "list";
}

if (tasks[current_task] == null) {
console.log(colors.red("Unknown command: " + current_task));
process.exit(1);
}

// Something is keeping the process open. I'm not sure what.
// Let's explicitly kill it until I can figure it out.
process.exit(runTask(current_task));
//runTask(current_task);
// // Check to see if we're working on a dapp meant for 0.2.x or older
// if (fs.existsSync(path.join(working_dir, "config", "app.json"))) {
// console.log("Your dapp is meant for an older version of Truffle. Don't worry, there are two solutions!")
// console.log("");
// console.log("1) Upgrade you're dapp using the followng instructions (it's easy):");
// console.log(" https://github.com/ConsenSys/truffle/wiki/Migrating-from-v0.2.x-to-v0.3.0");
// console.log("");
// console.log(" ( OR )")
// console.log("");
// console.log("2) Downgrade to Truffle 0.2.x");
// console.log("");
// console.log("Cheers! And file an issue if you run into trouble! https://github.com/ConsenSys/truffle/issues")
// process.exit();
// }
5 changes: 0 additions & 5 deletions contracts/Example.sol

This file was deleted.

1 change: 0 additions & 1 deletion environments/test/config.json

This file was deleted.

Loading