1
0

grunt-amberc.js 3.6 KB

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