Skip to content

Commit 0b9816c

Browse files
committed
extract utils into subcrate
1 parent 9511510 commit 0b9816c

File tree

19 files changed

+395
-315
lines changed

19 files changed

+395
-315
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,27 @@ exclude = [
2222

2323
[workspace.dependencies]
2424
anyhow = { version = "1.0.42", features = ["backtrace"]}
25+
chrono = { version = "0.4.11", default-features = false, features = ["clock", "serde"] }
2526
derive_more = { version = "2.0.0", features = ["display", "deref", "from", "into", "from_str"] }
2627
opentelemetry = "0.31.0"
2728
opentelemetry-otlp = { version = "0.31.0", features = ["grpc-tonic", "metrics"] }
2829
opentelemetry-resource-detectors = "0.10.0"
2930
opentelemetry_sdk = { version = "0.31.0", features = ["rt-tokio"] }
31+
regex = "1"
32+
tokio = { version = "1.0", features = ["rt-multi-thread", "signal", "macros", "process", "sync"] }
3033
tracing = "0.1.37"
3134
url = { version = "2.1.1", features = ["serde"] }
3235

3336
[dependencies]
3437
docs_rs_env_vars = { path = "crates/lib/docs_rs_env_vars" }
3538
docs_rs_opentelemetry = { path = "crates/lib/docs_rs_opentelemetry" }
39+
docs_rs_utils = { path = "crates/lib/docs_rs_utils" }
3640
sentry = { version = "0.46.0", features = ["panic", "tracing", "tower-http", "anyhow", "backtrace"] }
3741
log = "0.4"
3842
tracing = { workspace = true }
3943
tracing-subscriber = { version = "0.3.20", default-features = false, features = ["ansi", "fmt", "json", "env-filter", "tracing-log"] }
4044
tracing-log = "0.2.0"
41-
regex = "1"
45+
regex = { workspace = true }
4246
clap = { version = "4.0.22", features = [ "derive" ] }
4347
crates-index = { version = "3.0.0", default-features = false, features = ["git", "git-https", "git-performance", "parallel"] }
4448
rayon = "1.6.1"
@@ -79,7 +83,7 @@ derive_builder = "0.20.2"
7983

8084
# Async
8185
async-compression = { version = "0.4.32", features = ["tokio", "bzip2", "zstd", "gzip"] }
82-
tokio = { version = "1.0", features = ["rt-multi-thread", "signal", "macros", "process", "sync"] }
86+
tokio = { workspace = true }
8387
tokio-util = { version = "0.7.15", default-features = false, features = ["io"] }
8488
tracing-futures= { version = "0.2.5", features = ["std-future", "futures-03"] }
8589
futures-util = "0.3.5"
@@ -113,7 +117,7 @@ walkdir = "2"
113117
phf = "0.13.1"
114118

115119
# Date and Time utilities
116-
chrono = { version = "0.4.11", default-features = false, features = ["clock", "serde"] }
120+
chrono = { workspace = true }
117121

118122
# Transitive dependencies we don't use directly but need to have specific versions of
119123
constant_time_eq = "0.4.2"

build.rs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ type ETagMap<'a> = phf_codegen::Map<'a, String>;
7373
fn main() -> Result<()> {
7474
let out_dir = env::var("OUT_DIR").context("missing OUT_DIR")?;
7575
let out_dir = Path::new(&out_dir);
76-
read_git_version()?;
7776

7877
let mut etag_map: ETagMap = ETagMap::new();
7978

@@ -94,54 +93,6 @@ fn main() -> Result<()> {
9493
Ok(())
9594
}
9695

97-
fn read_git_version() -> Result<()> {
98-
if let Ok(v) = env::var("GIT_SHA") {
99-
// first try to read an externally provided git SAH, e.g., from CI
100-
println!("cargo:rustc-env=GIT_SHA={v}");
101-
} else {
102-
// then try to read the git repo.
103-
let maybe_hash = get_git_hash()?;
104-
let git_hash = maybe_hash.as_deref().unwrap_or("???????");
105-
println!("cargo:rustc-env=GIT_SHA={git_hash}");
106-
}
107-
108-
println!(
109-
"cargo:rustc-env=BUILD_DATE={}",
110-
time::OffsetDateTime::now_utc().date(),
111-
);
112-
113-
Ok(())
114-
}
115-
116-
fn get_git_hash() -> Result<Option<String>> {
117-
use std::process::Command;
118-
119-
let output = Command::new("git")
120-
.args(["rev-parse", "--short", "HEAD"])
121-
.output();
122-
123-
match output {
124-
Ok(output) if output.status.success() => {
125-
let hash = String::from_utf8(output.stdout)?.trim().to_string();
126-
127-
// TODO: are these right?
128-
tracked::track(".git/HEAD")?;
129-
tracked::track(".git/index")?;
130-
131-
Ok(Some(hash))
132-
}
133-
Ok(output) => {
134-
let err = String::from_utf8_lossy(&output.stderr);
135-
eprintln!("failed to get git repo: {}", err.trim());
136-
Ok(None)
137-
}
138-
Err(err) => {
139-
eprintln!("failed to execute git: {err}");
140-
Ok(None)
141-
}
142-
}
143-
}
144-
14596
fn etag_from_path(path: impl AsRef<Path>) -> Result<String> {
14697
Ok(etag_from_content(std::fs::read(&path)?))
14798
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "docs_rs_utils"
3+
version = "0.1.0"
4+
edition = "2024"
5+
build = "build.rs"
6+
7+
[dependencies]
8+
anyhow = { workspace = true }
9+
chrono = { workspace = true }
10+
regex = { workspace = true }
11+
tokio = { workspace = true }
12+
tracing = { workspace = true }
13+
14+
[build-dependencies]
15+
anyhow = { workspace = true }
16+
chrono = { workspace = true }
17+
time = "0.3"
18+
tokio = { workspace = true }

crates/lib/docs_rs_utils/build.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use anyhow::Result;
2+
use std::env;
3+
4+
fn main() -> Result<()> {
5+
read_git_version()?;
6+
Ok(())
7+
}
8+
9+
fn read_git_version() -> Result<()> {
10+
if let Ok(v) = env::var("GIT_SHA") {
11+
// first try to read an externally provided git SAH, e.g., from CI
12+
println!("cargo:rustc-env=GIT_SHA={v}");
13+
} else {
14+
// then try to read the git repo.
15+
let maybe_hash = get_git_hash()?;
16+
let git_hash = maybe_hash.as_deref().unwrap_or("???????");
17+
println!("cargo:rustc-env=GIT_SHA={git_hash}");
18+
}
19+
20+
println!(
21+
"cargo:rustc-env=BUILD_DATE={}",
22+
time::OffsetDateTime::now_utc().date(),
23+
);
24+
25+
Ok(())
26+
}
27+
28+
fn get_git_hash() -> Result<Option<String>> {
29+
use std::process::Command;
30+
31+
let output = Command::new("git")
32+
.args(["rev-parse", "--short", "HEAD"])
33+
.output();
34+
35+
match output {
36+
Ok(output) if output.status.success() => {
37+
let hash = String::from_utf8(output.stdout)?.trim().to_string();
38+
39+
Ok(Some(hash))
40+
}
41+
Ok(output) => {
42+
let err = String::from_utf8_lossy(&output.stderr);
43+
eprintln!("failed to get git repo: {}", err.trim());
44+
Ok(None)
45+
}
46+
Err(err) => {
47+
eprintln!("failed to execute git: {err}");
48+
Ok(None)
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)