2
0

grunt-amberc.js 3.7 KB

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