1
0

amberc-grunt.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 configuration out of the given target properties
  42. var configuration = generateCompilerConfiguration(this.data, grunt.config('amberc._config.amber_dir'));
  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(configuration, 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 generateCompilerConfiguration(data, amber_dir) {
  64. var configuration = amberc.createDefaults(amber_dir);
  65. var parameters = [];
  66. var libraries = data.libraries;
  67. if (undefined !== libraries) {
  68. configuration.load = libraries;
  69. }
  70. var initFile = data.init;
  71. if (undefined !== initFile) {
  72. configuration.init = initFile;
  73. }
  74. var mainClass = data.main_class;
  75. if (undefined !== mainClass) {
  76. configuration.main = mainClass;
  77. }
  78. var mainFile = data.main_file;
  79. if (undefined !== initFile) {
  80. configuration.mainfile = mainFile;
  81. }
  82. if (true === data.deploy) {
  83. configuration.deploy = true;
  84. }
  85. var outputSuffix = data.output_suffix;
  86. if (undefined !== outputSuffix) {
  87. configuration.suffix = outputSuffix;
  88. configuration.suffix_used = outputSuffix;
  89. }
  90. var librarySuffix = data.library_suffix;
  91. if (undefined !== librarySuffix) {
  92. configuration.loadsuffix = librarySuffix;
  93. configuration.suffix_used = librarySuffix;
  94. }
  95. var sourceFiles = data.src;
  96. if (undefined !== sourceFiles) {
  97. sourceFiles.forEach(function(currentItem){
  98. var fileSuffix = path.extname(currentItem);
  99. switch (fileSuffix) {
  100. case '.st':
  101. configuration.stFiles.push(currentItem);
  102. break;
  103. case '.js':
  104. configuration.jsFiles.push(currentItem);
  105. break;
  106. }
  107. });
  108. }
  109. var outputName = data.output_name;
  110. if (undefined !== outputName) {
  111. configuration.program = outputName;
  112. }
  113. return configuration;
  114. }
  115. /**
  116. * Replace '.st' suffix of \p files with '.js' or with \p replace_suffix if this parameter is given.
  117. * Afterwards move the files with replaced suffix to \p target_dir if the files exist.
  118. */
  119. function replaceFileSuffix_moveToTargetDir(files, target_dir, replace_suffix) {
  120. var suffix = replace_suffix || '.js';
  121. var compiledFiles = files.map(function(item) {
  122. return item.replace(/.st$/g, suffix);
  123. });
  124. compiledFiles.forEach(function(file) {
  125. if (path.existsSync(file)) {
  126. console.log('Move: ' + file + ' -> ' + path.join(target_dir, file));
  127. fs.renameSync(file, path.join(target_dir, file));
  128. }
  129. });
  130. }
  131. };