grunt-amberc.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. verbose: true // optional
  25. },
  26. },
  27. */
  28. grunt.registerMultiTask('amberc', 'Compile Smalltalk files with the amberc compiler', function() {
  29. // mark required properties
  30. this.requiresConfig('amberc._config.amber_dir');
  31. this.requiresConfig(['amberc', this.target, 'src']);
  32. // change working directory if the working_dir property is set on the target
  33. var current_dir = process.cwd();
  34. var working_dir = this.data.working_dir;
  35. if (undefined !== working_dir) {
  36. grunt.file.setBase(working_dir);
  37. }
  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._config.amber_dir'), grunt.config('amberc._config.closure_jar'));
  42. // generate the amberc configuration out of the given target properties
  43. var configuration = generateCompilerConfiguration(this.data, grunt.config('amberc._config.amber_dir'));
  44. // run the compiler
  45. // change back to the old working directory and call the async callback once finished
  46. var self = this;
  47. compiler.main(configuration, function(){
  48. if (undefined !== self.data.target_dir) {
  49. var absolute_target_dir = path.join(current_dir, self.data.target_dir);
  50. replaceFileSuffix_moveToTargetDir(self.data.src, absolute_target_dir);
  51. // if deploy is set also copy the deploy files
  52. if (self.data.deploy) {
  53. var suffix = self.data.output_suffix || 'deploy';
  54. suffix = '.' + suffix + '.js';
  55. replaceFileSuffix_moveToTargetDir(self.data.src, absolute_target_dir, suffix);
  56. }
  57. }
  58. // reset working directory
  59. grunt.file.setBase(current_dir);
  60. // signal that task has finished
  61. done();
  62. });
  63. });
  64. function generateCompilerConfiguration(data, amber_dir) {
  65. var configuration = amberc.createDefaults(amber_dir);
  66. var parameters = [];
  67. var libraries = data.libraries;
  68. if (undefined !== libraries) {
  69. configuration.load = libraries;
  70. }
  71. var initFile = data.init;
  72. if (undefined !== initFile) {
  73. configuration.init = initFile;
  74. }
  75. var mainClass = data.main_class;
  76. if (undefined !== mainClass) {
  77. configuration.main = mainClass;
  78. }
  79. var mainFile = data.main_file;
  80. if (undefined !== initFile) {
  81. configuration.mainfile = mainFile;
  82. }
  83. if (true === data.deploy) {
  84. configuration.deploy = true;
  85. }
  86. var outputSuffix = data.output_suffix;
  87. if (undefined !== outputSuffix) {
  88. configuration.suffix = outputSuffix;
  89. configuration.suffix_used = outputSuffix;
  90. }
  91. var librarySuffix = data.library_suffix;
  92. if (undefined !== librarySuffix) {
  93. configuration.loadsuffix = librarySuffix;
  94. configuration.suffix_used = librarySuffix;
  95. }
  96. var sourceFiles = data.src;
  97. if (undefined !== sourceFiles) {
  98. sourceFiles.forEach(function(currentItem){
  99. var fileSuffix = path.extname(currentItem);
  100. switch (fileSuffix) {
  101. case '.st':
  102. configuration.stFiles.push(currentItem);
  103. break;
  104. case '.js':
  105. configuration.jsFiles.push(currentItem);
  106. break;
  107. }
  108. });
  109. }
  110. var outputName = data.output_name;
  111. if (undefined !== outputName) {
  112. configuration.program = outputName;
  113. }
  114. return configuration;
  115. }
  116. /**
  117. * Replace '.st' suffix of \p files with '.js' or with \p replace_suffix if this parameter is given.
  118. * Afterwards move the files with replaced suffix to \p target_dir if the files exist.
  119. */
  120. function replaceFileSuffix_moveToTargetDir(files, target_dir, replace_suffix) {
  121. var suffix = replace_suffix || '.js';
  122. var compiledFiles = files.map(function(item) {
  123. return item.replace(/.st$/g, suffix);
  124. });
  125. compiledFiles.forEach(function(file) {
  126. if (fs.existsSync(file)) {
  127. console.log('Move: ' + file + ' -> ' + path.join(target_dir, file));
  128. fs.renameSync(file, path.join(target_dir, file));
  129. }
  130. });
  131. }
  132. };