From 7815dad712449c46422b5ad0b5abe51959b955ec Mon Sep 17 00:00:00 2001 From: Yohe-Am <56622350+Yohe-Am@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:05:15 +0300 Subject: [PATCH] refactor: use shadow_rs for versions (#523) ### Describe your change Replaces the `common::get_version` util function with the `PKG_VERSION` variables extracted from `shadow_rs`. If you know of any other opportunities where we can replace things from `shadow_rs`, we can add them in this PR as wel.. ### Motivation and context `shadow_rs` makes the old function redundant ### Checklist - [ ] The change come with new or modified tests - [ ] Hard-to-understand functions have explanatory comments - [ ] End-user documentation is updated to reflect the change --- Cargo.lock | 2 +- Cargo.toml | 1 + libs/common/src/lib.rs | 4 ---- meta-cli/Cargo.toml | 4 ++-- meta-cli/src/cli/doctor.rs | 3 +-- meta-cli/src/cli/upgrade.rs | 6 +++--- meta-cli/src/deploy/actors/pusher.rs | 2 +- meta-cli/src/main.rs | 2 +- typegate/engine/Cargo.toml | 4 ++++ typegate/engine/build.rs | 9 +++++++++ typegate/engine/src/lib.rs | 5 ++++- typegate/standalone/Cargo.toml | 1 - typegate/standalone/src/main.rs | 2 +- 13 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 typegate/engine/build.rs diff --git a/Cargo.lock b/Cargo.lock index 10fd286bd5..76b5112ae0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10781,7 +10781,6 @@ name = "typegate" version = "0.3.1" dependencies = [ "colored", - "common", "env_logger", "envconfig", "log", @@ -10815,6 +10814,7 @@ dependencies = [ "request-handlers", "schema-connector", "schema-core", + "shadow-rs", "tap", "tempfile", "temporal-client", diff --git a/Cargo.toml b/Cargo.toml index b1685218ff..88fa54b812 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,3 +38,4 @@ swc_ecma_codegen = "=0.144.1" swc_ecma_parser = "=0.139.0" swc_ecma_transforms_typescript = "=0.182.3" swc_ecma_visit = "=0.95.0" +shadow-rs = "0.24" diff --git a/libs/common/src/lib.rs b/libs/common/src/lib.rs index 87b57a2c22..6148d1cce0 100644 --- a/libs/common/src/lib.rs +++ b/libs/common/src/lib.rs @@ -3,7 +3,3 @@ pub mod archive; pub mod typegraph; - -pub fn get_version() -> &'static str { - env!("CARGO_PKG_VERSION") -} diff --git a/meta-cli/Cargo.toml b/meta-cli/Cargo.toml index 429fcabfcb..389baf8c50 100644 --- a/meta-cli/Cargo.toml +++ b/meta-cli/Cargo.toml @@ -81,7 +81,7 @@ actix = "0.13.1" ctrlc = "3.4.1" derive_more = "0.99.17" typegate_engine = { workspace = true, optional = true } -shadow-rs = "0.24" +shadow-rs.workspace = true env_logger.workspace = true clap = { workspace = true, features = ["derive", "env"] } clap_complete.workspace = true @@ -92,4 +92,4 @@ insta = "1.34.0" project-root = "0.2.2" [build-dependencies] -shadow-rs = "0.24" +shadow-rs.workspace = true diff --git a/meta-cli/src/cli/doctor.rs b/meta-cli/src/cli/doctor.rs index dac14c7e06..1dc5da4e2e 100644 --- a/meta-cli/src/cli/doctor.rs +++ b/meta-cli/src/cli/doctor.rs @@ -16,7 +16,6 @@ use anyhow::Result; use async_trait::async_trait; use clap::Parser; use colored::Colorize; -use common::get_version; use std::process::Command; #[derive(Parser, Debug)] @@ -51,7 +50,7 @@ impl Action for Doctor { "global config", &GlobalConfig::default_path()?.display().to_string(), ); - ui::cols(c, "meta-cli version", get_version()); + ui::cols(c, "meta-cli version", crate::build::PKG_VERSION); match shell(vec!["docker", "--version"]) { Ok(s) => { ui::cols(c, "docker version", &s); diff --git a/meta-cli/src/cli/upgrade.rs b/meta-cli/src/cli/upgrade.rs index 97d6bcf8cc..67cb1b3c0c 100644 --- a/meta-cli/src/cli/upgrade.rs +++ b/meta-cli/src/cli/upgrade.rs @@ -4,11 +4,11 @@ use crate::global_config::GlobalConfig; use super::{Action, GenArgs}; +use crate::build; use anyhow::{Ok, Result}; use async_trait::async_trait; use chrono::{Duration, Utc}; use clap::Parser; -use common::get_version; use self_update::{backends::github::Update, update::UpdateStatus}; use semver::Version; @@ -35,7 +35,7 @@ impl Action for Upgrade { .repo_name("metatype") .bin_name("meta") .show_download_progress(true) - .current_version(get_version()) + .current_version(build::PKG_VERSION) .no_confirm(opts.yes); if let Some(version) = opts.version { @@ -64,7 +64,7 @@ pub async fn upgrade_check() -> Result<()> { let mut local_config = GlobalConfig::load(&config_path).await?; if local_config.update_check + Duration::days(1) < Utc::now() { - let current_version = get_version(); + let current_version = build::PKG_VERSION; let latest = tokio::task::spawn_blocking(move || { let update = Update::configure() .repo_owner("metatypedev") diff --git a/meta-cli/src/deploy/actors/pusher.rs b/meta-cli/src/deploy/actors/pusher.rs index 8a28624832..41de49eaa4 100644 --- a/meta-cli/src/deploy/actors/pusher.rs +++ b/meta-cli/src/deploy/actors/pusher.rs @@ -102,7 +102,7 @@ impl PusherActor { Ok(serde_json::json!({ "tg": serde_json::to_string(&tg)?, "secrets": serde_json::to_string(secrets)?, - "cliVersion": common::get_version() + "cliVersion": crate::build::PKG_VERSION, })) } diff --git a/meta-cli/src/main.rs b/meta-cli/src/main.rs index e0c0582136..c4c97fe58d 100644 --- a/meta-cli/src/main.rs +++ b/meta-cli/src/main.rs @@ -49,7 +49,7 @@ fn main() -> Result<()> { }; if args.version { - println!("meta {}", common::get_version()); + println!("meta {}", build::PKG_VERSION); return Ok(()); } diff --git a/typegate/engine/Cargo.toml b/typegate/engine/Cargo.toml index 8ba15b44bc..0bd0e007c9 100644 --- a/typegate/engine/Cargo.toml +++ b/typegate/engine/Cargo.toml @@ -36,6 +36,10 @@ typescript.workspace = true wasmedge-sdk.workspace = true wasmedge-sys.workspace = true wasmedge-types.workspace = true +shadow-rs.workspace = true [dev-dependencies] env_logger.workspace = true + +[build-dependencies] +shadow-rs.workspace = true diff --git a/typegate/engine/build.rs b/typegate/engine/build.rs new file mode 100644 index 0000000000..dba61fbd72 --- /dev/null +++ b/typegate/engine/build.rs @@ -0,0 +1,9 @@ +// Copyright Metatype OÜ, licensed under the Elastic License 2.0. +// SPDX-License-Identifier: Elastic-2.0 + +// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. +// SPDX-License-Identifier: MPL-2.0 + +fn main() -> shadow_rs::SdResult<()> { + shadow_rs::new() +} diff --git a/typegate/engine/src/lib.rs b/typegate/engine/src/lib.rs index 49a48bff10..c89f95872a 100644 --- a/typegate/engine/src/lib.rs +++ b/typegate/engine/src/lib.rs @@ -28,6 +28,9 @@ pub use deno_core::{resolve_url, resolve_url_or_path}; pub use ext::extensions; #[rustfmt::skip] use deno_core as deno_core; // necessary for re-exported macros to work +use shadow_rs::shadow; + +shadow!(build); use crate::interlude::*; @@ -132,7 +135,7 @@ pub async fn launch_typegate_deno( #[deno_core::op2] #[string] fn op_get_version() -> &'static str { - common::get_version() + build::PKG_VERSION } #[cfg(test)] diff --git a/typegate/standalone/Cargo.toml b/typegate/standalone/Cargo.toml index 3fbc09cd3b..2afa58cbf3 100644 --- a/typegate/standalone/Cargo.toml +++ b/typegate/standalone/Cargo.toml @@ -10,4 +10,3 @@ sentry = { version = "0.31.8", features = ["anyhow"] } env_logger.workspace = true log = "0.4.20" typegate_engine.workspace = true -common.workspace = true diff --git a/typegate/standalone/src/main.rs b/typegate/standalone/src/main.rs index 3bc65ea7d0..c4d2a46c03 100644 --- a/typegate/standalone/src/main.rs +++ b/typegate/standalone/src/main.rs @@ -17,7 +17,7 @@ pub fn init_sentry(config: &Config) -> sentry::ClientInitGuard { sentry::init(( config.sentry_dsn.clone(), sentry::ClientOptions { - release: Some(Cow::from(common::get_version())), + release: Some(Cow::from(typegate_engine::build::PKG_VERSION)), environment: Some(Cow::from(env)), sample_rate: config.sentry_sample_rate, traces_sample_rate: config.sentry_traces_sample_rate,