@@ -2,33 +2,223 @@ import sys.FileSystem;
22import sys .io .File ;
33import haxe .Json ;
44
5- typedef Library = {
6- var name : String ;
7- var type : String ;
8- var ? version : String ;
9- var ? dir : String ;
10- var ? ref : String ;
11- var ? url : String ;
5+ /**
6+ * Represents the configuration structure for the HMM tool, containing an array of library dependencies.
7+ */
8+ typedef HmmConfig =
9+ {
10+ dependencies : Array <LibraryConfig >
1211}
1312
14- class Main {
15- public static function main (): Void {
16- if (! FileSystem .exists (' .haxelib' ))
17- FileSystem .createDirectory (' .haxelib' );
18-
19- final json : Array <Library > = Json .parse (File .getContent (' ./haxelibs.json' )).dependencies ;
20-
21- for (lib in json ) {
22- switch (lib .type ) {
23- case " haxelib" :
24- Sys .command (' haxelib --quiet install ${lib .name } ${lib .version != null ? lib .version : " " }' );
25- case " git" :
26- Sys .command (' haxelib --quiet git ${lib .name } ${lib .url }' );
27- default :
28- Sys .println (' Cannot resolve library of type " ${lib .type }"' );
29- }
30- }
31-
32- Sys .exit (0 );
33- }
34- }
13+ /**
14+ * Represents the configuration for a single library, including its name, type, version, directory, reference, and URL.
15+ */
16+ typedef LibraryConfig =
17+ {
18+ name : String ,
19+ type : String ,
20+ ? version : String ,
21+ ? dir : String ,
22+ ? ref : String ,
23+ ? url : String
24+ }
25+
26+ /**
27+ * The Main class is designed specifically for use with GitHub Actions.
28+ * Its purpose is to install libraries specified in a configuration file (`haxelibs.json`)
29+ * without their dependencies.
30+ */
31+ class Main
32+ {
33+ /**
34+ * The main function is the entry point of the program.
35+ * It checks for the existence of the `.haxelib` directory, creating it if it does not exist.
36+ * Then it reads the `haxelibs.json` configuration file, parses it, and installs each library
37+ * according to its specified type, using the appropriate commands while skipping dependencies.
38+ */
39+ public static function main (): Void
40+ {
41+ // Ensure the .haxelib directory exists
42+ if (! FileSystem .exists (' .haxelib' ))
43+ runCommand ([' haxelib' , ' newrepo' , ' --quiet' , ' --never' ]);
44+
45+ // Read and parse the hmm.json configuration file
46+ final config : HmmConfig = Json .parse (File .getContent (' ./haxelibs.json' ));
47+
48+ // Options to run with the commands
49+ final options : Array <String > = [' --quiet' , ' --never' , ' --skip-dependencies' ];
50+
51+ // Iterate over each library dependency in the configuration
52+ for (lib in config .dependencies )
53+ {
54+ switch (lib .type )
55+ {
56+ case ' haxelib' :
57+ // Prepare the haxelib install command arguments
58+ final args : Array <String > = [' haxelib' , ' install' ];
59+
60+ args .push (lib .name );
61+
62+ if (lib .version != null )
63+ args .push (lib .version );
64+
65+ // Execute the haxelib install command
66+ runCommand (args .concat (options ));
67+ case ' git' :
68+ // Prepare the haxelib git command arguments
69+ final args : Array <String > = [' haxelib' , ' git' ];
70+
71+ args .push (lib .name );
72+ args .push (lib .url );
73+
74+ if (lib .ref != null )
75+ args .push (lib .ref );
76+
77+ // Execute the haxelib git command
78+ runCommand (args .concat (options ));
79+ }
80+ }
81+
82+ // List installed haxelib packages
83+ runCommand ([' haxelib' , ' list' ]);
84+ }
85+
86+ /**
87+ * Executes a system command with the provided arguments.
88+ * @param args The command and its arguments to be executed.
89+ */
90+ public static function runCommand (args : Array <String >): Void
91+ {
92+ final command : String = args .join (' ' );
93+
94+ if (command != AnsiColors .yellow (command ))
95+ Sys .println (AnsiColors .yellow (command ));
96+
97+ Sys .command (args .shift (), args );
98+ }
99+ }
100+
101+ /**
102+ * Utility class for applying ANSI color codes to strings for terminal output.
103+ * @see https://github.com/andywhite37/hmm/blob/master/src/hmm/utils/AnsiColors.hx
104+ */
105+ class AnsiColors
106+ {
107+ /**
108+ * Colors the input string red.
109+ * @param input The input string.
110+ * @return The colored string.
111+ */
112+ public static inline function red (input : String ): String
113+ {
114+ return color (input , Red );
115+ }
116+
117+ /**
118+ * Colors the input string green.
119+ * @param input The input string.
120+ * @return The colored string.
121+ */
122+ public static inline function green (input : String ): String
123+ {
124+ return color (input , Green );
125+ }
126+
127+ /**
128+ * Colors the input string yellow.
129+ * @param input The input string.
130+ * @return The colored string.
131+ */
132+ public static inline function yellow (input : String ): String
133+ {
134+ return color (input , Yellow );
135+ }
136+
137+ /**
138+ * Colors the input string blue.
139+ * @param input The input string.
140+ * @return The colored string.
141+ */
142+ public static inline function blue (input : String ): String
143+ {
144+ return color (input , Blue );
145+ }
146+
147+ /**
148+ * Colors the input string magenta.
149+ * @param input The input string.
150+ * @return The colored string.
151+ */
152+ public static inline function magenta (input : String ): String
153+ {
154+ return color (input , Magenta );
155+ }
156+
157+ /**
158+ * Colors the input string cyan.
159+ * @param input The input string.
160+ * @return The colored string.
161+ */
162+ public static inline function cyan (input : String ): String
163+ {
164+ return color (input , Cyan );
165+ }
166+
167+ /**
168+ * Colors the input string gray.
169+ * @param input The input string.
170+ * @return The colored string.
171+ */
172+ public static inline function gray (input : String ): String
173+ {
174+ return color (input , Gray );
175+ }
176+
177+ /**
178+ * Colors the input string white.
179+ * @param input The input string.
180+ * @return The colored string.
181+ */
182+ public static inline function white (input : String ): String
183+ {
184+ return color (input , White );
185+ }
186+
187+ /**
188+ * Removes any color from the input string.
189+ * @param input The input string.
190+ * @return The uncolored string.
191+ */
192+ public static inline function none (input : String ): String
193+ {
194+ return color (input , None );
195+ }
196+
197+ /**
198+ * Applies the specified ANSI color code to the input string.
199+ * @param input The input string.
200+ * @param ansiColor The ANSI color code.
201+ * @return The colored string.
202+ */
203+ public static inline function color (input : String , ansiColor : AnsiColor ): String
204+ {
205+ return #if sys ' $ansiColor $input ${AnsiColor . None }' #else input #end;
206+ }
207+ }
208+
209+ /**
210+ * Enum abstract representing ANSI color codes.
211+ */
212+ enum abstract AnsiColor (String ) from String to String
213+ {
214+ var Black = ' \033 [0;30m' ;
215+ var Red = ' \033 [0;31m' ;
216+ var Green = ' \033 [0;32m' ;
217+ var Yellow = ' \033 [0;33m' ;
218+ var Blue = ' \033 [0;34m' ;
219+ var Magenta = ' \033 [0;35m' ;
220+ var Cyan = ' \033 [0;36m' ;
221+ var Gray = ' \033 [0;37m' ;
222+ var White = ' \033 [1;37m' ;
223+ var None = ' \033 [0;0m' ;
224+ }
0 commit comments