UNPKG

8.01 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 });
7const vscode_languageserver_types_1 = require("vscode-languageserver-types");
8const arg_1 = require("./instructions/arg");
9const cmd_1 = require("./instructions/cmd");
10const copy_1 = require("./instructions/copy");
11const env_1 = require("./instructions/env");
12const entrypoint_1 = require("./instructions/entrypoint");
13const from_1 = require("./instructions/from");
14const healthcheck_1 = require("./instructions/healthcheck");
15const onbuild_1 = require("./instructions/onbuild");
16const util_1 = require("./util");
17const workdir_1 = require("./instructions/workdir");
18class 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 * Gets all the ARG instructions that are defined in this image.
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 * Gets all the CMD instructions that are defined in this image.
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 * Gets all the COPY instructions that are defined in this image.
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 * Gets all the ENTRYPOINT instructions that are defined in this image.
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 * Gets all the ENV instructions that are defined in this image.
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 * Gets all the FROM instructions that are defined in this image.
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 * Gets all the HEALTHCHECK instructions that are defined in this image.
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 * Resolves a variable with the given name at the specified line
183 * to its value. If null is returned, then the variable has been
184 * defined but no value was given. If undefined is returned, then
185 * a variable with the given name has not been defined yet as of
186 * the given line.
187 *
188 * @param variable the name of the variable to resolve
189 * @param line the line number that the variable is on, zero-based
190 * @return the value of the variable as defined by an ARG or ENV
191 * instruction, or null if no value has been specified, or
192 * undefined if a variable with the given name has not
193 * been defined
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 // all templates should have instructions, this only happens for
221 // the initial set of instruction
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}
236exports.ImageTemplate = ImageTemplate;