Skip to content

Commit

Permalink
Make "dist migrate" convert from v0 to v1 config.
Browse files Browse the repository at this point in the history
  • Loading branch information
duckinator committed Dec 20, 2024
1 parent 55e3dc6 commit 8670bcb
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 4 deletions.
22 changes: 21 additions & 1 deletion cargo-dist/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cargo_dist_schema::{
use serde::{Deserialize, Serialize};

use crate::announce::TagSettings;
use crate::config::v1::DistWorkspaceConfig;
use crate::SortedMap;
use crate::{
errors::{DistError, DistResult},
Expand All @@ -22,7 +23,7 @@ pub mod v0;
pub mod v0_to_v1;
pub mod v1;

pub use v0::{DistMetadata, GenericConfig};
pub use v0::{DistMetadata, GenericConfig, V0WorkspaceConfig};

/// values of the form `permission-name: read`
pub type GithubPermissionMap = SortedMap<String, GithubPermission>;
Expand Down Expand Up @@ -929,6 +930,16 @@ impl std::fmt::Display for ProductionMode {
}
}

pub(crate) fn load_config(dist_manifest_path: &Utf8Path) -> DistResult<DistWorkspaceConfig> {
let src = SourceFile::load_local(dist_manifest_path)?;
parse_config(src)
}

pub(crate) fn parse_config(src: SourceFile) -> DistResult<DistWorkspaceConfig> {
let config: DistWorkspaceConfig = src.deserialize_toml()?;
Ok(config)
}

pub(crate) fn parse_metadata_table_or_manifest(
manifest_path: &Utf8Path,
dist_manifest_path: Option<&Utf8Path>,
Expand All @@ -945,6 +956,15 @@ pub(crate) fn parse_metadata_table_or_manifest(
}
}

pub(crate) fn load_v0_config(dist_manifest_path: &Utf8Path) -> DistResult<V0WorkspaceConfig> {
let src = SourceFile::load_local(dist_manifest_path)?;
parse_v0_config(src)
}

pub(crate) fn parse_v0_config(src: SourceFile) -> DistResult<V0WorkspaceConfig> {
Ok(src.deserialize_toml()?)
}

pub(crate) fn parse_generic_config(src: SourceFile) -> DistResult<DistMetadata> {
let config: GenericConfig = src.deserialize_toml()?;
Ok(config.dist.unwrap_or_default())
Expand Down
13 changes: 13 additions & 0 deletions cargo-dist/src/config/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ use super::*;
use crate::platform::MinGlibcVersion;
use crate::SortedMap;

use crate::config::v1::{WorkspaceTable, PackageTable};

/// A container to assist deserializing the entirety of `dist-workspace.toml`.
#[derive(Debug, Deserialize)]
pub struct V0WorkspaceConfig {
/// the `[workspace]` table.
pub workspace: Option<WorkspaceTable>,
/// the `[package]` table.
pub package: Option<PackageTable>,
/// the `[dist]` table.
pub dist: Option<DistMetadata>,
}

/// A container to assist deserializing metadata from dist(-workspace).tomls
#[derive(Debug, Deserialize)]
pub struct GenericConfig {
Expand Down
72 changes: 72 additions & 0 deletions cargo-dist/src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,78 @@ impl ApplyLayer for AppConfigInheritable {
}
}

/// The internal representation of the [package] table from dist[-workspace].toml.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct PackageTable {
/// The name of the package.
pub name: String,
/// The version of the package. Syntax must be a valid Cargo SemVer Version.
pub version: String,
/// A brief description of the package.
pub description: Option<String>,
/// The authors of the package.
pub authors: Option<Vec<String>>,
/// A URL to the repository hosting this package.
pub repository: Option<String>,
/// A URL to the homepage of the package.
pub homepage: Option<String>,
/// A URL to the documentation of the package.
pub documentation: Option<String>,
/// A relative path to the changelog file for your package.
pub changelog: Option<String>,
/// A relative path to the readme file for your package.
pub readme: Option<String>,
/// The license(s) of your package, in SPDX format.
pub license: Option<String>,
/// Relative paths to the license files for your package.
pub license_files: Option<Vec<String>>,
/// Names of binaries (without the extension) your package is expected
/// to build and distribute.
pub binaries: Option<Vec<String>>,
/// Names of c-style static libraries (without the extension) your
/// package is expected to build and distribute.
pub cstaticlibs: Option<Vec<String>>,
/// Names of c-style dynamic libraries (without the extension) your
/// package is expected to build and distribute.
pub cdylibs: Option<Vec<String>>,
/// A command to run in your package's root directory to build its
/// binaries, cstaticlibs, and cdylibs.
pub build_command: Option<Vec<String>>,
}

/// The internal representation of dist.toml.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct DistConfig {
/// The `[package]` table from dist.toml.
pub package: Option<PackageTable>,
/// The `[dist]` table from dist.toml.
pub dist: TomlLayer,
}

