|
1 | 1 | import * as c from "ansi-colors";
|
| 2 | +import {execSync} from "child_process"; |
2 | 3 | import * as deepExtend from "deep-extend";
|
3 | 4 | import * as fs from "fs-extra";
|
4 | 5 | import * as yaml from "yaml";
|
5 |
| - |
6 |
| -import { execSync } from "child_process"; |
| 6 | +import {Job} from "./job"; |
7 | 7 | import * as predefinedVariables from "./predefined_variables";
|
8 |
| -import { Job } from "./job"; |
9 |
| -import { Stage } from "./stage"; |
| 8 | +import {Stage} from "./stage"; |
10 | 9 |
|
11 | 10 | export class Parser {
|
12 | 11 |
|
13 |
| - public static loadYaml(filePath: string): any { |
14 |
| - const gitlabCiLocalYmlPath = `${filePath}`; |
15 |
| - if (!fs.existsSync(gitlabCiLocalYmlPath)) { |
16 |
| - return {}; |
17 |
| - } |
18 |
| - |
19 |
| - const fileContent = fs.readFileSync(`${filePath}`, "utf8"); |
20 |
| - const descRegEx = /#.*?@Description\s?(.*)\s(.*)?:/gm |
21 |
| - const parse = yaml.parse(fileContent) || {}; |
22 |
| - |
23 |
| - let match; |
24 |
| - while (match = descRegEx.exec(fileContent)) { |
25 |
| - if (match[1] && match[2]) { |
26 |
| - parse[match[2]].description = match[1] |
27 |
| - } |
28 |
| - } |
29 |
| - |
30 |
| - return parse; |
31 |
| - } |
32 |
| - |
| 12 | + public readonly maxJobNameLength: number = 0; |
33 | 13 | private readonly illigalJobNames = [
|
34 | 14 | "include", "local_configuration", "image", "services",
|
35 | 15 | "stages", "pages", "types", "before_script", "default",
|
36 | 16 | "after_script", "variables", "cache", "include",
|
37 | 17 | ];
|
38 | 18 | private readonly jobs: Map<string, Job> = new Map();
|
39 |
| - |
40 |
| - public readonly maxJobNameLength: number = 0; |
41 | 19 | private readonly stages: Map<string, Stage> = new Map();
|
42 | 20 | private readonly cwd: string;
|
| 21 | + private readonly bashCompletionPhase: boolean; |
43 | 22 |
|
44 |
| - public constructor(cwd: any, pipelineIid: number) { |
| 23 | + public constructor(cwd: any, pipelineIid: number, bashCompletionPhase: boolean = false) { |
45 | 24 | let path = '';
|
46 | 25 | let yamlDataList: any[] = [];
|
47 | 26 |
|
| 27 | + this.bashCompletionPhase = bashCompletionPhase; |
48 | 28 | this.cwd = cwd;
|
49 | 29 |
|
50 | 30 | path = `${cwd}/.gitlab-ci.yml`;
|
@@ -107,41 +87,24 @@ export class Parser {
|
107 | 87 | }
|
108 | 88 | }
|
109 | 89 |
|
110 |
| - private prepareIncludes(doc: any): any[] { |
111 |
| - let includeDatas: any[] = []; |
112 |
| - const cwd = this.cwd; |
113 |
| - |
114 |
| - for (const value of doc["include"] || []) { |
115 |
| - if (value["local"]) { |
116 |
| - const localDoc = Parser.loadYaml(`${this.cwd}/${value.local}`); |
117 |
| - includeDatas = includeDatas.concat(this.prepareIncludes(localDoc)); |
118 |
| - } else if (value["file"]) { |
119 |
| - const ref = value["ref"] || "master"; |
120 |
| - const file = value["file"]; |
121 |
| - const project = value["project"]; |
122 |
| - const gitlabCiLocalPath = `${cwd}/.gitlab-ci-local/includes/${project}/${ref}/`; |
123 |
| - |
124 |
| - const domainRegExp = /^.*@(.*):.*\(fetch\)/; |
125 |
| - const output = execSync(`git remote -v`, { |
126 |
| - cwd: `${this.cwd}` |
127 |
| - }); |
128 |
| - const exec = domainRegExp.exec(`${output}`); |
129 |
| - const gitDomain = exec ? exec[1] : null; |
130 |
| - fs.ensureDirSync(gitlabCiLocalPath); |
131 |
| - execSync(`git archive --remote=git@${gitDomain}:${project}.git ${ref} --format=zip ${file} | gunzip -c - > ${file}`, { |
132 |
| - cwd: gitlabCiLocalPath |
133 |
| - }); |
| 90 | + public static loadYaml(filePath: string): any { |
| 91 | + const gitlabCiLocalYmlPath = `${filePath}`; |
| 92 | + if (!fs.existsSync(gitlabCiLocalYmlPath)) { |
| 93 | + return {}; |
| 94 | + } |
134 | 95 |
|
135 |
| - const fileDoc = Parser.loadYaml(`${cwd}/.gitlab-ci-local/includes/${project}/${ref}/${file}`); |
136 |
| - includeDatas = includeDatas.concat(this.prepareIncludes(fileDoc)); |
| 96 | + const fileContent = fs.readFileSync(`${filePath}`, "utf8"); |
| 97 | + const descRegEx = /#.*?@Description\s?(.*)\s(.*)?:/gm; |
| 98 | + const parse = yaml.parse(fileContent) || {}; |
137 | 99 |
|
138 |
| - } else { |
139 |
| - process.stderr.write(`Didn't understand include ${JSON.stringify(value)}\n`); |
| 100 | + let match; |
| 101 | + while (match = descRegEx.exec(fileContent)) { |
| 102 | + if (match[1] && match[2]) { |
| 103 | + parse[match[2]].description = match[1]; |
140 | 104 | }
|
141 | 105 | }
|
142 | 106 |
|
143 |
| - includeDatas.push(doc); |
144 |
| - return includeDatas; |
| 107 | + return parse; |
145 | 108 | }
|
146 | 109 |
|
147 | 110 | public getJobByName(name: string): Job {
|
@@ -192,4 +155,42 @@ export class Parser {
|
192 | 155 |
|
193 | 156 | }
|
194 | 157 | }
|
| 158 | + |
| 159 | + private prepareIncludes(doc: any): any[] { |
| 160 | + let includeDatas: any[] = []; |
| 161 | + const cwd = this.cwd; |
| 162 | + |
| 163 | + for (const value of doc["include"] || []) { |
| 164 | + if (value["local"]) { |
| 165 | + const localDoc = Parser.loadYaml(`${this.cwd}/${value.local}`); |
| 166 | + includeDatas = includeDatas.concat(this.prepareIncludes(localDoc)); |
| 167 | + } else if (value["file"]) { |
| 168 | + const ref = value["ref"] || "master"; |
| 169 | + const file = value["file"]; |
| 170 | + const project = value["project"]; |
| 171 | + const gitlabCiLocalPath = `${cwd}/.gitlab-ci-local/includes/${project}/${ref}/`; |
| 172 | + |
| 173 | + const domainRegExp = /^.*@(.*):.*\(fetch\)/; |
| 174 | + const output = execSync(`git remote -v`, { |
| 175 | + cwd: `${this.cwd}` |
| 176 | + }); |
| 177 | + const exec = domainRegExp.exec(`${output}`); |
| 178 | + const gitDomain = exec ? exec[1] : null; |
| 179 | + fs.ensureDirSync(gitlabCiLocalPath); |
| 180 | + if (!this.bashCompletionPhase || !fs.existsSync(`${cwd}/.gitlab-ci-local/includes/${project}/${ref}/${file}`)) { |
| 181 | + execSync(`git archive --remote=git@${gitDomain}:${project}.git ${ref} --format=zip ${file} | gunzip -c - > ${file}`, { |
| 182 | + cwd: gitlabCiLocalPath |
| 183 | + }); |
| 184 | + } |
| 185 | + const fileDoc = Parser.loadYaml(`${cwd}/.gitlab-ci-local/includes/${project}/${ref}/${file}`); |
| 186 | + includeDatas = includeDatas.concat(this.prepareIncludes(fileDoc)); |
| 187 | + |
| 188 | + } else { |
| 189 | + process.stderr.write(`Didn't understand include ${JSON.stringify(value)}\n`); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + includeDatas.push(doc); |
| 194 | + return includeDatas; |
| 195 | + } |
195 | 196 | }
|
0 commit comments