Skip to content

Commit 7794371

Browse files
committed
Separate version check script into its own file
1 parent bc2370e commit 7794371

File tree

2 files changed

+141
-133
lines changed

2 files changed

+141
-133
lines changed

.github/workflows/version_check.jl

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Set up a temporary environment just to run this script
2+
using Pkg
3+
Pkg.activate(temp=true)
4+
Pkg.add(["YAML", "TOML", "JSON", "HTTP"])
5+
import YAML
6+
import TOML
7+
import JSON
8+
import HTTP
9+
10+
PROJECT_TOML_PATH = "Project.toml"
11+
QUARTO_YML_PATH = "_quarto.yml"
12+
MANIFEST_TOML_PATH = "Manifest.toml"
13+
14+
function major_minor_match(vs...)
15+
first = vs[1]
16+
all(v -> v.:major == first.:major && v.:minor == first.:minor, vs...)
17+
end
18+
19+
function major_minor_patch_match(vs...)
20+
first = vs[1]
21+
all(v -> v.:major == first.:major && v.:minor == first.:minor && v.:patch == first.:patch, vs...)
22+
end
23+
24+
"""
25+
Update the version number in Project.toml to match `target_version`.
26+
27+
This uses a naive regex replacement on lines, i.e. sed-like behaviour. Parsing
28+
the file, editing the TOML and then re-serialising also works and would be more
29+
correct, but the entries in the output file can end up being scrambled, which
30+
would lead to unnecessarily large diffs in the PR.
31+
"""
32+
function update_project_toml(filename, target_version::VersionNumber)
33+
lines = readlines(filename)
34+
open(filename, "w") do io
35+
for line in lines
36+
if occursin(r"^Turing\s*=\s*\"\d+\.\d+\"\s*$", line)
37+
println(io, "Turing = \"$(target_version.:major).$(target_version.:minor)\"")
38+
else
39+
println(io, line)
40+
end
41+
end
42+
end
43+
end
44+
45+
"""
46+
Update the version number in _quarto.yml to match `target_version`.
47+
48+
See `update_project_toml` for implementation rationale.
49+
"""
50+
function update_quarto_yml(filename, target_version::VersionNumber)
51+
# Don't deserialise/serialise as this will scramble lines
52+
lines = readlines(filename)
53+
open(filename, "w") do io
54+
for line in lines
55+
m = match(r"^(\s+)- text:\s*\"v\d+\.\d+\"\s*$", line)
56+
if m !== nothing
57+
println(io, "$(m[1])- text: \"v$(target_version.:major).$(target_version.:minor)\"")
58+
else
59+
println(io, line)
60+
end
61+
end
62+
end
63+
end
64+
65+
# Retain the original version number string for error messages, as
66+
# VersionNumber() will tack on a patch version of 0
67+
quarto_yaml = YAML.load_file(QUARTO_YML_PATH)
68+
quarto_version_str = quarto_yaml["website"]["navbar"]["right"][1]["text"]
69+
quarto_version = VersionNumber(quarto_version_str)
70+
println("_quarto.yml version: ", quarto_version_str)
71+
72+
project_toml = TOML.parsefile(PROJECT_TOML_PATH)
73+
project_version_str = project_toml["compat"]["Turing"]
74+
project_version = VersionNumber(project_version_str)
75+
println("Project.toml version: ", project_version_str)
76+
77+
manifest_toml = TOML.parsefile(MANIFEST_TOML_PATH)
78+
manifest_version = VersionNumber(manifest_toml["deps"]["Turing"][1]["version"])
79+
println("Manifest.toml version: ", manifest_version)
80+
81+
errors = []
82+
83+
if ENV["TARGET_IS_MASTER"] == "true"
84+
# This environment variable is set by the GitHub Actions workflow. If it is
85+
# true, fetch the latest version from GitHub and update files to match this
86+
# version if necessary.
87+
88+
resp = HTTP.get("https://github.com/api/repos/TuringLang/Turing.jl/releases/latest")
89+
latest_version = VersionNumber(JSON.parse(String(resp.body))["tag_name"])
90+
println("Latest Turing.jl version: ", latest_version)
91+
92+
if !major_minor_match(latest_version, project_version)
93+
push!(errors, "$(PROJECT_TOML_PATH) out of date")
94+
println("$(PROJECT_TOML_PATH) is out of date; updating")
95+
update_project_toml(PROJECT_TOML_PATH, latest_version)
96+
end
97+
98+
if !major_minor_match(latest_version, quarto_version)
99+
push!(errors, "$(QUARTO_YML_PATH) out of date")
100+
n_errors += 1
101+
println("$(QUARTO_YML_PATH) is out of date; updating")
102+
update_quarto_yml(QUARTO_YML_PATH, latest_version)
103+
end
104+
105+
if !major_minor_patch_match(latest_version, manifest_version)
106+
push!(errors, "$(MANIFEST_TOML_PATH) out of date")
107+
# Attempt to automatically update Manifest
108+
println("$(MANIFEST_TOML_PATH) is out of date; updating")
109+
old_env = Pkg.project().path
110+
Pkg.activate(".")
111+
Pkg.update()
112+
# Check if versions match now, error if not
113+
Pkg.activate(old_env)
114+
manifest_toml = TOML.parsefile(MANIFEST_TOML_PATH)
115+
manifest_version = VersionNumber(manifest_toml["deps"]["Turing"][1]["version"])
116+
if !major_minor_patch_match(latest_version, manifest_version)
117+
push!(errors, "Failed to update $(MANIFEST_TOML_PATH) to match latest Turing.jl version")
118+
end
119+
end
120+
121+
if isempty(errors)
122+
println("All good")
123+
else
124+
error("The following errors occurred during version checking: \n", join(errors, "\n"))
125+
end
126+
127+
else
128+
# If this is not true, then we are running on a backport-v* branch, i.e. docs
129+
# for a non-latest version. In this case we don't attempt to fetch the latest
130+
# patch version from GitHub to check the Manifest (we could, but it is more
131+
# work as it would involve paging through the list of releases). Instead,
132+
# we just check that the minor versions match.
133+
if !major_minor_match(quarto_version, project_version, manifest_version)
134+
error("The minor versions of Turing.jl in _quarto.yml, Project.toml, and Manifest.toml are inconsistent:
135+
- _quarto.yml: $quarto_version_str
136+
- Project.toml: $project_version_str
137+
- Manifest.toml: $manifest_version
138+
")
139+
end
140+
end

.github/workflows/version_check.yml

Lines changed: 1 addition & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -48,140 +48,8 @@ jobs:
4848
echo TARGET_IS_MASTER: ${{ env.TARGET_IS_MASTER }}
4949
5050
- name: Check version consistency
51-
shell: julia --color=yes --project=. {0}
5251
continue-on-error: true
53-
run: |
54-
using Pkg
55-
Pkg.activate(temp=true)
56-
Pkg.add(["YAML", "TOML", "JSON", "HTTP"])
57-
import YAML
58-
import TOML
59-
import JSON
60-
import HTTP
61-
62-
PROJECT_TOML_PATH = "Project.toml"
63-
QUARTO_YML_PATH = "_quarto.yml"
64-
MANIFEST_TOML_PATH = "Manifest.toml"
65-
66-
function major_minor_match(vs...)
67-
for v in vs
68-
if v.:major != vs[1].:major || v.:minor != vs[1].:minor
69-
return false
70-
end
71-
end
72-
return true
73-
end
74-
75-
function major_minor_patch_match(vs...)
76-
for v in vs
77-
if v.:major != vs[1].:major || v.:minor != vs[1].:minor || v.:patch != vs[1].:patch
78-
return false
79-
end
80-
end
81-
return true
82-
end
83-
84-
function update_project_toml(fname, target_version)
85-
# Don't deserialise/serialise as this will scramble lines
86-
lines = readlines(fname)
87-
open(fname, "w") do io
88-
for line in lines
89-
if occursin(r"^Turing\s*=\s*\"\d+\.\d+\"\s*$", line)
90-
println(io, "Turing = \"$(target_version.:major).$(target_version.:minor)\"")
91-
else
92-
println(io, line)
93-
end
94-
end
95-
end
96-
end
97-
98-
function update_quarto_yml(fname, target_version)
99-
# Don't deserialise/serialise as this will scramble lines
100-
lines = readlines(fname)
101-
open(fname, "w") do io
102-
for line in lines
103-
m = match(r"^(\s+)- text:\s*\"v\d+\.\d+\"\s*$", line)
104-
if m !== nothing
105-
println(io, "$(m[1])- text: \"v$(target_version.:major).$(target_version.:minor)\"")
106-
else
107-
println(io, line)
108-
end
109-
end
110-
end
111-
end
112-
113-
# Retain the original version number string for error messages, as
114-
# VersionNumber() will tack on a patch version of 0
115-
quarto_yaml = YAML.load_file(QUARTO_YML_PATH)
116-
quarto_version_str = quarto_yaml["website"]["navbar"]["right"][1]["text"]
117-
quarto_version = VersionNumber(quarto_version_str)
118-
println("_quarto.yml version: ", quarto_version_str)
119-
120-
project_toml = TOML.parsefile(PROJECT_TOML_PATH)
121-
project_version_str = project_toml["compat"]["Turing"]
122-
project_version = VersionNumber(project_version_str)
123-
println("Project.toml version: ", project_version_str)
124-
125-
manifest_toml = TOML.parsefile(MANIFEST_TOML_PATH)
126-
manifest_version = VersionNumber(manifest_toml["deps"]["Turing"][1]["version"])
127-
println("Manifest.toml version: ", manifest_version)
128-
129-
errors = []
130-
131-
if ENV["TARGET_IS_MASTER"] == "true"
132-
# Fetch the latest version from GitHub and update files to match this version
133-
# if necessary.
134-
135-
resp = HTTP.get("https://github.com/api/repos/TuringLang/Turing.jl/releases/latest")
136-
latest_version = VersionNumber(JSON.parse(String(resp.body))["tag_name"])
137-
println("Latest Turing.jl version: ", latest_version)
138-
139-
if !major_minor_match(latest_version, project_version)
140-
push!(errors, "$(PROJECT_TOML_PATH) out of date")
141-
println("$(PROJECT_TOML_PATH) is out of date; updating")
142-
update_project_toml(PROJECT_TOML_PATH, latest_version)
143-
end
144-
145-
if !major_minor_match(latest_version, quarto_version)
146-
push!(errors, "$(QUARTO_YML_PATH) out of date")
147-
n_errors += 1
148-
println("$(QUARTO_YML_PATH) is out of date; updating")
149-
update_quarto_yml(QUARTO_YML_PATH, latest_version)
150-
end
151-
152-
if !major_minor_patch_match(latest_version, manifest_version)
153-
push!(errors, "$(MANIFEST_TOML_PATH) out of date")
154-
# Attempt to automatically update Manifest
155-
println("$(MANIFEST_TOML_PATH) is out of date; updating")
156-
old_env = Pkg.project().path
157-
Pkg.activate(".")
158-
Pkg.update()
159-
# Check if versions match now, error if not
160-
Pkg.activate(old_env)
161-
manifest_toml = TOML.parsefile(MANIFEST_TOML_PATH)
162-
manifest_version = VersionNumber(manifest_toml["deps"]["Turing"][1]["version"])
163-
if !major_minor_patch_match(latest_version, manifest_version)
164-
push!(errors, "Failed to update $(MANIFEST_TOML_PATH) to match latest Turing.jl version")
165-
end
166-
end
167-
168-
if isempty(errors)
169-
println("All good")
170-
else
171-
error("The following errors occurred during version checking: \n", join(errors, "\n"))
172-
end
173-
174-
else
175-
# Don't attempt to fetch the latest version; just check for consistency
176-
# and error if versions don't match.
177-
if !major_minor_match(quarto_version, project_version, manifest_version)
178-
error("The minor versions of Turing.jl in _quarto.yml, Project.toml, and Manifest.toml are inconsistent:
179-
- _quarto.yml: $quarto_version_str
180-
- Project.toml: $project_version_str
181-
- Manifest.toml: $manifest_version
182-
")
183-
end
184-
end
52+
run: julia --color=yes --project=. .github/workflows/version_check.jl
18553

18654
- name: Create a PR with suggested changes
18755
id: create_pr

0 commit comments

Comments
 (0)