Skip to content

Commit 77b61a1

Browse files
committed
Simplify gulp tasks for extensions
1 parent a592b43 commit 77b61a1

4 files changed

Lines changed: 81 additions & 32 deletions

File tree

build/gulpfile.extensions.js

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ const tasks = compilations.map(function (tsconfigFile) {
4747
const clean = 'clean-extension:' + name;
4848
const compile = 'compile-extension:' + name;
4949
const watch = 'watch-extension:' + name;
50-
51-
// Build Tasks
52-
const cleanBuild = 'clean-extension-build:' + name;
5350
const compileBuild = 'compile-extension-build:' + name;
54-
const watchBuild = 'watch-extension-build:' + name;
5551

5652
const root = path.join('extensions', relativeDirname);
5753
const srcBase = path.join(root, 'src');
@@ -112,15 +108,18 @@ const tasks = compilations.map(function (tsconfigFile) {
112108
const srcOpts = { cwd: path.dirname(__dirname), base: srcBase };
113109

114110
gulp.task(clean, cb => rimraf(out, cb));
111+
const cleanTask = () => util.primraf(out);
115112

116-
gulp.task(compile, [clean], () => {
113+
gulp.task(compile, util.taskSeries(cleanTask, () => {
117114
const pipeline = createPipeline(false, true);
118115
const input = gulp.src(src, srcOpts);
119116

120-
return input
117+
return util.streamToPromise(
118+
input
121119
.pipe(pipeline())
122-
.pipe(gulp.dest(out));
123-
});
120+
.pipe(gulp.dest(out))
121+
);
122+
}));
124123

125124
gulp.task(watch, [clean], () => {
126125
const pipeline = createPipeline(false);
@@ -132,39 +131,24 @@ const tasks = compilations.map(function (tsconfigFile) {
132131
.pipe(gulp.dest(out));
133132
});
134133

135-
gulp.task(cleanBuild, cb => rimraf(out, cb));
136-
137-
gulp.task(compileBuild, [clean], () => {
134+
gulp.task(compileBuild, util.taskSeries(cleanTask, () => {
138135
const pipeline = createPipeline(true, true);
139136
const input = gulp.src(src, srcOpts);
140137

141-
return input
138+
return util.streamToPromise(
139+
input
142140
.pipe(pipeline())
143-
.pipe(gulp.dest(out));
144-
});
145-
146-
gulp.task(watchBuild, [clean], () => {
147-
const pipeline = createPipeline(true);
148-
const input = gulp.src(src, srcOpts);
149-
const watchInput = watcher(src, srcOpts);
150-
151-
return watchInput
152-
.pipe(util.incremental(() => pipeline(), input))
153-
.pipe(gulp.dest(out));
154-
});
141+
.pipe(gulp.dest(out))
142+
);
143+
}));
155144

156145
return {
157-
clean: clean,
158146
compile: compile,
159147
watch: watch,
160-
cleanBuild: cleanBuild,
161-
compileBuild: compileBuild,
162-
watchBuild: watchBuild
148+
compileBuild: compileBuild
163149
};
164150
});
165151

166152
gulp.task('compile-extensions', tasks.map(t => t.compile));
167153
gulp.task('watch-extensions', tasks.map(t => t.watch));
168-
169154
gulp.task('compile-extensions-build', tasks.map(t => t.compileBuild));
170-
gulp.task('watch-extensions-build', tasks.map(t => t.watchBuild));

build/lib/util.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,39 @@ function rimraf(dir) {
183183
return cb => retry(cb);
184184
}
185185
exports.rimraf = rimraf;
186+
/**
187+
* Like rimraf (with 5 retries), but with a promise instead of a callback.
188+
*/
189+
function primraf(dir) {
190+
const fn = rimraf(dir);
191+
return new Promise((resolve, reject) => {
192+
fn((err) => {
193+
if (err) {
194+
return reject(err);
195+
}
196+
resolve();
197+
});
198+
});
199+
}
200+
exports.primraf = primraf;
201+
/**
202+
* Convert a stream to a promise.
203+
*/
204+
function streamToPromise(stream) {
205+
return new Promise((resolve, reject) => {
206+
stream.on('end', _ => resolve());
207+
stream.on('error', err => reject(err));
208+
});
209+
}
210+
exports.streamToPromise = streamToPromise;
211+
function taskSeries(...tasks) {
212+
return async () => {
213+
for (let i = 0; i < tasks.length; i++) {
214+
await tasks[i]();
215+
}
216+
};
217+
}
218+
exports.taskSeries = taskSeries;
186219
function getVersion(root) {
187220
let version = process.env['BUILD_SOURCEVERSION'];
188221
if (!version || !/^[0-9a-f]{40}$/i.test(version)) {

build/lib/util.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,40 @@ export function rimraf(dir: string): (cb: any) => void {
237237
return cb => retry(cb);
238238
}
239239

240+
/**
241+
* Like rimraf (with 5 retries), but with a promise instead of a callback.
242+
*/
243+
export function primraf(dir: string): Promise<void> {
244+
const fn = rimraf(dir);
245+
return new Promise((resolve, reject) => {
246+
fn((err: any) => {
247+
if (err) {
248+
return reject(err);
249+
}
250+
resolve();
251+
});
252+
});
253+
}
254+
255+
/**
256+
* Convert a stream to a promise.
257+
*/
258+
export function streamToPromise(stream: NodeJS.ReadWriteStream): Promise<void> {
259+
return new Promise((resolve, reject) => {
260+
stream.on('end', _ => resolve());
261+
stream.on('error', err => reject(err));
262+
});
263+
}
264+
265+
export type PromiseTask = () => Promise<void>;
266+
export function taskSeries(...tasks: PromiseTask[]): () => Promise<void> {
267+
return async () => {
268+
for (let i = 0; i < tasks.length; i++) {
269+
await tasks[i]();
270+
}
271+
};
272+
}
273+
240274
export function getVersion(root: string): string | undefined {
241275
let version = process.env['BUILD_SOURCEVERSION'];
242276

gulpfile.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ gulp.task('watch-client', ['clean-client'], compilation.watchTask('out', false))
2121
// Full compile, including nls and inline sources in sourcemaps, for build
2222
gulp.task('clean-client-build', util.rimraf('out-build'));
2323
gulp.task('compile-client-build', ['clean-client-build'], compilation.compileTask('src', 'out-build', true));
24-
gulp.task('watch-client-build', ['clean-client-build'], compilation.watchTask('out-build', true));
2524

2625
// Default
2726
gulp.task('default', ['compile']);
@@ -32,7 +31,6 @@ gulp.task('watch', [/* 'monaco-typecheck-watch', */ 'watch-client', 'watch-exten
3231

3332
// All Build
3433
gulp.task('compile-build', ['compile-client-build', 'compile-extensions-build']);
35-
gulp.task('watch-build', ['watch-client-build', 'watch-extensions-build']);
3634

3735
process.on('unhandledRejection', (reason, p) => {
3836
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);

0 commit comments

Comments
 (0)