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