This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
pipeline.ts
359 lines (322 loc) · 13.2 KB
/
pipeline.ts
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/// <reference types="node" />
// The MIT License (MIT)
//
// vs-deploy (https://github.com/mkloubert/vs-deploy)
// Copyright (c) Marcel Joachim Kloubert <marcel.kloubert@gmx.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
import * as deploy_contracts from '../contracts';
import * as deploy_helpers from '../helpers';
import * as deploy_objects from '../objects';
import * as deploy_workspace from '../workspace';
import * as i18 from '../i18';
import * as Path from 'path';
import * as vscode from 'vscode';
/**
* A deploy target configuration for a "pipeline".
*/
export interface DeployTargetPipeline extends deploy_contracts.DeployTarget {
/**
* A state value for the ALL scripts that exists while the
* current session.
*/
globalState?: Object;
/**
* The optional data to use in execution of script functions.
*/
options?: any;
/**
* The script to execute.
*/
script: string;
/**
* A state value for the current script that exists while the
* current session.
*/
state?: any;
/**
* The target.
*/
target: string;
}
/**
* Arguments for the 'pipe()' function of a "pipeline" module.
*/
export interface PipeArguments extends deploy_contracts.ScriptArguments {
/**
* The (new) root directory to use.
*/
baseDirectory?: string;
/**
* Indicates if operation has been canceled or not.
*/
canceled?: boolean;
/**
* The underlying deploy context.
*/
context: deploy_contracts.DeployContext;
/**
* Deploy options.
*/
deployOptions: deploy_contracts.DeployFileOptions;
/**
* The (new) list of files to deploy.
*/
files?: string[];
/**
* Opens a HTML document in a new tab.
*
* @param {string} html The HTML document (source code).
* @param {string} [title] The custom title for the tab.
* @param {any} [id] The custom ID for the document in the storage.
*
* @returns {Promise<any>} The promise.
*/
openHtml: (html: string, title?: string, id?: any) => Promise<any>;
/**
* Handles a value as string and replaces placeholders.
*
* @param {any} val The value to parse.
*
* @return {string} The parsed value.
*/
replaceWithValues: (val: any) => string;
/**
* The underlying "parent" object.
*/
sender: any;
/**
* The target.
*/
target: DeployTargetPipeline;
/**
* Options from the target configuration.
*/
targetOptions: any;
}
interface PipeContext extends deploy_objects.MultiTargetContext {
}
/**
* A pipeline module.
*/
export interface PipelineModule {
/**
* An optional function that is called after (new) piped files
* has been processed by target(s), e.g. to do cleanup operations.
*
* @param {PipeArguments} args The arguments.
* @param {any} [err] The error from the target, if occurred.
*
* @return {void|Promise<PipeArguments>} The promise.
*/
onPipeCompleted?: (args: PipeArguments, err?: any) => void | Promise<PipeArguments>;
/**
* Pipes a list of source files.
*
* @param {PipeArguments} args The arguments.
*
* @return {void|Promise<PipeArguments>} The result.
*/
pipe: (args: PipeArguments) => void | Promise<PipeArguments>;
}
class PipelinePlugin extends deploy_objects.MultiTargetDeployPluginBase {
protected _globalState: Object = {};
protected _scriptStates: Object = {};
protected createContext(target: DeployTargetPipeline): PipeContext {
return {
hasCancelled: false,
targets: this.getTargetsWithPlugins(target, target.target),
};
}
public deployWorkspace(files: string[], target: DeployTargetPipeline, opts?: deploy_contracts.DeployWorkspaceOptions) {
if (!opts) {
opts = {};
}
let me = this;
let ctx = this.createContext(target);
let completed = (err?: any) => {
if (opts.onCompleted) {
opts.onCompleted(me, {
canceled: ctx.hasCancelled,
target: target,
});
}
};
me.onCancelling(() => ctx.hasCancelled = true, opts);
if (ctx.hasCancelled) {
completed(); // cancellation requested
}
else {
try {
let scriptFile = getScriptFile(target, me);
let relativeScriptPath = deploy_helpers.toRelativePath(scriptFile, opts.baseDirectory);
if (false === relativeScriptPath) {
relativeScriptPath = scriptFile;
}
let scriptModule = loadScriptModule(scriptFile);
if (!scriptModule.pipe) {
throw new Error(i18.t('plugins.pipeline.noPipeFunction', relativeScriptPath));
}
let allStates = me._scriptStates;
let args: PipeArguments = {
baseDirectory: opts.baseDirectory,
canceled: me.context.isCancelling(),
context: me.context,
deployOptions: opts,
emitGlobal: function() {
return me.context
.emitGlobal
.apply(me.context, arguments);
},
files: files,
globals: me.context.globals(),
openHtml: function() {
return me.context.openHtml
.apply(me.context, arguments);
},
replaceWithValues: (v) => me.context.replaceWithValues(v),
require: function(id) {
return me.context.require(id);
},
sender: me,
target: target,
targetOptions: target.options,
};
// args.globalState
Object.defineProperty(args, 'globalState', {
enumerable: true,
get: () => {
return me._globalState;
},
});
// args.state
Object.defineProperty(args, 'state', {
enumerable: true,
get: () => {
return allStates[scriptFile];
},
set: (v) => {
allStates[scriptFile] = v;
},
});
args.context.once('deploy.cancel', function() {
if (false === args.canceled) {
args.canceled = true;
}
});
Promise.resolve(<any>scriptModule.pipe(args)).then((a) => {
if (ctx.hasCancelled) {
completed();
return;
}
a = a || args;
let newFileList = deploy_helpers.asArray(a.files)
.filter(x => !deploy_helpers.isEmptyString(x));
super.deployWorkspace(newFileList,
target,
{
baseDirectory: a.baseDirectory,
context: opts.context,
onBeforeDeployFile: (sender, e) => {
if (opts.onBeforeDeployFile) {
opts.onBeforeDeployFile(sender, {
destination: e.destination,
file: e.file,
target: e.target,
});
}
},
onCompleted: (sender, e) => {
let pipeCompleted = () => {
ctx.hasCancelled = e.canceled;
completed(e.error);
};
try {
if (scriptModule.onPipeCompleted) {
Promise.resolve(<any>scriptModule.onPipeCompleted(a, e.error)).then(() => {
pipeCompleted();
}).catch((err) => {
me.context.log(i18.t('errors.withCategory', 'PipelinePlugin.deployWorkspace(2)', err));
pipeCompleted();
});
}
else {
pipeCompleted();
}
}
catch (ex) {
me.context.log(i18.t('errors.withCategory', 'PipelinePlugin.deployWorkspace(1)', ex));
pipeCompleted();
}
},
onFileCompleted: (sender, e) => {
if (opts.onFileCompleted) {
opts.onFileCompleted(sender, {
error: e.error,
file: e.file,
target: e.target,
});
}
}
});
}).catch((err) => {
completed(err);
});
}
catch (e) {
completed(e);
}
}
}
public info(): deploy_contracts.DeployPluginInfo {
return {
description: i18.t('plugins.pipeline.description'),
};
}
protected onConfigReloaded(cfg: deploy_contracts.DeployConfiguration) {
this._globalState = {};
this._scriptStates = {};
}
}
/**
* Creates a new Plugin.
*
* @param {deploy_contracts.DeployContext} ctx The deploy context.
*
* @returns {deploy_contracts.DeployPlugin} The new instance.
*/
export function createPlugin(ctx: deploy_contracts.DeployContext): deploy_contracts.DeployPlugin {
return new PipelinePlugin(ctx);
}
function getScriptFile(target: DeployTargetPipeline, plugin: PipelinePlugin): string {
let scriptFile = deploy_helpers.toStringSafe(target.script);
scriptFile = plugin.context.replaceWithValues(scriptFile);
if (!scriptFile) {
scriptFile = './pipeline.js';
}
if (!Path.isAbsolute(scriptFile)) {
scriptFile = Path.join(deploy_workspace.getRootPath(), scriptFile);
}
return scriptFile;
}
function loadScriptModule(scriptFile: string): PipelineModule {
scriptFile = Path.resolve(scriptFile);
delete require.cache[scriptFile];
return require(scriptFile);
}