|
| 1 | +/* |
| 2 | + * grunt-purescript |
| 3 | + * https://github.com/purescript/grunt-purescript |
| 4 | + * |
| 5 | + * Copyright (c) 2014 Gary Burgess |
| 6 | + * Licensed under the MIT license. |
| 7 | + */ |
| 8 | + |
| 9 | +"use strict"; |
| 10 | + |
| 11 | +module.exports = function (grunt) { |
| 12 | + |
| 13 | + var flagOptions = { |
| 14 | + noPrelude: "--no-prelude", |
| 15 | + noOpts: "--no-opts", |
| 16 | + noMagicDo: "--no-magic-do", |
| 17 | + noTco: "--no-tco", |
| 18 | + runtimeTypeChecks: "--runtime-type-checks" |
| 19 | + }; |
| 20 | + |
| 21 | + var argumentOptions = { |
| 22 | + browserNamespace: "--browser-namespace", |
| 23 | + externs: "--externs" |
| 24 | + }; |
| 25 | + |
| 26 | + var moduleOptions = { |
| 27 | + modules: "module", |
| 28 | + codegen: "codegen" |
| 29 | + }; |
| 30 | + |
| 31 | + var compile = function (dest, src, options, callback) { |
| 32 | + |
| 33 | + // Get source file and common command line arguments |
| 34 | + var args = getDefaultArgs(src, options); |
| 35 | + |
| 36 | + var addModuleOption = function (mo) { |
| 37 | + var option = options[mo]; |
| 38 | + if (option) { |
| 39 | + var optionArg = moduleOptions[mo]; |
| 40 | + if (typeof option === "string") { |
| 41 | + args.push("--" + optionArg + "=" + option); |
| 42 | + } else { |
| 43 | + option.forEach(function (module) { |
| 44 | + args.push("--" + optionArg + "=" + module); |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + // Add arguments that can accept a list of module names |
| 51 | + for (var mo in moduleOptions) { |
| 52 | + if (moduleOptions.hasOwnProperty(mo)) { |
| 53 | + addModuleOption(mo); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Add the main module argument |
| 58 | + if (options.main) { |
| 59 | + if (options.main === true) { |
| 60 | + args.push("--main"); |
| 61 | + } else { |
| 62 | + args.push("--main=" + options.main); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Add the destination file output argument |
| 67 | + args.push("--output=" + dest); |
| 68 | + |
| 69 | + // Run the compiler |
| 70 | + return grunt.util.spawn({ |
| 71 | + cmd: "psc", |
| 72 | + args: args, |
| 73 | + options: { cwd: process.cwd() } |
| 74 | + }, function (err, result) { |
| 75 | + if (err) { |
| 76 | + grunt.log.error("Error creating file " + dest); |
| 77 | + grunt.log.error(result.stdout); |
| 78 | + callback(err); |
| 79 | + } else { |
| 80 | + grunt.log.ok("Created file " + dest + "."); |
| 81 | + callback(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + }; |
| 86 | + |
| 87 | + var getDefaultArgs = function(src, options) { |
| 88 | + |
| 89 | + // Use the input file list as the initial arguments |
| 90 | + var args = src.filter(function (filepath) { |
| 91 | + if (!grunt.file.exists(filepath)) { |
| 92 | + grunt.log.warn("Source file \"" + filepath + "\" not found."); |
| 93 | + return false; |
| 94 | + } else { |
| 95 | + return true; |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + // Add any option flags |
| 100 | + for (var flag in flagOptions) { |
| 101 | + if (flagOptions.hasOwnProperty(flag)) { |
| 102 | + if (options[flag] === true) { |
| 103 | + args.push(flagOptions[flag]); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Add any option arguments |
| 109 | + for (var arg in argumentOptions) { |
| 110 | + if (argumentOptions.hasOwnProperty(arg)) { |
| 111 | + if (typeof options[arg] === "string") { |
| 112 | + args.push(argumentOptions[arg] + "=" + options[arg]); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return args; |
| 118 | + }; |
| 119 | + |
| 120 | + grunt.registerMultiTask("psc", "Compile PureScript files.", function () { |
| 121 | + |
| 122 | + var options = this.options(); |
| 123 | + var callback = this.async(); |
| 124 | + var files = this.files; |
| 125 | + |
| 126 | + var compileNext = function (err) { |
| 127 | + if (err) { |
| 128 | + callback(err); |
| 129 | + } else if (files.length === 0) { |
| 130 | + callback(); |
| 131 | + } else { |
| 132 | + var file = files.pop(); |
| 133 | + compile(file.dest, file.src, options, compileNext); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + compileNext(); |
| 138 | + }); |
| 139 | + |
| 140 | + grunt.registerMultiTask("pscMake", "Compile PureScript files in make mode.", function () { |
| 141 | + |
| 142 | + var callback = this.async(); |
| 143 | + |
| 144 | + // Get source file and common command line arguments |
| 145 | + var args = getDefaultArgs(this.filesSrc, this.options()); |
| 146 | + |
| 147 | + if (this.data.dest) { |
| 148 | + args.push("--output=" + this.data.dest); |
| 149 | + } |
| 150 | + |
| 151 | + // Run the compiler |
| 152 | + return grunt.util.spawn({ |
| 153 | + cmd: "psc-make", |
| 154 | + args: args, |
| 155 | + options: { cwd: process.cwd() } |
| 156 | + }, function (err, result) { |
| 157 | + if (err) { |
| 158 | + grunt.log.error("Make failed:"); |
| 159 | + grunt.log.error(result.stdout); |
| 160 | + callback(err); |
| 161 | + } else { |
| 162 | + grunt.log.ok("Make was successful."); |
| 163 | + callback(); |
| 164 | + } |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | +}; |
0 commit comments