forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatch.ts
72 lines (65 loc) · 3.23 KB
/
patch.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
namespace pxt.patching {
export function computePatches(version: string, kind?: string): ts.pxtc.UpgradePolicy[] {
const patches = pxt.appTarget.compile ? pxt.appTarget.compile.patches : undefined;
if (!patches) return undefined;
return parsePatches(version, patches, kind);
}
export function computePyPatches(version: string, kind?: string): ts.pxtc.UpgradePolicy[] {
const patches = pxt.appTarget.compile ? pxt.appTarget.compile.pyPatches : undefined;
if (!patches) return undefined;
return parsePatches(version, patches, kind);
}
function parsePatches(version: string, patches: Map<ts.pxtc.UpgradePolicy[]>, kind?: string): ts.pxtc.UpgradePolicy[] {
const v = pxt.semver.tryParse(version || "0.0.0") || pxt.semver.tryParse("0.0.0");
let r: ts.pxtc.UpgradePolicy[] = [];
Object.keys(patches)
.filter(rng => pxt.semver.inRange(rng, v))
.forEach(rng => r = r.concat(patches[rng]));
if (kind)
r = r.filter(p => p.type == kind);
return r.length ? r : undefined;
}
export function upgradePackageReference(pkgTargetVersion: string, pkg: string, val: string): string {
if (val != "*") return pkg;
const upgrades = pxt.patching.computePatches(pkgTargetVersion, "package");
let newPackage = pkg;
if (upgrades) {
upgrades.forEach(rule => {
Object.keys(rule.map).forEach(match => {
if (newPackage == match) {
newPackage = rule.map[match];
}
});
});
}
return newPackage;
}
export function patchJavaScript(pkgTargetVersion: string, fileContents: string): string {
const upgrades = pxt.patching.computePatches(pkgTargetVersion);
return patchTextCode(pkgTargetVersion, fileContents, upgrades);
}
export function patchPython(pkgTargetVersion: string, fileContents: string): string {
const upgrades = pxt.patching.computePyPatches(pkgTargetVersion);
return patchTextCode(pkgTargetVersion, fileContents, upgrades);
}
function patchTextCode(pkgTargetVersion: string, fileContents: string, upgrades: pxtc.UpgradePolicy[]): string {
let updatedContents = fileContents;
if (upgrades) {
upgrades.filter(u => u.type === "api").forEach(rule => {
Object.keys(rule.map).forEach(match => {
const regex = new RegExp(match, 'g');
updatedContents = updatedContents.replace(regex, rule.map[match]);
});
});
upgrades.filter(u => u.type === "userenum").forEach(rule => {
Object.keys(rule.map).forEach(enumName => {
const declRegex = new RegExp("enum\\s+" + enumName + "\\s*{", 'gm');
updatedContents = updatedContents.replace(declRegex, `enum ${rule.map[enumName]} {`);
const usageRegex = new RegExp(`(^|[^_a-zA-Z0-9])${enumName}(\\s*\\.)`, 'g');
updatedContents = updatedContents.replace(usageRegex, `$1${rule.map[enumName]}$2`);
});
});
}
return updatedContents;
}
}