Skip to content

Commit

Permalink
Merge pull request #1699 from axodotdev/config-version
Browse files Browse the repository at this point in the history
(config-migration) Add the ability to determine and track the config version.
  • Loading branch information
duckinator authored Jan 15, 2025
2 parents 12cadbd + 39d5b8a commit 214cd87
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cargo-dist/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions cargo-dist/src/config/v0_to_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions cargo-dist/src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl ApplyLayer for WorkspaceConfigInheritable {
allow_dirty,
dist_version,
dist_url_override,
config_version: _,
// app-scope only
dist: _,
targets: _,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -388,6 +390,10 @@ pub struct TomlLayer {
#[serde(skip_serializing_if = "Option::is_none")]
pub dist_version: Option<Version>,

/// 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<CargoDistUrlOverride>,
Expand Down
71 changes: 71 additions & 0 deletions cargo-dist/src/config/version.rs
Original file line number Diff line number Diff line change
@@ -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<ConfigVersion> {
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<ConfigVersion> {
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::<FauxConfig>() 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)
}
2 changes: 2 additions & 0 deletions cargo-dist/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use camino::Utf8PathBuf;
use dist_schema::TripleNameRef;
use semver::Version;
use serde::Deserialize;
use tracing::debug;

use crate::{
config::{
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit 214cd87

Please sign in to comment.