Skip to content

Commit e9129ea

Browse files
psteinroeclaude
andauthored
refactor: download vendor dir on build if not available (#633)
automates download and build of `vendor/` files without loosing reproducibility by comparing the commit SHA before running. --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6762a67 commit e9129ea

File tree

11 files changed

+2573
-790
lines changed

11 files changed

+2573
-790
lines changed

Cargo.lock

Lines changed: 790 additions & 787 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pgls_pglinter/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ pgls_console.workspace = true
2626
pgls_schema_cache = { workspace = true, features = ["db"] }
2727
pgls_test_utils.workspace = true
2828

29+
[build-dependencies]
30+
ureq = "2.9"
31+
2932
[lib]
3033
doctest = false

crates/pgls_pglinter/build.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::fs;
2+
use std::io::Write;
3+
use std::path::Path;
4+
5+
const EXPECTED_COMMIT: &str = "main";
6+
const REPO: &str = "pmpetit/pglinter";
7+
8+
fn main() {
9+
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
10+
let vendor_dir = Path::new(&manifest_dir).join("vendor");
11+
let sql_dir = vendor_dir.join("sql");
12+
let sha_file = vendor_dir.join("COMMIT_SHA.txt");
13+
let rules_file = sql_dir.join("rules.sql");
14+
15+
// Check if vendor files exist and SHA matches
16+
let needs_download = if sha_file.exists() && rules_file.exists() {
17+
let current_sha = fs::read_to_string(&sha_file).unwrap_or_default();
18+
current_sha.trim() != EXPECTED_COMMIT
19+
} else {
20+
true
21+
};
22+
23+
if needs_download {
24+
println!("cargo:warning=Downloading pglinter vendor files...");
25+
26+
// Create directories
27+
fs::create_dir_all(&sql_dir).expect("Failed to create vendor/sql directory");
28+
29+
// Download rules.sql using ureq (blocking HTTP client)
30+
let url =
31+
format!("https://raw.githubusercontent.com/{REPO}/{EXPECTED_COMMIT}/sql/rules.sql");
32+
33+
let response = ureq::get(&url)
34+
.call()
35+
.expect("Failed to download rules.sql");
36+
37+
let content = response.into_string().expect("Failed to read response");
38+
39+
let mut file = fs::File::create(&rules_file).expect("Failed to create rules.sql");
40+
file.write_all(content.as_bytes())
41+
.expect("Failed to write rules.sql");
42+
43+
// Write commit SHA
44+
fs::write(&sha_file, EXPECTED_COMMIT).expect("Failed to write COMMIT_SHA.txt");
45+
46+
println!("cargo:warning=Downloaded pglinter vendor files successfully");
47+
}
48+
49+
// Tell cargo to rerun if SHA changes
50+
println!("cargo:rerun-if-changed=vendor/COMMIT_SHA.txt");
51+
}

crates/pgls_pglinter/vendor/.gitkeep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main

0 commit comments

Comments
 (0)