-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage.ts
67 lines (53 loc) · 1.73 KB
/
stage.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
import { From } from './commands/from';
import { DockerfileBuildContext } from './dockerfile';
import { Arg } from './commands/arg';
export interface StageBuildContext extends DockerfileBuildContext {
stage: Stage;
}
export interface IDockerCommand {
toDockerCommand(context : StageBuildContext): string;
}
export interface Fromable {
getFromName(): string;
}
let stageUniqueId = 0;
export class Stage implements Fromable {
protected _from?: From;
protected _commands : IDockerCommand[] = [];
constructor(protected name: string = 'S' + (++stageUniqueId).toString(16).padStart(8, '0')) {
}
getName() {
return this.name;
}
getFromName() {
return this.name;
}
from() : From|undefined;
from(from : From|Fromable) : this;
from(from?: From|Fromable) : this | From | undefined {
if(from) {
this._from = from instanceof From ? from : new From(from);
return this;
}
return this._from;
}
commands() : IDockerCommand[];
commands(cmd: IDockerCommand, ...commands : IDockerCommand[]) : this;
commands(cmd?: IDockerCommand, ...commands : IDockerCommand[]) : this | IDockerCommand[] {
if(cmd) {
this._commands.push(cmd, ...commands);
return this;
}
return this._commands;
}
toString(context : DockerfileBuildContext) {
const buildContext : StageBuildContext = {
...context,
stage: this
}
return [ this.from()!.toDockerCommand(buildContext), '', ...this._commands.map(cmd => cmd.toDockerCommand(buildContext) ) ].join('\n');
}
}
export function stage(...args : ConstructorParameters<typeof Stage>) {
return new Stage(...args);
}