2
0

amberc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env node
  2. var path = require('path');
  3. var amberc = require('./amberc.js');
  4. // get parameters passed to the command line script
  5. // discard the first two parameters which are the node binary and the script name
  6. var parameters = process.argv.slice(2);
  7. // check if at least one parameter was passed to the script
  8. if (1 > parameters.length) {
  9. print_usage();
  10. process.exit();
  11. }
  12. // Get Amber root directory from the location of this script so that
  13. // we can find the st and js directories etc.
  14. var amber_dir = path.normalize(path.join(path.dirname(process.argv[1]), '..'));
  15. // Get default location of compiler.jar
  16. var closure_jar = path.resolve(path.join(process.env['HOME'], 'compiler.jar'));
  17. var compiler = new amberc.Compiler(amber_dir, closure_jar);
  18. var configuration = handle_options(parameters, amber_dir);
  19. compiler.main(configuration);
  20. /**
  21. * Process given program options and update defaults values.
  22. * Followed by check_for_closure_compiler() and then collect_files().
  23. */
  24. function handle_options(optionsArray, amber_dir) {
  25. var programName = [];
  26. var currentItem = optionsArray.shift();
  27. var defaults = amberc.createDefaults(amber_dir);
  28. while(undefined !== currentItem) {
  29. switch(currentItem) {
  30. case '-l':
  31. defaults.load.push.apply(defaults.load, optionsArray.shift().split(','));
  32. break;
  33. case '-g':
  34. defaults.jsGlobals.push.apply(defaults.jsGlobals, optionsArray.shift().split(','));
  35. break;
  36. case '-m':
  37. defaults.main = optionsArray.shift();
  38. break;
  39. case '-M':
  40. defaults.mainfile = optionsArray.shift();
  41. break;
  42. case '-n':
  43. defaults.amd_namespace = optionsArray.shift();
  44. break;
  45. case '-o':
  46. defaults.closure = true;
  47. defaults.closure_parts = true;
  48. break;
  49. case '-O':
  50. defaults.closure = true;
  51. defaults.closure_full = true;
  52. break;
  53. case '-A':
  54. defaults.closure = true;
  55. defaults.closure_options = defaults.closure_options + ' --compilation_level ADVANCED_OPTIMIZATIONS';
  56. defaults.closure_full = true;
  57. break;
  58. case '-d':
  59. defaults.deploy = true;
  60. break;
  61. case '-D':
  62. defaults.output_dir = optionsArray.shift();
  63. break;
  64. case '-s':
  65. defaults.suffix = optionsArray.shift();
  66. defaults.suffix_used = defaults.suffix;
  67. break;
  68. case '-S':
  69. defaults.loadsuffix = optionsArray.shift();
  70. defaults.suffix_used = defaults.suffix;
  71. break;
  72. case '-v':
  73. defaults.verbose = true;
  74. break;
  75. case '-h':
  76. case '--help':
  77. case '?':
  78. print_usage();
  79. break;
  80. default:
  81. var fileSuffix = path.extname(currentItem);
  82. switch (fileSuffix) {
  83. case '.st':
  84. defaults.stFiles.push(currentItem);
  85. break;
  86. case '.js':
  87. defaults.jsFiles.push(currentItem);
  88. break;
  89. default:
  90. // Will end up being the last non js/st argument
  91. programName.push(currentItem);
  92. break;
  93. };
  94. };
  95. currentItem = optionsArray.shift();
  96. }
  97. if(1 < programName.length) {
  98. throw new Error('More than one name for ProgramName given: ' + programName);
  99. } else {
  100. defaults.program = programName[0];
  101. }
  102. return defaults;
  103. };
  104. // print available flags
  105. function print_usage() {
  106. console.log('Usage: amberc [-l lib1,lib2...] [-i init_file] [-m main_class] [-M main_file]');
  107. console.log(' [-o] [-O|-A] [-d] [-s suffix] [-S suffix] [file1 [file2 ...]] [Program]');
  108. console.log('');
  109. console.log(' amberc compiles Amber files - either separately or into a complete runnable');
  110. console.log(' program. If no .st files are listed only a linking stage is performed.');
  111. console.log(' Files listed will be handled using the following rules:');
  112. console.log('');
  113. console.log(' *.js');
  114. console.log(' Files are linked (concatenated) in listed order.');
  115. console.log(' If not found we look in $AMBER/js/');
  116. console.log('');
  117. console.log(' *.st');
  118. console.log(' Files are compiled into .js files before concatenation.');
  119. console.log(' If not found we look in $AMBER/st/.');
  120. console.log('');
  121. console.log(' NOTE: Each .st file is currently considered to be a fileout of a single class');
  122. console.log(' category of the same name as the file!');
  123. console.log('');
  124. console.log(' If no <Program> is specified each given .st file will be compiled into');
  125. console.log(' a matching .js file. Otherwise a <Program>.js file is linked together based on');
  126. console.log(' the given options:');
  127. console.log(' -l library1,library2');
  128. console.log(' Add listed JavaScript libraries in listed order.');
  129. console.log(' Libraries are not separated by spaces or end with .js.');
  130. console.log('');
  131. console.log(' -g jsGlobal1,jsGlobal2');
  132. console.log(' Comma separated list of JS global variable names.');
  133. console.log(' The names are added to a list containing "window", "document" and others.');
  134. console.log('');
  135. console.log(' -m main_class');
  136. console.log(' Add a call to the class method main_class>>main at the end of <Program>.');
  137. console.log('');
  138. console.log(' -M main_file');
  139. console.log(' Add <main_file> at the end of <Program.js> acting as #main.');
  140. console.log('');
  141. console.log(' -n amd_namespace');
  142. console.log(' Export packages with <amd_namespace> as the require.js namespace.');
  143. console.log(' Default value is "amber".');
  144. console.log('');
  145. console.log(' -o');
  146. console.log(' Optimize each .js file using the Google closure compiler.');
  147. console.log(' Using Closure compiler found at ~/compiler.jar');
  148. console.log('');
  149. console.log(' -O');
  150. console.log(' Optimize final <Program>.js using the Google closure compiler.');
  151. console.log(' Using Closure compiler found at ~/compiler.jar');
  152. console.log('');
  153. console.log(' -A Same as -O but use --compilation_level ADVANCED_OPTIMIZATIONS');
  154. console.log('');
  155. console.log(' -d');
  156. console.log(' Additionally export code for deploy - stripped from source etc.');
  157. console.log(' Uses suffix ".deploy.js" in addition to any explicit suffic set by -s.');
  158. console.log('');
  159. console.log(' -D');
  160. console.log(' Specifies the output directory for all generated .js files.');
  161. console.log(' The hierarchy of the input files is not maintaned.');
  162. console.log(' If this option is omitted all generated .js files are placed next to their input files');
  163. console.log('');
  164. console.log(' -s suffix');
  165. console.log(' Add <suffix> to compiled .js files. File.st is then compiled into');
  166. console.log(' File.<suffix>.js.');
  167. console.log('');
  168. console.log(' -S suffix');
  169. console.log(' Use <suffix> for all libraries accessed using -l. This makes it possible');
  170. console.log(' to have multiple flavors of Amber and libraries in the same place.');
  171. console.log('');
  172. console.log('');
  173. console.log(' Example invocations:');
  174. console.log('');
  175. console.log(' Just compile Kernel-Objects.st to Kernel-Objects.js:');
  176. console.log('');
  177. console.log(' amberc Kernel-Objects.st');
  178. console.log('');
  179. console.log(' Compile Hello.st to Hello.js and create complete program called Program.js.');
  180. console.log(' Additionally add a call to the class method Hello>>main:');
  181. console.log('');
  182. console.log(' amberc -m Hello Hello.st Program');
  183. console.log('');
  184. console.log(' Compile Cat1.st and Cat2.st files into corresponding .js files.');
  185. console.log(' Link them with myboot.js and myKernel.js and add myinit.js as custom');
  186. console.log(' initializer file. Add main.js last which contains the startup code');
  187. console.log(' and merge everything into a complete program named Program.js:');
  188. console.log('');
  189. console.log(' amberc -M main.js -i myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program');
  190. };