forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.plugins.js
171 lines (138 loc) · 4.77 KB
/
gulpfile.plugins.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// Increase max listeners for event emitters
require('events').EventEmitter.defaultMaxListeners = 100;
var gulp = require('gulp');
var path = require('path');
var tsb = require('gulp-tsb');
var es = require('event-stream');
var cp = require('child_process');
var filter = require('gulp-filter');
var rename = require('gulp-rename');
var rimraf = require('rimraf');
var util = require('./build/lib/util');
var watcher = require('./build/lib/watch');
var createReporter = require('./build/lib/reporter');
var glob = require('glob');
var fs = require('fs');
var JSONC = require('json-comments');
function getTSConfig(plugin) {
var script = (plugin.desc && plugin.desc.scripts && plugin.desc.scripts['vscode:prepublish']) || '';
var match = /^node \.\.\/\.\.\/node\_modules\/gulp\/bin\/gulp\.js \-\-gulpfile \.\.\/\.\.\/gulpfile\.plugins\.js compile-plugin:([^ ]+) ?(.*tsconfig\.json)?/.exec(script);
if (!match) {
return;
}
var pluginRoot = path.join(__dirname, 'extensions', plugin.desc.name);
if (match[2]) {
return path.join(pluginRoot, match[2]);
}
return {
noLib: true,
target: 'ES5',
module: 'amd',
declaration: false,
sourceMap: true,
rootDir: path.join(pluginRoot, 'src'),
sourceRoot: util.toFileUri(path.join(pluginRoot, 'src'))
};
}
function noop() {}
function readAllPlugins() {
var PLUGINS_FOLDER = path.join(__dirname, 'extensions');
var extensions = glob.sync('*/package.json', {
cwd: PLUGINS_FOLDER
});
var result = [];
extensions.forEach(function (relativeJSONPath) {
var relativePath = path.dirname(relativeJSONPath);
var fullJSONPath = path.join(PLUGINS_FOLDER, relativeJSONPath);
var contents = fs.readFileSync(fullJSONPath).toString();
var desc = JSONC.parse(contents);
result.push({
relativePath: relativePath,
desc: desc
});
});
return result;
}
var tasks = readAllPlugins()
.map(function (plugin) {
var name = plugin.desc.name;
var pluginRoot = path.join(__dirname, 'extensions', name);
var clean = 'clean-plugin:' + name;
var compile = 'compile-plugin:' + name;
var watch = 'watch-plugin:' + name;
var npmInstall = 'npm-install-plugin:' + name;
var hasnpmTask = (plugin.desc.dependencies && Object.keys(plugin.desc.dependencies).length > 0);
if (hasnpmTask) {
gulp.task(npmInstall, function (cb) {
cp.exec('npm install', { cwd: pluginRoot }, cb);
});
}
var options = getTSConfig(plugin);
if (options) {
var sources = 'extensions/' + name + '/src/**';
var deps = [
'src/vs/vscode.d.ts',
'src/typings/mocha.d.ts',
'extensions/declares.d.ts',
'extensions/node.d.ts',
'extensions/lib.core.d.ts'
];
var pipeline = (function () {
var reporter = createReporter();
var compilation = tsb.create(options, true, null, function (err) { reporter(err.toString()); });
return function () {
var input = es.through();
var tsFilter = filter(['**/*.ts', '!**/lib/lib*.d.ts'], { restore: true });
var output = input
.pipe(tsFilter)
.pipe(compilation())
.pipe(tsFilter.restore)
.pipe(reporter());
return es.duplex(input, output);
};
})();
var sourcesRoot = path.join(pluginRoot, 'src');
var sourcesOpts = { cwd: __dirname, base: sourcesRoot };
var depsOpts = { cwd: __dirname };
gulp.task(clean, function (cb) {
rimraf(path.join(pluginRoot, 'out'), cb);
});
gulp.task(compile, hasnpmTask ? [clean, npmInstall] : [clean], function () {
var src = es.merge(gulp.src(sources, sourcesOpts), gulp.src(deps, depsOpts));
return src
.pipe(pipeline())
.pipe(gulp.dest('extensions/' + name + '/out'));
});
gulp.task(watch, [clean], function () {
var src = es.merge(gulp.src(sources, sourcesOpts), gulp.src(deps, depsOpts));
var watchSrc = es.merge(watcher(sources, sourcesOpts), watcher(deps, depsOpts));
return watchSrc
.pipe(util.incremental(pipeline, src))
.pipe(gulp.dest('extensions/' + name + '/out'));
});
} else {
if (hasnpmTask) {
gulp.task(clean, noop);
gulp.task(compile, [npmInstall], noop);
gulp.task(watch, noop);
} else {
return null;
}
}
return {
clean: clean,
compile: compile,
watch: watch
};
});
// remove null tasks
tasks = tasks.filter(function(task) {
return !!task;
})
gulp.task('clean-plugins', tasks.map(function (t) { return t.clean; }));
gulp.task('compile-plugins', tasks.map(function (t) { return t.compile; }));
gulp.task('watch-plugins', tasks.map(function (t) { return t.watch; }));