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

Commit 7d370b1

Browse files
committed
Initial commit
0 parents  commit 7d370b1

File tree

8 files changed

+202
-0
lines changed

8 files changed

+202
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
*.log
3+
node_modules

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
*.log
3+
node_modules

Fixture.purs.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Fixture where
2+
3+
fixture = "fixture"

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2014 Eric Thul
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a
4+
copy of this software and associated documentation files (the
5+
"Software"), in the Software without restriction, including without
6+
limitation the rights to use, copy, modify, merge, publish, distribute,
7+
sublicense, and/or sell copies of the Software, and to permit persons to
8+
whom the Software is furnished to do so, subject to the following
9+
conditions:
10+
11+
The above copyright notice and this permission notice shall be included
12+
in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gulp-purescript

index.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
'use strict';
2+
3+
var gutil = require('gulp-util')
4+
, through = require('through2')
5+
, cp = require('child_process')
6+
, PLUGIN = 'gulp-purescript'
7+
, OPTIONS = {
8+
psc: {
9+
flags: {
10+
noPrelude: '--no-prelude',
11+
noOpts: '--no-opts',
12+
noMagicDo: '--no-magic-do',
13+
noTco: '--no-tco',
14+
runtimeTypeChecks: '--runtime-type-checks',
15+
main: '--main',
16+
verboseErrors: '--verbose-errors'
17+
}
18+
, single: {browserNamespace: '--browser-namespace', externs: '--externs', main: '--main'}
19+
, multi: {modules: '--module', codegen: '--codegen'}
20+
},
21+
pscMake: {
22+
flags: {
23+
noPrelude: '--no-prelude',
24+
noOpts: '--no-opts',
25+
noMagicDo: '--no-magic-do',
26+
noTco: '--no-tco',
27+
runtimeTypeChecks: '--runtime-type-checks',
28+
verboseErrors: '--verbose-errors'
29+
}
30+
, single: {browserNamespace: '--browser-namespace'}
31+
, multi: {}
32+
}
33+
}
34+
;
35+
36+
function options(o, opts) {
37+
return Object.keys(opts || {}).reduce(function(b, a){
38+
if (a in o.flags && opts[a] === true) return b.concat([o.flags[a]]);
39+
else if (a in o.single && typeof opts[a] === "string") return b.concat([o.single[a] + '=' + opts[a]]);
40+
else if (a in o.multi) {
41+
if (typeof opts[a] === "string") return b.concat([o.multi[a] + '=' + opts[a]]);
42+
else {
43+
return b.concat(opts[a].map(function(x){
44+
return o.multi[a] + '=' + x;
45+
}));
46+
}
47+
}
48+
else return b;
49+
}, []);
50+
}
51+
52+
function acc(f) {
53+
var files = [];
54+
return through.obj(function(file, env, cb){
55+
if (file.isNull()) {
56+
this.push(file);
57+
return cb();
58+
}
59+
if (file.isStream()) {
60+
this.emit('error', new gutil.PluginError(PLUGIN, 'Streaming not supported'));
61+
return cb();
62+
}
63+
files.push(file.path);
64+
cb();
65+
}, function(cb){f.apply(this, [files, cb]);});
66+
}
67+
68+
function psc(opts) {
69+
return acc(function(files, cb){
70+
var args = files.concat(options(OPTIONS.psc, opts))
71+
, cmd = cp.spawn('psc', args)
72+
, buffer = new Buffer(0)
73+
, that = this
74+
;
75+
cmd.stdout.on('data', function(stdout){buffer = Buffer.concat([buffer, new Buffer(stdout)]);});
76+
cmd.stderr.on('data', function(stderr){
77+
gutil.log('Stderr from \'' + gutil.colors.cyan('psc') + '\'\n' + gutil.colors.magenta(stderr));
78+
});
79+
cmd.on('close', function(code){
80+
if (!!code) that.emit('error', new gutil.PluginError(PLUGIN, buffer.toString()));
81+
else {
82+
that.push(new gutil.File({
83+
path: 'psc.js',
84+
contents: buffer
85+
}));
86+
}
87+
cb();
88+
});
89+
});
90+
}
91+
92+
function pscMake(opts) {
93+
return acc(function(files, cb){
94+
var args = options(OPTIONS.pscMake, opts).concat(files)
95+
, cmd = cp.spawn('psc-make', args)
96+
, that = this
97+
;
98+
cmd.stdout.on('data', function(stdout){
99+
gutil.log('Stdout from \'' + gutil.colors.cyan('psc-make') + '\'\n' + gutil.colors.magenta(stdout));
100+
});
101+
cmd.stderr.on('data', function(stderr){
102+
gutil.log('Stderr from \'' + gutil.colors.cyan('psc-make') + '\'\n' + gutil.colors.magenta(stderr));
103+
});
104+
cmd.on('close', function(code){
105+
if (!!code) that.emit('error', new gutil.PluginError(PLUGIN, 'psc-make has failed'));
106+
cb();
107+
});
108+
});
109+
}
110+
111+
module.exports = {
112+
psc: psc,
113+
pscMake: pscMake
114+
}

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "gulp-purescript",
3+
"description": "Run the PureScript compiler",
4+
"version": "0.0.1",
5+
"license": "MIT",
6+
"repository": "ethul/gulp-purescript",
7+
"author": {
8+
"name": "Eric",
9+
"email": "[email protected]"
10+
},
11+
"engineStrict": true,
12+
"engines": {"node": ">=0.10"},
13+
"files": ["index.js"],
14+
"scripts": {"test": "mocha"},
15+
"keywords": [
16+
"gulpplugin",
17+
"purescript"
18+
],
19+
"dependencies": {
20+
"through2": "^0.4.1",
21+
"gulp-util": "^2.2.14"
22+
},
23+
"devDependencies": {
24+
"mocha": "*"
25+
}
26+
}

test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
var gutil = require('gulp-util')
4+
, assert = require('assert')
5+
, fs = require('fs')
6+
, purescript = require('./')
7+
;
8+
9+
it('should compile purescript', function(cb){
10+
var stream = purescript.psc({noPrelude: true})
11+
, fixture = 'Fixture.purs.hs'
12+
;
13+
14+
stream.on('data', function(file){
15+
assert(/Fixture/.test(file.contents.toString()));
16+
cb();
17+
});
18+
19+
fs.readFile(fixture, function(e, buffer){
20+
if (e) cb(assert(false));
21+
else {
22+
stream.write(new gutil.File({
23+
cwd: __dirname,
24+
base: __dirname,
25+
path: __dirname + '/' + fixture,
26+
contents: buffer,
27+
stat: {mtime: new Date()}
28+
}));
29+
stream.end();
30+
}
31+
});
32+
});

0 commit comments

Comments
 (0)