UNPKG

7.93 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.From = void 0;
4const vscode_languageserver_types_1 = require("vscode-languageserver-types");
5const modifiableInstruction_1 = require("../modifiableInstruction");
6class From extends modifiableInstruction_1.ModifiableInstruction {
7 constructor(document, range, dockerfile, escapeChar, instruction, instructionRange) {
8 super(document, range, dockerfile, escapeChar, instruction, instructionRange);
9 }
10 stopSearchingForFlags(argument) {
11 return argument.indexOf("--") === -1;
12 }
13 getImage() {
14 return this.getRangeContent(this.getImageRange());
15 }
16 /**
17 * Returns the name of the image that will be used as the base image.
18 *
19 * @return the base image's name, or null if unspecified
20 */
21 getImageName() {
22 return this.getRangeContent(this.getImageNameRange());
23 }
24 /**
25 * Returns the range that covers the name of the image used by
26 * this instruction.
27 *
28 * @return the range of the name of this instruction's argument,
29 * or null if no image has been specified
30 */
31 getImageNameRange() {
32 let range = this.getImageRange();
33 if (range) {
34 let registryRange = this.getRegistryRange();
35 if (registryRange) {
36 range.start = this.document.positionAt(this.document.offsetAt(registryRange.end) + 1);
37 }
38 let tagRange = this.getImageTagRange();
39 let digestRange = this.getImageDigestRange();
40 if (tagRange === null) {
41 if (digestRange !== null) {
42 range.end = this.document.positionAt(this.document.offsetAt(digestRange.start) - 1);
43 }
44 }
45 else {
46 range.end = this.document.positionAt(this.document.offsetAt(tagRange.start) - 1);
47 }
48 return range;
49 }
50 return null;
51 }
52 /**
53 * Returns the range that covers the image argument of this
54 * instruction. This includes the tag or digest of the image if
55 * it has been specified by the instruction.
56 *
57 * @return the range of the image argument, or null if no image
58 * has been specified
59 */
60 getImageRange() {
61 let args = this.getArguments();
62 return args.length !== 0 ? args[0].getRange() : null;
63 }
64 getImageTag() {
65 return this.getRangeContent(this.getImageTagRange());
66 }
67 /**
68 * Returns the range in the document that the tag of the base
69 * image encompasses.
70 *
71 * @return the base image's tag's range in the document, or null
72 * if no tag has been specified
73 */
74 getImageTagRange() {
75 const range = this.getImageRange();
76 if (range) {
77 if (this.getImageDigestRange() === null) {
78 let content = this.getRangeContent(range);
79 let index = this.lastIndexOf(this.document.offsetAt(range.start), content, ':');
80 // the colon might be for a private registry's port and not a tag
81 if (index > content.indexOf('/')) {
82 return vscode_languageserver_types_1.Range.create(range.start.line, range.start.character + index + 1, range.end.line, range.end.character);
83 }
84 }
85 }
86 return null;
87 }
88 getImageDigest() {
89 return this.getRangeContent(this.getImageDigestRange());
90 }
91 /**
92 * Returns the range in the document that the digest of the base
93 * image encompasses.
94 *
95 * @return the base image's digest's range in the document, or null
96 * if no digest has been specified
97 */
98 getImageDigestRange() {
99 let range = this.getImageRange();
100 if (range) {
101 let content = this.getRangeContent(range);
102 let index = this.lastIndexOf(this.document.offsetAt(range.start), content, '@');
103 if (index !== -1) {
104 return vscode_languageserver_types_1.Range.create(range.start.line, range.start.character + index + 1, range.end.line, range.end.character);
105 }
106 }
107 return null;
108 }
109 indexOf(documentOffset, content, searchString) {
110 let index = content.indexOf(searchString);
111 const variables = this.getVariables();
112 for (let i = 0; i < variables.length; i++) {
113 const position = documentOffset + index;
114 const variableRange = variables[i].getRange();
115 if (this.document.offsetAt(variableRange.start) < position && position < this.document.offsetAt(variableRange.end)) {
116 const offset = this.document.offsetAt(variableRange.end) - documentOffset;
117 const substring = content.substring(offset);
118 const subIndex = substring.indexOf(searchString);
119 if (subIndex === -1) {
120 return -1;
121 }
122 index = subIndex + offset;
123 i = -1;
124 continue;
125 }
126 }
127 return index;
128 }
129 lastIndexOf(documentOffset, content, searchString) {
130 let index = content.lastIndexOf(searchString);
131 const variables = this.getVariables();
132 for (let i = 0; i < variables.length; i++) {
133 const position = documentOffset + index;
134 const variableRange = variables[i].getRange();
135 if (this.document.offsetAt(variableRange.start) < position && position < this.document.offsetAt(variableRange.end)) {
136 index = content.substring(0, index).lastIndexOf(searchString);
137 if (index === -1) {
138 return -1;
139 }
140 i = -1;
141 continue;
142 }
143 }
144 return index;
145 }
146 getRegistry() {
147 return this.getRangeContent(this.getRegistryRange());
148 }
149 getRegistryRange() {
150 const range = this.getImageRange();
151 if (range) {
152 const tagRange = this.getImageTagRange();
153 const digestRange = this.getImageDigestRange();
154 if (tagRange === null) {
155 if (digestRange !== null) {
156 range.end = this.document.positionAt(this.document.offsetAt(digestRange.start) - 1);
157 }
158 }
159 else {
160 range.end = this.document.positionAt(this.document.offsetAt(tagRange.start) - 1);
161 }
162 const content = this.getRangeContent(range);
163 const rangeStart = this.document.offsetAt(range.start);
164 const portIndex = this.indexOf(rangeStart, content, ':');
165 const dotIndex = this.indexOf(rangeStart, content, '.');
166 const startingSlashIndex = this.indexOf(rangeStart, content, '/');
167 // hostname detected
168 if (portIndex !== -1 || dotIndex !== -1) {
169 return vscode_languageserver_types_1.Range.create(range.start, this.document.positionAt(rangeStart + startingSlashIndex));
170 }
171 const registry = content.substring(0, startingSlashIndex);
172 // localhost registry detected
173 if (registry === 'localhost') {
174 return vscode_languageserver_types_1.Range.create(range.start, this.document.positionAt(rangeStart + startingSlashIndex));
175 }
176 }
177 return null;
178 }
179 getBuildStage() {
180 let range = this.getBuildStageRange();
181 return range === null ? null : this.getRangeContent(range);
182 }
183 getBuildStageRange() {
184 let args = this.getArguments();
185 if (args.length > 2 && args[1].getValue().toUpperCase() === "AS") {
186 return args[2].getRange();
187 }
188 return null;
189 }
190 getPlatformFlag() {
191 let flags = super.getFlags();
192 return flags.length === 1 && flags[0].getName() === "platform" ? flags[0] : null;
193 }
194}
195exports.From = From;