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;
|
2017-03-04 04:14:39 +00:00
|
|
|
import 'package:path/path.dart' as p;
|
2016-12-24 04:42:55 +00:00
|
|
|
import 'key.dart';
|
2017-03-04 04:14:39 +00:00
|
|
|
import 'rename.dart';
|
2016-09-18 03:56:08 +00:00
|
|
|
|
2017-04-26 13:21:25 +00:00
|
|
|
final RegExp _leadingSlashes = new RegExp(r'^/+');
|
|
|
|
|
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.";
|
|
|
|
|
2017-03-24 20:25:50 +00:00
|
|
|
InitCommand() {
|
|
|
|
argParser.addFlag('pub-get', defaultsTo: true);
|
|
|
|
}
|
2016-09-18 03:56:08 +00:00
|
|
|
|
|
|
|
@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);
|
2017-03-02 03:26:11 +00:00
|
|
|
// await preBuild(projectDir);
|
2016-12-24 04:42:55 +00:00
|
|
|
var secret = rs.randomAlphaNumeric(32);
|
2016-12-31 18:19:51 +00:00
|
|
|
print('Generated new development JWT secret: $secret');
|
2016-12-24 04:42:55 +00:00
|
|
|
await _key.changeSecret(
|
|
|
|
new File.fromUri(projectDir.uri.resolve('config/default.yaml')),
|
|
|
|
secret);
|
2016-12-31 18:19:51 +00:00
|
|
|
|
|
|
|
secret = rs.randomAlphaNumeric(32);
|
|
|
|
print('Generated new production JWT secret: $secret');
|
2016-12-24 04:42:55 +00:00
|
|
|
await _key.changeSecret(
|
|
|
|
new File.fromUri(projectDir.uri.resolve('config/production.yaml')),
|
|
|
|
secret);
|
2017-03-04 04:14:39 +00:00
|
|
|
|
2017-03-24 20:25:50 +00:00
|
|
|
var name = p.basenameWithoutExtension(
|
|
|
|
projectDir.absolute.uri.normalizePath().toFilePath());
|
2017-03-04 04:14:39 +00:00
|
|
|
print('Renaming project from "angel" to "$name"...');
|
2017-03-04 21:34:49 +00:00
|
|
|
await renamePubspec(projectDir, 'angel', name);
|
2017-03-04 04:14:39 +00:00
|
|
|
await renameDartFiles(projectDir, 'angel', name);
|
2017-03-24 20:25:50 +00:00
|
|
|
|
|
|
|
if (argResults['pub-get'] != false) {
|
|
|
|
print('Now running pub get...');
|
|
|
|
await _pubGet(projectDir);
|
|
|
|
}
|
|
|
|
|
2017-03-04 21:34:49 +00:00
|
|
|
_pen.green();
|
2017-03-24 20:25:50 +00:00
|
|
|
_pen("${Icon.CHECKMARK} Successfully initialized Angel project.");
|
2017-03-04 21:34:49 +00:00
|
|
|
_pen();
|
2017-03-24 20:25:50 +00:00
|
|
|
_pen
|
|
|
|
..reset()
|
|
|
|
..text('\nCongratulations! You are ready to start developing with Angel!')
|
|
|
|
..text('\nTo start the server (with file watching), run ')
|
|
|
|
..magenta()
|
|
|
|
..text('`angel start`')
|
|
|
|
..normal()
|
|
|
|
..text(' in your terminal.')
|
|
|
|
..text('\nHappy coding!')
|
|
|
|
..call();
|
|
|
|
}
|
|
|
|
|
|
|
|
_deleteRecursive(FileSystemEntity entity, [bool self = true]) async {
|
|
|
|
if (entity is Directory) {
|
|
|
|
await for (var entity in entity.list(recursive: true)) {
|
|
|
|
try {
|
|
|
|
await _deleteRecursive(entity);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (self != false) await entity.delete(recursive: true);
|
|
|
|
} catch (e) {}
|
|
|
|
} else if (entity is File) {
|
|
|
|
try {
|
|
|
|
await entity.delete(recursive: true);
|
|
|
|
} catch (e) {}
|
|
|
|
} else if (entity is Link) {
|
|
|
|
var path = await entity.resolveSymbolicLinks();
|
|
|
|
var stat = await FileStat.stat(path);
|
|
|
|
|
|
|
|
switch (stat.type) {
|
|
|
|
case FileSystemEntityType.DIRECTORY:
|
|
|
|
return await _deleteRecursive(new Directory(path));
|
|
|
|
case FileSystemEntityType.FILE:
|
|
|
|
return await _deleteRecursive(new File(path));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 03:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_cloneRepo(Directory projectDir) async {
|
|
|
|
try {
|
|
|
|
if (await projectDir.exists()) {
|
|
|
|
var chooser = new Chooser(["Yes", "No"],
|
|
|
|
message:
|
2017-03-24 20:25:50 +00:00
|
|
|
"Directory '${projectDir.absolute.path}' already exists. Overwrite it? (Yes/No)");
|
2016-09-18 03:56:08 +00:00
|
|
|
|
|
|
|
if (await chooser.choose() != "Yes")
|
|
|
|
throw new Exception("Chose not to overwrite existing directory.");
|
2017-03-24 20:25:50 +00:00
|
|
|
else if (projectDir.absolute.uri.normalizePath().toFilePath() !=
|
|
|
|
Directory.current.absolute.uri.normalizePath().toFilePath())
|
|
|
|
await projectDir.delete(recursive: true);
|
|
|
|
else {
|
|
|
|
await _deleteRecursive(projectDir, false);
|
|
|
|
}
|
2016-09-18 03:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-03-24 20:25:50 +00:00
|
|
|
var exec = new File(Platform.resolvedExecutable);
|
|
|
|
var pubPath = exec.parent.uri.resolve('pub').path;
|
2017-04-26 13:21:25 +00:00
|
|
|
if (Platform.isWindows) pubPath = pubPath.replaceAll(_leadingSlashes, '');
|
2017-03-24 20:25:50 +00:00
|
|
|
print('Running pub at "$pubPath"...');
|
|
|
|
var pub = await Process.start(pubPath, ["get"],
|
2016-12-24 04:42:55 +00:00
|
|
|
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...');
|
2016-12-31 18:19:51 +00:00
|
|
|
|
2016-12-31 16:50:35 +00:00
|
|
|
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.');
|
|
|
|
}
|