From 422c92098adfb6d2cf19a1fe9365cce654dc0b41 Mon Sep 17 00:00:00 2001 From: thomashii Date: Tue, 7 Sep 2021 08:11:42 +0800 Subject: [PATCH] Fixed NNBD issues --- CHANGELOG.md | 4 ++ README.md | 2 +- lib/src/commands/deploy/systemd.dart | 15 ++++--- lib/src/commands/install.dart | 12 ++--- lib/src/commands/make/controller.dart | 10 +++-- lib/src/commands/make/maker.dart | 10 ++--- lib/src/commands/make/migration.dart | 4 +- lib/src/commands/make/model.dart | 16 ++++--- lib/src/commands/make/plugin.dart | 4 +- lib/src/commands/make/service.dart | 45 +++++++++++-------- lib/src/commands/make/test.dart | 8 ++-- lib/src/commands/rename.dart | 8 ++-- .../commands/service_generators/custom.dart | 4 +- .../service_generators/file_service.dart | 4 +- .../service_generators/generator.dart | 8 ++-- lib/src/commands/service_generators/map.dart | 2 +- .../commands/service_generators/mongo.dart | 4 +- .../commands/service_generators/rethink.dart | 4 +- lib/src/util.dart | 6 +-- pubspec.yaml | 2 +- 20 files changed, 97 insertions(+), 75 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d23017..ff840b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 3.1.1 + +* Fixed NNBD issues + ## 3.1.0 * Upgraded to support `analyzer` 2.0.0 major release diff --git a/README.md b/README.md index f4a1e36..eda1c1e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Screenshot of Terminal](screenshots/angel3-screenshot.png)](https://github.com/dukefirehawk/angel3-cli) -[![version](https://img.shields.io/badge/pub-v3.1.0-brightgreen)](https://pub.dartlang.org/angel3_cli) +[![version](https://img.shields.io/badge/pub-v3.1.1-brightgreen)](https://pub.dartlang.org/angel3_cli) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) diff --git a/lib/src/commands/deploy/systemd.dart b/lib/src/commands/deploy/systemd.dart index b0db8e9..3783464 100644 --- a/lib/src/commands/deploy/systemd.dart +++ b/lib/src/commands/deploy/systemd.dart @@ -30,7 +30,7 @@ class SystemdCommand extends Command { Future run() async { var projectPath = p.absolute(p.current); var pubspec = await loadPubspec(); - var user = argResults!['user']; + var user = argResults?['user']; var systemdText = ''' [Unit] Description=`${pubspec.name}` server @@ -47,14 +47,15 @@ WantedBy=multi-user.target ''' .trim(); - if (!argResults!.wasParsed('out') && !argResults!.wasParsed('install')) { + if (argResults?.wasParsed('out') != true && + argResults?.wasParsed('install') != true) { print(systemdText); - } else if (argResults!.wasParsed('install')) { - var systemdPath = argResults!.wasParsed('out') - ? (argResults!['out'] as String?)! + } else if (argResults?.wasParsed('install') == true) { + var systemdPath = argResults?.wasParsed('out') == true + ? (argResults?['out'] as String) : p.join('etc', 'systemd', 'system'); var serviceFilename = p.join(systemdPath, - p.setExtension(argResults!['install'] as String, '.service')); + p.setExtension(argResults?['install'] as String, '.service')); var file = File(serviceFilename); await file.create(recursive: true); await file.writeAsString(systemdText); @@ -76,7 +77,7 @@ WantedBy=multi-user.target print(red.wrap('$ballot Failed to install service system-wide.')); } } else { - var file = File(argResults!['out'] as String); + var file = File(argResults?['out'] as String); await file.create(recursive: true); await file.writeAsString(systemdText); print(green.wrap( diff --git a/lib/src/commands/install.dart b/lib/src/commands/install.dart index fd56c8e..9cc0aae 100644 --- a/lib/src/commands/install.dart +++ b/lib/src/commands/install.dart @@ -55,16 +55,16 @@ class InstallCommand extends Command { 'See here: https://github.com/angel-dart/install.git\n\n' 'To stop seeing this, downgrade to `package:angel_cli@<=2.0.0`.')); - if (argResults!['wipe'] as bool) { + if (argResults?['wipe'] as bool) { if (await installRepo.exists()) await installRepo.delete(recursive: true); - } else if (argResults!['list'] as bool) { + } else if (argResults?['list'] as bool) { var addons = await list(); print('${addons.length} add-on(s) installed:'); for (var addon in addons) { print(' * ${addon.name}@${addon.version}: ${addon.description}'); } - } else if (argResults!['update'] as bool) { + } else if (argResults?['update'] as bool) { await update(); } else if (argResults!.rest.isNotEmpty) { if (!await installRepo.exists()) { @@ -73,7 +73,7 @@ class InstallCommand extends Command { var pubspec = await loadPubspec(); - for (var packageName in argResults!.rest) { + for (var packageName in argResults?.rest ?? []) { var packageDir = Directory.fromUri(installRepo.uri.resolve(packageName)); @@ -98,7 +98,7 @@ class InstallCommand extends Command { } return null; }) - .where((d) => d != null) + .whereType() .toList(); deps.addAll(projectPubspec.devDependencies.keys.map((k) { @@ -107,7 +107,7 @@ class InstallCommand extends Command { return MakerDependency(k, dep.version.toString(), dev: true); } return null; - }).where((d) => d != null)); + }).whereType()); await depend(deps); diff --git a/lib/src/commands/make/controller.dart b/lib/src/commands/make/controller.dart index 9692ca9..536ca97 100644 --- a/lib/src/commands/make/controller.dart +++ b/lib/src/commands/make/controller.dart @@ -32,7 +32,9 @@ class ControllerCommand extends Command { @override Future run() async { String? name; - if (argResults!.wasParsed('name')) name = argResults!['name'] as String?; + if (argResults?.wasParsed('name') == true) { + name = argResults!['name'] as String?; + } if (name?.isNotEmpty != true) { name = prompts.get('Name of controller class'); @@ -46,7 +48,7 @@ class ControllerCommand extends Command { var rc = ReCase(name!); var controllerLib = Library((controllerLib) { - if (argResults!['websocket'] as bool) { + if (argResults?['websocket'] as bool) { deps.add(const MakerDependency('angel3_websocket', '^4.0.0')); controllerLib.directives .add(Directive.import('package:angel3_websocket/server.dart')); @@ -58,7 +60,7 @@ class ControllerCommand extends Command { controllerLib.body.add(Class((clazz) { clazz ..name = '${rc.pascalCase}Controller' - ..extend = refer(argResults!['websocket'] as bool + ..extend = refer(argResults?['websocket'] as bool ? 'WebSocketController' : 'Controller'); @@ -106,7 +108,7 @@ class ControllerCommand extends Command { }); var outputDir = Directory.fromUri( - Directory.current.uri.resolve(argResults!['output-dir'] as String)); + Directory.current.uri.resolve(argResults?['output-dir'] as String)); var controllerFile = File.fromUri(outputDir.uri.resolve('${rc.snakeCase}.dart')); if (!await controllerFile.exists()) { diff --git a/lib/src/commands/make/maker.dart b/lib/src/commands/make/maker.dart index 1cd20bf..d0aafba 100644 --- a/lib/src/commands/make/maker.dart +++ b/lib/src/commands/make/maker.dart @@ -12,13 +12,13 @@ class MakerDependency implements Comparable { int compareTo(MakerDependency other) => name.compareTo(other.name); } -Future depend(Iterable deps) async { +Future depend(Iterable deps) async { var pubspec = await loadPubspec(); - var missing = []; + var missing = []; for (var dep in deps) { var isPresent = false; - if (dep!.dev) { + if (dep.dev) { isPresent = pubspec.devDependencies.containsKey(dep.name); } else { isPresent = pubspec.dependencies.containsKey(dep.name); @@ -41,8 +41,8 @@ Future depend(Iterable deps) async { } } - var missingDeps = missing.where((d) => !d!.dev).toList()..sort(); - var missingDevDeps = missing.where((d) => d!.dev).toList()..sort(); + var missingDeps = missing.where((d) => !d.dev).toList()..sort(); + var missingDevDeps = missing.where((d) => d.dev).toList()..sort(); var totalCount = missingDeps.length + missingDevDeps.length; if (totalCount > 0) { diff --git a/lib/src/commands/make/migration.dart b/lib/src/commands/make/migration.dart index 442919e..18520e2 100644 --- a/lib/src/commands/make/migration.dart +++ b/lib/src/commands/make/migration.dart @@ -29,7 +29,9 @@ class MigrationCommand extends Command { @override FutureOr run() async { String? name; - if (argResults!.wasParsed('name')) name = argResults!['name'] as String?; + if (argResults?.wasParsed('name') == true) { + name = argResults?['name'] as String?; + } if (name?.isNotEmpty != true) { name = prompts.get('Name of model class'); diff --git a/lib/src/commands/make/model.dart b/lib/src/commands/make/model.dart index 0eb0286..f2e6a17 100644 --- a/lib/src/commands/make/model.dart +++ b/lib/src/commands/make/model.dart @@ -34,7 +34,9 @@ class ModelCommand extends Command { @override Future run() async { String? name; - if (argResults!.wasParsed('name')) name = argResults!['name'] as String?; + if (argResults?.wasParsed('name') == true) { + name = argResults?['name'] as String?; + } if (name?.isNotEmpty != true) { name = prompts.get('Name of model class'); @@ -47,14 +49,14 @@ class ModelCommand extends Command { var rc = ReCase(name!); var modelLib = Library((modelLib) { - if (argResults!['orm'] as bool && argResults!['migration'] as bool) { + if (argResults?['orm'] as bool && argResults?['migration'] as bool) { modelLib.directives.addAll([ Directive.import('package:angel3_migration/angel3_migration.dart'), ]); } var needsSerialize = - argResults!['serializable'] as bool || argResults!['orm'] as bool; + argResults?['serializable'] as bool || argResults?['orm'] as bool; // argResults['migration'] as bool; if (needsSerialize) { @@ -71,7 +73,7 @@ class ModelCommand extends Command { // deps.add(const MakerDependency('angel_model', '^1.0.0')); // } - if (argResults!['orm'] as bool) { + if (argResults?['orm'] as bool) { modelLib.directives.addAll([ Directive.import('package:angel3_orm/angel3_orm.dart'), ]); @@ -93,8 +95,8 @@ class ModelCommand extends Command { modelClazz.annotations.add(refer('serializable')); } - if (argResults!['orm'] as bool) { - if (argResults!['migration'] as bool) { + if (argResults?['orm'] as bool) { + if (argResults?['migration'] as bool) { modelClazz.annotations.add(refer('orm')); } else { modelClazz.annotations.add( @@ -106,7 +108,7 @@ class ModelCommand extends Command { // Save model file var outputDir = Directory.fromUri( - Directory.current.uri.resolve(argResults!['output-dir'] as String)); + Directory.current.uri.resolve(argResults?['output-dir'] as String)); var modelFile = File.fromUri(outputDir.uri.resolve('${rc.snakeCase}.dart')); if (!await modelFile.exists()) await modelFile.create(recursive: true); diff --git a/lib/src/commands/make/plugin.dart b/lib/src/commands/make/plugin.dart index 4f20e88..59766fa 100644 --- a/lib/src/commands/make/plugin.dart +++ b/lib/src/commands/make/plugin.dart @@ -28,7 +28,9 @@ class PluginCommand extends Command { Future run() async { var pubspec = await loadPubspec(); String? name; - if (argResults!.wasParsed('name')) name = argResults!['name'] as String?; + if (argResults?.wasParsed('name') == true) { + name = argResults?['name'] as String?; + } if (name?.isNotEmpty != true) { name = prompts.get('Name of plug-in class'); diff --git a/lib/src/commands/make/service.dart b/lib/src/commands/make/service.dart index 3974fcb..87d9bb8 100644 --- a/lib/src/commands/make/service.dart +++ b/lib/src/commands/make/service.dart @@ -34,7 +34,9 @@ class ServiceCommand extends Command { void run() async { await loadPubspec(); String? name; - if (argResults!.wasParsed('name')) name = argResults!['name'] as String?; + if (argResults?.wasParsed('name') == true) { + name = argResults?['name'] as String?; + } if (name?.isNotEmpty != true) { name = prompts.get('Name of service'); @@ -61,11 +63,11 @@ class ServiceCommand extends Command { if (!deps.any((d) => d.name == dep.name)) deps.add(dep); } - if (generator.goesFirst) { + if (name != null && generator.goesFirst) { generator.applyToLibrary(serviceLib, name, rc.snakeCase); serviceLib.directives.add( Directive.import('package:angel3_framework/angel3_framework.dart')); - } else { + } else if (name != null) { serviceLib.directives.add( Directive.import('package:angel3_framework/angel3_framework.dart')); generator.applyToLibrary(serviceLib, name, rc.snakeCase); @@ -83,8 +85,10 @@ class ServiceCommand extends Command { ..returns = refer('AngelConfigurer'); configureServer.body = Block((block) { - generator.applyToConfigureServer( - serviceLib, configureServer, block, name, rc.snakeCase); + if (name != null) { + generator.applyToConfigureServer( + serviceLib, configureServer, block, name, rc.snakeCase); + } // return (Angel app) async {} var closure = Method((closure) { @@ -93,24 +97,27 @@ class ServiceCommand extends Command { ..requiredParameters.add(Parameter((b) => b ..name = 'app' ..type = refer('Angel'))); + closure.body = Block((block) { - generator.beforeService(serviceLib, block, name, rc.snakeCase); + if (name != null) { + generator.beforeService(serviceLib, block, name, rc.snakeCase); - // app.use('/api/todos', new MapService()); - var service = generator.createInstance( - serviceLib, closure, name, rc.snakeCase); + // app.use('/api/todos', new MapService()); + var service = generator.createInstance( + serviceLib, closure, name, rc.snakeCase); - if (argResults!['typed'] as bool) { - var tb = TypeReference((b) => b - ..symbol = 'TypedService' - ..types.add(refer(rc.pascalCase))); - service = tb.newInstance([service]); + if (argResults!['typed'] as bool) { + var tb = TypeReference((b) => b + ..symbol = 'TypedService' + ..types.add(refer(rc.pascalCase))); + service = tb.newInstance([service]); + } + + block.addExpression(refer('app').property('use').call([ + literal('/api/${pluralize(rc.snakeCase)}'), + service, + ])); } - - block.addExpression(refer('app').property('use').call([ - literal('/api/${pluralize(rc.snakeCase)}'), - service, - ])); }); }); diff --git a/lib/src/commands/make/test.dart b/lib/src/commands/make/test.dart index dd432b2..70bb44b 100644 --- a/lib/src/commands/make/test.dart +++ b/lib/src/commands/make/test.dart @@ -31,7 +31,9 @@ class TestCommand extends Command { Future run() async { var pubspec = await loadPubspec(); String? name; - if (argResults!.wasParsed('name')) name = argResults!['name'] as String?; + if (argResults?.wasParsed('name') == true) { + name = argResults?['name'] as String?; + } if (name?.isNotEmpty != true) { name = prompter.get('Name of test'); @@ -45,7 +47,7 @@ class TestCommand extends Command { var rc = ReCase(name!); final testDir = Directory.fromUri( - Directory.current.uri.resolve(argResults!['output-dir'] as String)); + Directory.current.uri.resolve(argResults?['output-dir'] as String)); final testFile = File.fromUri(testDir.uri.resolve('${rc.snakeCase}_test.dart')); if (!await testFile.exists()) await testFile.create(recursive: true); @@ -69,7 +71,7 @@ class TestCommand extends Command { } } - String _generateRunConfiguration(String? name, ReCase rc) { + String _generateRunConfiguration(String name, ReCase rc) { return ''' diff --git a/lib/src/commands/rename.dart b/lib/src/commands/rename.dart index 7e2f46f..1a1d8c1 100644 --- a/lib/src/commands/rename.dart +++ b/lib/src/commands/rename.dart @@ -21,15 +21,15 @@ class RenameCommand extends Command { @override Future run() async { - String newName; + String? newName; - if (argResults!.rest.isNotEmpty) { - newName = argResults!.rest.first; + if (argResults?.rest.isNotEmpty == true) { + newName = argResults?.rest.first; } else { newName = prompts.get('Rename project to'); } - newName = ReCase(newName).snakeCase; + newName = ReCase(newName!).snakeCase; var choice = prompts.getBool('Rename the project to `$newName`?'); diff --git a/lib/src/commands/service_generators/custom.dart b/lib/src/commands/service_generators/custom.dart index ca000a7..9008b69 100644 --- a/lib/src/commands/service_generators/custom.dart +++ b/lib/src/commands/service_generators/custom.dart @@ -11,7 +11,7 @@ class CustomServiceGenerator extends ServiceGenerator { const CustomServiceGenerator() : super('Custom'); @override - void applyToLibrary(LibraryBuilder library, String? name, String lower) { + void applyToLibrary(LibraryBuilder library, String name, String lower) { library.body.add(Class((clazz) { clazz ..name = '${name}Service' @@ -21,7 +21,7 @@ class CustomServiceGenerator extends ServiceGenerator { @override Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, - String? name, String lower) { + String name, String lower) { return refer('${name}Service').newInstance([]); } } diff --git a/lib/src/commands/service_generators/file_service.dart b/lib/src/commands/service_generators/file_service.dart index dfa5fd6..e4e2fa5 100644 --- a/lib/src/commands/service_generators/file_service.dart +++ b/lib/src/commands/service_generators/file_service.dart @@ -26,7 +26,7 @@ class FileServiceGenerator extends ServiceGenerator { } @override - void applyToLibrary(LibraryBuilder library, String? name, String lower) { + void applyToLibrary(LibraryBuilder library, String name, String lower) { library.directives.addAll([ Directive.import('package:angel3_file_service/angel3_file_service.dart'), ]); @@ -34,7 +34,7 @@ class FileServiceGenerator extends ServiceGenerator { @override Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, - String? name, String lower) { + String name, String lower) { library.directives.addAll([ Directive.import('package:file/file.dart'), ]); diff --git a/lib/src/commands/service_generators/generator.dart b/lib/src/commands/service_generators/generator.dart index 82328cb..6328958 100644 --- a/lib/src/commands/service_generators/generator.dart +++ b/lib/src/commands/service_generators/generator.dart @@ -25,20 +25,20 @@ class ServiceGenerator { bool get goesFirst => false; - void applyToLibrary(LibraryBuilder library, String? name, String lower) {} + void applyToLibrary(LibraryBuilder library, String name, String lower) {} - void beforeService(LibraryBuilder library, BlockBuilder builder, String? name, + void beforeService(LibraryBuilder library, BlockBuilder builder, String name, String lower) {} void applyToConfigureServer( LibraryBuilder library, MethodBuilder configureServer, BlockBuilder block, - String? name, + String name, String lower) {} Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, - String? name, String lower) => + String name, String lower) => literal(null); @override diff --git a/lib/src/commands/service_generators/map.dart b/lib/src/commands/service_generators/map.dart index acb28c9..545961d 100644 --- a/lib/src/commands/service_generators/map.dart +++ b/lib/src/commands/service_generators/map.dart @@ -9,7 +9,7 @@ class MapServiceGenerator extends ServiceGenerator { @override Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, - String? name, String lower) { + String name, String lower) { return refer('MapService').newInstance([]); } } diff --git a/lib/src/commands/service_generators/mongo.dart b/lib/src/commands/service_generators/mongo.dart index e0f7445..be09079 100644 --- a/lib/src/commands/service_generators/mongo.dart +++ b/lib/src/commands/service_generators/mongo.dart @@ -26,7 +26,7 @@ class MongoServiceGenerator extends ServiceGenerator { } @override - void applyToLibrary(LibraryBuilder library, String? name, String lower) { + void applyToLibrary(LibraryBuilder library, String name, String lower) { library.directives.addAll([ Directive.import('package:angel3_mongo/angel3_mongo.dart'), Directive.import('package:mongo_dart/mongo_dart.dart'), @@ -35,7 +35,7 @@ class MongoServiceGenerator extends ServiceGenerator { @override Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, - String? name, String lower) { + String name, String lower) { return refer('MongoService').newInstance([ refer('db').property('collection').call([literal(pluralize(lower))]) ]); diff --git a/lib/src/commands/service_generators/rethink.dart b/lib/src/commands/service_generators/rethink.dart index 3f57dc2..dcc18c8 100644 --- a/lib/src/commands/service_generators/rethink.dart +++ b/lib/src/commands/service_generators/rethink.dart @@ -31,7 +31,7 @@ class RethinkServiceGenerator extends ServiceGenerator { } @override - void applyToLibrary(LibraryBuilder library, String? name, String lower) { + void applyToLibrary(LibraryBuilder library, String name, String lower) { library.directives.addAll([ 'package:angel3_rethink/angel3_rethink.dart', 'package:rethinkdb_dart/rethinkdb_dart.dart' @@ -40,7 +40,7 @@ class RethinkServiceGenerator extends ServiceGenerator { @override Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, - String? name, String lower) { + String name, String lower) { return refer('RethinkService').newInstance([ refer('connection'), refer('r').property('table').call([literal(pluralize(lower))]) diff --git a/lib/src/util.dart b/lib/src/util.dart index 6927c86..589e1d3 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -9,10 +9,10 @@ final String checkmark = ansiOutputEnabled ? '\u2714' : '[Success]'; final String ballot = ansiOutputEnabled ? '\u2717' : '[Failure]'; -String? get homeDirPath => - Platform.environment['HOME'] ?? Platform.environment['USERPROFILE']; +String get homeDirPath => + Platform.environment['HOME'] ?? Platform.environment['USERPROFILE'] ?? '.'; -Directory get homeDir => Directory(homeDirPath!); +Directory get homeDir => Directory(homeDirPath); Directory get angelDir => Directory(p.join(homeDir.path, '.angel')); diff --git a/pubspec.yaml b/pubspec.yaml index 8b0e762..f37f625 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_cli -version: 3.1.0 +version: 3.1.1 description: Command line tools for the Angel3 framework, including scaffolding. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel3-cli