Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit a6d882a

Browse files
committed
Split up tasks, add docgen task
1 parent fa09679 commit a6d882a

File tree

4 files changed

+277
-181
lines changed

4 files changed

+277
-181
lines changed

tasks/compiler.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
};

tasks/docgen.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 compile = function (dest, src, options, callback) {
14+
15+
var args = src.filter(function (filepath) {
16+
if (!grunt.file.exists(filepath)) {
17+
grunt.log.warn("Source file \"" + filepath + "\" not found.");
18+
return false;
19+
} else {
20+
return true;
21+
}
22+
});
23+
24+
return grunt.util.spawn({
25+
cmd: "docgen",
26+
args: args,
27+
options: { cwd: process.cwd() }
28+
}, function (err, result) {
29+
if (err) {
30+
grunt.log.error("Error creating file " + dest);
31+
grunt.log.error(result.stdout);
32+
callback(err);
33+
} else {
34+
grunt.log.ok("Created file " + dest + ".");
35+
callback();
36+
}
37+
});
38+
39+
};
40+
41+
grunt.registerMultiTask("docgen", "Generate markdown documentation for PureScript modules.", function () {
42+
43+
var options = this.options();
44+
var output = [];
45+
var callback = this.async();
46+
var files = this.files;
47+
48+
var compileNext = function (err) {
49+
if (err) {
50+
callback(err);
51+
} else if (files.length === 0) {
52+
callback();
53+
} else {
54+
var file = files.pop();
55+
compile(file.dest, file.src, options, compileNext);
56+
}
57+
};
58+
59+
compileNext();
60+
});
61+
62+
};

tasks/psci.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
grunt.registerMultiTask("dotPsci", "Generate/update .psci file to load a set of modules on startup.", function () {
14+
15+
var entries = [];
16+
var isNew = true;
17+
18+
try {
19+
entries = grunt.file.read(".psci").split("\n");
20+
isNew = false;
21+
} catch (e) {
22+
if (e.origError.code !== "ENOENT") {
23+
grunt.log.error(e);
24+
return;
25+
}
26+
}
27+
28+
entries = entries.filter(function (entry) {
29+
return entry.indexOf(":m") !== 0;
30+
});
31+
32+
entries = this.filesSrc.map(function (file) {
33+
return ":m " + file;
34+
}).concat(entries);
35+
36+
grunt.file.write(".psci", entries.join("\n"), "utf8");
37+
grunt.log.ok((isNew ? "Created" : "Updated") + " .psci file");
38+
});
39+
40+
};

0 commit comments

Comments
 (0)