platform/lib/src/commands/init.dart

105 lines
3 KiB
Dart
Raw Normal View History

2016-09-18 03:56:08 +00:00
import "dart:io";
import "package:args/command_runner.dart";
import "package:console/console.dart";
2016-12-24 04:42:55 +00:00
import 'package:random_string/random_string.dart' as rs;
import 'key.dart';
2016-09-18 03:56:08 +00:00
class InitCommand extends Command {
2016-12-24 04:42:55 +00:00
final KeyCommand _key = new KeyCommand();
2016-09-18 03:56:08 +00:00
final TextPen _pen = new TextPen();
@override
String get name => "init";
@override
String get description =>
"Initializes a new Angel project in the current directory.";
InitCommand() {}
@override
run() async {
Directory projectDir = new Directory(
argResults.arguments.isEmpty ? "." : argResults.arguments[0]);
print("Creating new Angel project in ${projectDir.absolute.path}...");
2016-12-24 04:42:55 +00:00
await _cloneRepo(projectDir);
_pen.green();
_pen(
"${Icon.CHECKMARK} Successfully initialized Angel project. Now running pub get...");
2016-09-18 03:56:08 +00:00
_pen();
2016-09-21 23:47:50 +00:00
await _pubGet(projectDir);
2016-12-31 16:50:35 +00:00
await preBuild(projectDir);
2016-12-24 04:42:55 +00:00
var secret = rs.randomAlphaNumeric(32);
print('Generated new JWT secret: $secret');
await _key.changeSecret(
new File.fromUri(projectDir.uri.resolve('config/default.yaml')),
secret);
await _key.changeSecret(
new File.fromUri(projectDir.uri.resolve('config/production.yaml')),
secret);
2016-09-18 03:56:08 +00:00
}
_cloneRepo(Directory projectDir) async {
try {
if (await projectDir.exists()) {
var chooser = new Chooser(["Yes", "No"],
message:
"Directory '${projectDir.absolute.path}' exists. Overwrite it? (Yes/No)");
if (await chooser.choose() != "Yes")
throw new Exception("Chose not to overwrite existing directory.");
await projectDir.delete(recursive: true);
}
var git = await Process.start("git", [
"clone",
"--depth",
"1",
"https://github.com/angel-dart/angel",
projectDir.absolute.path
]);
2016-12-31 16:39:00 +00:00
stdout.addStream(git.stdout);
stderr.addStream(git.stderr);
2016-09-18 03:56:08 +00:00
if (await git.exitCode != 0) {
throw new Exception("Could not clone repo.");
}
var gitDir = new Directory.fromUri(projectDir.uri.resolve(".git"));
2016-12-24 04:42:55 +00:00
if (await gitDir.exists()) await gitDir.delete(recursive: true);
2016-09-18 03:56:08 +00:00
} catch (e) {
print(e);
_pen.red();
_pen("${Icon.BALLOT_X} Could not initialize Angel project.");
_pen();
rethrow;
}
}
2016-09-21 23:47:50 +00:00
_pubGet(Directory projectDir) async {
2016-12-24 04:42:55 +00:00
var pub = await Process.start("pub", ["get"],
workingDirectory: projectDir.absolute.path);
2016-12-31 16:39:00 +00:00
stdout.addStream(pub.stdout);
stderr.addStream(pub.stderr);
2016-09-18 03:56:08 +00:00
var code = await pub.exitCode;
print("Pub process exited with code $code");
}
}
2016-12-31 16:50:35 +00:00
preBuild(Directory projectDir) async {
// Run build
print('Pre-building resources...');
var build = await Process.start(Platform.executable, ['tool/build.dart'],
workingDirectory: projectDir.absolute.path);
stdout.addStream(build.stdout);
stderr.addStream(build.stderr);
var buildCode = await build.exitCode;
if (buildCode != 0) throw new Exception('Failed to pre-build resources.');
}