From d7558e4f655782f2c4cd1aab88e3517535ad63a1 Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Thu, 15 Aug 2024 10:40:03 -0700 Subject: [PATCH 01/14] Add debug logging --- .github/workflows/build-cli.yml | 3 +++ .github/workflows/ci.yml | 1 + crates/web5/Cargo.toml | 1 + crates/web5/src/lib.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 32 insertions(+) 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/.github/workflows/ci.yml b/.github/workflows/ci.yml index e504e1bf..6469e664 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,7 @@ env: CARGO_TERM_COLOR: always # Make sure CI fails on all warnings, including Clippy lints RUSTFLAGS: "-Dwarnings" + WEB5_SDK_LOG_LEVEL: debug jobs: build: diff --git a/crates/web5/Cargo.toml b/crates/web5/Cargo.toml index 7c27cb07..505837ae 100644 --- a/crates/web5/Cargo.toml +++ b/crates/web5/Cargo.toml @@ -15,6 +15,7 @@ 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" diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index f4d67188..ccbb8b7d 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -4,3 +4,30 @@ pub mod dids; #[cfg(test)] mod test_helpers; + +lazy_static::lazy_static! { + pub(crate) static ref LOG_LEVEL: Option = { + std::env::var("WEB5_SDK_LOG_LEVEL").ok() + }; +} + +pub(crate) mod logging { + #[macro_export] + macro_rules! log_dbg { + ($msg:expr, $($arg:tt)*) => { + if let Some(ref level) = *$crate::LOG_LEVEL { + if level == "DEBUG" { + println!("[DEBUG] {}", format!($msg, $($arg)*)); + } + } + }; + ($closure:expr) => { + if let Some(ref level) = *$crate::LOG_LEVEL { + if level == "DEBUG" { + let msg = $closure(); + println!("[DEBUG] {}", msg); + } + } + }; + } +} From 523abec321cc1b8f7c3eea6ec049ffb841457dde Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Thu, 15 Aug 2024 12:27:40 -0700 Subject: [PATCH 02/14] PR comments --- Justfile | 2 +- crates/web5/build.rs | 18 ++++++++++++++++++ crates/web5/src/lib.rs | 4 ++-- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 crates/web5/build.rs diff --git a/Justfile b/Justfile index 2b22e572..787a03d2 100644 --- a/Justfile +++ b/Justfile @@ -6,7 +6,7 @@ setup: source bin/activate-hermit git submodule update --init --recursive if [[ "$(cargo 2>&1)" == *"rustup could not choose a version of cargo to run"* ]]; then - rustup default 1.78.0 + rustup default 1.80.0 rustup target add aarch64-apple-darwin fi diff --git a/crates/web5/build.rs b/crates/web5/build.rs new file mode 100644 index 00000000..27511c1d --- /dev/null +++ b/crates/web5/build.rs @@ -0,0 +1,18 @@ +use std::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(); + + // Pass the commit hash to the compiler as an environment variable + println!("cargo:rustc-env=WEB5_GIT_COMMIT_HASH={}", git_hash_trimmed); +} \ No newline at end of file diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index ccbb8b7d..6bd5888b 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -17,7 +17,7 @@ pub(crate) mod logging { ($msg:expr, $($arg:tt)*) => { if let Some(ref level) = *$crate::LOG_LEVEL { if level == "DEBUG" { - println!("[DEBUG] {}", format!($msg, $($arg)*)); + println!("[DEBUG] {}:{}", env!("GIT_COMMIT_HASH"), format!($msg, $($arg)*)); } } }; @@ -25,7 +25,7 @@ pub(crate) mod logging { if let Some(ref level) = *$crate::LOG_LEVEL { if level == "DEBUG" { let msg = $closure(); - println!("[DEBUG] {}", msg); + println!("[DEBUG] {}:{}", env!("GIT_COMMIT_HASH"), msg); } } }; From 8e4f6f1660aed814bd1034d951104337e83b3df8 Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Thu, 15 Aug 2024 13:22:29 -0700 Subject: [PATCH 03/14] Lint --- crates/web5/build.rs | 4 ++-- crates/web5/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/web5/build.rs b/crates/web5/build.rs index 27511c1d..9d322b71 100644 --- a/crates/web5/build.rs +++ b/crates/web5/build.rs @@ -3,7 +3,7 @@ use std::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"]) + .args(["rev-parse", "HEAD"]) .output() .expect("Failed to execute git command"); @@ -15,4 +15,4 @@ fn main() { // Pass the commit hash to the compiler as an environment variable println!("cargo:rustc-env=WEB5_GIT_COMMIT_HASH={}", git_hash_trimmed); -} \ No newline at end of file +} diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 6bd5888b..2e8c28ad 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -12,8 +12,8 @@ lazy_static::lazy_static! { } pub(crate) mod logging { - #[macro_export] - macro_rules! log_dbg { + #[macro_export] + macro_rules! log_dbg { ($msg:expr, $($arg:tt)*) => { if let Some(ref level) = *$crate::LOG_LEVEL { if level == "DEBUG" { From 4c57d3ca78b38ac564996832d809290349b9bdef Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Thu, 15 Aug 2024 17:47:03 -0700 Subject: [PATCH 04/14] Add commit hash to kotlin debug logging --- bound/kt/pom.xml | 25 +++++++++++++++++++ .../main/kotlin/web5/sdk/rust/SystemTarget.kt | 10 +++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/bound/kt/pom.xml b/bound/kt/pom.xml index 3dae4668..52e70727 100644 --- a/bound/kt/pom.xml +++ b/bound/kt/pom.xml @@ -106,6 +106,31 @@ + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + + get-git-commit-id + + exec + + + initialize + + + git + + rev-parse + --short + HEAD + + ${project.build.directory}/git-commit-id.txt + + + + 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 343700af..fa28fdec 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt @@ -5,13 +5,21 @@ import java.io.File object SystemTarget { @Volatile private var isSet = false + private var gitCommitHash = "" fun set() { val logLevel = System.getenv("WEB5_SDK_LOG_LEVEL")?.lowercase() + val commitFile = File("target/git-commit-id.txt") + if (commitFile.exists()) { + gitCommitHash = commitFile.readText().trim() + } else { + println("Git commit hash not found.") + } + fun log(message: String) { if (logLevel == "debug") { - println("web5 sdk SystemArchitecture $message") + println("web5 sdk SystemArchitecture $gitCommitHash: $message") } } From e4155d758d6590705b21d6bf7053e7f1d086960d Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Fri, 16 Aug 2024 17:11:28 -0700 Subject: [PATCH 05/14] Fix and test log_dbg --- crates/web5/src/lib.rs | 59 +++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 2e8c28ad..b4a4e52c 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; @@ -5,29 +7,44 @@ pub mod dids; #[cfg(test)] mod test_helpers; -lazy_static::lazy_static! { - pub(crate) static ref LOG_LEVEL: Option = { - std::env::var("WEB5_SDK_LOG_LEVEL").ok() - }; -} +pub const GIT_COMMIT_HASH: &str = env!("WEB5_GIT_COMMIT_HASH"); + +// TODO: https://github.com/TBD54566975/web5-rs/issues/287 +#[allow(dead_code)] +static LOG_LEVEL: LazyLock> = LazyLock::new(|| { + // Default log level if the environment variable is not set + env::var("LOG_LEVEL").ok() +}); pub(crate) mod logging { #[macro_export] macro_rules! log_dbg { - ($msg:expr, $($arg:tt)*) => { - if let Some(ref level) = *$crate::LOG_LEVEL { - if level == "DEBUG" { - println!("[DEBUG] {}:{}", env!("GIT_COMMIT_HASH"), format!($msg, $($arg)*)); - } - } - }; - ($closure:expr) => { - if let Some(ref level) = *$crate::LOG_LEVEL { - if level == "DEBUG" { - let msg = $closure(); - println!("[DEBUG] {}:{}", env!("GIT_COMMIT_HASH"), msg); - } - } - }; - } + ($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)*)); + } + } + }; + ($closure:expr) => { + if let Some(ref level) = *$crate::LOG_LEVEL { + if level == "DEBUG" { + let msg = $closure(); + println!("[DEBUG] {}:{}", env!("WEB5_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 }); + } } From e1aac22b0799191d1794e567bdb058266e710c55 Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Fri, 16 Aug 2024 17:38:49 -0700 Subject: [PATCH 06/14] Remove stuff I didnt mean to put there --- bound/kt/pom.xml | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/bound/kt/pom.xml b/bound/kt/pom.xml index be2d5be5..aa09b19a 100644 --- a/bound/kt/pom.xml +++ b/bound/kt/pom.xml @@ -539,24 +539,6 @@ ${kotlin.jvm.target} - - org.apache.maven.plugins - maven-compiler-plugin - - 9 - 9 - - - - org.apache.maven.plugins - maven-surefire-plugin - 3.2.5 - - - *Test - - - org.codehaus.mojo exec-maven-plugin From efa431051965d00a0588d172eda6a4eba61fbd8d Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Fri, 16 Aug 2024 17:44:56 -0700 Subject: [PATCH 07/14] Lint --- bindings/web5_uniffi_wrapper/src/errors.rs | 1 - crates/web5/src/lib.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) 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/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 2d398b57..dcfc1c45 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -51,4 +51,4 @@ mod test { log_dbg!("Log debugging with arguments {}", "Some value"); log_dbg!(|| { 2 + 2 }); } -} \ No newline at end of file +} From 0b11d23d099fe4d41837dabcc8ddd08d2292cdea Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Fri, 16 Aug 2024 17:49:41 -0700 Subject: [PATCH 08/14] Add back mod test_helpers --- crates/web5/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index dcfc1c45..0d5d96f8 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -10,6 +10,8 @@ pub mod rfc3339; #[cfg(test)] mod test_vectors; +#[cfg(test)] +mod test_helpers; pub const GIT_COMMIT_HASH: &str = env!("WEB5_GIT_COMMIT_HASH"); From 2af9bdd40a05f3f3b241b24fc330d502f3754b50 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 19 Aug 2024 16:30:03 -0400 Subject: [PATCH 09/14] Add web5_proc_macros with git_sha proc macro (#290) * 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 --- Cargo.toml | 1 + .../x86_64_unknown_linux_gnu/Dockerfile | 1 + .../x86_64_unknown_linux_musl/Dockerfile | 1 + bound/kt/pom.xml | 23 ------------------- .../main/kotlin/web5/sdk/rust/SystemTarget.kt | 11 +-------- crates/web5/Cargo.toml | 2 +- crates/web5/build.rs | 18 --------------- crates/web5/src/lib.rs | 19 ++++++--------- .../build/x86_64_unknown_linux_gnu/Dockerfile | 1 + .../x86_64_unknown_linux_musl/Dockerfile | 1 + crates/web5_proc_macros/Cargo.toml | 15 ++++++++++++ crates/web5_proc_macros/src/lib.rs | 23 +++++++++++++++++++ 12 files changed, 52 insertions(+), 64 deletions(-) delete mode 100644 crates/web5/build.rs create mode 100644 crates/web5_proc_macros/Cargo.toml create mode 100644 crates/web5_proc_macros/src/lib.rs 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/bound/kt/pom.xml b/bound/kt/pom.xml index 1c16b415..c271b81a 100644 --- a/bound/kt/pom.xml +++ b/bound/kt/pom.xml @@ -539,29 +539,6 @@ ${kotlin.jvm.target} - - org.codehaus.mojo - exec-maven-plugin - 3.0.0 - - - get-git-commit-id - - exec - - initialize - - git - - rev-parse - --short - HEAD - - ${project.build.directory}/git-commit-id.txt - - - - org.apache.maven.plugins maven-compiler-plugin 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 a26bcd74..a31fb046 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt @@ -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") } } diff --git a/crates/web5/Cargo.toml b/crates/web5/Cargo.toml index 505837ae..ec80bddb 100644 --- a/crates/web5/Cargo.toml +++ b/crates/web5/Cargo.toml @@ -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" @@ -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] diff --git a/crates/web5/build.rs b/crates/web5/build.rs deleted file mode 100644 index 9d322b71..00000000 --- a/crates/web5/build.rs +++ /dev/null @@ -1,18 +0,0 @@ -use std::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(); - - // Pass the commit hash to the compiler as an environment variable - println!("cargo:rustc-env=WEB5_GIT_COMMIT_HASH={}", git_hash_trimmed); -} diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 0d5d96f8..6088c1bd 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -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> = LazyLock::new(|| { - // Default log level if the environment variable is not set - env::var("LOG_LEVEL").ok() -}); +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 == "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); } } }; 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) +} From a3495395da5d3be20e8aac1466934cd5e62092bd Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Mon, 19 Aug 2024 16:39:21 -0700 Subject: [PATCH 10/14] Build resources file with git SHA --- crates/web5/build.rs | 5 ++++- crates/web5/src/lib.rs | 2 +- crates/web5/src/resources/.gitkeep | 0 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 crates/web5/src/resources/.gitkeep diff --git a/crates/web5/build.rs b/crates/web5/build.rs index 9d322b71..072463ea 100644 --- a/crates/web5/build.rs +++ b/crates/web5/build.rs @@ -1,4 +1,4 @@ -use std::process::Command; +use std::{fs, process::Command}; fn main() { // Execute the `git rev-parse HEAD` command to get the current commit hash @@ -13,6 +13,9 @@ fn main() { // 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"); + // Pass the commit hash to the compiler as an environment variable println!("cargo:rustc-env=WEB5_GIT_COMMIT_HASH={}", git_hash_trimmed); } diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 0d5d96f8..36739d48 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -13,7 +13,7 @@ mod test_vectors; #[cfg(test)] mod test_helpers; -pub const GIT_COMMIT_HASH: &str = env!("WEB5_GIT_COMMIT_HASH"); +pub const GIT_COMMIT_HASH: &str = include_str!("resources/git_sha.txt"); // TODO: https://github.com/TBD54566975/web5-rs/issues/287 #[allow(dead_code)] diff --git a/crates/web5/src/resources/.gitkeep b/crates/web5/src/resources/.gitkeep new file mode 100644 index 00000000..e69de29b From b76c67563276d6618e9db1cdbe7e2f1d9901f770 Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Mon, 19 Aug 2024 16:44:27 -0700 Subject: [PATCH 11/14] Remove git hash from kotlin logger --- bound/kt/pom.xml | 23 ------------------- .../main/kotlin/web5/sdk/rust/SystemTarget.kt | 11 +-------- 2 files changed, 1 insertion(+), 33 deletions(-) diff --git a/bound/kt/pom.xml b/bound/kt/pom.xml index 1c16b415..c271b81a 100644 --- a/bound/kt/pom.xml +++ b/bound/kt/pom.xml @@ -539,29 +539,6 @@ ${kotlin.jvm.target} - - org.codehaus.mojo - exec-maven-plugin - 3.0.0 - - - get-git-commit-id - - exec - - initialize - - git - - rev-parse - --short - HEAD - - ${project.build.directory}/git-commit-id.txt - - - - org.apache.maven.plugins maven-compiler-plugin 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 a26bcd74..3e071928 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt @@ -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") } } From cfa3cc615a0aae3bf23b737448256dfbc11e5cfc Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Mon, 19 Aug 2024 16:45:47 -0700 Subject: [PATCH 12/14] git ignore git sha resource --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 From 6c83a546a1646077e6ef95cf191389bb3e25ae15 Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Mon, 19 Aug 2024 16:46:51 -0700 Subject: [PATCH 13/14] Remove git sha compiler flag --- crates/web5/build.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/web5/build.rs b/crates/web5/build.rs index 072463ea..7cadc327 100644 --- a/crates/web5/build.rs +++ b/crates/web5/build.rs @@ -15,7 +15,4 @@ fn main() { let dest_path = format!("src/resources/git_sha.txt"); fs::write(dest_path, git_hash_trimmed).expect("Unable to write file"); - - // Pass the commit hash to the compiler as an environment variable - println!("cargo:rustc-env=WEB5_GIT_COMMIT_HASH={}", git_hash_trimmed); } From 6f28b1df91bc196d0bb483250ce780b22039a41c Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Mon, 19 Aug 2024 17:17:00 -0700 Subject: [PATCH 14/14] Use resource in log_dbg --- crates/web5/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 5c4c1c0c..a04609b4 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -25,7 +25,7 @@ pub(crate) mod logging { ($msg:literal $(, $arg:tt)*) => { if let Some(ref level) = *$crate::LOG_LEVEL { if level.to_lowercase() == "debug" { - println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), format!($msg, $($arg)*)); + println!("[DEBUG] {}: {}", $crate::GIT_COMMIT_HASH, format!($msg, $($arg)*)); } } }; @@ -33,7 +33,7 @@ pub(crate) mod logging { if let Some(ref level) = *$crate::LOG_LEVEL { if level.to_lowercase() == "debug" { let msg = $closure(); - println!("[DEBUG] {}: {}", web5_proc_macros::git_sha!(), msg); + println!("[DEBUG] {}: {}", $crate::GIT_COMMIT_HASH, msg); } } };