-
Notifications
You must be signed in to change notification settings - Fork 68
/
tokenizer.ts
185 lines (161 loc) · 6.76 KB
/
tokenizer.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as tl from 'azure-pipelines-task-lib/task';
import * as sh from 'shelljs';
import * as fs from 'fs';
import * as os from 'os';
function shouldReplaceProp(includeSet: Set<string>, excludeSet: Set<string>, propPath: string) {
return (!includeSet && !excludeSet) ||
(includeSet && includeSet.has(propPath)) ||
(excludeSet && !excludeSet.has(propPath));
}
function arrayIsPrimitiveArray(item: any[]) {
// if `i === Object(i)` then i is a primitive
return item.every(p => {
console.info(`-----> Checking ${p}`);
let isPrimitive = p !== Object(p);
console.info(`-----> isPrim? ${isPrimitive}`);
return isPrimitive;
});
}
function replaceProps(nullBehavior: string, obj: any, parent: string, includeSet: Set<string>, excludeSet: Set<string>) {
let success = true;
for (let prop of Object.keys(obj)) {
let propPath = parent === '' ? `${prop}` : `${parent}.${prop}`;
if (obj[prop] == null) {
let msg = `Property ${propPath} is null`;
if (nullBehavior === "warning") {
tl.warning(msg);
} else {
tl.error(msg);
success = false;
}
continue;
}
let propType = typeof (obj[prop])
console.info(`${propPath} has typeof ${propType}`)
if (obj[prop] instanceof Array) {
if (arrayIsPrimitiveArray(obj[prop])) {
console.info(`${propPath} is a primitive array`);
if (shouldReplaceProp(includeSet, excludeSet, propPath)) {
console.info(`Tokenizing ${propPath}`);
obj[prop] = [`__${propPath}[]__`];
} else {
console.info(`Skipping ${propPath}`);
}
}
else {
console.info(`${propPath} is a complex array`);
obj[prop].forEach((arrayObj, position) => {
// if we're already in an array, we need to update the index
var posOfBracket = propPath.indexOf("[");
if (posOfBracket > -1) {
propPath = propPath.substr(0, posOfBracket);
}
// now append the index
propPath += `[${position}]`;
success = success && replaceProps(nullBehavior, arrayObj, propPath, includeSet, excludeSet);
});
}
} else if (typeof (obj[prop]) === 'object') {
success = success && replaceProps(nullBehavior, obj[prop], propPath, includeSet, excludeSet);
} else {
if (shouldReplaceProp(includeSet, excludeSet, propPath)) {
console.info(`Tokenizing ${propPath}`);
obj[prop] = `__${propPath}__`;
}
}
}
return success;
}
async function run() {
try {
tl.debug("Starting Tokenizer task");
// get the task vars
var sourcePath = tl.getPathInput("sourcePath");
if (!sourcePath || sourcePath.length === 0) {
sourcePath = tl.getVariable("Build.SourcesDirectory");
}
// clear leading and trailing quotes for paths with spaces
sourcePath = sourcePath.replace(/"/g, "");
// remove trailing slash
if (sourcePath.endsWith("\\") || sourcePath.endsWith("/")) {
tl.debug("Trimming separator off sourcePath");
sourcePath = sourcePath.substr(0, sourcePath.length - 1);
}
tl.checkPath(sourcePath, "sourcePath");
let filePattern = tl.getInput("filePattern", true);
let tokenizeType = tl.getInput("tokenizeType", true);
let includes = tl.getInput("includes", false);
let excludes = tl.getInput("excludes", false);
if (!includes) {
includes = '';
}
if (!excludes) {
excludes = '';
}
let nullBehavior = tl.getInput("nullBehavior", true);
tl.debug(`sourcePath: [${sourcePath}]`);
tl.debug(`filePattern: [${filePattern}]`);
tl.debug(`tokenizeType: [${tokenizeType}]`);
tl.debug(`includes: [${includes}]`);
tl.debug(`excludes: [${excludes}]`);
tl.debug(`nullBehavior: [${nullBehavior}]`);
// only one or the other can be specified
if (includes && includes.length > 0 && excludes && excludes.length > 0) {
throw `You cannot specify includes and excludes - please specify one or the other`;
}
if (!filePattern || filePattern.length === 0) {
filePattern = "*.*";
}
tl.debug(`Using [${filePattern}] as filePattern`);
// create a glob removing any spurious quotes
if (os.platform() !== "win32") {
// replace \ with /
filePattern = filePattern.replace(/\\/g, "/");
}
// get the files
let files = tl.findMatch(sourcePath, filePattern);
if (!files || files.length === 0) {
let msg = `Could not find files with glob [${filePattern}].`;
if (os.platform() !== "win32") {
tl.warning("No files found for pattern. Non-windows file systems are case sensitvive, so check the case of your path and file patterns.");
}
tl.setResult(tl.TaskResult.Failed, msg);
}
// comma-split the include and excludes
let includeSet: Set<string>, excludeSet: Set<string>;
if (includes && includes.length > 0) {
includeSet = new Set(includes.split(','));
tl.debug(`Includeset has ${includeSet.size} elements`);
}
if (excludes && excludes.length > 0) {
excludeSet = new Set(excludes.split(','));
tl.debug(`Excludeset has ${excludeSet.size} elements`);
}
for (var i = 0; i < files.length; i++) {
let file = files[i];
console.info(`Starting tokenization in [${file}]!`);
let contents = fs.readFileSync(file).toString();
// remove BOM if present
contents = contents.replace(String.fromCharCode(65279), '');
let json = JSON.parse(contents);
// find the include properties recursively
let success = replaceProps(nullBehavior, json, '', includeSet, excludeSet);
if (!success) {
throw "Tokenization failed - please check previous logs.";
}
tl.debug("Writing new values to file...");
// make the file writable
sh.chmod(666, file);
fs.writeFileSync(file, JSON.stringify(json, null, 2));
}
}
catch (err) {
let msg = err;
if (err.message) {
msg = err.message;
}
tl.setResult(tl.TaskResult.Failed, msg);
}
tl.debug("Leaving Tokenize task");
}
run();