Skip to content

Commit

Permalink
Moved WasmerEnv into the wasmer-registry crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jun 21, 2023
1 parent 7107293 commit 2a34a9e
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 81 deletions.
17 changes: 9 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 1 addition & 3 deletions lib/cli/src/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -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]`
Expand Down
11 changes: 6 additions & 5 deletions lib/cli/src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
4 changes: 2 additions & 2 deletions lib/cli/src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions lib/cli/src/commands/publish.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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);

Expand Down
6 changes: 2 additions & 4 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions lib/cli/src/commands/whoami.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 0 additions & 3 deletions lib/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
49 changes: 25 additions & 24 deletions lib/registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion lib/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 17 additions & 20 deletions lib/cli/src/wasmer_env.rs → lib/registry/src/wasmer_env.rs
Original file line number Diff line number Diff line change
@@ -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<Registry>,
/// 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<String>,
}

impl WasmerEnv {
pub(crate) fn new(
wasmer_dir: PathBuf,
registry: Option<Registry>,
token: Option<String>,
) -> Self {
pub fn new(wasmer_dir: PathBuf, registry: Option<Registry>, token: Option<String>) -> Self {
WasmerEnv {
wasmer_dir,
registry,
Expand Down Expand Up @@ -84,18 +80,19 @@ impl Default for WasmerEnv {
}
}

/// The default value for `$WASMER_DIR`.
pub static WASMER_DIR: Lazy<PathBuf> =
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)]
Expand All @@ -109,7 +106,7 @@ impl Registry {

/// Get the GraphQL endpoint for this [`Registry`].
pub fn graphql_endpoint(&self) -> Result<Url, Error> {
let url = wasmer_registry::format_graphql(self.as_str()).parse()?;
let url = crate::format_graphql(self.as_str()).parse()?;
Ok(url)
}
}
Expand Down

0 comments on commit 2a34a9e

Please sign in to comment.