/// The internal representation of the [workspace] table from dist-workspace.toml.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct WorkspaceTable {
/// The various projects/workspaces/packages to be managed by dist.
pub members: Vec<String>,
}

/// The internal representation of dist-workspace.toml.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct DistWorkspaceConfig {
/// The `[workspace]` table.
#[serde(skip_serializing_if = "Option::is_none")]
pub workspace: Option<WorkspaceTable>,
/// The `[dist]` table
pub dist: TomlLayer,
/// The `[package]` table.
#[serde(skip_serializing_if = "Option::is_none")]
pub package: Option<PackageTable>,
}

/// The "raw" input from a toml file containing config
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down
4 changes: 4 additions & 0 deletions cargo-dist/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub enum DistError {
#[error(transparent)]
TripleError(#[from] cargo_dist_schema::target_lexicon::ParseError),

/// error when using axoasset::toml::to_string() or similar
#[error(transparent)]
AxoassetTomlSerErr(#[from] axoasset::toml::ser::Error),

/// A problem with a jinja template, which is always a dist bug
#[error("Failed to render template")]
#[diagnostic(
Expand Down
47 changes: 44 additions & 3 deletions cargo-dist/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axoasset::{toml_edit, LocalAsset};
use axoasset::{toml, toml_edit, LocalAsset};
use axoproject::{WorkspaceGraph, WorkspaceInfo, WorkspaceKind};
use camino::Utf8PathBuf;
use cargo_dist_schema::TripleNameRef;
Expand Down Expand Up @@ -199,11 +199,52 @@ fn do_migrate_from_dist_toml() -> DistResult<()> {
Ok(())
}

fn do_migrate_from_v0() -> DistResult<()> {
let workspaces = config::get_project()?;
let root_workspace = workspaces.root_workspace();
let manifest_path = &root_workspace.manifest_path;

if config::load_config(manifest_path).is_ok() {
// We're already on a V1 config, no need to migrate!
return Ok(());
}

// Load in the root workspace toml to edit and write back
let Ok(old_config) = config::load_v0_config(manifest_path) else {
// We don't have a valid v0 _or_ v1 config. No migration can be done.
// It feels weird to return Ok(()) here, but I think it's right?
return Ok(());
};

let Some(dist_metadata) = &old_config.dist else {
// We don't have a valid v0 config. No migration can be done.
return Ok(());
};

let dist = dist_metadata.to_toml_layer(true);

let workspace = old_config.workspace;
let package = None;

let config = config::v1::DistWorkspaceConfig {
dist,
workspace,
package,
};

let workspace_toml_text = toml::to_string(&config)?;

// Write new config file.
axoasset::LocalAsset::write_new(&workspace_toml_text, manifest_path)?;

Ok(())
}

/// Run `dist migrate`
pub fn do_migrate() -> DistResult<()> {
do_migrate_from_rust_workspace()?;
do_migrate_from_dist_toml()?;
//do_migrate_from_v0()?;
do_migrate_from_v0()?;
Ok(())
}

Expand Down Expand Up @@ -275,7 +316,7 @@ pub fn do_init(cfg: &Config, args: &InitArgs) -> DistResult<()> {
.interact()?;

if is_migrating {
do_migrate_from_rust_workspace()?;
do_migrate()?;
return do_init(cfg, args);
}
}
Expand Down

0 comments on commit 8670bcb

Please sign in to comment.