-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wkg-core): Adds new
wkg-core
crate with lockfile support
This includes support for an optional config and tests for locking Signed-off-by: Taylor Thomas <taylor@cosmonic.com>
- Loading branch information
1 parent
4cfec08
commit 71631d3
Showing
12 changed files
with
1,018 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[package] | ||
name = "wkg-core" | ||
description = "Wasm Package Tools core libraries for wkg" | ||
repository = "https://github.com/bytecodealliance/wasm-pkg-tools/tree/main/crates/wkg-core" | ||
edition.workspace = true | ||
version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
futures-util = { workspace = true } | ||
semver = { workspace = true } | ||
serde = { workspace = true } | ||
tokio = { workspace = true, features = ["macros", "rt"] } | ||
toml = { workspace = true } | ||
tracing = { workspace = true } | ||
wasm-pkg-common = { workspace = true } | ||
wasm-pkg-client = { workspace = true } | ||
|
||
[target.'cfg(unix)'.dependencies.libc] | ||
version = "0.2.153" | ||
|
||
[target.'cfg(windows)'.dependencies.windows-sys] | ||
version = "0.52" | ||
features = [ | ||
"Win32_Foundation", | ||
"Win32_Storage", | ||
"Win32_Storage_FileSystem", | ||
"Win32_System", | ||
"Win32_System_IO", | ||
"Win32_Security", | ||
"Win32_System_Console", | ||
] | ||
|
||
[dev-dependencies] | ||
tempfile = { workspace = true } | ||
sha2 = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
//! Type definitions and functions for working with `wkg.toml` files. | ||
|
||
use std::{ | ||
collections::HashMap, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use anyhow::{Context, Result}; | ||
use semver::VersionReq; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::io::AsyncWriteExt; | ||
|
||
/// The default name of the configuration file. | ||
pub const CONFIG_FILE_NAME: &str = "wkg.toml"; | ||
|
||
/// The structure for a wkg.toml configuration file. This file is entirely optional and is used for | ||
/// overriding and annotating wasm packages. | ||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)] | ||
pub struct Config { | ||
/// Overrides for various packages | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub overrides: Option<HashMap<String, Override>>, | ||
/// Additional metadata about the package. This will override any metadata already set by other | ||
/// tools. | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub metadata: Option<Metadata>, | ||
} | ||
|
||
impl Config { | ||
/// Loads a configuration file from the given path. | ||
pub async fn load_from_path(path: impl AsRef<Path>) -> Result<Config> { | ||
let contents = tokio::fs::read_to_string(path) | ||
.await | ||
.context("unable to load config from file")?; | ||
let config: Config = toml::from_str(&contents).context("unable to parse config file")?; | ||
Ok(config) | ||
} | ||
|
||
/// Attempts to load the configuration from the current directory. Most of the time, users of this | ||
/// crate should use this function. Right now it just checks for a `wkg.toml` file in the current | ||
/// directory, but we could add more resolution logic in the future. If the file is not found, a | ||
/// default empty config is returned. | ||
pub async fn load() -> Result<Config> { | ||
let config_path = PathBuf::from(CONFIG_FILE_NAME); | ||
if !tokio::fs::try_exists(&config_path).await? { | ||
return Ok(Config::default()); | ||
} | ||
Self::load_from_path(config_path).await | ||
} | ||
|
||
/// Serializes and writes the configuration to the given path. | ||
pub async fn write(&self, path: impl AsRef<Path>) -> Result<()> { | ||
let contents = toml::to_string_pretty(self)?; | ||
let mut file = tokio::fs::File::create(path).await?; | ||
file.write_all(contents.as_bytes()) | ||
.await | ||
.context("unable to write config to path") | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)] | ||
pub struct Override { | ||
/// A path to the package on disk. If this is set, the package will be loaded from the given | ||
/// path. If this is not set, the package will be loaded from the registry. | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub path: Option<PathBuf>, | ||
/// Overrides the version of a package specified in a world file. This is for advanced use only | ||
/// and may break things. | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub version: Option<VersionReq>, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)] | ||
pub struct Metadata { | ||
/// The authors of the package. | ||
#[serde(default, skip_serializing_if = "Vec::is_empty")] | ||
pub authors: Vec<String>, | ||
/// The categories of the package. | ||
#[serde(default, skip_serializing_if = "Vec::is_empty")] | ||
pub categories: Vec<String>, | ||
/// The package description. | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub description: Option<String>, | ||
/// The package license. | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub license: Option<String>, | ||
/// The package documentation URL. | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub documentation: Option<String>, | ||
/// The package homepage URL. | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub homepage: Option<String>, | ||
/// The package repository URL. | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub repository: Option<String>, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_roundtrip() { | ||
let tempdir = tempfile::tempdir().unwrap(); | ||
let config_path = tempdir.path().join(CONFIG_FILE_NAME); | ||
let config = Config { | ||
overrides: Some(HashMap::from([( | ||
"foo:bar".to_string(), | ||
Override { | ||
path: Some(PathBuf::from("bar")), | ||
version: Some(VersionReq::parse("1.0.0").unwrap()), | ||
}, | ||
)])), | ||
metadata: Some(Metadata { | ||
authors: vec!["foo".to_string(), "bar".to_string()], | ||
categories: vec!["foo".to_string(), "bar".to_string()], | ||
description: Some("foo".to_string()), | ||
license: Some("foo".to_string()), | ||
documentation: Some("foo".to_string()), | ||
homepage: Some("foo".to_string()), | ||
repository: Some("foo".to_string()), | ||
}), | ||
}; | ||
|
||
config | ||
.write(&config_path) | ||
.await | ||
.expect("unable to write config"); | ||
let loaded_config = Config::load_from_path(config_path) | ||
.await | ||
.expect("unable to load config"); | ||
assert_eq!( | ||
config, loaded_config, | ||
"config loaded from file does not match original config" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! A library with reusable helpers and types for the `wkg` CLI. This is intended to be used by | ||
//! other downstream CLI tools that may need to leverage some of the same functionality provided by | ||
//! `wkg`. | ||
|
||
pub mod config; | ||
pub mod lock; |
Oops, something went wrong.