amberc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. var compiler = new amberc.Compiler(amber_dir);
  16. var configuration = handle_options(parameters, amber_dir);
  17. compiler.main(configuration);
  18. /**
  19. * Process given program options and update defaults values.
  20. * Followed by check_for_closure_compiler() and then collect_files().
  21. */
  22. function handle_options(optionsArray, amber_dir) {
  23. var programName = [];
  24. var currentItem = optionsArray.shift();
  25. var defaults = amberc.createDefaults(amber_dir);
  26. while(undefined !== currentItem) {
  27. switch(currentItem) {
  28. case '-l':
  29. defaults.load.push.apply(defaults.load, optionsArray.shift().split(','));
  30. break;
  31. case '-g':
  32. defaults.jsGlobals.push.apply(defaults.jsGlobals, optionsArray.shift().split(','));
  33. break;
  34. case '-m':
  35. defaults.main = optionsArray.shift();
  36. break;
  37. case '-M':
  38. defaults.mainfile = optionsArray.shift();
  39. break;
  40. case '-n':
  41. defaults.amd_namespace = optionsArray.shift();
  42. break;
  43. case '-D':
  44. defaults.output_dir = optionsArray.shift();
  45. break;
  46. case '-s':
  47. defaults.suffix = optionsArray.shift();
  48. defaults.suffix_used = defaults.suffix;
  49. break;
  50. case '-S':
  51. defaults.loadsuffix = optionsArray.shift();
  52. defaults.suffix_used = defaults.suffix;
  53. break;
  54. case '-v':
  55. defaults.verbose = true;
  56. break;
  57. case '-h':
  58. case '--help':
  59. case '?':
  60. print_usage();
  61. break;
  62. default:
  63. var fileSuffix = path.extname(currentItem);
  64. switch (fileSuffix) {
  65. case '.st':
  66. defaults.stFiles.push(currentItem);
  67. break;
  68. case '.js':
  69. defaults.jsFiles.push(currentItem);
  70. break;
  71. default:
  72. // Will end up being the last non js/st argument
  73. programName.push(currentItem);
  74. break;
  75. };
  76. };
  77. currentItem = optionsArray.shift();
  78. }
  79. if(1 < programName.length) {
  80. throw new Error('More than one name for ProgramName given: ' + programName);
  81. } else {
  82. defaults.program = programName[0];
  83. }
  84. return defaults;
  85. };
  86. // print available flags
  87. function print_usage() {
  88. console.log('Usage: amberc [-l lib1,lib2...] [-i init_file] [-m main_class] [-M main_file]');
  89. console.log(' [-o] [-O|-A] [-d] [-s suffix] [-S suffix] [file1 [file2 ...]] [Program]');
  90. console.log('');
  91. console.log(' amberc compiles Amber files - either separately or into a complete runnable');
  92. console.log(' program. If no .st files are listed only a linking stage is performed.');
  93. console.log(' Files listed will be handled using the following rules:');
  94. console.log('');
  95. console.log(' *.js');
  96. console.log(' Files are linked (concatenated) in listed order.');
  97. console.log(' If not found we look in $AMBER/js/');
  98. console.log('');
  99. console.log(' *.st');
  100. console.log(' Files are compiled into .js files before concatenation.');
  101. console.log(' If not found we look in $AMBER/st/.');
  102. console.log('');
  103. console.log(' NOTE: Each .st file is currently considered to be a fileout of a single class');
  104. console.log(' category of the same name as the file!');
  105. console.log('');
  106. console.log(' If no <Program> is specified each given .st file will be compiled into');
  107. console.log(' a matching .js file. Otherwise a <Program>.js file is linked together based on');
  108. console.log(' the given options:');
  109. console.log(' -l library1,library2');
  110. console.log(' Add listed JavaScript libraries in listed order.');
  111. console.log(' Libraries are not separated by spaces or end with .js.');
  112. console.log('');
  113. console.log(' -g jsGlobal1,jsGlobal2');
  114. console.log(' Comma separated list of JS global variable names.');
  115. console.log(' The names are added to a list containing "window", "document" and others.');
  116. console.log('');
  117. console.log(' -m main_class');
  118. console.log(' Add a call to the class method main_class>>main at the end of <Program>.');
  119. console.log('');
  120. console.log(' -M main_file');
  121. console.log(' Add <main_file> at the end of <Program.js> acting as #main.');
  122. console.log('');
  123. console.log(' -n amd_namespace');
  124. console.log(' Export packages with <amd_namespace> as the require.js namespace.');
  125. console.log(' Default value is "amber_core".');
  126. console.log('');
  127. console.log(' -D');
  128. console.log(' Specifies the output directory for all generated .js files.');
  129. console.log(' The hierarchy of the input files is not maintaned.');
  130. console.log(' If this option is omitted all generated .js files are placed next to their input files');
  131. console.log('');
  132. console.log(' -s suffix');
  133. console.log(' Add <suffix> to compiled .js files. File.st is then compiled into');
  134. console.log(' File.<suffix>.js.');
  135. console.log('');
  136. console.log(' -S suffix');
  137. console.log(' Use <suffix> for all libraries accessed using -l. This makes it possible');
  138. console.log(' to have multiple flavors of Amber and libraries in the same place.');
  139. console.log('');
  140. console.log('');
  141. console.log(' Example invocations:');
  142. console.log('');
  143. console.log(' Just compile Kernel-Objects.st to Kernel-Objects.js:');
  144. console.log('');
  145. console.log(' amberc Kernel-Objects.st');
  146. console.log('');
  147. console.log(' Compile Hello.st to Hello.js and create complete program called Program.js.');
  148. console.log(' Additionally add a call to the class method Hello>>main:');
  149. console.log('');
  150. console.log(' amberc -m Hello Hello.st Program');
  151. console.log('');
  152. console.log(' Compile Cat1.st and Cat2.st files into corresponding .js files.');
  153. console.log(' Link them with myboot.js and myKernel.js and add myinit.js as custom');
  154. console.log(' initializer file. Add main.js last which contains the startup code');
  155. console.log(' and merge everything into a complete program named Program.js:');
  156. console.log('');
  157. console.log(' amberc -M main.js -i myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program');
  158. };