grunt-amberc.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. module.exports = function (grunt) {
  2. var path = require('path');
  3. var fs = require('fs');
  4. var amberc = require('../lib/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/src/HelloWorld.st'], // REQUIRED
  26. output_dir: 'projects/HelloWorld/src', // optional
  27. libraries: 'Web', // optional
  28. jsGlobals: ['global1', 'global2'], // optional
  29. amd_namespace: 'MyNamespace', // optional (default: 'amber')
  30. },
  31. },
  32. */
  33. grunt.registerMultiTask('amberc', 'Compile Smalltalk files with the amberc compiler', function () {
  34. // mark task as async task
  35. var done = this.async();
  36. var options = this.options({
  37. amber_dir: undefined,
  38. library_dirs: [],
  39. verbose: grunt.option('verbose') || false
  40. });
  41. this.data.verbose = options.verbose;
  42. this.data.library_dirs = options.library_dirs;
  43. // mark required properties
  44. this.requiresConfig('amberc.options.amber_dir');
  45. // raise error on missing source files
  46. if (this.filesSrc.length === 0) {
  47. grunt.fail.fatal('No source files to compile or link.');
  48. }
  49. // create and initialize amberc
  50. var compiler = new amberc.Compiler(grunt.config('amberc.options.amber_dir'));
  51. // generate the amberc configuration out of the given target properties
  52. var configuration = generateCompilerConfiguration(this.data, this.filesSrc);
  53. // run the compiler and call the async callback once finished
  54. var self = this;
  55. compiler.main(configuration, function () {
  56. // signal that task has finished
  57. done();
  58. });
  59. });
  60. function generateCompilerConfiguration(data, sourceFiles) {
  61. var configuration = amberc.createDefaultConfiguration();
  62. var parameters = [];
  63. var libraries = data.libraries;
  64. if (undefined !== libraries) {
  65. configuration.load = libraries;
  66. }
  67. var library_dirs = data.library_dirs;
  68. if (undefined !== library_dirs) {
  69. configuration.jsLibraryDirs = library_dirs;
  70. }
  71. if (undefined !== sourceFiles) {
  72. configuration.stFiles = sourceFiles;
  73. }
  74. var amdNamespace = data.amd_namespace;
  75. if (undefined !== amdNamespace) {
  76. configuration.amd_namespace = amdNamespace;
  77. }
  78. if (undefined !== data.output_dir) {
  79. configuration.output_dir = data.output_dir;
  80. }
  81. if (undefined !== data.jsGlobals) {
  82. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  83. }
  84. configuration.verbose = data.verbose;
  85. return configuration;
  86. }
  87. };