Skip to content

Commit c149eea

Browse files
authored
fix: cache the versions.json in build script (#373)
resolves #365 This allows publishing clang-tools-manager crate with a cached versions.json file, so the docs.rs build can succeed. The cached file is invalid after 24 hours, meaning the cached file is updated at least once a day. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Strengthened CI/CD publishing pipeline with improved job dependencies and artifact management for binary releases * Enhanced build script with intelligent local version data caching to improve overall build efficiency and reduce external requests * Updated package metadata configuration and optimized dependency features for improved distribution and compatibility <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent fd15454 commit c149eea

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

.github/workflows/binary-builds.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ jobs:
186186
publish:
187187
if: startswith(github.ref, 'refs/tags/cpp-linter/v') || startswith(github.ref, 'refs/tags/clang-tools-manager/v')
188188
runs-on: ubuntu-latest
189-
needs: [create-assets]
189+
needs: [create-assets, seed-build-script]
190190
permissions:
191191
id-token: write
192192
contents: write
@@ -210,6 +210,12 @@ jobs:
210210
run: |
211211
files=$(ls dist/${BIN_NAME}*)
212212
gh release upload "${GIT_REF}" ${files}
213+
- name: Restore build script seed
214+
if: startsWith(github.ref_name, 'clang-tools-manager/v')
215+
uses: actions/download-artifact@v8
216+
with:
217+
name: ${{ needs.seed-build-script.outputs.artifact-name }}
218+
path: clang-tools-manager
213219
- name: Establish provenance
214220
id: auth
215221
uses: rust-lang/crates-io-auth-action@bbd81622f20ce9e2dd9622e3218b975523e45bbe # v1.0.4

clang-tools-manager/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ homepage.workspace = true
1010
license.workspace = true
1111
edition.workspace = true
1212
repository.workspace = true
13+
# Include a pre-seeded versions.json file in package published to crates.io.
14+
# This is invalidated after 24 hours, but it allows docs.rs builds to succeed.
15+
include = ["versions.json", "src/**", "Cargo.toml", "README.md", "build.rs", "tests/**"]
1316

1417
[[bin]]
1518
path = "src/main.rs"
@@ -21,7 +24,7 @@ required-features = ["bin"]
2124
[dependencies]
2225
anyhow = { workspace = true, optional = true }
2326
blake2 = "0.10.6"
24-
clap = { workspace = true, features = ["derive", "env"], optional = true }
27+
clap = { workspace = true, features = ["derive"], optional = true }
2528
colored = { workspace = true, optional = true }
2629
directories = "6.0.0"
2730
log = { workspace = true }

clang-tools-manager/build.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::{fs, path::PathBuf, time::Duration};
1+
use std::{
2+
fs,
3+
io::Write,
4+
path::PathBuf,
5+
time::{Duration, SystemTime},
6+
};
27

38
use reqwest::blocking::ClientBuilder;
49

@@ -11,11 +16,19 @@ struct VersionInfo {
1116
}
1217
fn main() {
1318
let pre_seed = PathBuf::from("versions.json");
14-
let version_info_str = if pre_seed.exists() {
19+
let version_info_str = if pre_seed.exists()
20+
&& let Ok(metadata) = fs::metadata(&pre_seed)
21+
&& metadata.modified().is_ok_and(|d| {
22+
SystemTime::now()
23+
.duration_since(d)
24+
// repopulate cached file in case of error
25+
.unwrap_or(Duration::from_hours(25))
26+
< Duration::from_hours(24)
27+
}) {
1528
println!("cargo:warning=Using pre-seeded version info from {pre_seed:?}");
1629
fs::read_to_string(&pre_seed).unwrap()
1730
} else {
18-
ClientBuilder::new()
31+
let versions = ClientBuilder::new()
1932
.user_agent("cpp-linter-rs/clang-tools-manager")
2033
.timeout(Duration::from_secs(30))
2134
.build()
@@ -24,7 +37,16 @@ fn main() {
2437
.send()
2538
.unwrap()
2639
.text()
27-
.unwrap()
40+
.unwrap();
41+
let mut cached_seed = fs::OpenOptions::new()
42+
.write(true)
43+
.create(true)
44+
.truncate(true)
45+
.open(&pre_seed)
46+
.unwrap();
47+
cached_seed.write_all(versions.as_bytes()).unwrap();
48+
cached_seed.set_modified(SystemTime::now()).unwrap();
49+
versions
2850
};
2951
let version_info: VersionInfo = serde_json::from_str(&version_info_str).unwrap();
3052
let (min_ver, max_ver) = {

0 commit comments

Comments
 (0)