1
0

grunt-amberc.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. amd_namespace: 'MyNamespace', // optional (default: 'amber')
  21. main_file: 'myMain.js', // 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 and call the async callback once finished
  44. var self = this;
  45. compiler.main(configuration, function(){
  46. // signal that task has finished
  47. done();
  48. });
  49. });
  50. function generateCompilerConfiguration(data, amber_dir) {
  51. var configuration = amberc.createDefaults(amber_dir);
  52. var parameters = [];
  53. var libraries = data.libraries;
  54. if (undefined !== libraries) {
  55. configuration.load = libraries;
  56. }
  57. var mainClass = data.main_class;
  58. if (undefined !== mainClass) {
  59. configuration.main = mainClass;
  60. }
  61. var mainFile = data.main_file;
  62. if (undefined !== mainFile) {
  63. configuration.mainfile = mainFile;
  64. }
  65. var outputSuffix = data.output_suffix;
  66. if (undefined !== outputSuffix) {
  67. configuration.suffix = outputSuffix;
  68. configuration.suffix_used = outputSuffix;
  69. }
  70. var librarySuffix = data.library_suffix;
  71. if (undefined !== librarySuffix) {
  72. configuration.loadsuffix = librarySuffix;
  73. configuration.suffix_used = librarySuffix;
  74. }
  75. var sourceFiles = data.src;
  76. if (undefined !== sourceFiles) {
  77. sourceFiles.forEach(function(currentItem){
  78. var fileSuffix = path.extname(currentItem);
  79. switch (fileSuffix) {
  80. case '.st':
  81. configuration.stFiles.push(currentItem);
  82. break;
  83. case '.js':
  84. configuration.jsFiles.push(currentItem);
  85. break;
  86. }
  87. });
  88. }
  89. var outputName = data.output_name;
  90. if (undefined !== outputName) {
  91. configuration.program = outputName;
  92. }
  93. var amdNamespace = data.amd_namespace;
  94. if (undefined !== amdNamespace) {
  95. configuration.amd_namespace = amdNamespace;
  96. }
  97. if (undefined !== data.output_dir) {
  98. configuration.output_dir = data.output_dir;
  99. }
  100. if (undefined !== data.jsGlobals) {
  101. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  102. }
  103. configuration.verbose = data.verbose;
  104. return configuration;
  105. }
  106. };