UNPKG

8.04 kBJavaScriptView Raw
1/* --------------------------------------------------------------------------------------------
2 * Copyright (c) Remy Suen. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 * ------------------------------------------------------------------------------------------ */
5'use strict';
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.ImageTemplate = void 0;
8const vscode_languageserver_types_1 = require("vscode-languageserver-types");
9const arg_1 = require("./instructions/arg");
10const cmd_1 = require("./instructions/cmd");
11const copy_1 = require("./instructions/copy");
12const env_1 = require("./instructions/env");
13const entrypoint_1 = require("./instructions/entrypoint");
14const from_1 = require("./instructions/from");
15const healthcheck_1 = require("./instructions/healthcheck");
16const onbuild_1 = require("./instructions/onbuild");
17const util_1 = require("./util");
18const workdir_1 = require("./instructions/workdir");
19class 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 * Gets all the ARG instructions that are defined in this image.
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 * Gets all the CMD instructions that are defined in this image.
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 * Gets all the COPY instructions that are defined in this image.
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 * Gets all the ENTRYPOINT instructions that are defined in this image.
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 * Gets all the ENV instructions that are defined in this image.
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 * Gets all the FROM instructions that are defined in this image.
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 * Gets all the HEALTHCHECK instructions that are defined in this image.
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 * Resolves a variable with the given name at the specified line
184 * to its value. If null is returned, then the variable has been
185 * defined but no value was given. If undefined is returned, then
186 * a variable with the given name has not been defined yet as of
187 * the given line.
188 *
189 * @param variable the name of the variable to resolve
190 * @param line the line number that the variable is on, zero-based
191 * @return the value of the variable as defined by an ARG or ENV
192 * instruction, or null if no value has been specified, or
193 * undefined if a variable with the given name has not
194 * been defined
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 // all templates should have instructions, this only happens for
222 // the initial set of instruction
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}
237exports.ImageTemplate = ImageTemplate;