Skip to content

Commit

Permalink
Merge pull request #1723 from axodotdev/v1-loader
Browse files Browse the repository at this point in the history
(config-migration) Add dist::config::load() and friends
  • Loading branch information
duckinator authored Jan 25, 2025
2 parents b43d8c7 + 0ddc61e commit 934cc26
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
123 changes: 123 additions & 0 deletions cargo-dist/src/config/v1/loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use crate::{
config::{self, v1::DistConfig, v1::TomlLayer},
errors::{DistError, DistResult},
};
use axoasset::SourceFile;
use camino::Utf8Path;

/// Load the dist(-workspace).toml for the root workspace.
pub fn load_root() -> DistResult<DistConfig> {
let workspaces = config::get_project()?;
let root_workspace = workspaces.root_workspace();

let Some(path) = root_workspace.dist_manifest_path.as_deref() else {
return Err(DistError::NoConfigFile {});
};

load(path)
}

/// Loads a dist(-workspace).toml from disk.
pub fn load(dist_manifest_path: &Utf8Path) -> DistResult<DistConfig> {
let src = SourceFile::load_local(dist_manifest_path)?;
parse(src)
}

/// Load a dist(-workspace).toml from disk and return its `[dist]` table.
pub fn load_dist(dist_manifest_path: &Utf8Path) -> DistResult<TomlLayer> {
Ok(load(dist_manifest_path)?.dist.unwrap_or_default())
}

/// Given a SourceFile of a dist(-workspace).toml, deserializes it.
pub fn parse(src: SourceFile) -> DistResult<DistConfig> {
// parse() can probably be consolidated into load() eventually.
Ok(src.deserialize_toml()?)
}

/// Given a SourceFile of a dist(-workspace).toml, deserialize its `[dist]` table.
pub fn parse_dist(src: SourceFile) -> DistResult<TomlLayer> {
Ok(parse(src)?.dist.unwrap_or_default())
}

#[cfg(test)]
mod tests {
use super::*;
use axoasset::SourceFile;

#[test]
fn parse_v1_succeeds() {
let file = SourceFile::new("fake-v1-dist-workspace.toml", r##"
[workspace]
members = ["cargo:*"]
[package]
name = "whatever"
version = "1.0.0"
# Config for 'dist'
[dist]
dist-version = "1.0.0"
targets = ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "aarch64-unknown-linux-musl", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl", "x86_64-pc-windows-msvc"]
[dist.build]
min-glibc-version."*" = "2.18"
[dist.ci]
github = true
pr-run-mode = "plan"
#publish-jobs = ["homebrew", "./publish-crates"]
[dist.hosts]
github = true
[dist.installers]
install-path = "CARGO_HOME"
shell = true
updater = false
[dist.installers.homebrew]
tap = "axodotdev/homebrew-tap"
"##.to_string());

assert!(parse(file).is_ok());
}

#[test]
fn parse_v0_fails() {
let file = SourceFile::new("fake-v0-dist-workspace.toml", r##"
[workspace]
members = ["cargo:*"]
[package]
name = "whatever"
version = "1.0.0"
# Config for 'dist'
[dist]
# The preferred dist version to use in CI (Cargo.toml SemVer syntax)
cargo-dist-version = "0.13.1-prerelease.1"
# CI backends to support
ci = "github"
# The installers to generate for each app
installers = ["shell", "powershell", "homebrew"]
# A GitHub repo to push Homebrew formulas to
tap = "axodotdev/homebrew-tap"
# Publish jobs to run in CI
publish-jobs = ["homebrew", "./publish-crates"]
# Target platforms to build apps for (Rust target-triple syntax)
targets = ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "aarch64-unknown-linux-musl", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl", "x86_64-pc-windows-msvc"]
# Which actions to run on pull requests
pr-run-mode = "plan"
# Where to host releases
hosting = ["github"]
# Whether to install an updater program
install-updater = false
# Path that installers should place binaries in
install-path = "CARGO_HOME"
# The minimum glibc version supported by the package (overrides auto-detection)
min-glibc-version."*" = "2.18"
"##.to_string());

assert!(parse(file).is_err());
}
}
16 changes: 15 additions & 1 deletion cargo-dist/src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ pub mod builds;
pub mod ci;
pub mod hosts;
pub mod installers;
mod loader;
pub mod publishers;

use axoproject::generic::{Package, Workspace};
use axoproject::{PackageIdx, WorkspaceGraph};
use semver::Version;
use v0::CargoDistUrlOverride;
Expand All @@ -124,10 +126,22 @@ use builds::*;
use ci::*;
use hosts::*;
use installers::*;
pub use loader::*;
use publishers::*;

use tracing::log::warn;

/// A representation of an entire dist(-workspace).toml.
#[derive(Default, Deserialize, Serialize)]
pub struct DistConfig {
/// the `[workspace]` table.
pub workspace: Option<Workspace>,
/// the `[package]` table.
pub package: Option<Package>,
/// the `[dist]` table.
pub dist: Option<TomlLayer>,
}

/// Compute the workspace-level config
pub fn workspace_config(
workspaces: &WorkspaceGraph,
Expand Down Expand Up @@ -378,7 +392,7 @@ impl ApplyLayer for AppConfigInheritable {
}

/// The "raw" input from a toml file containing config
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TomlLayer {
/// The intended version of dist to build with. (normal Cargo SemVer syntax)
Expand Down
7 changes: 7 additions & 0 deletions cargo-dist/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,13 @@ pub enum DistError {
/// Version the project uses
your_version: semver::Version,
},

/// No dist manifest
#[error("No configuration file (e.g. dist-workspace.toml) was found")]
#[diagnostic(help(
"Did you run 'dist init'? If you did, this is probably a bug; please file an issue!"
))]
NoConfigFile {},
}

impl From<minijinja::Error> for DistError {
Expand Down

0 comments on commit 934cc26

Please sign in to comment.