-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgulpfile.js
More file actions
287 lines (238 loc) · 8.92 KB
/
Copy pathgulpfile.js
File metadata and controls
287 lines (238 loc) · 8.92 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import gulp from "gulp"
import fileInclude from "gulp-file-include"
import replace from 'gulp-replace'
import insert from 'gulp-insert'
import clean from 'gulp-clean'
import htmlmin from 'gulp-html-minifier-terser'
import rename from "gulp-rename"
import fs from 'fs'
import path from 'path'
import { createRequire } from 'module'
const require = createRequire(import.meta.url);
const manifest = require('./package.json');
const coreManifest = require('./node_modules/@rekarel/core/package.json');
import { exec } from 'child_process';
const mainPath = "html/index.html"
const mainPathDist = "webapp/"
const docs = [
"html/docs/index.html",
];
const printSubDocs = [
"html/docs/java/",
"html/docs/pascal/",
"html/docs/rekarel/",
];
const subDocs= [
...printSubDocs,
"html/docs/",
"html/docs/java_tutorial/",
"html/docs/pascal_tutorial/",
]
const docsDist = "webapp/docs"
const printDocsDist = "webapp/docs/print"
gulp.task('bundle-html', ()=> {
return gulp.src([mainPath])
.pipe(
fileInclude({
prefix: '@@',
basepath: '@file',
context: {
shortVersion: manifest.version.replace(/[\.\-]/g,"_"),
longVersion: manifest.version,
coreVersion: manifest.dependencies["@rekarel/core"].replace(/^\^/, ""),
languageVersion: coreManifest.rekarel.language
}
}))
.pipe(htmlmin({
collapseWhitespace:true,
removeComments:true
}))
.pipe(gulp.dest(mainPathDist))
})
// Task to copy images
gulp.task('bundle-images', function () {
return gulp.src('resources/img/**/*', {encoding:false})
.pipe(gulp.dest('webapp/img'));
});
// Task to copy CSS
gulp.task('bundle-css', function () {
return gulp.src('resources/css/**/*')
.pipe(gulp.dest('webapp/css'));
});
// Task to copy JavaScript
gulp.task('bundle-js', function () {
return gulp.src('resources/js/**/*')
.pipe(gulp.dest('webapp/js'));
});
// Default task to run all copy tasks
gulp.task('bundle-resources', gulp.parallel('bundle-images', 'bundle-css', 'bundle-js'));
const useTemplate = (template, contentPath, destPath, outFile, webRoot, extra) => (
() => {
const substancePath = Array.isArray(contentPath) ? contentPath : [[contentPath,{}]];
const substance = substancePath.map(([p,c]) => {
const header = path.basename(p, path.extname(p));
return `@@include('${p}', ${JSON.stringify({header ,...c})} )`
}).join('\n');
return gulp.src(template, {encoding: false})
.pipe(fileInclude({
prefix: "@@",
basepath: "./",
context: {
languageVersion: coreManifest.rekarel.language,
substance: substance,
webRoot: webRoot,
extra: extra ?? {}
}
}))
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true
}))
.pipe(rename(outFile))
.pipe(gulp.dest(destPath));
}
);
const bundleDocsTasks = () => {
const tasks = subDocs.flatMap(subDoc => {
return fs.readdirSync(subDoc)
.filter(file => file.endsWith(".html"))
.map(file => {
const path = subDoc + file;
const destPath = docsDist + "/" + subDoc.replace("html/docs/", "");
const template = "html/docs/prettyBase.template";
const out = file === 'index.html' ? file: `${file.replace(".html", "")}/index.html` ;
const count = (subDoc.match(/\//g) || []).length; // counts slashes safely
let root = ".";
for (let i = 1; i < count; i++) {
root += "/..";
}
const bundleOneDoc = useTemplate(template, path, destPath, out, root);
bundleOneDoc.displayName = `docs:${path}`;
return bundleOneDoc;
});
});
return gulp.parallel(...tasks);
};
const bundlePrintableDocsTasks = () => {
const tasks = subDocs.map(subDoc => {
const pageDataPath = path.join( subDoc,'.pageData.json')
if (!fs.existsSync(pageDataPath)) {
return;
}
const pageDataFile = fs.readFileSync(pageDataPath);
const pageData = JSON.parse(pageDataFile);
const paths = pageData.content.filter((c)=>c.showOnSingle).map(
(c, idx)=>[
subDoc+c.file,
{
title:`${c.title}`,
number: idx + 1
}
]
)
const destPath = printDocsDist + "/" + subDoc.replace("html/docs/", "");
const template = "html/docs/print.template";
const out = "index.html";
const bundleOneDoc = useTemplate(template, paths, destPath, out, '../..',
pageData
);
bundleOneDoc.displayName = `printable:${subDoc}`;
return bundleOneDoc;
;
});
return gulp.parallel(...(tasks.filter(task=>task)));
};
gulp.task("bundle-docs", bundleDocsTasks());
gulp.task("bundle-printable-docs", bundlePrintableDocsTasks());
gulp.task('clean-docs', ()=> {
return gulp.src(docsDist, {allowEmpty: true})
.pipe(clean());
})
gulp.task('process-jison-java', ()=> {
const path = 'js/kareljava.js'
return gulp.src(path)
.pipe(insert.append('\nfunction javaParser() {\n return kareljava.parse.apply(kareljava, arguments);\n}\nexport {kareljava, javaParser }\n'))
.pipe(replace("_token_stack:", '// _token_stack:'))
.pipe(replace("loc: yyloc,", 'loc: lexer.yylloc, // Implement fix: https://github.com/zaach/jison/pull/356'))
.pipe(gulp.dest('js/'));
})
gulp.task('process-jison-pascal', ()=> {
const path = 'js/karelpascal.js'
return gulp.src(path)
.pipe(insert.append('\nfunction pascalParser () {\n return karelpascal.parse.apply(karelpascal, arguments);\n}\nexport {karelpascal, pascalParser}'))
.pipe(replace("_token_stack:", '// _token_stack:'))
.pipe(replace("loc: yyloc,", 'loc: lexer.yylloc, // Implement fix: https://github.com/zaach/jison/pull/356'))
.pipe(gulp.dest('js/'));
})
gulp.task('process-jison-java2pascal', ()=> {
const path = 'js/java2pascal.js'
return gulp.src(path)
.pipe(insert.append('\nfunction java2pascalParser () {\n return java2pascal.parse.apply(java2pascal, arguments);\n}\nexport {java2pascal, java2pascalParser}'))
.pipe(replace("_token_stack:", '// _token_stack:'))
.pipe(replace("loc: yyloc,", 'loc: lexer.yylloc, // Implement fix: https://github.com/zaach/jison/pull/356'))
.pipe(gulp.dest('js/'));
})
gulp.task('process-jison-pascal2java', ()=> {
const path = 'js/pascal2java.js'
return gulp.src(path)
.pipe(insert.append('\nfunction pascal2javaParser () {\n return pascal2java.parse.apply(pascal2java, arguments);\n}\nexport {pascal2java, pascal2javaParser}'))
.pipe(replace("_token_stack:", '// _token_stack:'))
.pipe(replace("loc: yyloc,", 'loc: lexer.yylloc, // Implement fix: https://github.com/zaach/jison/pull/356'))
.pipe(gulp.dest('js/'));
})
const paths = {
html: [
'./webapp/index.html',
'./webapp/ayuda.html',
'./webapp/ayuda-java.html',
'./webapp/ayuda-pascal.html',
],
js: [
'./webapp/js/cindex.js',
],
img: './webapp/img/*',
css: './webapp/css/*',
license: './LICENSE',
build: './build'
};
gulp.task('clean-webapp', ()=> {
return gulp.src('./webapp', {allowEmpty: true, read: false})
.pipe(clean());
})
gulp.task('clean', ()=> {
return gulp.src(paths.build, {allowEmpty: true, read: false})
.pipe(clean());
});
gulp.task('copy-html', () => {
return gulp.src(paths.html)
.pipe(gulp.dest(paths.build));
});
gulp.task('copy-js', () => {
return gulp.src(paths.js)
.pipe(gulp.dest(`${paths.build}/js`));
});
gulp.task('copy-img', () => {
return gulp.src(paths.img)
.pipe(gulp.dest(`${paths.build}/img`));
});
gulp.task('copy-css', () => {
return gulp.src(paths.css)
.pipe(gulp.dest(`${paths.build}/css`));
});
gulp.task('copy-license', () => {
return gulp.src(paths.license)
.pipe(gulp.dest(`${paths.build}`));
});
gulp.task('default', gulp.series('bundle-html', 'bundle-docs', 'bundle-printable-docs', 'bundle-resources'));
gulp.task('build', gulp.series('clean', 'copy-html', 'copy-js', 'copy-img', 'copy-css','copy-license'));
gulp.task('buildsource', (done) => {
exec('npm run build:source', (err, stdout, stderr) => {
if (stdout) process.stdout.write(stdout);
if (stderr) process.stderr.write(stderr);
done(err);
});
});
gulp.task('watch', () => {
gulp.watch(['html/**/*', 'resources/**/*'], gulp.series('default'));
gulp.watch(['src/**/*'], gulp.series('buildsource'));
});