platform-cli/lib/src/commands/make/plugin.dart

70 lines
2 KiB
Dart
Raw Normal View History

2017-08-08 01:45:37 +00:00
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:dart_style/dart_style.dart';
2018-07-14 21:50:19 +00:00
import 'package:io/ansi.dart';
import 'package:prompts/prompts.dart' as prompts;
2018-07-14 21:47:49 +00:00
import 'package:pubspec_parse/pubspec_parse.dart';
2017-08-08 01:45:37 +00:00
import 'package:recase/recase.dart';
2018-07-14 21:50:19 +00:00
import '../../util.dart';
2017-08-08 01:45:37 +00:00
import 'maker.dart';
class PluginCommand extends Command {
@override
2021-06-11 05:07:53 +00:00
String get name => 'plugin';
2017-08-08 01:45:37 +00:00
@override
2021-06-11 05:07:53 +00:00
String get description => 'Creates a new plug-in within the given project.';
2017-08-08 01:45:37 +00:00
PluginCommand() {
argParser
..addOption('name',
abbr: 'n', help: 'Specifies a name for the plug-in class.')
..addOption('output-dir',
help: 'Specifies a directory to create the plug-in class in.',
defaultsTo: 'lib/src/config/plugins');
}
@override
2021-06-11 05:07:53 +00:00
Future run() async {
2018-07-14 21:50:19 +00:00
var pubspec = await loadPubspec();
2021-06-12 04:10:34 +00:00
String? name;
if (argResults!.wasParsed('name')) name = argResults!['name'] as String?;
2017-08-08 01:45:37 +00:00
if (name?.isNotEmpty != true) {
2018-07-14 21:50:19 +00:00
name = prompts.get('Name of plug-in class');
2017-08-08 01:45:37 +00:00
}
2021-06-11 05:07:53 +00:00
var deps = <MakerDependency>[
2019-07-17 15:18:49 +00:00
const MakerDependency('angel_framework', '^2.0.0')
2017-08-08 01:45:37 +00:00
];
2021-06-12 04:10:34 +00:00
var rc = ReCase(name!);
2021-06-11 05:07:53 +00:00
final pluginDir = Directory.fromUri(
2021-06-12 04:10:34 +00:00
Directory.current.uri.resolve(argResults!['output-dir'] as String));
2017-08-08 01:45:37 +00:00
final pluginFile =
2021-06-11 05:07:53 +00:00
File.fromUri(pluginDir.uri.resolve('${rc.snakeCase}.dart'));
2017-08-08 01:45:37 +00:00
if (!await pluginFile.exists()) await pluginFile.create(recursive: true);
2021-06-11 05:07:53 +00:00
await pluginFile
.writeAsString(DartFormatter().format(_generatePlugin(pubspec, rc)));
2017-08-08 01:45:37 +00:00
if (deps.isNotEmpty) await depend(deps);
2018-07-14 21:50:19 +00:00
print(green.wrap(
'$checkmark Successfully generated plug-in file "${pluginFile.absolute.path}".'));
2017-08-08 01:45:37 +00:00
}
2018-07-14 21:47:49 +00:00
String _generatePlugin(Pubspec pubspec, ReCase rc) {
2017-08-08 01:45:37 +00:00
return '''
library ${pubspec.name}.src.config.plugins.${rc.snakeCase};
import 'package:angel_framework/angel_framework.dart';
2018-07-14 23:26:05 +00:00
AngelConfigurer ${rc.camelCase}() {
return (Angel app) async {
2017-08-08 01:45:37 +00:00
// Work some magic...
2018-07-14 23:26:05 +00:00
};
2017-08-08 01:45:37 +00:00
}
''';
}
}