grunt-amberc.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. module.exports = function(grunt) {
  2. var path = require('path');
  3. var fs = require('fs');
  4. var amberc = require('../../bin/amberc.js');
  5. /**
  6. A full example entry for a Gruntfile.js is available below.
  7. Please note that the verbose level is either specified globally
  8. or on a target specific level.
  9. However, it can additionally be triggered on the commandline by
  10. adding the '-v' or '--verbose' flag.
  11. Example Gruntfile.js entry:
  12. amberc: {
  13. options: {
  14. amber_dir: process.cwd(), // REQUIRED
  15. verbose: true // optional
  16. },
  17. helloWorld: {
  18. src: ['projects/HelloWorld/st/HelloWorld.st'], // REQUIRED
  19. output_dir: 'projects/HelloWorld/js', // optional
  20. libraries: 'Canvas', // optional
  21. jsGlobals: ['global1', 'global2'], // optional
  22. main_class: 'HelloWorld', // optional
  23. output_name: 'helloWorld', // optional
  24. amd_namespace: 'MyNamespace', // optional (default: 'amber')
  25. main_file: 'myMain.js', // optional
  26. output_suffix: 'mySuffix', // optional
  27. library_suffix: '-0.9', // optional
  28. options: {
  29. verbose: true
  30. }
  31. },
  32. },
  33. */
  34. grunt.registerMultiTask('amberc', 'Compile Smalltalk files with the amberc compiler', function() {
  35. // mark required properties
  36. this.requiresConfig('amberc.options.amber_dir');
  37. this.requiresConfig(['amberc', this.target, 'src']);
  38. var options = this.options({
  39. amber_dir: undefined,
  40. verbose: grunt.option('verbose') || false
  41. });
  42. this.data.verbose = options.verbose;
  43. // mark task as async task
  44. var done = this.async();
  45. // create and initialize amberc
  46. var compiler = new amberc.Compiler(grunt.config('amberc.options.amber_dir'));
  47. // generate the amberc configuration out of the given target properties
  48. var configuration = generateCompilerConfiguration(this.data, grunt.config('amberc.options.amber_dir'));
  49. // run the compiler and call the async callback once finished
  50. var self = this;
  51. compiler.main(configuration, function(){
  52. // signal that task has finished
  53. done();
  54. });
  55. });
  56. function generateCompilerConfiguration(data, amber_dir) {
  57. var configuration = amberc.createDefaults(amber_dir);
  58. var parameters = [];
  59. var libraries = data.libraries;
  60. if (undefined !== libraries) {
  61. configuration.load = libraries;
  62. }
  63. var mainClass = data.main_class;
  64. if (undefined !== mainClass) {
  65. configuration.main = mainClass;
  66. }
  67. var mainFile = data.main_file;
  68. if (undefined !== mainFile) {
  69. configuration.mainfile = mainFile;
  70. }
  71. var outputSuffix = data.output_suffix;
  72. if (undefined !== outputSuffix) {
  73. configuration.suffix = outputSuffix;
  74. configuration.suffix_used = outputSuffix;
  75. }
  76. var librarySuffix = data.library_suffix;
  77. if (undefined !== librarySuffix) {
  78. configuration.loadsuffix = librarySuffix;
  79. configuration.suffix_used = librarySuffix;
  80. }
  81. var sourceFiles = data.src;
  82. if (undefined !== sourceFiles) {
  83. sourceFiles.forEach(function(currentItem){
  84. var fileSuffix = path.extname(currentItem);
  85. switch (fileSuffix) {
  86. case '.st':
  87. configuration.stFiles.push(currentItem);
  88. break;
  89. case '.js':
  90. configuration.jsFiles.push(currentItem);
  91. break;
  92. }
  93. });
  94. }
  95. var outputName = data.output_name;
  96. if (undefined !== outputName) {
  97. configuration.program = outputName;
  98. }
  99. var amdNamespace = data.amd_namespace;
  100. if (undefined !== amdNamespace) {
  101. configuration.amd_namespace = amdNamespace;
  102. }
  103. if (undefined !== data.output_dir) {
  104. configuration.output_dir = data.output_dir;
  105. }
  106. if (undefined !== data.jsGlobals) {
  107. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  108. }
  109. configuration.verbose = data.verbose;
  110. return configuration;
  111. }
  112. };