grunt-amberc.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. module.exports = function(grunt) {
  2. var path = require('path');
  3. var fs = require('fs');
  4. var amberc = require('../../bin/amberc.js');
  5. /**
  6. Full config looks like this:
  7. amberc: {
  8. _config: {
  9. amber_dir: process.cwd(), // REQUIRED
  10. closure_jar: '', // optional
  11. verbose: true // optional
  12. },
  13. helloWorld: {
  14. src: ['projects/HelloWorld/st/HelloWorld.st'], // REQUIRED
  15. output_dir: 'projects/HelloWorld/js', // optional
  16. libraries: 'Canvas', // optional
  17. jsGlobals: ['global1', 'global2'], // optional
  18. main_class: 'HelloWorld', // optional
  19. output_name: 'helloWorld', // optional
  20. init: 'myInit', // optional
  21. main_file: 'myMain.js', // optional
  22. deploy: true, // optional
  23. output_suffix: 'mySuffix', // optional
  24. library_suffix: '-0.9' // optional
  25. },
  26. },
  27. */
  28. grunt.registerMultiTask('amberc', 'Compile Smalltalk files with the amberc compiler', function() {
  29. // mark required properties
  30. this.requiresConfig('amberc.options.amber_dir');
  31. this.requiresConfig(['amberc', this.target, 'src']);
  32. var options = this.options({
  33. amber_dir: undefined,
  34. closure_jar: '',
  35. verbose: false
  36. });
  37. this.data.verbose = options.verbose;
  38. // mark task as async task
  39. var done = this.async();
  40. // create and initialize amberc
  41. var compiler = new amberc.Compiler(grunt.config('amberc.options.amber_dir'), grunt.config('amberc.options.closure_jar'));
  42. // generate the amberc configuration out of the given target properties
  43. var configuration = generateCompilerConfiguration(this.data, grunt.config('amberc.options.amber_dir'));
  44. // run the compiler
  45. // change back to the old working directory and call the async callback once finished
  46. var self = this;
  47. compiler.main(configuration, function(){
  48. // signal that task has finished
  49. done();
  50. });
  51. });
  52. function generateCompilerConfiguration(data, amber_dir) {
  53. var configuration = amberc.createDefaults(amber_dir);
  54. var parameters = [];
  55. var libraries = data.libraries;
  56. if (undefined !== libraries) {
  57. configuration.load = libraries;
  58. }
  59. var initFile = data.init;
  60. if (undefined !== initFile) {
  61. configuration.init = initFile;
  62. }
  63. var mainClass = data.main_class;
  64. if (undefined !== mainClass) {
  65. configuration.main = mainClass;
  66. }
  67. var mainFile = data.main_file;
  68. if (undefined !== mainFile) {
  69. configuration.mainfile = mainFile;
  70. }
  71. if (true === data.deploy) {
  72. configuration.deploy = true;
  73. }
  74. var outputSuffix = data.output_suffix;
  75. if (undefined !== outputSuffix) {
  76. configuration.suffix = outputSuffix;
  77. configuration.suffix_used = outputSuffix;
  78. }
  79. var librarySuffix = data.library_suffix;
  80. if (undefined !== librarySuffix) {
  81. configuration.loadsuffix = librarySuffix;
  82. configuration.suffix_used = librarySuffix;
  83. }
  84. var sourceFiles = data.src;
  85. if (undefined !== sourceFiles) {
  86. sourceFiles.forEach(function(currentItem){
  87. var fileSuffix = path.extname(currentItem);
  88. switch (fileSuffix) {
  89. case '.st':
  90. configuration.stFiles.push(currentItem);
  91. break;
  92. case '.js':
  93. configuration.jsFiles.push(currentItem);
  94. break;
  95. }
  96. });
  97. }
  98. var outputName = data.output_name;
  99. if (undefined !== outputName) {
  100. configuration.program = outputName;
  101. }
  102. if (undefined !== data.output_dir) {
  103. configuration.output_dir = data.output_dir;
  104. }
  105. if (undefined !== data.jsGlobals) {
  106. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  107. }
  108. configuration.verbose = data.verbose;
  109. return configuration;
  110. }
  111. };