amberc-grunt.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. },
  12. helloWorld: {
  13. src: ['HelloWorld.st'], // REQUIRED
  14. working_dir: 'projects/HelloWorld/st', // optional
  15. target_dir: 'projects/HelloWorld/js', // optional
  16. main_class: 'HelloWorld', // optional
  17. output_name: 'helloWorld', // optional
  18. libraries: 'Canvas', // optional
  19. init: 'myInit', // optional
  20. main_file: 'myMain.js', // optional
  21. deploy: true, // 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._config.amber_dir');
  30. this.requiresConfig(['amberc', this.target, 'src']);
  31. // change working directory if the working_dir property is set on the target
  32. var current_dir = process.cwd();
  33. var working_dir = this.data.working_dir;
  34. if (undefined !== working_dir) {
  35. grunt.file.setBase(working_dir);
  36. }
  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._config.amber_dir'), grunt.config('amberc._config.closure_jar'));
  41. // generate the amberc parameter string out of the given target properties
  42. var parameters = generateParameterArray(this.data);
  43. // run the compiler
  44. // change back to the old working directory and call the async callback once finished
  45. var self = this;
  46. compiler.main(parameters, function(){
  47. if (undefined !== self.data.target_dir) {
  48. var absolute_target_dir = path.join(current_dir, self.data.target_dir);
  49. replaceFileSuffix_moveToTargetDir(self.data.src, absolute_target_dir);
  50. // if deploy is set also copy the deploy files
  51. if (self.data.deploy) {
  52. var suffix = self.data.output_suffix || 'deploy';
  53. suffix = '.' + suffix + '.js';
  54. replaceFileSuffix_moveToTargetDir(self.data.src, absolute_target_dir, suffix);
  55. }
  56. }
  57. // reset working directory
  58. grunt.file.setBase(current_dir);
  59. // signal that task has finished
  60. done();
  61. });
  62. });
  63. function generateParameterArray(data) {
  64. var parameters = [];
  65. var libraries = data.libraries;
  66. if (undefined !== libraries) {
  67. parameters.push('-l');
  68. parameters.push(libraries.join(','));
  69. }
  70. var initFile = data.init;
  71. if (undefined !== initFile) {
  72. parameters.push('-i');
  73. parameters.push(initFile);
  74. }
  75. var mainClass = data.main_class;
  76. if (undefined !== mainClass) {
  77. parameters.push('-m');
  78. parameters.push(mainClass);
  79. }
  80. var mainFile = data.main_file;
  81. if (undefined !== initFile) {
  82. parameters.push('-M');
  83. parameters.push(mainFile);
  84. }
  85. if (true === data.deploy) {
  86. parameters.push('-d');
  87. }
  88. var outputSuffix = data.output_suffix;
  89. if (undefined !== outputSuffix) {
  90. parameters.push('-s');
  91. parameters.push(outputSuffix);
  92. }
  93. var librarySuffix = data.library_suffix;
  94. if (undefined !== librarySuffix) {
  95. parameters.push('-S');
  96. parameters.push(librarySuffix);
  97. }
  98. var sourceFiles = data.src;
  99. if (undefined !== sourceFiles) {
  100. parameters.push.apply(parameters, sourceFiles);
  101. }
  102. var outpuName = data.output_name;
  103. if (undefined !== outpuName) {
  104. parameters.push(outpuName);
  105. }
  106. return parameters;
  107. }
  108. /**
  109. * Replace '.st' suffix of \p files with '.js' or with \p replace_suffix if this parameter is given.
  110. * Afterwards move the files with replaced suffix to \p target_dir if the files exist.
  111. */
  112. function replaceFileSuffix_moveToTargetDir(files, target_dir, replace_suffix) {
  113. var suffix = replace_suffix || '.js';
  114. var compiledFiles = files.map(function(item) {
  115. return item.replace(/.st$/g, suffix);
  116. });
  117. compiledFiles.forEach(function(file) {
  118. if (path.existsSync(file)) {
  119. fs.renameSync(file, path.join(target_dir, file));
  120. }
  121. });
  122. }
  123. };