diff --git a/.gitignore b/.gitignore index c6097d6..58e5c86 100644 --- a/.gitignore +++ b/.gitignore @@ -74,3 +74,4 @@ doc/api/ pubspec.lock /sample_project/lib/src/services/ /sample_project/test/services/ +/sample_project/ diff --git a/.idea/runConfigurations/Init.xml b/.idea/runConfigurations/Init.xml new file mode 100644 index 0000000..c074007 --- /dev/null +++ b/.idea/runConfigurations/Init.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/bin/angel.dart b/bin/angel.dart index dff43cd..0433165 100644 --- a/bin/angel.dart +++ b/bin/angel.dart @@ -13,6 +13,7 @@ main(List args) { runner.addCommand(new DoctorCommand()); runner.addCommand(new ServiceCommand()); + runner.addCommand(new InitCommand()); return runner.run(args).then((_) {}).catchError((exc) { stderr.writeln("Oops, something went wrong: $exc"); diff --git a/lib/src/commands/commands.dart b/lib/src/commands/commands.dart index f3c15b7..e6c5fae 100644 --- a/lib/src/commands/commands.dart +++ b/lib/src/commands/commands.dart @@ -1,4 +1,5 @@ library angel_cli.commands; export "doctor.dart"; +export "init.dart"; export "service.dart"; \ No newline at end of file diff --git a/lib/src/commands/init.dart b/lib/src/commands/init.dart new file mode 100644 index 0000000..5cd2030 --- /dev/null +++ b/lib/src/commands/init.dart @@ -0,0 +1,76 @@ +import "dart:convert"; +import "dart:io"; +import "package:args/command_runner.dart"; +import "package:console/console.dart"; + +class InitCommand extends Command { + 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}..."); + await _cloneRepo(projectDir);_pen.green(); + _pen("${Icon.CHECKMARK} Successfully initialized Angel project. Now running pub get..."); + _pen(); + await _pubGet(); + } + + _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 + ]); + + git.stdout.transform(UTF8.decoder).listen(stdout.write); + git.stderr.transform(UTF8.decoder).listen(stderr.write); + + if (await git.exitCode != 0) { + throw new Exception("Could not clone repo."); + } + + var gitDir = new Directory.fromUri(projectDir.uri.resolve(".git")); + + if (await gitDir.exists()) + await gitDir.delete(recursive: true); + } catch (e) { + print(e); + _pen.red(); + _pen("${Icon.BALLOT_X} Could not initialize Angel project."); + _pen(); + rethrow; + } + } + + _pubGet() async { + var pub = await Process.start("pub", ["get"]); + pub.stdout.pipe(stdout); + pub.stderr.pipe(stderr); + var code = await pub.exitCode; + print("Pub process exited with code $code"); + } +} diff --git a/sample_project/angel.dart b/sample_project/angel.dart deleted file mode 100644 index 67acefc..0000000 --- a/sample_project/angel.dart +++ /dev/null @@ -1,11 +0,0 @@ -import "dart:async"; -import "dart:io"; -import "package:angel_framework/angel_framework.dart"; -import "package:angel/src/services/services.dart"; - -class SampleServer extends Angel { - @override - startServer(address, port) async { - return await super.startServer(address, port); - } -} \ No newline at end of file diff --git a/sample_project/pubspec.yaml b/sample_project/pubspec.yaml deleted file mode 100644 index 9731a80..0000000 --- a/sample_project/pubspec.yaml +++ /dev/null @@ -1,8 +0,0 @@ -name: angel -publish_to: none -dependencies: - angel_framework: ^1.0.0-dev - angel_mongo: ^1.0.0-dev -dev_dependencies: - http: ^0.11.3 - test: ^0.12.13 \ No newline at end of file