2
0

grunt-amberc.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 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 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 mainClass = data.main_class;
  59. if (undefined !== mainClass) {
  60. configuration.main = mainClass;
  61. }
  62. var mainFile = data.main_file;
  63. if (undefined !== mainFile) {
  64. configuration.mainfile = mainFile;
  65. }
  66. if (true === data.deploy) {
  67. configuration.deploy = true;
  68. }
  69. var outputSuffix = data.output_suffix;
  70. if (undefined !== outputSuffix) {
  71. configuration.suffix = outputSuffix;
  72. configuration.suffix_used = outputSuffix;
  73. }
  74. var librarySuffix = data.library_suffix;
  75. if (undefined !== librarySuffix) {
  76. configuration.loadsuffix = librarySuffix;
  77. configuration.suffix_used = librarySuffix;
  78. }
  79. var sourceFiles = data.src;
  80. if (undefined !== sourceFiles) {
  81. sourceFiles.forEach(function(currentItem){
  82. var fileSuffix = path.extname(currentItem);
  83. switch (fileSuffix) {
  84. case '.st':
  85. configuration.stFiles.push(currentItem);
  86. break;
  87. case '.js':
  88. configuration.jsFiles.push(currentItem);
  89. break;
  90. }
  91. });
  92. }
  93. var outputName = data.output_name;
  94. if (undefined !== outputName) {
  95. configuration.program = outputName;
  96. }
  97. var amdNamespace = data.amd_namespace;
  98. if (undefined !== amdNamespace) {
  99. configuration.amd_namespace = amdNamespace;
  100. }
  101. if (undefined !== data.output_dir) {
  102. configuration.output_dir = data.output_dir;
  103. }
  104. if (undefined !== data.jsGlobals) {
  105. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  106. }
  107. configuration.verbose = data.verbose;
  108. return configuration;
  109. }
  110. };