grunt-amberc.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. module.exports = function (grunt) {
  2. var amberc = require('../lib/amberc');
  3. /**
  4. A full example entry for a Gruntfile.js is available below.
  5. Please note that the verbose level is either specified globally
  6. or on a target specific level.
  7. However, it can additionally be triggered on the commandline by
  8. adding the '-v' or '--verbose' flag.
  9. Example Gruntfile.js entry:
  10. amberc: {
  11. options: {
  12. amber_dir: process.cwd(), // REQUIRED
  13. library_dirs: ['dir1', '/usr/local/js'], // optional
  14. verbose: true // optional
  15. },
  16. helloWorld: {
  17. // this 'options' object is optional as well as all parameters inside it
  18. // they can be used to override the global 'options'
  19. options: {
  20. library_dirs: ['dir1', '/usr/local/js'], // optional
  21. verbose: true
  22. },
  23. src: ['projects/HelloWorld/src/HelloWorld.st'], // REQUIRED
  24. outputDir: 'projects/HelloWorld/src', // optional
  25. libraries: 'Web', // optional
  26. jsGlobals: ['global1', 'global2'], // optional
  27. amdNamespace: 'MyNamespace', // optional (default: 'amber')
  28. },
  29. },
  30. */
  31. grunt.registerMultiTask('amberc', 'Compile Smalltalk files with the amberc compiler', function () {
  32. // mark task as async task
  33. var done = this.async();
  34. var options = this.options({
  35. amber_dir: undefined,
  36. library_dirs: [],
  37. verbose: grunt.option('verbose') || false
  38. });
  39. this.data.verbose = options.verbose;
  40. this.data.library_dirs = options.library_dirs;
  41. // mark required properties
  42. this.requiresConfig('amberc.options.amber_dir');
  43. // raise error on missing source files
  44. if (this.filesSrc.length === 0) {
  45. grunt.fail.fatal('No source files to compile or link.');
  46. }
  47. // create and initialize amberc
  48. var compiler = new amberc.Compiler(grunt.config('amberc.options.amber_dir'));
  49. // generate the amberc configuration out of the given target properties
  50. var configuration = generateCompilerConfiguration(this.data, this.filesSrc);
  51. // run the compiler and call the async callback once finished
  52. var self = this;
  53. compiler.main(configuration, function () {
  54. // signal that task has finished
  55. done();
  56. });
  57. });
  58. function generateCompilerConfiguration(data, sourceFiles) {
  59. var configuration = amberc.createDefaultConfiguration();
  60. if (data.libraries != null) {
  61. configuration.load = data.libraries;
  62. }
  63. if (data.library_dirs != null) {
  64. configuration.jsLibraryDirs = data.library_dirs;
  65. }
  66. if (sourceFiles != null) {
  67. configuration.stFiles = sourceFiles;
  68. }
  69. if (data.amd_namespace != null) {
  70. configuration.amdNamespace = data.amd_namespace;
  71. }
  72. if (data.output_dir != null) {
  73. configuration.outputDir = data.output_dir;
  74. }
  75. if (data.jsGlobals != null) {
  76. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  77. }
  78. if (data.verbose != null) {
  79. configuration.verbose = data.verbose;
  80. }
  81. return configuration;
  82. }
  83. };