1+ import 'dart:io' ;
2+
13import 'package:args/args.dart' ;
24import 'package:args/command_runner.dart' ;
3- import 'package:cli_hook/cli_hook.dart' ;
45import 'package:cli_util/cli_logging.dart' ;
5- import 'package:cli_wrapper/cli_wrapper .dart' ;
6+ import 'package:tuple/tuple .dart' ;
67
78import 'commands/help.dart' ;
8- import 'hook.dart' ;
9+ import 'exception.dart' ;
10+ import 'logger.dart' ;
11+ import 'search.dart' ;
12+ import 'shell.dart' ;
913import 'version.g.dart' ;
1014
1115/// A class that can run Flutterw with scripts and command hooks system support.
@@ -17,26 +21,21 @@ import 'version.g.dart';
1721///
1822/// await flutterw.run(['pub', 'get']);
1923/// ```
20- class FlutterwRunner extends CommandRunner with WrapperRunner , HookRunner {
24+ class FlutterwRunner extends CommandRunner {
2125 FlutterwRunner ({
2226 this .scripts = const {},
23- Logger ? logger ,
24- }) : logger = logger ?? Logger . standard () ,
25- super ('flutterw' ,
27+ this .runOrigin = runFlutter ,
28+ this .logger ,
29+ }) : super ('flutterw' ,
2630 'flutterw wraps flutter with scripts and command hooks support' );
2731
28- /// Scripts map.
29- /// Each can be a single string or a list of strings
30- final Map <String , dynamic > scripts;
31-
32- /// Flutterw Logger
33- final Logger logger;
32+ final Map <String , List <String >> scripts;
3433
35- @override
36- String get originExecutableName => 'flutter' ;
34+ /// Logger
35+ final Logger ? logger ;
3736
38- @override
39- Map < String , Hook > get hooks => ScriptHook . transform (scripts, logger) ;
37+ ///
38+ final Future < int > Function ( List < String >) runOrigin ;
4039
4140 @override
4241 String ? get usageFooter =>
@@ -56,13 +55,69 @@ class FlutterwRunner extends CommandRunner with WrapperRunner, HookRunner {
5655 }
5756
5857 @override
59- Future runCommand (ArgResults topLevelResults) {
60- if (topLevelResults.command == null ) {
61- if (topLevelResults.rest.isNotEmpty &&
62- topLevelResults.rest.contains ('--version' )) {
63- logger.stderr ('Flutterw $kFlutterwVersion ' );
58+ ArgResults parse (Iterable <String > args) {
59+ ArgResults results;
60+ try {
61+ results = argParser.parse (args);
62+ } on ArgParserException catch (e) {
63+ if (e.commands.isEmpty) {
64+ return ArgParser .allowAnything ().parse (args);
65+ }
66+ var command = commands[e.commands.first]! ;
67+ for (var commandName in e.commands.skip (1 )) {
68+ command = command.subcommands[commandName]! ;
69+ }
70+
71+ command.usageException (e.message);
72+ }
73+ return results;
74+ }
75+
76+ @override
77+ Future run (Iterable <String > args) async {
78+ // No arguments or contains -h,--help
79+ if (args.isEmpty || args.contains ('-h' ) || args.contains ('--help' )) {
80+ return super .run (args);
81+ }
82+ final topLevelResults = parse (args);
83+ final commandHooks = searchCommandHooks (args: args, scripts: scripts);
84+ final pre = commandHooks.item1;
85+ if (pre != null ) {
86+ await _hook (pre);
87+ }
88+ final command = commandHooks.item2;
89+ dynamic result;
90+ if (command != null ) {
91+ result = await _hook (command);
92+ } else {
93+ result = await runCommand (topLevelResults);
94+ }
95+ final post = commandHooks.item3;
96+ if (post != null ) {
97+ await _hook (post);
98+ }
99+ return result;
100+ }
101+
102+ @override
103+ Future <dynamic > runCommand (ArgResults topLevelResults) {
104+ if (topLevelResults.command == null && topLevelResults.rest.isNotEmpty) {
105+ if (topLevelResults.rest.contains ('--version' )) {
106+ stdout.writeln ('Flutterw $kFlutterwVersion ' );
64107 }
108+ return Future .sync (() => runOrigin (topLevelResults.arguments));
65109 }
66110 return super .runCommand (topLevelResults);
67111 }
112+
113+ /// Run hook defined in [scripts]
114+ Future <int > _hook (Tuple2 <String , List <String >> hook) async {
115+ logger? .script (hook.item1, scripts[hook.item1]);
116+ final code =
117+ await runShells (scripts[hook.item1]! , hook.item2, logger: logger);
118+ if (code != 0 ) {
119+ throw ScriptException (hook.item1);
120+ }
121+ return 0 ;
122+ }
68123}
0 commit comments