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
String get name => "plugin";
@override
String get description => "Creates a new plug-in within the given project.";
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
run() async {
2018-07-14 21:50:19 +00:00
var pubspec = await loadPubspec();
2017-08-08 01:45:37 +00:00
String name;
2018-07-14 21:50:19 +00:00
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
}
List<MakerDependency> deps = [
const MakerDependency('angel_framework', '^1.0.0')
];
var rc = new ReCase(name);
final pluginDir = new Directory.fromUri(
2018-07-14 21:50:19 +00:00
Directory.current.uri.resolve(argResults['output-dir'] as String));
2017-08-08 01:45:37 +00:00
final pluginFile =
new File.fromUri(pluginDir.uri.resolve("${rc.snakeCase}.dart"));
if (!await pluginFile.exists()) await pluginFile.create(recursive: true);
await pluginFile.writeAsString(
new DartFormatter().format(_generatePlugin(pubspec, rc)));
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
}
''';
}
}