|
1 | 1 | package hmm;
|
2 | 2 |
|
| 3 | +using Lambda; |
| 4 | + |
| 5 | +using thx.Functions; |
| 6 | + |
3 | 7 | import hmm.commands.*;
|
| 8 | +import hmm.errors.*; |
4 | 9 | import hmm.utils.Log;
|
5 | 10 | import hmm.utils.Shell;
|
6 |
| -using Lambda; |
7 | 11 |
|
8 | 12 | class Hmm {
|
9 | 13 | public static var commands(default, null) : Array<ICommand>;
|
10 | 14 |
|
11 | 15 | public static function main() {
|
12 |
| - commands = [ |
13 |
| - new HelpCommand(), |
14 |
| - new VersionCommand(), |
15 |
| - new SetupCommand(), |
16 |
| - new InitCommand(), |
17 |
| - new FromHxmlCommand(), |
18 |
| - new ToHxmlCommand(), |
19 |
| - new InstallCommand(), |
20 |
| - new HaxelibCommand(), |
21 |
| - new GitCommand(), |
22 |
| - new HgCommand(), |
23 |
| - new UpdateCommand(), |
24 |
| - new RemoveCommand(), |
25 |
| - new CheckCommand(), |
26 |
| - new CleanCommand(), |
27 |
| - new HmmUpdateCommand(), |
28 |
| - new HmmRemoveCommand(), |
29 |
| - ]; |
| 16 | + try { |
| 17 | + commands = [ |
| 18 | + new HelpCommand(), |
| 19 | + new VersionCommand(), |
| 20 | + new SetupCommand(), |
| 21 | + new InitCommand(), |
| 22 | + new FromHxmlCommand(), |
| 23 | + new ToHxmlCommand(), |
| 24 | + new InstallCommand(), |
| 25 | + new ReinstallCommand(), |
| 26 | + new HaxelibCommand(), |
| 27 | + new GitCommand(), |
| 28 | + new HgCommand(), |
| 29 | + new UpdateCommand(), |
| 30 | + new RemoveCommand(), |
| 31 | + new CheckCommand(), |
| 32 | + new CleanCommand(), |
| 33 | + new HmmUpdateCommand(), |
| 34 | + new HmmRemoveCommand(), |
| 35 | + ]; |
30 | 36 |
|
31 |
| - var args = Sys.args(); |
| 37 | + var args = Sys.args().copy(); |
32 | 38 |
|
33 |
| - Shell.init({ |
34 |
| - hmmDirectory: Sys.getCwd(), |
35 |
| - workingDirectory: args.pop() |
36 |
| - }); |
| 39 | + Shell.init({ |
| 40 | + hmmDirectory: Sys.getCwd(), |
| 41 | + workingDirectory: args.pop() |
| 42 | + }); |
37 | 43 |
|
38 |
| - var commandType = args.shift(); |
| 44 | + var commandType = args.shift(); |
| 45 | + if (commandType == null) { |
| 46 | + throw new ValidationError('no command given', 1); |
| 47 | + } |
39 | 48 |
|
40 |
| - var command = commands.find(function(command) { |
41 |
| - return command.type == commandType; |
42 |
| - }); |
| 49 | + var command = commands.find(function(command) { |
| 50 | + return command.type == commandType; |
| 51 | + }); |
43 | 52 |
|
44 |
| - if (command == null) { |
45 |
| - Log.error('invalid command: $commandType'); |
46 |
| - printUsageAndExit(); |
47 |
| - Sys.exit(1); |
48 |
| - } |
| 53 | + if (command == null) { |
| 54 | + throw new ValidationError('unrecognized command: $commandType', 1); |
| 55 | + } |
49 | 56 |
|
50 |
| - //Log.info("hmm"); |
51 |
| - //Log.info('working directory: ${Shell.workingDirectory}'); |
52 |
| - //Log.info('hmm directory: ${Shell.hmmDirectory}'); |
53 |
| - //Log.info('command: $commandType'); |
| 57 | + command.run(args); |
| 58 | + } catch (e : ValidationError) { |
| 59 | + die('Validation error: ${e.message}', e.statusCode); |
| 60 | + } catch (e : SystemError) { |
| 61 | + die('Execution error: ${e.message}', e.statusCode); |
| 62 | + } catch (e : Dynamic) { |
| 63 | + die('Unexpected error: $e', 1); |
| 64 | + } |
| 65 | + } |
54 | 66 |
|
55 |
| - command.run(args); |
| 67 | + static function die(message : String, statusCode : Int) : Void { |
| 68 | + Log.error(message); |
| 69 | + Log.println('Use "hmm help" to see usage'); |
| 70 | + Sys.exit(statusCode); |
56 | 71 | }
|
57 | 72 |
|
58 |
| - public static function printUsageAndExit() { |
| 73 | + public static function printUsageAndExit(statusCode : Int) : Void { |
59 | 74 | Log.println("Usage: hmm <command> [options]");
|
60 | 75 | Log.println("");
|
61 | 76 | Log.println("commands:");
|
62 | 77 | Log.println("");
|
63 |
| - for (command in commands) { |
| 78 | + printUsagesAndExit(commands.map.fn(_.type), statusCode); |
| 79 | + } |
| 80 | + |
| 81 | + public static function printUsagesAndExit(types : Array<String>, statusCode : Int) : Void { |
| 82 | + for (type in types) { |
| 83 | + var command = commands.find.fn(_.type == type); |
| 84 | + if (command == null) { |
| 85 | + throw new ValidationError('invalid command: $type', 1); |
| 86 | + } |
64 | 87 | Log.println(' ${command.type} - ${command.getUsage()}');
|
65 | 88 | Log.println("");
|
66 | 89 | }
|
67 |
| - Sys.exit(1); |
| 90 | + Sys.exit(statusCode); |
68 | 91 | }
|
69 | 92 | }
|
70 | 93 |
|
0 commit comments