From 0a59aa77dfe4c09755a7dcf59cc2391d3f1ba5b8 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 14 Jul 2018 17:47:49 -0400 Subject: [PATCH] New generators for Controller + Model --- .idea/angel_cli.iml | 1 + .idea/runConfigurations/build_dart.xml | 7 - analysis_options.yaml | 3 +- lib/src/commands/controller.dart | 6 +- lib/src/commands/install.dart | 12 +- lib/src/commands/make/controller.dart | 117 +++++++------- lib/src/commands/make/maker.dart | 96 ++++++++---- lib/src/commands/make/model.dart | 205 +++++++++++++++---------- lib/src/commands/make/plugin.dart | 6 +- lib/src/commands/make/service.dart | 4 +- lib/src/commands/make/test.dart | 6 +- lib/src/commands/plugin.dart | 6 +- lib/src/commands/pubspec.update.g.dart | 26 ---- lib/src/commands/rename.dart | 8 +- lib/src/commands/service.dart | 10 +- lib/src/commands/test.dart | 6 +- lib/src/util.dart | 18 +++ pubspec.yaml | 51 +++--- tool/build.dart | 4 - tool/phases.dart | 6 - tool/watch.dart | 4 - 21 files changed, 328 insertions(+), 274 deletions(-) delete mode 100644 .idea/runConfigurations/build_dart.xml delete mode 100644 lib/src/commands/pubspec.update.g.dart create mode 100644 lib/src/util.dart delete mode 100644 tool/build.dart delete mode 100644 tool/phases.dart delete mode 100644 tool/watch.dart diff --git a/.idea/angel_cli.iml b/.idea/angel_cli.iml index 78848ff9..6118a7ae 100644 --- a/.idea/angel_cli.iml +++ b/.idea/angel_cli.iml @@ -2,6 +2,7 @@ + diff --git a/.idea/runConfigurations/build_dart.xml b/.idea/runConfigurations/build_dart.xml deleted file mode 100644 index 1bf4884c..00000000 --- a/.idea/runConfigurations/build_dart.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/analysis_options.yaml b/analysis_options.yaml index 518eb901..eae1e42a 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,2 +1,3 @@ analyzer: - strong-mode: true \ No newline at end of file + strong-mode: + implicit-casts: false \ No newline at end of file diff --git a/lib/src/commands/controller.dart b/lib/src/commands/controller.dart index ce9458e6..995aba29 100644 --- a/lib/src/commands/controller.dart +++ b/lib/src/commands/controller.dart @@ -2,7 +2,7 @@ import 'dart:io'; import 'package:args/command_runner.dart'; import 'package:code_builder/code_builder.dart'; import "package:console/console.dart"; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:recase/recase.dart'; import 'deprecated.dart'; @@ -31,7 +31,7 @@ class ControllerCommand extends Command { await controllerFile.create(recursive: true); await controllerFile.writeAsString( - _generateController(await PubSpec.load(Directory.current), recase)); + _generateController(await Pubspec.load(Directory.current), recase)); _pen.green(); _pen("${Icon.CHECKMARK} Successfully generated controller $name."); @@ -41,7 +41,7 @@ class ControllerCommand extends Command { NewInstanceBuilder _expose(String path) => new TypeBuilder('Expose') .constInstance([], namedArguments: {'path': literal(path)}); - String _generateController(PubSpec pubspec, ReCase recase) { + String _generateController(Pubspec pubspec, ReCase recase) { var lower = recase.snakeCase; var lib = new LibraryBuilder('${pubspec.name}.routes.controllers.$lower'); lib.addDirective( diff --git a/lib/src/commands/install.dart b/lib/src/commands/install.dart index b75d8fa4..b81a6008 100644 --- a/lib/src/commands/install.dart +++ b/lib/src/commands/install.dart @@ -6,7 +6,7 @@ import 'package:glob/glob.dart'; import 'package:homedir/homedir.dart'; import 'package:mustache4dart/mustache4dart.dart' as mustache; import 'package:path/path.dart' as p; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:yaml/yaml.dart' as yaml; import 'make/maker.dart'; @@ -61,7 +61,7 @@ class InstallCommand extends Command { if (!await installRepo.exists()) throw 'No local add-on database exists. Run `angel install --update` first.'; - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await Pubspec.load(Directory.current); for (var packageName in argResults.rest) { var packageDir = @@ -78,7 +78,7 @@ class InstallCommand extends Command { List globs = []; - var projectPubspec = await PubSpec.load(packageDir); + var projectPubspec = await Pubspec.load(packageDir); var deps = projectPubspec.dependencies.keys .map((k) { var dep = projectPubspec.dependencies[k]; @@ -207,16 +207,16 @@ class InstallCommand extends Command { } } - Future> list() async { + Future> list() async { if (!await installRepo.exists()) { throw 'No local add-on database exists. Run `angel install --update` first.'; } else { - List repos = []; + List repos = []; await for (var entity in installRepo.list()) { if (entity is Directory) { try { - repos.add(await PubSpec.load(entity)); + repos.add(await Pubspec.load(entity)); } catch (_) { // Ignore failures... } diff --git a/lib/src/commands/make/controller.dart b/lib/src/commands/make/controller.dart index ae36bd80..428c9e68 100644 --- a/lib/src/commands/make/controller.dart +++ b/lib/src/commands/make/controller.dart @@ -1,15 +1,14 @@ import 'dart:io'; import 'package:args/command_runner.dart'; -import 'package:code_builder/dart/core.dart'; import 'package:code_builder/code_builder.dart'; -import 'package:console/console.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:dart_style/dart_style.dart'; +import 'package:io/ansi.dart'; +import 'package:prompts/prompts.dart' as prompts; import 'package:recase/recase.dart'; +import '../../util.dart'; import 'maker.dart'; class ControllerCommand extends Command { - final TextPen _pen = new TextPen(); - @override String get name => 'controller'; @@ -32,73 +31,83 @@ class ControllerCommand extends Command { @override run() async { - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await loadPubspec(); String name; - if (argResults.wasParsed('name')) name = argResults['name']; + if (argResults.wasParsed('name')) name = argResults['name'] as String; if (name?.isNotEmpty != true) { - var p = new Prompter('Name of Controller class: '); - name = await p.prompt(checker: (s) => s.isNotEmpty); + name = prompts.get('Name of controller class'); } List deps = [ const MakerDependency('angel_framework', '^1.0.0') ]; + // ${pubspec.name}.src.models.${rc.snakeCase} + var rc = new ReCase(name); - var controllerLib = - new LibraryBuilder('${pubspec.name}.src.controllers.${rc.snakeCase}'); + var controllerLib = new Library((controllerLib) { + if (argResults['websocket'] as bool) { + deps.add(const MakerDependency('angel_websocket', '^1.0.0')); + controllerLib.directives + .add(new Directive.import('package:angel_websocket/server.dart')); + } else { + controllerLib.directives.add(new Directive.import( + 'package:angel_framework/angel_framework.dart')); + } - if (argResults['websocket']) { - deps.add(const MakerDependency('angel_websocket', '^1.0.0')); - controllerLib.addDirective( - new ImportBuilder('package:angel_websocket/server.dart')); - } else - controllerLib.addDirective( - new ImportBuilder('package:angel_framework/angel_framework.dart')); + controllerLib.body.add(new Class((clazz) { + clazz + ..name = '${rc.pascalCase}Controller' + ..extend = refer(argResults['websocket'] as bool + ? 'WebSocketController' + : 'Controller'); - TypeBuilder parentType = new TypeBuilder( - argResults['websocket'] ? 'WebSocketController' : 'Controller'); - ClassBuilder clazz = - new ClassBuilder('${rc.pascalCase}Controller', asExtends: parentType); - controllerLib.addMember(clazz); - - if (argResults['websocket']) { - var meth = new MethodBuilder('hello', returnType: lib$core.$void); - meth.addAnnotation(new TypeBuilder('ExposeWs') - .constInstance([literal('get_${rc.snakeCase}')])); - meth.addPositional( - parameter('socket', [new TypeBuilder('WebSocketContext')])); - meth.addStatement(reference('socket').invoke('send', [ - literal('got_${rc.snakeCase}'), - map({'message': literal('Hello, world!')}) - ])); - clazz.addMethod(meth); - } else { - clazz.addAnnotation(new TypeBuilder('Expose') - .constInstance([literal('/${rc.snakeCase}')])); - - var meth = new MethodBuilder('hello', - returnType: lib$core.String, returns: literal('Hello, world!')); - meth.addAnnotation( - new TypeBuilder('Expose').constInstance([literal('/')])); - clazz.addMethod(meth); - } + if (argResults['websocket'] as bool) { + clazz.methods.add(new Method((meth) { + meth + ..name = 'hello' + ..returns = refer('void') + ..annotations.add(refer('ExposeWs') + .constInstance([literal('get_${rc.snakeCase}')])) + ..requiredParameters.add(new Parameter((b) => b + ..name = 'socket' + ..type = refer('WebSocketContext'))) + ..body = new Block((block) { + block.addExpression(refer('socket').property('send').call([ + literal('got_${rc.snakeCase}'), + literalMap({'message': literal('Hello, world!')}), + ])); + }); + })); + } else { + clazz + ..annotations.add( + refer('Expose').constInstance([literal('/${rc.snakeCase}')])) + ..methods.add(new Method((meth) { + meth + ..name = 'hello' + ..returns = refer('String') + ..body = literal('Hello, world').returned.statement + ..annotations.add(refer('Expose').constInstance([ + literal('/'), + ])); + })); + } + })); + }); var outputDir = new Directory.fromUri( - Directory.current.uri.resolve(argResults['output-dir'])); + Directory.current.uri.resolve(argResults['output-dir'] as String)); var controllerFile = new File.fromUri(outputDir.uri.resolve('${rc.snakeCase}.dart')); if (!await controllerFile.exists()) await controllerFile.create(recursive: true); - await controllerFile - .writeAsString(prettyToSource(controllerLib.buildAst())); - _pen - ..green() - ..call( - '${Icon.CHECKMARK} Created controller file "${controllerFile.absolute.path}".') - ..call() - ..reset(); + await controllerFile.writeAsString(new DartFormatter() + .format(controllerLib.accept(new DartEmitter()).toString())); + + print(green.wrap( + '$checkmark Created controller file "${controllerFile.absolute.path}"')); if (deps.isNotEmpty) await depend(deps); } diff --git a/lib/src/commands/make/maker.dart b/lib/src/commands/make/maker.dart index bbee58b4..3e9e832b 100644 --- a/lib/src/commands/make/maker.dart +++ b/lib/src/commands/make/maker.dart @@ -1,18 +1,21 @@ import 'dart:async'; import 'dart:io'; -import 'package:pubspec/pubspec.dart'; +import 'package:io/ansi.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:pub_semver/pub_semver.dart'; import '../pub.dart'; +import '../../util.dart'; class MakerDependency { final String name, version; final bool dev; + const MakerDependency(this.name, this.version, {this.dev: false}); } Future depend(Iterable deps) async { - var pubspec = await PubSpec.load(Directory.current); - Map newDeps = {}, newDevDeps = {}; + var pubspec = await loadPubspec(); + var missing = []; for (var dep in deps) { var isPresent = false; @@ -22,37 +25,62 @@ Future depend(Iterable deps) async { isPresent = pubspec.dependencies.containsKey(dep.name); if (!isPresent) { - print('Installing ${dep.name}@${dep.version}...'); - - if (dep.dev) - newDevDeps[dep.name] = - new HostedReference(new VersionConstraint.parse(dep.version)); - else - newDeps[dep.name] = - new HostedReference(new VersionConstraint.parse(dep.version)); - } - - if (newDeps.isNotEmpty || newDevDeps.isNotEmpty) { - var newPubspec = pubspec.copy( - dependencies: - new Map.from(pubspec.dependencies) - ..addAll(newDeps), - devDependencies: - new Map.from(pubspec.devDependencies) - ..addAll(newDevDeps)); - - await newPubspec.save(Directory.current); - var pubPath = resolvePub(); - - print('Now running `$pubPath get`...'); - - var pubGet = await Process.start(pubPath, ['get']); - pubGet.stdout.listen(stdout.add); - pubGet.stderr.listen(stderr.add); - - var code = await pubGet.exitCode; - - if (code != 0) throw 'pub get terminated with exit code $code'; +// TODO: https://github.com/dart-lang/pubspec_parse/issues/17: +// print('Installing ${dep.name}@${dep.version}...'); +// +// if (dep.dev) { +// pubspec.devDependencies[dep.name] = new HostedDependency( +// version: new VersionConstraint.parse(dep.version), +// ); +// } else { +// pubspec.dependencies[dep.name] = new HostedDependency( +// version: new VersionConstraint.parse(dep.version), +// ); +// } } } + + missing.sort((a, b) { + if (!a.dev) { + if (b.dev) { + return -1; + } else { + return 0; + } + } else { + if (b.dev) { + return 0; + } else { + return 1; + } + } + }); + + if (missing.isNotEmpty) { + print(yellow.wrap(missing.length == 1 + ? 'You are missing one dependency:' + : 'You are missing ${missing.length} dependencies:')); + print('\n'); + + for (var dep in missing) { + var m = ' * ${dep.name}@${dep.version}'; + if (dep.dev) m += ' (dev dependency)'; + print(yellow.wrap(m)); + } + } + +// if (isPresent) { +// TODO: https://github.com/dart-lang/pubspec_parse/issues/17 +// await savePubspec(pubspec); +// var pubPath = resolvePub(); +// +// print('Now running `$pubPath get`...'); +// +// var pubGet = await Process.start(pubPath, ['get']); +// pubGet.stdout.listen(stdout.add); +// pubGet.stderr.listen(stderr.add); +// +// var code = await pubGet.exitCode; +// +// if (code != 0) throw 'pub get terminated with exit code $code'; } diff --git a/lib/src/commands/make/model.dart b/lib/src/commands/make/model.dart index 9b544420..899ced3a 100644 --- a/lib/src/commands/make/model.dart +++ b/lib/src/commands/make/model.dart @@ -1,16 +1,15 @@ import 'dart:io'; import 'package:args/command_runner.dart'; -import 'package:code_builder/dart/core.dart'; import 'package:code_builder/code_builder.dart'; -import 'package:console/console.dart'; +import 'package:dart_style/dart_style.dart'; import 'package:inflection/inflection.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:io/ansi.dart'; +import 'package:prompts/prompts.dart' as prompts; import 'package:recase/recase.dart'; +import '../../util.dart'; import 'maker.dart'; class ModelCommand extends Command { - final TextPen _pen = new TextPen(); - @override String get name => 'model'; @@ -38,13 +37,12 @@ class ModelCommand extends Command { @override run() async { - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await loadPubspec(); String name; - if (argResults.wasParsed('name')) name = argResults['name']; + if (argResults.wasParsed('name')) name = argResults['name'] as String; if (name?.isNotEmpty != true) { - var p = new Prompter('Name of Model class: '); - name = await p.prompt(checker: (s) => s.isNotEmpty); + name = prompts.get('Name of model class'); } List deps = [ @@ -53,107 +51,148 @@ class ModelCommand extends Command { ]; var rc = new ReCase(name); - var modelLib = - new LibraryBuilder('${pubspec.name}.src.models.${rc.snakeCase}'); - modelLib.addDirective( - new ImportBuilder('package:angel_model/angel_model.dart')); - var needsSerialize = argResults['serializable'] || argResults['orm']; + var modelLib = new Library((modelLib) { + modelLib.directives + .add(new Directive.import('package:angel_model/angel_model.dart')); - if (needsSerialize) { - modelLib.addDirective( - new ImportBuilder('package:angel_serialize/angel_serialize.dart')); - deps.add(const MakerDependency('angel_serialize', '^1.0.0-alpha')); - } + var needsSerialize = + argResults['serializable'] as bool || argResults['orm'] as bool; - if (argResults['orm']) { - modelLib - .addDirective(new ImportBuilder('package:angel_orm/angel_orm.dart')); - deps.add(const MakerDependency('angel_orm', '^1.0.0-alpha')); - } + if (needsSerialize) { + modelLib.directives.add(new Directive.import( + 'package:angel_serialize/angel_serialize.dart')); + deps.add(const MakerDependency('angel_serialize', '^2.0.0')); + } - var modelClazz = new ClassBuilder( - needsSerialize ? '_${rc.pascalCase}' : rc.pascalCase, - asExtends: new TypeBuilder('Model')); - modelLib.addMember(modelClazz); + if (argResults['orm'] as bool) { + modelLib.directives + .add(new Directive.import('package:angel_orm/angel_orm.dart')); + deps.add(const MakerDependency('angel_orm', '^1.0.0-alpha')); + } - if (needsSerialize) { - modelLib.addDirective(new PartBuilder('${rc.snakeCase}.g.dart')); - modelClazz.addAnnotation(reference('serializable')); - } + modelLib.body.add(new Class((modelClazz) { + modelClazz + ..name = needsSerialize ? '_${rc.pascalCase}' : rc.pascalCase + ..extend = refer('Model'); - if (argResults['orm']) { - modelClazz.addAnnotation(reference('orm')); - } + if (needsSerialize) { + // TODO: Add parts + // modelLib.addDirective(new PartBuilder('${rc.snakeCase}.g.dart')); + modelClazz.annotations.add(refer('serializable')); + } + + if (argResults['orm'] as bool) { + modelClazz.annotations.add(refer('orm')); + } + })); + }); // Save model file var outputDir = new Directory.fromUri( - Directory.current.uri.resolve(argResults['output-dir'])); + Directory.current.uri.resolve(argResults['output-dir'] as String)); var modelFile = new File.fromUri(outputDir.uri.resolve('${rc.snakeCase}.dart')); if (!await modelFile.exists()) await modelFile.create(recursive: true); - await modelFile.writeAsString(prettyToSource(modelLib.buildAst())); - _pen - ..green() - ..call( - '${Icon.CHECKMARK} Created model file "${modelFile.absolute.path}".') - ..call() - ..reset(); - if (argResults['migration']) { + await modelFile.writeAsString(new DartFormatter() + .format(modelLib.accept(new DartEmitter()).toString())); + + print(green + .wrap('$checkmark Created model file "${modelFile.absolute.path}".')); + + if (argResults['migration'] as bool) { deps.add(const MakerDependency('angel_migration', '^1.0.0-alpha')); - var migrationLib = new LibraryBuilder() - ..addDirective( - new ImportBuilder('package:angel_migration/angel_migration.dart')); - var migrationClazz = new ClassBuilder('${rc.pascalCase}Migration', - asExtends: new TypeBuilder('Migration')); - migrationLib.addMember(migrationClazz); - var tableName = pluralize(rc.snakeCase); + var migrationLib = new Library((migrationLib) { + migrationLib + ..directives.add(new Directive.import( + 'package:angel_migration.dart/angel_migration.dart')) + ..body.add(new Class((migrationClazz) { + migrationClazz + ..name = '${rc.pascalCase}Migration' + ..extend = refer('Migration'); - // up() - var up = new MethodBuilder('up', returnType: lib$core.$void); - migrationClazz.addMethod(up); - up.addAnnotation(lib$core.override); - up.addPositional(parameter('schema', [new TypeBuilder('Schema')])); + var tableName = pluralize(rc.snakeCase); - // (table) { ... } - var callback = new MethodBuilder.closure(); - callback.addPositional(parameter('table')); + // up() + migrationClazz.methods.add(new Method((up) { + up + ..name = 'up' + ..returns = refer('void') + ..annotations.add(refer('override')) + ..requiredParameters.add(new Parameter((b) => b + ..name = 'schema' + ..type = refer('Schema'))) + ..body = new Block((block) { + // (table) { ... } + var callback = new Method((callback) { + callback + ..requiredParameters + .add(new Parameter((b) => b..name = 'table')) + ..body = new Block((block) { + var table = refer('table'); - var cascade = reference('table').cascade((table) => [ - table.invoke('serial', [literal('id')]).invoke('primaryKey', []), - table.invoke('date', [literal('created_at')]), - table.invoke('date', [literal('updated_at')]) - ]); - callback.addStatement(cascade); + block.addExpression( + (table.property('serial').call([literal('id')])) + .property('primaryKey') + .call([]), + ); - up.addStatement( - reference('schema').invoke('create', [literal(tableName), callback])); + block.addExpression( + table.property('date').call([ + literal('created_at'), + ]), + ); - // down() - var down = new MethodBuilder('down', returnType: lib$core.$void); - migrationClazz.addMethod(down); - down.addAnnotation(lib$core.override); - down.addPositional(parameter('schema', [new TypeBuilder('Schema')])); - down.addStatement( - reference('schema').invoke('drop', [literal(tableName)])); + block.addExpression( + table.property('date').call([ + literal('updated_at'), + ]), + ); + }); + }); + + block.addExpression(refer('schema').property('create').call([ + literal(tableName), + callback.closure, + ])); + }); + })); + + // down() + migrationClazz.methods.add(new Method((down) { + down + ..name = 'down' + ..returns = refer('void') + ..annotations.add(refer('override')) + ..requiredParameters.add(new Parameter((b) => b + ..name = 'schema' + ..type = refer('Schema'))) + ..body = new Block((block) { + block.addExpression( + refer('schema').property('drop').call([ + literal(tableName), + ]), + ); + }); + })); + })); + }); // Save migration file var migrationDir = new Directory.fromUri( - Directory.current.uri.resolve(argResults['migration-dir'])); + Directory.current.uri.resolve(argResults['migration-dir'] as String)); var migrationFile = new File.fromUri(migrationDir.uri.resolve('${rc.snakeCase}.dart')); if (!await migrationFile.exists()) await migrationFile.create(recursive: true); - await migrationFile - .writeAsString(prettyToSource(migrationLib.buildAst())); - _pen - ..green() - ..call( - '${Icon.CHECKMARK} Created migration file "${migrationFile.absolute.path}".') - ..call() - ..reset(); + + await migrationFile.writeAsString(new DartFormatter() + .format(migrationLib.accept(new DartEmitter()).toString())); + + print(green.wrap( + '$checkmark Created migration file "${migrationFile.absolute.path}".')); } if (deps.isNotEmpty) await depend(deps); diff --git a/lib/src/commands/make/plugin.dart b/lib/src/commands/make/plugin.dart index b6c1183f..6547d95a 100644 --- a/lib/src/commands/make/plugin.dart +++ b/lib/src/commands/make/plugin.dart @@ -2,7 +2,7 @@ import 'dart:io'; import 'package:args/command_runner.dart'; import "package:console/console.dart"; import 'package:dart_style/dart_style.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:recase/recase.dart'; import 'maker.dart'; @@ -26,7 +26,7 @@ class PluginCommand extends Command { @override run() async { - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await Pubspec.load(Directory.current); String name; if (argResults.wasParsed('name')) name = argResults['name']; @@ -56,7 +56,7 @@ class PluginCommand extends Command { _pen(); } - String _generatePlugin(PubSpec pubspec, ReCase rc) { + String _generatePlugin(Pubspec pubspec, ReCase rc) { return ''' library ${pubspec.name}.src.config.plugins.${rc.snakeCase}; diff --git a/lib/src/commands/make/service.dart b/lib/src/commands/make/service.dart index 62707fd4..c2f7489a 100644 --- a/lib/src/commands/make/service.dart +++ b/lib/src/commands/make/service.dart @@ -3,7 +3,7 @@ import 'package:args/command_runner.dart'; import 'package:code_builder/code_builder.dart'; import 'package:console/console.dart'; import 'package:inflection/inflection.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:recase/recase.dart'; import '../service_generators/service_generators.dart'; import 'maker.dart'; @@ -32,7 +32,7 @@ class ServiceCommand extends Command { @override run() async { - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await Pubspec.load(Directory.current); String name; if (argResults.wasParsed('name')) name = argResults['name']; diff --git a/lib/src/commands/make/test.dart b/lib/src/commands/make/test.dart index 417e6eb5..fd15f0fa 100644 --- a/lib/src/commands/make/test.dart +++ b/lib/src/commands/make/test.dart @@ -2,7 +2,7 @@ import 'dart:io'; import 'package:args/command_runner.dart'; import "package:console/console.dart"; import 'package:dart_style/dart_style.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:recase/recase.dart'; import 'maker.dart'; @@ -29,7 +29,7 @@ class TestCommand extends Command { @override run() async { - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await Pubspec.load(Directory.current); String name; if (argResults.wasParsed('name')) name = argResults['name']; @@ -87,7 +87,7 @@ class TestCommand extends Command { .trim(); } - String _generateTest(PubSpec pubspec, ReCase rc) { + String _generateTest(Pubspec pubspec, ReCase rc) { return ''' import 'dart:io'; import 'package:${pubspec.name}/${pubspec.name}.dart'; diff --git a/lib/src/commands/plugin.dart b/lib/src/commands/plugin.dart index 49d5fc10..d8facda9 100644 --- a/lib/src/commands/plugin.dart +++ b/lib/src/commands/plugin.dart @@ -2,7 +2,7 @@ import 'dart:io'; import 'package:args/command_runner.dart'; import "package:console/console.dart"; import 'package:dart_style/dart_style.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:recase/recase.dart'; import 'deprecated.dart'; @@ -19,7 +19,7 @@ class PluginCommand extends Command { run() async { warnDeprecated(this.name, _pen); - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await Pubspec.load(Directory.current); final name = await readInput("Name of Plugin: "), lower = new ReCase(name).snakeCase; final testDir = new Directory("lib/src/config/plugins"); @@ -35,7 +35,7 @@ class PluginCommand extends Command { _pen(); } - String _generatePlugin(PubSpec pubspec, String name, String lower) { + String _generatePlugin(Pubspec pubspec, String name, String lower) { return ''' library ${pubspec.name}.config.plugins.$lower; diff --git a/lib/src/commands/pubspec.update.g.dart b/lib/src/commands/pubspec.update.g.dart deleted file mode 100644 index 07f80537..00000000 --- a/lib/src/commands/pubspec.update.g.dart +++ /dev/null @@ -1,26 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; -import 'package:http/src/base_client.dart' as http; -import 'package:pub_semver/pub_semver.dart'; - -final Version PACKAGE_VERSION = new Version(1, 2, 0, build: '5'); -Future fetchCurrentVersion(http.BaseClient client) async { - var response = - await client.get('https://pub.dartlang.org/api/packages/angel_cli'); - var json = JSON.decode(response.body) as Map; - if (!(json.containsKey('latest')) || - !(json['latest'].containsKey('pubspec')) || - !(json['latest']['pubspec'].containsKey('version'))) { - throw new StateError( - 'GET https://pub.dartlang.org/api/packages/angel_cli returned an invalid pub API response.'); - } - return new Version.parse(json['latest']['pubspec']['version']); -} - -Future checkForUpdate(http.BaseClient client) async { - var current = await fetchCurrentVersion(client); - if (PACKAGE_VERSION.compareTo(current) < 0) { - return current; - } - return null; -} diff --git a/lib/src/commands/rename.dart b/lib/src/commands/rename.dart index f64c4047..b0c42912 100644 --- a/lib/src/commands/rename.dart +++ b/lib/src/commands/rename.dart @@ -4,7 +4,7 @@ import 'package:args/command_runner.dart'; import 'package:console/console.dart'; import 'package:dart_style/dart_style.dart'; import 'package:glob/glob.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'pub.dart'; class RenameCommand extends Command { @@ -40,7 +40,7 @@ class RenameCommand extends Command { if (!await pubspecFile.exists()) { throw new Exception('No pubspec.yaml found in current directory.'); } else { - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await Pubspec.load(Directory.current); var oldName = pubspec.name; await renamePubspec(Directory.current, oldName, newName); await renameDartFiles(Directory.current, oldName, newName); @@ -57,8 +57,8 @@ class RenameCommand extends Command { } renamePubspec(Directory dir, String oldName, String newName) async { - var pubspec = await PubSpec.load(dir); - var newPubspec = new PubSpec.fromJson(pubspec.toJson()..['name'] = newName); + var pubspec = await Pubspec.load(dir); + var newPubspec = new Pubspec.fromJson(pubspec.toJson()..['name'] = newName); await newPubspec.save(dir); } diff --git a/lib/src/commands/service.dart b/lib/src/commands/service.dart index 9fd28165..ed60431d 100644 --- a/lib/src/commands/service.dart +++ b/lib/src/commands/service.dart @@ -4,7 +4,7 @@ import 'package:code_builder/code_builder.dart'; import 'package:console/console.dart'; import 'package:dart_style/dart_style.dart'; import 'package:inflection/inflection.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:recase/recase.dart'; import 'service_generators/service_generators.dart'; import 'deprecated.dart'; @@ -23,7 +23,7 @@ class ServiceCommand extends Command { run() async { warnDeprecated(this.name, _pen); - var pubspec = await PubSpec.load(Directory.current); + var pubspec = await Pubspec.load(Directory.current); var name = await readInput('Name of Service (not plural): '); var chooser = new Chooser( serviceGenerators.map((g) => g.name).toList(), @@ -151,7 +151,7 @@ class ServiceCommand extends Command { } _generateModel( - PubSpec pubspec, String name, String lower, DartFormatter fmt) async { + Pubspec pubspec, String name, String lower, DartFormatter fmt) async { var file = new File('lib/src/models/$lower.dart'); if (!await file.exists()) await file.createSync(recursive: true); @@ -173,7 +173,7 @@ class $name extends Model { } _generateValidator( - PubSpec pubspec, String lower, ReCase rc, DartFormatter fmt) async { + Pubspec pubspec, String lower, ReCase rc, DartFormatter fmt) async { var file = new File('lib/src/validators/$lower.dart'); if (!await file.exists()) await file.createSync(recursive: true); @@ -204,7 +204,7 @@ final Validator create${rc.pascalCase} = ${rc.camelCase}.extend({}) .trim(); } - _generateTests(PubSpec pubspec, String lower, DartFormatter fmt) { + _generateTests(Pubspec pubspec, String lower, DartFormatter fmt) { return fmt.format(''' import 'dart:io'; import 'package:${pubspec.name}/${pubspec.name}.dart'; diff --git a/lib/src/commands/test.dart b/lib/src/commands/test.dart index b519b62e..d973a85c 100644 --- a/lib/src/commands/test.dart +++ b/lib/src/commands/test.dart @@ -2,7 +2,7 @@ import 'dart:io'; import 'package:args/command_runner.dart'; import "package:console/console.dart"; import 'package:dart_style/dart_style.dart'; -import 'package:pubspec/pubspec.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:recase/recase.dart'; import 'deprecated.dart'; @@ -28,7 +28,7 @@ class TestCommand extends Command { if (!await testFile.exists()) await testFile.create(recursive: true); await testFile.writeAsString(new DartFormatter() - .format(_generateTest(await PubSpec.load(Directory.current), lower))); + .format(_generateTest(await Pubspec.load(Directory.current), lower))); final runConfig = new File('./.idea/runConfigurations/${name}_tests.xml'); @@ -56,7 +56,7 @@ class TestCommand extends Command { .trim(); } - String _generateTest(PubSpec pubspec, String lower) { + String _generateTest(Pubspec pubspec, String lower) { return ''' import 'dart:io'; import 'package:${pubspec.name}/${pubspec.name}.dart'; diff --git a/lib/src/util.dart b/lib/src/util.dart new file mode 100644 index 00000000..b1ea7ef7 --- /dev/null +++ b/lib/src/util.dart @@ -0,0 +1,18 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:io/ansi.dart'; +import 'package:pubspec_parse/pubspec_parse.dart'; +import 'package:yamlicious/yamlicious.dart'; + +final String checkmark = ansiOutputEnabled ? '\u2713' : '[Success]'; + +Future loadPubspec() { + var file = new File('pubspec.yaml'); + return file + .readAsString() + .then((yaml) => new Pubspec.parse(yaml, sourceUrl: file.uri)); +} + +Future savePubspec(Pubspec pubspec) async { + var text = toYamlString(pubspec); +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index ca7b9426..b2f905a5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,30 +1,35 @@ -author: "Tobe O " -description: "Command-line tools for the Angel framework." -homepage: "https://github.com/angel-dart/angel_cli" -name: "angel_cli" -version: 1.2.0+5 +author: Tobe O +description: Command-line tools for the Angel framework. +homepage: https://github.com/angel-dart/angel_cli +name: angel_cli +version: 1.3.0 dependencies: - # analyzer: "^0.29.0" - args: ^0.13.4 - code_builder: ^1.0.0 - console: "^2.2.3" + # analyzer: ^0.29.0 + args: ^1.0.0 + code_builder: ^3.0.0 + #console: ^2.2.3 + dart2_constant: ^1.0.0 dart_style: ^1.0.0 - glob: "^1.1.0" + glob: ^1.1.0 homedir: ^0.0.4 http: ^0.11.3 - id: "^1.0.0" - inflection: "^0.4.1" - mustache4dart: ^2.1.0 - pubspec: "^0.0.14" - random_string: "^0.0.1" - recase: "^1.0.0" - watcher: "^0.9.7" - yaml: "^2.0.0" + id: ^1.0.0 + io: ^0.3.2 + inflection: ^0.4.1 + #mustache4dart: ^2.1.0 + path: ^1.0.0 + prompts: ^1.0.0 + pubspec_parse: ^0.1.2 + random_string: ^0.0.1 + recase: ^1.0.0 + watcher: ^0.9.7 + yaml: ^2.0.0 + yamlicious: ^0.0.5 dev_dependencies: - build: ^0.7.0 - build_runner: ^0.3.0 - check_for_update: ^1.0.0 + #build: ^0.7.0 + #build_runner: ^0.3.0 + #check_for_update: ^1.0.0 environment: - sdk: ">=1.19.0" + sdk: ">=2.0.0-dev <3.0.0" executables: - angel: "angel" + angel: angel diff --git a/tool/build.dart b/tool/build.dart deleted file mode 100644 index c07165fd..00000000 --- a/tool/build.dart +++ /dev/null @@ -1,4 +0,0 @@ -import 'package:build_runner/build_runner.dart'; -import 'phases.dart'; - -main() => build(phaseGroup, deleteFilesByDefault: true); diff --git a/tool/phases.dart b/tool/phases.dart deleted file mode 100644 index 94bcdbf6..00000000 --- a/tool/phases.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:build_runner/build_runner.dart'; -import 'package:check_for_update/builder.dart'; - -final PhaseGroup phaseGroup = new PhaseGroup.singleAction( - new CheckForUpdateBuilder(subDirectory: 'lib/src/commands'), - new InputSet('angel_cli', const ['pubspec.yaml'])); diff --git a/tool/watch.dart b/tool/watch.dart deleted file mode 100644 index cd2ce7db..00000000 --- a/tool/watch.dart +++ /dev/null @@ -1,4 +0,0 @@ -import 'package:build_runner/build_runner.dart'; -import 'phases.dart'; - -main() => watch(phaseGroup, deleteFilesByDefault: true);