diff --git a/.github/workflows/build-cli.yml b/.github/workflows/build-cli.yml index 6856c5e3..66fd335a 100644 --- a/.github/workflows/build-cli.yml +++ b/.github/workflows/build-cli.yml @@ -6,6 +6,9 @@ on: - main pull_request: +env: + WEB5_SDK_LOG_LEVEL: debug + jobs: build_aarch64_apple_darwin: runs-on: macos-latest diff --git a/.gitignore b/.gitignore index eb86c4ed..69418164 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,7 @@ Cargo.lock # Do not put native binaries in source control bound/kt/src/main/resources/*.dylib -bound/kt/src/main/resources/*.so \ No newline at end of file +bound/kt/src/main/resources/*.so + +# Created by crate on build +crates/web5/src/resources/git_sha.txt \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index f7b9cef4..513443f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "crates/web5", "crates/web5_cli", + "crates/web5_proc_macros", "bindings/web5_uniffi", "bindings/web5_uniffi_wrapper", ] diff --git a/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile b/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile index aa8a30a6..202b6873 100644 --- a/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile +++ b/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_gnu/Dockerfile @@ -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 diff --git a/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile b/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile index 8a2df5a0..6c2bc3d2 100644 --- a/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile +++ b/bindings/web5_uniffi/libtargets/x86_64_unknown_linux_musl/Dockerfile @@ -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 diff --git a/bindings/web5_uniffi_wrapper/src/errors.rs b/bindings/web5_uniffi_wrapper/src/errors.rs index db489676..d81f2bd4 100644 --- a/bindings/web5_uniffi_wrapper/src/errors.rs +++ b/bindings/web5_uniffi_wrapper/src/errors.rs @@ -207,4 +207,3 @@ impl From for Web5Error { } pub type Result = std::result::Result; - diff --git a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt index a31fb046..3e071928 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt @@ -6,7 +6,7 @@ internal val logLevel = System.getenv("WEB5_SDK_LOG_LEVEL")?.lowercase() internal fun log(message: String) { if (logLevel == "debug") { - println("web5 sdk SystemArchitecture $message") + println("web5 sdk SystemArchitecture: $message") } } diff --git a/crates/web5/Cargo.toml b/crates/web5/Cargo.toml index 7c27cb07..ec80bddb 100644 --- a/crates/web5/Cargo.toml +++ b/crates/web5/Cargo.toml @@ -26,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] diff --git a/crates/web5/build.rs b/crates/web5/build.rs new file mode 100644 index 00000000..7cadc327 --- /dev/null +++ b/crates/web5/build.rs @@ -0,0 +1,18 @@ +use std::{fs, process::Command}; + +fn main() { + // Execute the `git rev-parse HEAD` command to get the current commit hash + let output = Command::new("git") + .args(["rev-parse", "HEAD"]) + .output() + .expect("Failed to execute git command"); + + // Convert the output to a string + let git_hash = String::from_utf8(output.stdout).expect("Invalid UTF-8 sequence"); + + // Remove the newline character from the commit hash + let git_hash_trimmed = git_hash.trim(); + + let dest_path = format!("src/resources/git_sha.txt"); + fs::write(dest_path, git_hash_trimmed).expect("Unable to write file"); +} diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index e0b15c9a..a04609b4 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -1,3 +1,5 @@ +use std::{env, sync::LazyLock}; + pub mod credentials; pub mod crypto; pub mod dids; @@ -9,4 +11,43 @@ pub mod rfc3339; #[cfg(test)] mod test_helpers; #[cfg(test)] -mod test_vectors; \ No newline at end of file +mod test_vectors; + +pub const GIT_COMMIT_HASH: &str = include_str!("resources/git_sha.txt"); + +// TODO: https://github.com/TBD54566975/web5-rs/issues/287 +#[allow(dead_code)] +static LOG_LEVEL: LazyLock> = 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.to_lowercase() == "debug" { + println!("[DEBUG] {}: {}", $crate::GIT_COMMIT_HASH, format!($msg, $($arg)*)); + } + } + }; + ($closure:expr) => { + if let Some(ref level) = *$crate::LOG_LEVEL { + if level.to_lowercase() == "debug" { + let msg = $closure(); + println!("[DEBUG] {}: {}", $crate::GIT_COMMIT_HASH, msg); + } + } + }; + } +} + +#[cfg(test)] +mod test { + use crate::log_dbg; + + #[test] + fn can_log_dbg() { + log_dbg!("Log debugging without arguments"); + log_dbg!("Log debugging with arguments {}", "Some value"); + log_dbg!(|| { 2 + 2 }); + } +} diff --git a/crates/web5/src/resources/.gitkeep b/crates/web5/src/resources/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile b/crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile index 62b68e23..bc7822f6 100644 --- a/crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile +++ b/crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile @@ -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 diff --git a/crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile b/crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile index 011cb040..14c6f2bf 100644 --- a/crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile +++ b/crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile @@ -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 diff --git a/crates/web5_proc_macros/Cargo.toml b/crates/web5_proc_macros/Cargo.toml new file mode 100644 index 00000000..07c8bb5c --- /dev/null +++ b/crates/web5_proc_macros/Cargo.toml @@ -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 \ No newline at end of file diff --git a/crates/web5_proc_macros/src/lib.rs b/crates/web5_proc_macros/src/lib.rs new file mode 100644 index 00000000..f555c8e2 --- /dev/null +++ b/crates/web5_proc_macros/src/lib.rs @@ -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) +}