grunt-amberc.js 3.7 KB

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