Skip to content

Commit

Permalink
Add web5_proc_macros with git_sha proc macro (#290)
Browse files Browse the repository at this point in the history
* Add web5_proc_macros with git_sha proc macro

* Use short SHA, case insensitive log level, remove kt code

* PR review

* Fix linting, fix linux builds

* Fix CLI build
  • Loading branch information
KendallWeihe authored Aug 19, 2024
1 parent 0b11d23 commit 2af9bdd
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 64 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"crates/web5",
"crates/web5_cli",
"crates/web5_proc_macros",
"bindings/web5_uniffi",
"bindings/web5_uniffi_wrapper",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli
COPY crates/web5_proc_macros ./crates/web5_proc_macros

# Execute the build
RUN cargo build --release --package web5_uniffi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli
COPY crates/web5_proc_macros ./crates/web5_proc_macros

# Build the static lib (override the lib type)
RUN sed -i 's/crate-type = \["cdylib"\]/crate-type = \["staticlib"\]/' bindings/web5_uniffi/Cargo.toml
Expand Down
23 changes: 0 additions & 23 deletions bound/kt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -539,29 +539,6 @@
<jvmTarget>${kotlin.jvm.target}</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>get-git-commit-id</id>
<goals>
<goal>exec</goal>
</goals>
<phase>initialize</phase>
<configuration>
<executable>git</executable>
<arguments>
<argument>rev-parse</argument>
<argument>--short</argument>
<argument>HEAD</argument>
</arguments>
<outputFile>${project.build.directory}/git-commit-id.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
11 changes: 1 addition & 10 deletions bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@ package web5.sdk.rust
import java.io.File

internal val logLevel = System.getenv("WEB5_SDK_LOG_LEVEL")?.lowercase()
internal val gitCommitHash = run {
val commitFile = File("target/git-commit-id.txt")
if (commitFile.exists()) {
commitFile.readText().trim()
} else {
println("Git commit hash not found.")
""
}
}

internal fun log(message: String) {
if (logLevel == "debug") {
println("web5 sdk SystemArchitecture $gitCommitHash: $message")
println("web5 sdk SystemArchitecture $message")
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/web5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ josekit = "0.8.6"
jsonpath-rust = "0.5.1"
jsonschema = { version = "0.18.0", default-features = false }
k256 = { version = "0.13.3", features = ["ecdsa", "jwk"] }
lazy_static = "1.5.0"
tokio = "1.38.0"
rand = { workspace = true }
regex = "1.10.4"
Expand All @@ -27,6 +26,7 @@ simple-dns = "0.7.0"
thiserror = { workspace = true }
url = "2.5.0"
uuid = { workspace = true }
web5_proc_macros = { path = "../web5_proc_macros" }
zbase32 = "0.1.2"

[dev-dependencies]
Expand Down
18 changes: 0 additions & 18 deletions crates/web5/build.rs

This file was deleted.

19 changes: 7 additions & 12 deletions crates/web5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,30 @@ pub mod errors;
pub mod json;
pub mod rfc3339;

#[cfg(test)]
mod test_vectors;
#[cfg(test)]
mod test_helpers;

pub const GIT_COMMIT_HASH: &str = env!("WEB5_GIT_COMMIT_HASH");
#[cfg(test)]
mod test_vectors;

// TODO: https://github.com/TBD54566975/web5-rs/issues/287
#[allow(dead_code)]
static LOG_LEVEL: LazyLock<Option<String>> = LazyLock::new(|| {
// Default log level if the environment variable is not set
env::var("LOG_LEVEL").ok()
});
static LOG_LEVEL: LazyLock<Option<String>> = LazyLock::new(|| env::var("WEB5_SDK_LOG_LEVEL").ok());

pub(crate) mod logging {
#[macro_export]
macro_rules! log_dbg {
($msg:literal $(, $arg:tt)*) => {
if let Some(ref level) = *$crate::LOG_LEVEL {
if level == "DEBUG" {
println!("[DEBUG] {}:{}", env!("WEB5_GIT_COMMIT_HASH"), format!($msg, $($arg)*));
if level.to_lowercase() == "debug" {
println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), format!($msg, $($arg)*));
}
}
};
($closure:expr) => {
if let Some(ref level) = *$crate::LOG_LEVEL {
if level == "DEBUG" {
if level.to_lowercase() == "debug" {
let msg = $closure();
println!("[DEBUG] {}:{}", env!("WEB5_GIT_COMMIT_HASH"), msg);
println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), msg);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli
COPY crates/web5_proc_macros ./crates/web5_proc_macros

# Execute the build
RUN cargo build --release --package web5_cli
Expand Down
1 change: 1 addition & 0 deletions crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli
COPY crates/web5_proc_macros ./crates/web5_proc_macros

RUN cargo build --release --package web5_cli

Expand Down
15 changes: 15 additions & 0 deletions crates/web5_proc_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "web5_proc_macros"
version = "0.1.0"
edition = "2021"
homepage.workspace = true
repository.workspace = true
license-file.workspace = true

[dependencies]
proc-macro2 = "1.0.86"
quote = "1.0.36"
syn = "2.0.75"

[lib]
proc-macro = true
23 changes: 23 additions & 0 deletions crates/web5_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use std::process::Command;

#[proc_macro]
pub fn git_sha(_input: TokenStream) -> TokenStream {
let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.expect("Failed to execute git command");

let git_hash = String::from_utf8(output.stdout)
.expect("Invalid UTF-8 sequence")
.trim()
.to_string();

let expanded = quote! {
#git_hash
};

TokenStream::from(expanded)
}

0 comments on commit 2af9bdd

Please sign in to comment.