grunt-amberc.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. options: { // the 'options' object is optional
  19. verbose: true
  20. },
  21. src: ['projects/HelloWorld/st/HelloWorld.st'], // REQUIRED
  22. output_dir: 'projects/HelloWorld/js', // optional
  23. libraries: 'Canvas', // optional
  24. jsGlobals: ['global1', 'global2'], // optional
  25. main_class: 'HelloWorld', // optional
  26. output_name: 'helloWorld', // optional
  27. amd_namespace: 'MyNamespace', // optional (default: 'amber')
  28. main_file: 'myMain.js', // optional
  29. output_suffix: 'mySuffix', // optional
  30. library_suffix: '-0.9' // optional
  31. },
  32. },
  33. */
  34. grunt.registerMultiTask('amberc', 'Compile Smalltalk files with the amberc compiler', function() {
  35. // mark task as async task
  36. var done = this.async();
  37. var options = this.options({
  38. amber_dir: undefined,
  39. verbose: grunt.option('verbose') || false
  40. });
  41. this.data.verbose = options.verbose;
  42. // mark required properties
  43. this.requiresConfig('amberc.options.amber_dir');
  44. // raise error on missing source files
  45. if (this.filesSrc.length === 0) {
  46. grunt.fail.fatal('No source files to compile or link.');
  47. }
  48. // create and initialize amberc
  49. var compiler = new amberc.Compiler(grunt.config('amberc.options.amber_dir'));
  50. // generate the amberc configuration out of the given target properties
  51. var configuration = generateCompilerConfiguration(this.data, this.filesSrc, grunt.config('amberc.options.amber_dir'));
  52. // run the compiler and call the async callback once finished
  53. var self = this;
  54. compiler.main(configuration, function(){
  55. // signal that task has finished
  56. done();
  57. });
  58. });
  59. function generateCompilerConfiguration(data, sourceFiles, amber_dir) {
  60. var configuration = amberc.createDefaults(amber_dir);
  61. var parameters = [];
  62. var libraries = data.libraries;
  63. if (undefined !== libraries) {
  64. configuration.load = libraries;
  65. }
  66. var mainClass = data.main_class;
  67. if (undefined !== mainClass) {
  68. configuration.main = mainClass;
  69. }
  70. var mainFile = data.main_file;
  71. if (undefined !== mainFile) {
  72. configuration.mainfile = mainFile;
  73. }
  74. var outputSuffix = data.output_suffix;
  75. if (undefined !== outputSuffix) {
  76. configuration.suffix = outputSuffix;
  77. configuration.suffix_used = outputSuffix;
  78. }
  79. var librarySuffix = data.library_suffix;
  80. if (undefined !== librarySuffix) {
  81. configuration.loadsuffix = librarySuffix;
  82. configuration.suffix_used = librarySuffix;
  83. }
  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. var amdNamespace = data.amd_namespace;
  102. if (undefined !== amdNamespace) {
  103. configuration.amd_namespace = amdNamespace;
  104. }
  105. if (undefined !== data.output_dir) {
  106. configuration.output_dir = data.output_dir;
  107. }
  108. if (undefined !== data.jsGlobals) {
  109. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  110. }
  111. configuration.verbose = data.verbose;
  112. return configuration;
  113. }
  114. };