From 2a34a9ed28b6899fd01e6b6d6082629876a000f5 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 21 Jun 2023 16:25:33 +0800 Subject: [PATCH] Moved WasmerEnv into the wasmer-registry crate --- Cargo.lock | 17 +++++---- lib/cli/Cargo.toml | 2 +- lib/cli/src/commands/add.rs | 4 +- lib/cli/src/commands/config.rs | 4 +- lib/cli/src/commands/init.rs | 11 +++--- lib/cli/src/commands/login.rs | 4 +- lib/cli/src/commands/publish.rs | 3 +- lib/cli/src/commands/run.rs | 6 +-- lib/cli/src/commands/run/wasi.rs | 6 +-- lib/cli/src/commands/whoami.rs | 3 +- lib/cli/src/lib.rs | 3 -- lib/registry/Cargo.toml | 49 +++++++++++++------------ lib/registry/src/lib.rs | 3 +- lib/{cli => registry}/src/wasmer_env.rs | 37 +++++++++---------- 14 files changed, 71 insertions(+), 81 deletions(-) rename lib/{cli => registry}/src/wasmer_env.rs (75%) diff --git a/Cargo.lock b/Cargo.lock index d334035ea1e..c99406c8aaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -560,9 +560,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.3.4" +version = "4.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80672091db20273a15cf9fdd4e47ed43b5091ec9841bf4c6145c9dfbbcae09ed" +checksum = "2686c4115cb0810d9a984776e197823d08ec94f176549a89a9efded477c456dc" dependencies = [ "clap_builder", "clap_derive", @@ -575,15 +575,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1eef05769009513df2eb1c3b4613e7fad873a14c600ff025b08f250f59fee7de" dependencies = [ - "clap 4.3.4", + "clap 4.3.5", "log", ] [[package]] name = "clap_builder" -version = "4.3.4" +version = "4.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1458a1df40e1e2afebb7ab60ce55c1fa8f431146205aa5f4887e0b111c27636" +checksum = "2e53afce1efce6ed1f633cf0e57612fe51db54a1ee4fd8f8503d078fe02d69ae" dependencies = [ "anstream", "anstyle", @@ -5643,7 +5643,7 @@ dependencies = [ "cargo_metadata", "cfg-if 1.0.0", "chrono", - "clap 4.3.4", + "clap 4.3.5", "colored 2.0.0", "dialoguer", "dirs", @@ -5735,7 +5735,7 @@ dependencies = [ "atty", "bytesize", "cfg-if 1.0.0", - "clap 4.3.4", + "clap 4.3.5", "colored 2.0.0", "distance", "fern", @@ -5815,7 +5815,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "401220ca92419731ffb670b6d6f8442fc32b110f6e9bb5658c4cfa658c48ab32" dependencies = [ "anyhow", - "clap 4.3.4", + "clap 4.3.5", "clap-verbosity-flag", "comfy-table", "dialoguer", @@ -6025,6 +6025,7 @@ name = "wasmer-registry" version = "5.1.0" dependencies = [ "anyhow", + "clap 4.3.5", "console", "dirs", "filetime", diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index b0a04270fa7..7000c9bd9ff 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -39,7 +39,7 @@ wasmer-wasix-experimental-io-devices = { version = "0.8.0", path = "../wasi-expe wasmer-wast = { version = "=4.0.0-beta.3", path = "../../tests/lib/wast", optional = true } wasmer-cache = { version = "=4.0.0-beta.3", path = "../cache", features = ["blake3-pure"] } wasmer-types = { version = "=4.0.0-beta.3", path = "../types", features = ["enable-serde"] } -wasmer-registry = { version = "5.1.0", path = "../registry", features = ["build-package"] } +wasmer-registry = { version = "5.1.0", path = "../registry", features = ["build-package", "clap"] } wasmer-object = { version = "=4.0.0-beta.3", path = "../object", optional = true } virtual-fs = { version = "0.6.0", path = "../virtual-fs", default-features = false, features = ["host-fs"] } virtual-net = { version = "0.3.0", path = "../virtual-net" } diff --git a/lib/cli/src/commands/add.rs b/lib/cli/src/commands/add.rs index f21a4a1423f..ddf33ba476d 100644 --- a/lib/cli/src/commands/add.rs +++ b/lib/cli/src/commands/add.rs @@ -2,9 +2,7 @@ use std::process::{Command, Stdio}; use anyhow::{Context, Error}; use clap::Parser; -use wasmer_registry::{Bindings, ProgrammingLanguage}; - -use crate::WasmerEnv; +use wasmer_registry::{wasmer_env::WasmerEnv, Bindings, ProgrammingLanguage}; /// Add a Wasmer package's bindings to your application. #[derive(Debug, Parser)] diff --git a/lib/cli/src/commands/config.rs b/lib/cli/src/commands/config.rs index 85934f1dcdc..119d8b67c07 100644 --- a/lib/cli/src/commands/config.rs +++ b/lib/cli/src/commands/config.rs @@ -1,8 +1,8 @@ -use crate::{WasmerEnv, VERSION}; +use crate::VERSION; use anyhow::{Context, Result}; use clap::Parser; use std::str::ParseBoolError; -use wasmer_registry::WasmerConfig; +use wasmer_registry::{wasmer_env::WasmerEnv, WasmerConfig}; #[derive(Debug, Parser)] /// The options for the `wasmer config` subcommand: `wasmer config get --OPTION` or `wasmer config set [FLAG]` diff --git a/lib/cli/src/commands/init.rs b/lib/cli/src/commands/init.rs index a69f53e93f4..393554833b4 100644 --- a/lib/cli/src/commands/init.rs +++ b/lib/cli/src/commands/init.rs @@ -1,12 +1,13 @@ +use std::{ + collections::HashMap, + path::{Path, PathBuf}, +}; + use anyhow::Context; use cargo_metadata::{CargoOpt, MetadataCommand}; use clap::Parser; use indexmap::IndexMap; -use std::collections::HashMap; -use std::path::Path; -use std::path::PathBuf; - -use crate::WasmerEnv; +use wasmer_registry::wasmer_env::WasmerEnv; static NOTE: &str = "# See more keys and definitions at https://docs.wasmer.io/registry/manifest"; diff --git a/lib/cli/src/commands/login.rs b/lib/cli/src/commands/login.rs index 9ffe9aca3f9..773fd97a7d1 100644 --- a/lib/cli/src/commands/login.rs +++ b/lib/cli/src/commands/login.rs @@ -4,13 +4,13 @@ use clap::Parser; #[cfg(not(test))] use dialoguer::Input; -use crate::{Registry, WasmerEnv}; +use wasmer_registry::wasmer_env::{Registry, WasmerEnv, WASMER_DIR}; /// Subcommand for listing packages #[derive(Debug, Clone, Parser)] pub struct Login { /// Set Wasmer's home directory - #[clap(long, env = "WASMER_DIR", default_value = crate::WASMER_DIR.as_os_str())] + #[clap(long, env = "WASMER_DIR", default_value = WASMER_DIR.as_os_str())] pub wasmer_dir: PathBuf, /// The registry to fetch packages from (inferred from the environment by /// default) diff --git a/lib/cli/src/commands/publish.rs b/lib/cli/src/commands/publish.rs index 6dacea868bc..0de1868b689 100644 --- a/lib/cli/src/commands/publish.rs +++ b/lib/cli/src/commands/publish.rs @@ -1,10 +1,9 @@ use std::path::Path; use clap::Parser; +use wasmer_registry::wasmer_env::WasmerEnv; use wasmer_wasix::runtime::resolver::WapmSource; -use crate::WasmerEnv; - /// Publish a package to the package registry. #[derive(Debug, Parser)] pub struct Publish { diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 60aebf9f975..b5184b262bb 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -29,7 +29,7 @@ use wasmer::{ }; #[cfg(feature = "compiler")] use wasmer_compiler::ArtifactBuild; -use wasmer_registry::Package; +use wasmer_registry::{wasmer_env::WasmerEnv, Package}; use wasmer_wasix::{ bin_factory::BinaryPackage, runners::{MappedDirectory, Runner}, @@ -49,9 +49,7 @@ use wasmer_wasix::{ }; use webc::{metadata::Manifest, Container}; -use crate::{ - commands::run::wasi::Wasi, error::PrettyError, logging::Output, store::StoreOptions, WasmerEnv, -}; +use crate::{commands::run::wasi::Wasi, error::PrettyError, logging::Output, store::StoreOptions}; const TICK: Duration = Duration::from_millis(250); diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index 1e2ff2424ef..7bcb81e9de7 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -12,6 +12,7 @@ use tokio::runtime::Handle; use url::Url; use virtual_fs::{DeviceFile, FileSystem, PassthruFileSystem, RootFileSystemBuilder}; use wasmer::{Engine, Function, Instance, Memory32, Memory64, Module, RuntimeError, Store, Value}; +use wasmer_registry::wasmer_env::WasmerEnv; use wasmer_wasix::{ bin_factory::BinaryPackage, capabilities::Capabilities, @@ -35,10 +36,7 @@ use wasmer_wasix::{ WasiVersion, }; -use crate::{ - utils::{parse_envvar, parse_mapdir}, - WasmerEnv, -}; +use crate::utils::{parse_envvar, parse_mapdir}; const WAPM_SOURCE_CACHE_TIMEOUT: Duration = Duration::from_secs(10 * 60); diff --git a/lib/cli/src/commands/whoami.rs b/lib/cli/src/commands/whoami.rs index 72aa8c0e549..e6ec1dc2a1c 100644 --- a/lib/cli/src/commands/whoami.rs +++ b/lib/cli/src/commands/whoami.rs @@ -1,6 +1,5 @@ use clap::Parser; - -use crate::WasmerEnv; +use wasmer_registry::wasmer_env::WasmerEnv; #[derive(Debug, Parser)] /// The options for the `wasmer whoami` subcommand diff --git a/lib/cli/src/lib.rs b/lib/cli/src/lib.rs index 0adb77afdc0..5e9d4597aad 100644 --- a/lib/cli/src/lib.rs +++ b/lib/cli/src/lib.rs @@ -27,9 +27,6 @@ pub mod package_source; pub mod store; pub mod suggestions; pub mod utils; -mod wasmer_env; /// Version number of this crate. pub const VERSION: &str = env!("CARGO_PKG_VERSION"); - -pub use crate::wasmer_env::{Registry, WasmerEnv, WASMER_DIR}; diff --git a/lib/registry/Cargo.toml b/lib/registry/Cargo.toml index 33deb99227f..6964f2d5214 100644 --- a/lib/registry/Cargo.toml +++ b/lib/registry/Cargo.toml @@ -13,39 +13,40 @@ rust-version.workspace = true build-package = ["rusqlite", "indexmap", "wasmer-wasm-interface", "wasmparser", "rpassword", "minisign", "time"] [dependencies] -dirs = "4.0.0" -graphql_client = "0.11.0" -serde = { version = "1.0.145", features = ["derive"] } anyhow = "1.0.65" -futures-util = "0.3.25" -whoami = "1.2.3" -serde_json = "1.0.85" -url = "2.3.1" -thiserror = "1.0.37" -toml = "0.5.9" -wasmer-toml = "0.6.0" -tar = "0.4.38" +clap = { version = "4.3.5", default-features = false, features = ["derive", "env"], optional = true } +console = "0.15.2" +dirs = "4.0.0" +filetime = "0.2.19" flate2 = "1.0.24" -semver = "1.0.14" -lzma-rs = "0.2.0" +futures-util = "0.3.25" +graphql_client = "0.11.0" hex = "0.4.3" -tokio = "1.24.0" -log = "0.4.17" -regex = "1.7.0" -filetime = "0.2.19" -tldextract = "0.6.0" -console = "0.15.2" +indexmap = { version = "1.9.3", optional = true } indicatif = "0.17.2" lazy_static = "1.4.0" -tempfile = "3.4.0" +log = "0.4.17" +lzma-rs = "0.2.0" +minisign = { version = "0.7.2", optional = true } +regex = "1.7.0" +reqwest = { version = "0.11.12", default-features = false, features = ["blocking", "multipart", "json", "stream"] } +rpassword = { version = "7.2.0", optional = true } rusqlite = { version = "0.28.0", optional = true, features = ["bundled"] } +semver = "1.0.14" +serde = { version = "1.0.145", features = ["derive"] } +serde_json = "1.0.85" +tar = "0.4.38" +tempfile = "3.4.0" +thiserror = "1.0.37" time = { version = "0.3.17", default-features = false, features = ["parsing", "std", "formatting"], optional = true } -indexmap = { version = "1.9.3", optional = true } +tldextract = "0.6.0" +tokio = "1.24.0" +toml = "0.5.9" +url = "2.3.1" +wasmer-toml = "0.6.0" wasmer-wasm-interface = { version = "4.0.0-beta.3", path = "../wasm-interface", optional = true } wasmparser = { version = "0.51.4", optional = true } -rpassword = { version = "7.2.0", optional = true } -minisign = { version = "0.7.2", optional = true } -reqwest = { version = "0.11.12", default-features = false, features = ["blocking", "multipart", "json", "stream"] } +whoami = "1.2.3" [dev-dependencies] pretty_assertions = "1.3.0" diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index 522f174871a..88ce6b5948f 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -18,8 +18,9 @@ pub mod package; pub mod publish; pub mod types; pub mod utils; +pub mod wasmer_env; -pub use client::RegistryClient; +pub use crate::client::RegistryClient; use std::{ fmt, diff --git a/lib/cli/src/wasmer_env.rs b/lib/registry/src/wasmer_env.rs similarity index 75% rename from lib/cli/src/wasmer_env.rs rename to lib/registry/src/wasmer_env.rs index 3a34fa78b59..b7a2c214dc1 100644 --- a/lib/cli/src/wasmer_env.rs +++ b/lib/registry/src/wasmer_env.rs @@ -1,35 +1,31 @@ use std::path::{Path, PathBuf}; +use crate::WasmerConfig; use anyhow::{Context, Error}; -use once_cell::sync::Lazy; use url::Url; -use wasmer_registry::WasmerConfig; /// Command-line flags for determining the local "Wasmer Environment". /// /// This is where you access `$WASMER_DIR`, the `$WASMER_DIR/wasmer.toml` config /// file, and specify the current registry. -#[derive(Debug, Clone, PartialEq, clap::Parser)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "clap", derive(clap::Parser))] pub struct WasmerEnv { /// Set Wasmer's home directory - #[clap(long, env = "WASMER_DIR", default_value = WASMER_DIR.as_os_str())] + #[cfg_attr(feature = "clap", clap(long, env = "WASMER_DIR", default_value = WASMER_DIR.as_os_str()))] wasmer_dir: PathBuf, /// The registry to fetch packages from (inferred from the environment by /// default) - #[clap(long, env = "WASMER_REGISTRY")] + #[cfg_attr(feature = "clap", clap(long, env = "WASMER_REGISTRY"))] registry: Option, /// The API token to use when communicating with the registry (inferred from /// the environment by default) - #[clap(long, env = "WASMER_TOKEN")] + #[cfg_attr(feature = "clap", clap(long, env = "WASMER_TOKEN"))] token: Option, } impl WasmerEnv { - pub(crate) fn new( - wasmer_dir: PathBuf, - registry: Option, - token: Option, - ) -> Self { + pub fn new(wasmer_dir: PathBuf, registry: Option, token: Option) -> Self { WasmerEnv { wasmer_dir, registry, @@ -84,18 +80,19 @@ impl Default for WasmerEnv { } } -/// The default value for `$WASMER_DIR`. -pub static WASMER_DIR: Lazy = - Lazy::new(|| match wasmer_registry::WasmerConfig::get_wasmer_dir() { +lazy_static::lazy_static! { + /// The default value for `$WASMER_DIR`. + pub static ref WASMER_DIR: PathBuf = match crate::WasmerConfig::get_wasmer_dir() { Ok(path) => path, Err(e) => { - if let Some(install_prefix) = std::env::var_os("WASMER_INSTALL_PREFIX") { - PathBuf::from(install_prefix) - } else { - panic!("Unable to determine the wasmer dir: {e}"); + if let Some(install_prefix) = option_env!("WASMER_INSTALL_PREFIX") { + return PathBuf::from(install_prefix); } + + panic!("Unable to determine the wasmer dir: {e}"); } - }); + }; +} /// A registry as specified by the user. #[derive(Debug, Clone, PartialEq, Eq)] @@ -109,7 +106,7 @@ impl Registry { /// Get the GraphQL endpoint for this [`Registry`]. pub fn graphql_endpoint(&self) -> Result { - let url = wasmer_registry::format_graphql(self.as_str()).parse()?; + let url = crate::format_graphql(self.as_str()).parse()?; Ok(url) } }