forked from paritytech/substrate-debug-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_cargo.js
107 lines (96 loc) · 3.14 KB
/
update_cargo.js
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
const fs = require('fs')
const path = require('path')
const dirs = ["remote-externalities", "offline-election", "sub-storage", "sub-du"]
const VERSION_TYPE = {
EXACT: "2.0.0",
BRANCH: "master",
COMMIT: "6687fa111e5efaef6c91ec840dc7fb92d4a72820",
LOCAL: "",
}
const SPECIAL_VERSIONS = {
"frame-metadata": "12.0.0",
"sp-externalities": "0.8.0",
};
function set_exact(package, version, with_optional) {
if (SPECIAL_VERSIONS[package]) {
version = SPECIAL_VERSIONS[package]
console.log(`Overriding ${version} for ${package}`)
}
return `${package} = { version = "${version}"${with_optional ? ", optional = true " : " "}}\n`
}
function set_branch(package, branch, with_optional) {
return `${package} = { git = "https://github.com/paritytech/substrate", branch = "${branch}"${with_optional ? ", optional = true " : " "}}\n`
}
function set_commit(package, commit, with_optional) {
return `${package} = { git = "https://github.com/paritytech/substrate", rev = "${commit}"${with_optional ? ", optional = true " : " "}}\n`
}
function set_local(package, folder, local_package, with_optional) {
return `${package} = { path = "../../substrate/${folder}/${local_package}"${with_optional ? ", optional = true " : " "}}\n`
}
function do_update(content, version) {
let output = ""
for (let line of content.split("\n")) {
if (line.startsWith("sp-")) {
let package = line.split(" ")[0]
switch(version) {
case VERSION_TYPE.EXACT :
output += set_exact(package, version, line.includes("optional"))
break
case VERSION_TYPE.BRANCH :
output += set_branch(package, version, line.includes("optional"))
break
case VERSION_TYPE.COMMIT :
output += set_commit(package, version, line.includes("optional"))
break
case VERSION_TYPE.LOCAL :
let primitive_package = package.split("-").slice(1).join("-")
output += set_local(package, "primitives", primitive_package, line.includes("optional"))
break
}
} else if (line.startsWith("frame-") || line.startsWith("pallet-")) {
let package = line.split(" ")[0]
switch(version) {
case VERSION_TYPE.EXACT :
output += set_exact(package, version, line.includes("optional"))
break
case VERSION_TYPE.BRANCH :
output += set_branch(package, version, line.includes("optional"))
break
case VERSION_TYPE.COMMIT :
output += set_commit(package, version, line.includes("optional"))
break
case VERSION_TYPE.LOCAL :
let frame_package = package.split("-").slice(1).join("-")
output += set_local(package, "frame", frame_package, line.includes("optional"))
break
}
} else {
output += (line + "\n")
}
}
return output
}
function main(version) {
for (let d of dirs) {
let cargo_file = path.join(d, "Cargo.toml")
let content = String(fs.readFileSync(cargo_file))
let new_content = do_update(content, version)
fs.writeFileSync(cargo_file, new_content.trimRight() + "\n")
}
}
switch(process.argv[2]) {
case "branch":
main(VERSION_TYPE.BRANCH)
break
case "commit":
main(VERSION_TYPE.COMMIT)
break
case "local":
main(VERSION_TYPE.LOCAL)
break
case "exact":
main(VERSION_TYPE.EXACT)
break
default:
main(VERSION_TYPE.EXACT)
}