2
0

grunt-amberc.js 4.6 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. 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. library_dirs: ['dir1', '/usr/local/js'], // optional
  16. verbose: true // optional
  17. },
  18. helloWorld: {
  19. // this 'options' object is optional as well as all parameters inside it
  20. // they can be used to override the global 'options'
  21. options: {
  22. library_dirs: ['dir1', '/usr/local/js'], // optional
  23. verbose: true
  24. },
  25. src: ['projects/HelloWorld/st/HelloWorld.st'], // REQUIRED
  26. output_dir: 'projects/HelloWorld/js', // optional
  27. libraries: 'Canvas', // optional
  28. jsGlobals: ['global1', 'global2'], // optional
  29. main_class: 'HelloWorld', // optional
  30. output_name: 'helloWorld', // optional
  31. amd_namespace: 'MyNamespace', // optional (default: 'amber')
  32. main_file: 'myMain.js', // optional
  33. output_suffix: 'mySuffix', // optional
  34. library_suffix: '-0.9' // optional
  35. },
  36. },
  37. */
  38. grunt.registerMultiTask('amberc', 'Compile Smalltalk files with the amberc compiler', function() {
  39. // mark task as async task
  40. var done = this.async();
  41. var options = this.options({
  42. amber_dir: undefined,
  43. library_dirs: [],
  44. verbose: grunt.option('verbose') || false
  45. });
  46. this.data.verbose = options.verbose;
  47. this.data.library_dirs = options.library_dirs;
  48. // mark required properties
  49. this.requiresConfig('amberc.options.amber_dir');
  50. // raise error on missing source files
  51. if (this.filesSrc.length === 0) {
  52. grunt.fail.fatal('No source files to compile or link.');
  53. }
  54. // create and initialize amberc
  55. var compiler = new amberc.Compiler(grunt.config('amberc.options.amber_dir'));
  56. // generate the amberc configuration out of the given target properties
  57. var configuration = generateCompilerConfiguration(this.data, this.filesSrc);
  58. // run the compiler and call the async callback once finished
  59. var self = this;
  60. compiler.main(configuration, function(){
  61. // signal that task has finished
  62. done();
  63. });
  64. });
  65. function generateCompilerConfiguration(data, sourceFiles) {
  66. var configuration = amberc.createDefaults();
  67. var parameters = [];
  68. var libraries = data.libraries;
  69. if (undefined !== libraries) {
  70. configuration.load = libraries;
  71. }
  72. var library_dirs = data.library_dirs;
  73. if (undefined !== library_dirs) {
  74. configuration.jsLibraryDirs = library_dirs;
  75. }
  76. var mainClass = data.main_class;
  77. if (undefined !== mainClass) {
  78. configuration.main = mainClass;
  79. }
  80. var mainFile = data.main_file;
  81. if (undefined !== mainFile) {
  82. configuration.mainfile = mainFile;
  83. }
  84. var outputSuffix = data.output_suffix;
  85. if (undefined !== outputSuffix) {
  86. configuration.suffix = outputSuffix;
  87. configuration.suffix_used = outputSuffix;
  88. }
  89. var librarySuffix = data.library_suffix;
  90. if (undefined !== librarySuffix) {
  91. configuration.loadsuffix = librarySuffix;
  92. configuration.suffix_used = librarySuffix;
  93. }
  94. if (undefined !== sourceFiles) {
  95. sourceFiles.forEach(function(currentItem){
  96. var fileSuffix = path.extname(currentItem);
  97. switch (fileSuffix) {
  98. case '.st':
  99. configuration.stFiles.push(currentItem);
  100. break;
  101. case '.js':
  102. configuration.jsFiles.push(currentItem);
  103. break;
  104. }
  105. });
  106. }
  107. var outputName = data.output_name;
  108. if (undefined !== outputName) {
  109. configuration.program = outputName;
  110. }
  111. var amdNamespace = data.amd_namespace;
  112. if (undefined !== amdNamespace) {
  113. configuration.amd_namespace = amdNamespace;
  114. }
  115. if (undefined !== data.output_dir) {
  116. configuration.output_dir = data.output_dir;
  117. }
  118. if (undefined !== data.jsGlobals) {
  119. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  120. }
  121. configuration.verbose = data.verbose;
  122. return configuration;
  123. }
  124. };