forked from colindembovsky/cols-agent-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versionAssemblies.ts
139 lines (120 loc) · 5.81 KB
/
versionAssemblies.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as tl from 'vsts-task-lib/task';
import * as sh from 'shelljs';
import * as fs from 'fs';
import * as os from 'os';
async function run() {
try {
tl.debug("Starting Version Assemblies step");
// get the task lets
let filePattern = tl.getInput("filePattern", true);
let versionSource = tl.getInput("versionSource", true);
let versionFormat = tl.getInput("versionFormat", true);
let customNumberVariable = tl.getInput("customNumberVariable", false);
let customBuildRegex = tl.getInput("customBuildRegex", false);
let buildRegexIndex = tl.getInput("buildRegexIndex", false);
let replaceVersionFormat = tl.getInput("replaceVersionFormat", true);
let customReplaceRegex = tl.getInput("customReplaceRegex", false);
let replacePrefix = tl.getInput("replacePrefix", false);
let replacePostfix = tl.getInput("replacePostfix", false);
let failIfNoMatchFound = tl.getBoolInput("failIfNoMatchFound", false);
let sourcePath = tl.getPathInput("sourcePath");
if (!sourcePath || sourcePath.length === 0) {
sourcePath = tl.getVariable("Build.SourcesDirectory");
}
tl.checkPath(sourcePath, "sourcePath");
// clear leading and trailing quotes for paths with spaces
sourcePath = sourcePath.replace(/"/g, "");
// get the build number from the env lets
let buildNumber = tl.getVariable("Build.BuildNumber");
// these will be null if not specified - change to empty string
if (!replacePrefix) replacePrefix = "";
if (!replacePostfix) replacePostfix = "";
tl.debug(`replacePrefix: ${replacePrefix}`);
tl.debug(`replacePostfix: ${replacePostfix}`);
tl.debug(`buildNumber: ${buildNumber}`);
let buildRegex = customBuildRegex;
switch (versionFormat) {
default:
case "fourParts": buildRegex = "\\d+\\.\\d+\\.\\d+\\.\\d+"; break;
case "threeParts": buildRegex = "\\d+\\.\\d+\\.\\d+"; break;
case "custom": buildRegex = customBuildRegex; break;
}
let replaceRegex = customReplaceRegex;
switch (replaceVersionFormat) {
default:
case "fourParts": replaceRegex = "\\d+\\.\\d+\\.\\d+\\.\\d+"; break;
case "threeParts": replaceRegex = "\\d+\.\\d+\\.\\d+"; break;
case "custom": replaceRegex = customReplaceRegex; break;
}
tl.debug(`Using ${buildRegex} as the build regex`);
tl.debug(`Using ${replaceRegex} as the replacement regex`);
if (!buildRegexIndex || buildRegexIndex.length === 0){
buildRegexIndex = "0";
}
tl.debug(`Using ${buildRegexIndex} as the build regex group index`);
let separator = os.platform() === "win32" ? "\\" : "/";
let versionNum = "";
let skip = false;
switch (versionSource) {
case "variable": {
versionNum = tl.getVariable(customNumberVariable);
console.info(`Using ${versionNum} for the custom version number`);
break;
}
default:
case "buildNumber": {
let buildRegexObj = new RegExp(buildRegex);
if (buildRegexObj.test(buildNumber)) {
versionNum = buildRegexObj.exec(buildNumber)[buildRegexIndex];
} else {
skip = true;
tl.warning(`Could not extract a version from [${buildNumber}] using pattern [${buildRegex}]`);
}
break;
}
}
if (!skip) {
console.info(`Using prefix [${replacePrefix}] and version [${versionNum}] and postfix [${replacePostfix}] in folder [${sourcePath}]`);
if (os.platform() !== "win32") {
// replace \ with /
filePattern = filePattern.replace(/\\/g, "/");
}
let filesToReplace = tl.findMatch(sourcePath, filePattern);
if (!filesToReplace || filesToReplace.length === 0) {
tl.warning("No files found");
} else {
for (let i = 0; i < filesToReplace.length; i++) {
let file = filesToReplace[i];
console.info(`Changing version in ${file}`);
let contents = fs.readFileSync(file, 'utf8').toString();
let checkMatches = new RegExp(replaceRegex).exec(contents);
if (!checkMatches || checkMatches.length === 0) {
if (failIfNoMatchFound) {
tl.setResult(tl.TaskResult.Failed, `No matches for regex [${replaceRegex}] found in file ${file}`);
process.exit(1);
} else {
tl.warning(`No matches for regex [${replaceRegex}] found in file ${file}`);
}
} else {
console.info(`${checkMatches.length} matches for regex [${replaceRegex}] found in file ${file}`);
let replaceVal = replacePrefix + versionNum + replacePostfix;
let result = contents.replace(new RegExp(replaceRegex, "g"), replaceVal);
// make the file writable
sh.chmod(666, file);
fs.writeFileSync(file, result, { encoding: 'utf8' });
}
}
console.info(`Processed ${filesToReplace.length} files`);
}
}
}
catch (err) {
let msg = err;
if (err.message) {
msg = err.message;
}
tl.setResult(tl.TaskResult.Failed, msg);
}
tl.debug("Leaving Version Assemblies step");
}
run();