diff --git a/cargo-dist/src/config/mod.rs b/cargo-dist/src/config/mod.rs index 438961bd8..21d51f47b 100644 --- a/cargo-dist/src/config/mod.rs +++ b/cargo-dist/src/config/mod.rs @@ -18,6 +18,9 @@ use crate::{ METADATA_DIST, }; +mod version; +pub use version::{get_version, ConfigVersion}; + pub mod v0; pub mod v0_to_v1; pub mod v1; diff --git a/cargo-dist/src/config/v0_to_v1.rs b/cargo-dist/src/config/v0_to_v1.rs index 6ffb0ef5a..58859cd5f 100644 --- a/cargo-dist/src/config/v0_to_v1.rs +++ b/cargo-dist/src/config/v0_to_v1.rs @@ -354,6 +354,8 @@ impl DistMetadata { TomlLayer { dist_version: cargo_dist_version, + // v0_to_v1 means we're using a v0 config, so just set that. + config_version: crate::config::ConfigVersion::V0, dist_url_override: cargo_dist_url_override, dist, allow_dirty, diff --git a/cargo-dist/src/config/v1/mod.rs b/cargo-dist/src/config/v1/mod.rs index bd10f1121..ee8738109 100644 --- a/cargo-dist/src/config/v1/mod.rs +++ b/cargo-dist/src/config/v1/mod.rs @@ -250,6 +250,7 @@ impl ApplyLayer for WorkspaceConfigInheritable { allow_dirty, dist_version, dist_url_override, + config_version: _, // app-scope only dist: _, targets: _, @@ -361,6 +362,7 @@ impl ApplyLayer for AppConfigInheritable { allow_dirty: _, dist_version: _, dist_url_override: _, + config_version: _, }: Self::Layer, ) { self.artifacts.apply_val_layer(artifacts); @@ -388,6 +390,10 @@ pub struct TomlLayer { #[serde(skip_serializing_if = "Option::is_none")] pub dist_version: Option, + /// The configuration file version. + #[serde(default)] + pub config_version: crate::config::version::ConfigVersion, + /// see [`CargoDistUrlOverride`] #[serde(skip_serializing_if = "Option::is_none")] pub dist_url_override: Option, diff --git a/cargo-dist/src/config/version.rs b/cargo-dist/src/config/version.rs new file mode 100644 index 000000000..c52d596d9 --- /dev/null +++ b/cargo-dist/src/config/version.rs @@ -0,0 +1,71 @@ +//! For determining which configuration version we're using. + +use axoasset::SourceFile; +use camino::Utf8PathBuf; +use serde::{Deserialize, Serialize}; + +use crate::DistResult; + +/// Represents all known configuration versions. +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[serde(untagged)] +pub enum ConfigVersion { + /// The original legacy configuration formats are all lumped in as V0. + #[serde(rename = "0")] + V0 = 0, + /// The current configuration format. + #[serde(rename = "1")] + #[default] + V1 = 1, +} + +impl std::fmt::Display for ConfigVersion { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{}", self.clone() as i64) + } +} + +// Extremely minimal struct designed to differentiate between config versions. +// V0 does not have the `config-version` field, so will fail to parse. +// V1+ should have it, so will parse, and contain a `config_version` field. +#[derive(Deserialize)] +#[serde(rename_all = "kebab-case")] +struct FauxDistTable { + #[allow(dead_code)] + config_version: ConfigVersion, +} + +#[derive(Deserialize)] +struct FauxConfig { + #[allow(dead_code)] + dist: FauxDistTable, +} + +/// Return the config version used for the root workspace. +pub fn get_version() -> DistResult { + let workspaces = super::get_project()?; + let root_workspace = workspaces.root_workspace(); + + get_version_for_manifest(root_workspace.manifest_path.to_owned()) +} + +/// Given a path to a dist manifest (e.g. `dist-workspace.toml`), returns +/// the config version being used. +pub fn get_version_for_manifest(dist_manifest_path: Utf8PathBuf) -> DistResult { + if dist_manifest_path.file_name() != Some("dist-workspace.toml") { + // If the manifest is in Cargo.toml or dist.toml, we're + // definitely using a v0 config. + return Ok(ConfigVersion::V0); + } + + let src = SourceFile::load_local(&dist_manifest_path)?; + + let Ok(config) = src.deserialize_toml::() else { + // If we could load it, but can't parse it, it's likely v0. + return Ok(ConfigVersion::V0); + }; + + let version = config.dist.config_version; + + Ok(version) +} diff --git a/cargo-dist/src/init.rs b/cargo-dist/src/init.rs index a8d549e96..df9e04023 100644 --- a/cargo-dist/src/init.rs +++ b/cargo-dist/src/init.rs @@ -4,6 +4,7 @@ use camino::Utf8PathBuf; use dist_schema::TripleNameRef; use semver::Version; use serde::Deserialize; +use tracing::debug; use crate::{ config::{ @@ -203,6 +204,7 @@ fn do_migrate_from_dist_toml() -> DistResult<()> { pub fn do_migrate() -> DistResult<()> { do_migrate_from_rust_workspace()?; do_migrate_from_dist_toml()?; + debug!("dist.config-version = {}", config::get_version()?); //do_migrate_from_v0()?; Ok(()) }