|
| 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 |
0 commit comments