1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.ImageTemplate = void 0;
|
8 | const vscode_languageserver_types_1 = require("vscode-languageserver-types");
|
9 | const arg_1 = require("./instructions/arg");
|
10 | const cmd_1 = require("./instructions/cmd");
|
11 | const copy_1 = require("./instructions/copy");
|
12 | const env_1 = require("./instructions/env");
|
13 | const entrypoint_1 = require("./instructions/entrypoint");
|
14 | const from_1 = require("./instructions/from");
|
15 | const healthcheck_1 = require("./instructions/healthcheck");
|
16 | const onbuild_1 = require("./instructions/onbuild");
|
17 | const util_1 = require("./util");
|
18 | const workdir_1 = require("./instructions/workdir");
|
19 | class ImageTemplate {
|
20 | constructor() {
|
21 | this.comments = [];
|
22 | this.instructions = [];
|
23 | }
|
24 | addComment(comment) {
|
25 | this.comments.push(comment);
|
26 | }
|
27 | getComments() {
|
28 | return this.comments;
|
29 | }
|
30 | addInstruction(instruction) {
|
31 | this.instructions.push(instruction);
|
32 | }
|
33 | getInstructions() {
|
34 | return this.instructions;
|
35 | }
|
36 | getInstructionAt(line) {
|
37 | for (let instruction of this.instructions) {
|
38 | if (util_1.Util.isInsideRange(vscode_languageserver_types_1.Position.create(line, 0), instruction.getRange())) {
|
39 | return instruction;
|
40 | }
|
41 | }
|
42 | return null;
|
43 | }
|
44 | |
45 |
|
46 |
|
47 | getARGs() {
|
48 | let args = [];
|
49 | for (let instruction of this.instructions) {
|
50 | if (instruction instanceof arg_1.Arg) {
|
51 | args.push(instruction);
|
52 | }
|
53 | }
|
54 | return args;
|
55 | }
|
56 | |
57 |
|
58 |
|
59 | getCMDs() {
|
60 | let cmds = [];
|
61 | for (let instruction of this.instructions) {
|
62 | if (instruction instanceof cmd_1.Cmd) {
|
63 | cmds.push(instruction);
|
64 | }
|
65 | }
|
66 | return cmds;
|
67 | }
|
68 | |
69 |
|
70 |
|
71 | getCOPYs() {
|
72 | let copies = [];
|
73 | for (let instruction of this.instructions) {
|
74 | if (instruction instanceof copy_1.Copy) {
|
75 | copies.push(instruction);
|
76 | }
|
77 | }
|
78 | return copies;
|
79 | }
|
80 | |
81 |
|
82 |
|
83 | getENTRYPOINTs() {
|
84 | let froms = [];
|
85 | for (let instruction of this.instructions) {
|
86 | if (instruction instanceof entrypoint_1.Entrypoint) {
|
87 | froms.push(instruction);
|
88 | }
|
89 | }
|
90 | return froms;
|
91 | }
|
92 | |
93 |
|
94 |
|
95 | getENVs() {
|
96 | let args = [];
|
97 | for (let instruction of this.instructions) {
|
98 | if (instruction instanceof env_1.Env) {
|
99 | args.push(instruction);
|
100 | }
|
101 | }
|
102 | return args;
|
103 | }
|
104 | getFROM() {
|
105 | for (const instruction of this.instructions) {
|
106 | if (instruction instanceof from_1.From) {
|
107 | return instruction;
|
108 | }
|
109 | }
|
110 | return null;
|
111 | }
|
112 | |
113 |
|
114 |
|
115 | getFROMs() {
|
116 | let froms = [];
|
117 | for (let instruction of this.instructions) {
|
118 | if (instruction instanceof from_1.From) {
|
119 | froms.push(instruction);
|
120 | }
|
121 | }
|
122 | return froms;
|
123 | }
|
124 | |
125 |
|
126 |
|
127 | getHEALTHCHECKs() {
|
128 | let froms = [];
|
129 | for (let instruction of this.instructions) {
|
130 | if (instruction instanceof healthcheck_1.Healthcheck) {
|
131 | froms.push(instruction);
|
132 | }
|
133 | }
|
134 | return froms;
|
135 | }
|
136 | getWORKDIRs() {
|
137 | const workdirs = [];
|
138 | for (const instruction of this.instructions) {
|
139 | if (instruction instanceof workdir_1.Workdir) {
|
140 | workdirs.push(instruction);
|
141 | }
|
142 | }
|
143 | return workdirs;
|
144 | }
|
145 | getOnbuildTriggers() {
|
146 | let triggers = [];
|
147 | for (let instruction of this.instructions) {
|
148 | if (instruction instanceof onbuild_1.Onbuild) {
|
149 | let trigger = instruction.getTriggerInstruction();
|
150 | if (trigger) {
|
151 | triggers.push(trigger);
|
152 | }
|
153 | }
|
154 | }
|
155 | return triggers;
|
156 | }
|
157 | getAvailableVariables(currentLine) {
|
158 | const variables = [];
|
159 | for (const arg of this.getARGs()) {
|
160 | if (arg.isBefore(currentLine)) {
|
161 | const property = arg.getProperty();
|
162 | if (property) {
|
163 | const variable = property.getName();
|
164 | if (variables.indexOf(variable) === -1) {
|
165 | variables.push(variable);
|
166 | }
|
167 | }
|
168 | }
|
169 | }
|
170 | for (const env of this.getENVs()) {
|
171 | if (env.isBefore(currentLine)) {
|
172 | for (const property of env.getProperties()) {
|
173 | const variable = property.getName();
|
174 | if (variables.indexOf(variable) === -1) {
|
175 | variables.push(variable);
|
176 | }
|
177 | }
|
178 | }
|
179 | }
|
180 | return variables;
|
181 | }
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | resolveVariable(variable, line) {
|
197 | let envs = this.getENVs();
|
198 | for (let i = envs.length - 1; i >= 0; i--) {
|
199 | if (envs[i].isBefore(line)) {
|
200 | for (let property of envs[i].getProperties()) {
|
201 | if (property.getName() === variable) {
|
202 | return property.getValue();
|
203 | }
|
204 | }
|
205 | }
|
206 | }
|
207 | let args = this.getARGs();
|
208 | for (let i = args.length - 1; i >= 0; i--) {
|
209 | if (args[i].isBefore(line)) {
|
210 | let property = args[i].getProperty();
|
211 | if (property && property.getName() === variable) {
|
212 | return property.getValue();
|
213 | }
|
214 | }
|
215 | }
|
216 | return undefined;
|
217 | }
|
218 | getRange() {
|
219 | const instructions = this.getInstructions();
|
220 | if (instructions.length === 0) {
|
221 |
|
222 |
|
223 | return vscode_languageserver_types_1.Range.create(0, 0, 0, 0);
|
224 | }
|
225 | const instructionStart = instructions[0].getRange().start;
|
226 | const instructionEnd = instructions[instructions.length - 1].getRange().end;
|
227 | return vscode_languageserver_types_1.Range.create(instructionStart, instructionEnd);
|
228 | }
|
229 | contains(position) {
|
230 | const range = this.getRange();
|
231 | if (range === null) {
|
232 | return false;
|
233 | }
|
234 | return util_1.Util.isInsideRange(position, range);
|
235 | }
|
236 | }
|
237 | exports.ImageTemplate = ImageTemplate;
|