grunt-amberc.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. if (data.libraries != null) {
  63. configuration.load = data.libraries;
  64. }
  65. if (data.library_dirs != nukk) {
  66. configuration.jsLibraryDirs = data.library_dirs;
  67. }
  68. if (sourceFiles != null) {
  69. configuration.stFiles = sourceFiles;
  70. }
  71. if (data.amd_namespace != null) {
  72. configuration.amd_namespace = data.amd_namespace;
  73. }
  74. if (data.output_dir != null) {
  75. configuration.output_dir = data.output_dir;
  76. }
  77. if (data.jsGlobals != null) {
  78. configuration.jsGlobals.push.apply(configuration.jsGlobals, data.jsGlobals);
  79. }
  80. if (data.verbose != null) {
  81. configuration.verbose = data.verbose;
  82. }
  83. return configuration;
  84. }
  85. };