-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
executable file
·282 lines (243 loc) · 6.78 KB
/
Copy pathgulpfile.js
File metadata and controls
executable file
·282 lines (243 loc) · 6.78 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
const gulp = require('gulp');
const del = require('del');
const fs = require('fs');
const path = require('path');
const through = require('through2');
const gulpZip = require('gulp-zip');
const minifyLua = require('./tools/luamin');
const compileMoonscript = require('./tools/moonscript');
const optimizeLua = require ('./tools/optimizations');
const discourageLuaMod = require ('./tools/discourageLuaModification');
const MATERIAL_GLOBS = [
'src/materials/**/*',
'!src/materials/**/*.svg',
];
const SOUND_GLOBS = [
'src/sound/**/*',
];
const MODEL_GLOBS = [
'src/models/**/*',
];
const STATIC_DATA_GLOBS = [
'src/data_static/**/*',
];
const SHADER_GLOBS = [
'src/shaders/fxc/*.vcs',
];
const METADATA_GLOBS = [
'src/addon.json',
];
const E2_EXTENSION_GLOBS = [
'src/lua/entities/gmod_wire_expression2/core/custom/**/*.lua',
];
let atomicWriteId = 0;
/**
* Write each Vinyl file through a same-directory temporary file and rename it
* into place. The rename is atomic on the target filesystem, so live-reload
* consumers never observe a truncated output file.
*/
function atomicDest(directory) {
const root = path.resolve(directory);
return through.obj(function(file, encoding, callback) {
if(file.isNull()) {
callback(null, file);
return;
}
if(!file.isBuffer()) {
callback(new Error('Cannot atomically write streamed file: ' + file.path));
return;
}
const target = path.join(root, file.relative);
const temporary = target + '.tmp-' + process.pid + '-' + atomicWriteId++;
try {
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.writeFileSync(temporary, file.contents, {
mode: file.stat && file.stat.mode,
});
fs.renameSync(temporary, target);
callback(null, file);
} catch(error) {
try {
fs.unlinkSync(temporary);
} catch(_) {
// Preserve the original write error.
}
callback(error);
}
});
}
/**
* Cleans the build.
*/
function clean() {
return del(['dest/**/*']);
}
clean.description = "Cleans the build.";
/**
* Minifies lua files.
*/
function lua() {
return gulp.src([
'src/**/*.lua',
'!src/lua/entities/gmod_wire_expression2/core/custom/**/*.lua',
], { since: gulp.lastRun(lua) })
.pipe(optimizeLua())
.pipe(minifyLua())
.pipe(atomicDest('dest'));
}
lua.description = "Copies and minifies lua files.";
/**
* Copies WireMod E2 extension sources without transforming them.
*/
function e2Extensions() {
return gulp.src(E2_EXTENSION_GLOBS, { base: 'src', since: gulp.lastRun(e2Extensions) })
.pipe(atomicDest('dest'));
}
e2Extensions.description = "Copies WireMod E2 extensions without transforming them.";
/**
* Compiles moonscript files.
*/
function moon() {
return gulp.src('src/**/*.moon', { since: gulp.lastRun(moon) })
.pipe(compileMoonscript())
.pipe(optimizeLua())
.pipe(discourageLuaMod())
.pipe(minifyLua())
.pipe(atomicDest('dest'));
}
moon.description = "Compiles moonscript files.";
/**
* Builds the addon scripts.
*/
const scripts = gulp.parallel(lua, moon, e2Extensions);
scripts.description = "Builds the addon scripts.";
/**
* Watches lua files and compiles changes.
*/
function watchScripts() {
return gulp.watch(
['src/**/*.lua', 'src/**/*.moon']
, scripts
)
}
watchScripts.displayName = "watch-scripts";
watchScripts.description = "Watches lua and moon files and compiles changes.";
/**
* Copies materials.
*/
function materials() {
return gulp.src(MATERIAL_GLOBS, { since: gulp.lastRun(materials) })
.pipe(atomicDest('dest/materials'));
}
materials.description = "Copies materials.";
/**
* Copies sound files.
*/
function sound() {
return gulp.src(SOUND_GLOBS, { since: gulp.lastRun(sound) })
.pipe(atomicDest('dest/sound'));
}
sound.description = "Copies sound files.";
/**
* Copies model files.
*/
function model() {
return gulp.src(MODEL_GLOBS, { since: gulp.lastRun(model) })
.pipe(atomicDest('dest/models'));
}
model.description = "Copies model files.";
/**
* Copies immutable addon-shipped data. Garry's Mod mounts this through GAME;
* it is deliberately kept separate from the writable DATA realm.
*/
function staticData() {
return gulp.src(STATIC_DATA_GLOBS, { base: 'src', since: gulp.lastRun(staticData) })
.pipe(atomicDest('dest'));
}
staticData.description = "Copies immutable static data files.";
/**
* Copies precompiled Source shaders. Shader compilation is deliberately a
* developer task so ordinary builds and CI do not require Wine or DirectX.
*/
function shaders() {
return gulp.src(SHADER_GLOBS, { base: 'src', since: gulp.lastRun(shaders) })
.pipe(atomicDest('dest'));
}
shaders.description = "Copies precompiled Source shaders.";
/**
* Copies metadata files.
*/
function metadata() {
return gulp.src(METADATA_GLOBS, { since: gulp.lastRun(metadata) })
.pipe(atomicDest('dest'));
}
metadata.description = "Copies metadata files.";
/**
* Packages the compiled addon as a loose-file zip. The archive contains the
* addon root rather than a `dest/` wrapper and excludes generated tests and
* stale archives retained by the live-refresh build.
*/
function packageZip() {
return gulp.src([
'dest/**/*',
'!dest/test/**',
'!dest/**/*.zip',
], { base: 'dest', allowEmpty: false })
.pipe(gulpZip('moonpanel.zip'))
.pipe(atomicDest('artifacts'));
}
packageZip.description = "Packages the compiled addon as a zip.";
/**
* Generates and moves assets.
*/
const assets = gulp.parallel(materials, sound, model, staticData, shaders, metadata);
assets.description = "Generates and copies assets.";
/**
* Watches asset files and compiles/copies changes.
*/
function watchAssets() {
return gulp.watch(
[
...MATERIAL_GLOBS,
...SOUND_GLOBS,
...MODEL_GLOBS,
...STATIC_DATA_GLOBS,
...SHADER_GLOBS,
...METADATA_GLOBS,
]
, assets
)
}
watchAssets.displayName = "watch-assets";
watchAssets.description = "Watches assets.";
/**
* Builds everything.
*/
const build = gulp.parallel(scripts, assets);
build.description = "Builds everything.";
/**
* Builds the project without deleting the live output tree, and then watches
* files for changes. This is intentionally safe for Garry's Mod live refresh.
*/
const watch = gulp.series(build, gulp.parallel(watchAssets, watchScripts));
watch.description = "Builds in place and watches files for changes.";
/**
* Explicit destructive reset for recovering from stale or orphaned output.
*/
const rebuild = gulp.series(clean, build);
rebuild.description = "Deletes the output tree, then performs a clean build.";
exports.clean = clean;
exports.materials = materials;
exports.staticData = staticData;
exports.shaders = shaders;
exports.assets = assets;
exports.watchAssets = watchAssets;
exports.packageZip = packageZip;
exports.lua = lua;
exports.moon = moon;
exports.scripts = scripts;
exports.watchScripts = watchScripts;
exports.build = build;
exports.rebuild = rebuild;
exports.watch = watch;
exports.default = watch;