forked from TopQuadrant/shacl-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
149 lines (129 loc) · 5.11 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var gulp = require('gulp');
var nodeunit = require('gulp-nodeunit');
var browserify = require('gulp-browserify');
var fs = require('fs');
var serve = require('gulp-serve');
var http = require("https");
var fetch = function (url, c) {
var acc = "";
http.get(url, function (res) {
res.on('data', function (d) {
acc += d;
});
res.on('end', function () {
c(acc);
});
});
};
gulp.task('checkJavaFiles', function (cb) {
// In the current design, the version of dash.ttl in the Java repo (and TopBraid) is slightly different
// because the Java version requires some hard-coded features from the tosh namespace, so this is left
// out of the synch test for now
var files = [
//"./vocabularies/dash.ttl": "https://raw.githubusercontent.com/TopQuadrant/shacl/master/src/main/resources/etc/dash.ttl",
["./vocabularies/shacl.ttl", "https://raw.githubusercontent.com/TopQuadrant/shacl/master/src/main/resources/rdf/shacl.ttl"],
["./shared/dash.js", "https://raw.githubusercontent.com/TopQuadrant/shacl/master/src/main/resources/js/dash.js"],
["./shared/rdfquery.js", "https://raw.githubusercontent.com/TopQuadrant/shacl/master/src/main/resources/js/rdfquery.js"]
];
var uptodate = true;
var checkFile = function (fileInfo, cb) {
var p = fileInfo[0];
var url = fileInfo[1];
var read = fs.readFileSync(p).toString();
fetch(url, function (data) {
console.log(url);
console.log(read === data);
uptodate = uptodate && (read === data);
cb();
});
};
var checkFiles = function (files) {
if (files.length === 0) {
if (uptodate) {
cb();
} else {
cb(new Error("Some Java files are not in sync"));
}
} else {
var file = files.shift();
checkFile(file, function () {
checkFiles(files);
});
}
};
checkFiles(files);
});
gulp.task('test', function () {
gulp.src('./test/**/*.js')
.pipe(nodeunit({}));
});
gulp.task('browserify', function () {
if (fs.existsSync('dist/index.js')) {
fs.unlinkSync('dist/index.js');
}
if (fs.existsSync('dist/shacl.js')) {
fs.unlinkSync('dist/shacl.js');
}
gulp.src('index.js')
.pipe(browserify({
standalone: 'SHACLValidator'
}))
.pipe(gulp.dest('dist'))
.on('end', function () {
fs.renameSync('dist/index.js', 'dist/shacl.js');
});
});
gulp.task('generate-vocabularies', function () {
var vocabularies = fs.readdirSync("./vocabularies");
var acc = {};
for (var i = 0; i < vocabularies.length; i++) {
console.log("Generating " + vocabularies[i]);
acc[vocabularies[i].split(".ttl")[0]] = fs.readFileSync("./vocabularies/" + vocabularies[i]).toString();
fs.writeFileSync("./src/vocabularies.js", "module.exports = " + JSON.stringify(acc));
}
});
/**
* We generate rdfquery from the shared library and we add it to the validator code and to the
* list of libraries that must be loaded.
* Dash.js is only added to the list of loaded libraries.
*/
gulp.task('generate-libraries', function () {
var libraries = {
"http://datashapes.org/js/dash.js": "./shared/dash.js",
"http://datashapes.org/js/rdfquery.js": "./shared/rdfquery.js"
};
var acc = {};
for (var library in libraries) {
console.log("Generating " + library);
acc[library] = fs.readFileSync(libraries[library]).toString();
fs.writeFileSync("./src/libraries.js", "module.exports = " + JSON.stringify(acc));
}
var rdfqueryTemplate = fs.readFileSync("./templates/rdfquery.js").toString();
var rdfqueryData = fs.readFileSync("./shared/rdfquery.js").toString();
var generated = rdfqueryTemplate.replace("</content>", rdfqueryData);
fs.writeFileSync("./src/rdfquery.js", generated);
});
gulp.task('browserify-public-tests', function () {
if (fs.existsSync('public/test.js')) {
fs.unlinkSync('public/test.js');
}
fs.createReadStream('dist/shacl.js').pipe(fs.createWriteStream('public/shacl.js'));
gulp.src('public/src/test.js')
.pipe(browserify())
.pipe(gulp.dest('public'));
});
gulp.task('generate-public-test-cases', function () {
var testCases = [];
if (!fs.existsSync(__dirname + "/public/data"))
fs.mkdirSync(__dirname + "/public/data");
fs.readdirSync(__dirname + "/test/data/core").forEach(function (dir) {
fs.readdirSync(__dirname + "/test/data/core/" + dir).forEach(function (file) {
var read = fs.readFileSync(__dirname + "/test/data/core/" + dir + "/" + file).toString();
fs.writeFileSync(__dirname + "/public/data/" + dir + "_" + file, read);
testCases.push("data/" + dir + "_" + file);
});
});
fs.writeFileSync(__dirname + "/public/test_cases.json", JSON.stringify(testCases));
});
gulp.task('test-web', ['generate-public-test-cases', 'browserify-public-tests'], serve('public'));
gulp.task('default', ['test', 'browserify']);