-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.py
210 lines (168 loc) · 7.19 KB
/
updater.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
An updater of Rust crates according to input arguments for use in the GitHub Action.
Can be as simple as just one crate. However also works in monorepos.
Has several steps:
- Finds local `Cargo.toml`s using `cargo metadata` command
- (for monorepos) Figures out flat dependencies
- (for monorepos) Sorts local crates in order of which they import each other
- Parses command line argument
- (for monorepos) A JSON object with crate-name to argument pairs.
- Else just a single argument for a single crate
- For package(s)
- Applies argument(s) to version(s)
- Saves changes to the filesystem
- (for monorepos)
- Updates version references for local dependencies to their updated version
- Prints to `GITHUB_OUTPUT`
- A human readable description of changes
- A JSON list of new version to crate name pairs (useful for git tagging)
- A JSON object of package names
- A JSON list of paths to publish
"""
import sys
import subprocess
import json
from os import environ
from tomlkit import dumps, parse
from semver import bump_major, bump_minor, bump_patch, VersionInfo
from functools import cmp_to_key
command = "cargo metadata --offline --format-version 1 --no-deps".split(" ")
output = subprocess.run(command, capture_output=True).stdout
packages = json.loads(output)["packages"]
flat_packages = dict()
local_pkgs = set()
awaiting = dict()
publishable_packages = list()
for package in packages:
if isinstance(package["publish"], list) and len(package["publish"]) == 0:
continue
name = package["name"]
dependency_names = list(map(lambda pkg: pkg["name"], package["dependencies"]))
for dependency_name in dependency_names.copy():
if dependency_name in local_pkgs:
if dependency_name in flat_packages:
dependency_names.extend(flat_packages[dependency_name])
else:
awaiting.setdefault(dependency_name, []).append(name)
for awaitee in awaiting.pop(name, []):
flat_packages[awaitee].extend(dependency_names)
flat_packages[name] = dependency_names
local_pkgs.add(name)
publishable_packages.append(package)
argument = sys.argv[1]
dry_run = sys.argv[2] == "--dry-run" if len(sys.argv) > 2 else False
def package_order(package1, package2):
global flat_packages
if package1 == package2:
return 0
elif package1["name"] in flat_packages[package2["name"]]:
return -1
elif package2["name"] in flat_packages[package1["name"]]:
return 1
else:
return 0
packages = sorted(publishable_packages, key=cmp_to_key(package_order))
def toml_file_to_dict(path):
with open(path, "r") as f:
toml = parse(f.read())
f.close()
return toml
def bump_version(existing_version, behavior):
match behavior:
case "patch":
return bump_patch(existing_version)
case "minor":
return bump_minor(existing_version)
case "major":
return bump_major(existing_version)
case _:
raise ValueError(
f"Expected 'patch', 'minor', 'major' or 'none'. Found '{behavior}'"
)
def write_toml(path, toml):
if not dry_run:
with open(path, "w") as f:
f.write(dumps(toml))
f.close()
def update_cargo_toml(crate_cargo_toml, argument):
current_version = crate_cargo_toml["package"]["version"]
if argument == "none":
return current_version
if VersionInfo.isvalid(argument):
if VersionInfo.parse(argument) <= VersionInfo.parse(current_version):
error = f"New version '{argument}' must be greater than current version '{current_version}'"
raise ValueError(error)
new_version = argument
else:
new_version = bump_version(current_version, argument)
crate_cargo_toml["package"]["version"] = new_version
return new_version
def format_change_list(iter):
msg = ""
for iteration, (name, value) in enumerate(iter, 1):
if not iteration == 1:
if len(iter) == iteration:
msg += " and "
else:
msg += ", "
msg += f"{name} to {value}"
return msg
if argument.startswith("{"):
arguments = json.loads(argument)
updated_crates = dict()
updated_manifests = list()
for package in packages:
argument = arguments.pop(package["name"], "none")
if argument == "none":
# Check whether it needs updating because a dependency changed
for depedency in flat_packages[package["name"]]:
if depedency in updated_crates:
error = f"'{package['name']}' needs to be updated as '{depedency}' is updated"
raise ValueError(error)
continue
manifest_path = package["manifest_path"]
crate_cargo_toml = toml_file_to_dict(manifest_path)
new_version = update_cargo_toml(crate_cargo_toml, argument)
updated_crates[package["name"]] = new_version
updated_manifests.append(manifest_path)
for dependency_name, argument in crate_cargo_toml["dependencies"].items():
if isinstance(argument, dict):
dependency_name = argument.get("package", dependency_name)
if dependency_name in local_pkgs and dependency_name in updated_crates:
new_version = updated_crates[dependency_name]
argument["version"] = new_version
write_toml(manifest_path, crate_cargo_toml)
changes = ",".join(map(lambda t: f'"{t[0]}-{t[1]}"', updated_crates.items()))
stream = sys.stdout if dry_run else open(environ["GITHUB_OUTPUT"], "w")
with stream as f:
print("new-version=none", file=f)
print(f"new-versions=[{changes}]", file=f)
print(
f"new-versions-description={format_change_list(updated_crates.items())}",
file=f,
)
print(f"new-versions-json-object={json.dumps(updated_crates)}", file=f)
print(f"updated-cargo-toml-paths={json.dumps(updated_manifests)}", file=f)
else:
if len(packages) == 1:
package = packages[0]
manifest_path = package["manifest_path"]
crate_cargo_toml = toml_file_to_dict(manifest_path)
crate_name = crate_cargo_toml["package"]["name"]
if argument == "none":
raise ValueError("Argument for single package cannot be 'none'")
new_version = update_cargo_toml(crate_cargo_toml, argument)
write_toml(manifest_path, crate_cargo_toml)
stream = sys.stdout if dry_run else open(environ["GITHUB_OUTPUT"], "w")
with stream as f:
print(f"new-version={new_version}", file=f)
print(f'new-versions=["{new_version}"]', file=f)
print(f"new-versions-description={new_version}", file=f)
print(
f'new-versions-json-object={{"{crate_name}":"{new_version}"}}', file=f
)
print(f'updated-cargo-toml-paths=["{manifest_path}"]', file=f)
else:
package_names = list(map(lambda pkg: pkg["name"], packages))
error = f"Expected single package in workspace for non-JSON '{argument}' argument, found: {package_names}"
raise ValueError(error)