Skip to content

Commit ce26547

Browse files
committed
[WIP] Make "dist migrate" convert from v0 to v1 config.
1 parent 569cb15 commit ce26547

File tree

4 files changed

+130
-4
lines changed

4 files changed

+130
-4
lines changed

cargo-dist/src/config/mod.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use cargo_dist_schema::{
1212
use serde::{Deserialize, Serialize};
1313

1414
use crate::announce::TagSettings;
15+
use crate::config::v1::DistWorkspaceConfig;
1516
use crate::SortedMap;
1617
use crate::{
1718
errors::{DistError, DistResult},
@@ -929,6 +930,21 @@ impl std::fmt::Display for ProductionMode {
929930
}
930931
}
931932

933+
pub(crate) fn load_config(dist_manifest_path: &Utf8Path) -> DistResult<DistWorkspaceConfig> {
934+
let src = SourceFile::load_local(dist_manifest_path)?;
935+
parse_config(src)
936+
}
937+
938+
pub(crate) fn parse_config(src: SourceFile) -> DistResult<DistWorkspaceConfig> {
939+
let config: DistWorkspaceConfig = src.deserialize_toml()?;
940+
Ok(config)
941+
}
942+
943+
pub(crate) fn load_generic_v0_config(dist_manifest_path: &Utf8Path) -> DistResult<DistMetadata> {
944+
let src = SourceFile::load_local(dist_manifest_path)?;
945+
parse_generic_config(src)
946+
}
947+
932948
pub(crate) fn parse_metadata_table_or_manifest(
933949
manifest_path: &Utf8Path,
934950
dist_manifest_path: Option<&Utf8Path>,
@@ -937,8 +953,7 @@ pub(crate) fn parse_metadata_table_or_manifest(
937953
if let Some(dist_manifest_path) = dist_manifest_path {
938954
reject_metadata_table(manifest_path, dist_manifest_path, metadata_table)?;
939955
// Generic dist.toml
940-
let src = SourceFile::load_local(dist_manifest_path)?;
941-
parse_generic_config(src)
956+
load_generic_v0_config(dist_manifest_path)
942957
} else {
943958
// Pre-parsed Rust metadata table
944959
parse_metadata_table(manifest_path, metadata_table)

cargo-dist/src/config/v1/mod.rs

+72
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,78 @@ impl ApplyLayer for AppConfigInheritable {
373373
}
374374
}
375375

376+
/// The internal representation of the [package] table from dist[-workspace].toml.
377+
#[derive(Debug, Clone, Serialize, Deserialize)]
378+
#[serde(rename_all = "kebab-case")]
379+
pub struct PackageTable {
380+
/// The name of the package.
381+
pub name: String,
382+
/// The version of the package. Syntax must be a valid Cargo SemVer Version.
383+
pub version: String,
384+
/// A brief description of the package.
385+
pub description: Option<String>,
386+
/// The authors of the package.
387+
pub authors: Option<Vec<String>>,
388+
/// A URL to the repository hosting this package.
389+
pub repository: Option<String>,
390+
/// A URL to the homepage of the package.
391+
pub homepage: Option<String>,
392+
/// A URL to the documentation of the package.
393+
pub documentation: Option<String>,
394+
/// A relative path to the changelog file for your package.
395+
pub changelog: Option<String>,
396+
/// A relative path to the readme file for your package.
397+
pub readme: Option<String>,
398+
/// The license(s) of your package, in SPDX format.
399+
pub license: Option<String>,
400+
/// Relative paths to the license files for your package.
401+
pub license_files: Option<Vec<String>>,
402+
/// Names of binaries (without the extension) your package is expected
403+
/// to build and distribute.
404+
pub binaries: Option<Vec<String>>,
405+
/// Names of c-style static libraries (without the extension) your
406+
/// package is expected to build and distribute.
407+
pub cstaticlibs: Option<Vec<String>>,
408+
/// Names of c-style dynamic libraries (without the extension) your
409+
/// package is expected to build and distribute.
410+
pub cdylibs: Option<Vec<String>>,
411+
/// A command to run in your package's root directory to build its
412+
/// binaries, cstaticlibs, and cdylibs.
413+
pub build_command: Option<Vec<String>>,
414+
}
415+
416+
/// The internal representation of dist.toml.
417+
#[derive(Debug, Clone, Serialize, Deserialize)]
418+
#[serde(rename_all = "kebab-case")]
419+
pub struct DistConfig {
420+
/// The `[package]` table from dist.toml.
421+
pub package: Option<PackageTable>,
422+
/// The `[dist]` table from dist.toml.
423+
pub dist: TomlLayer,
424+
}
425+
426+
/// The internal representation of the [workspace] table from dist-workspace.toml.
427+
#[derive(Debug, Clone, Serialize, Deserialize)]
428+
#[serde(rename_all = "kebab-case")]
429+
pub struct WorkspaceTable {
430+
/// The various projects/workspaces/packages to be managed by dist.
431+
pub members: Vec<String>,
432+
}
433+
434+
/// The internal representation of dist-workspace.toml.
435+
#[derive(Debug, Clone, Serialize, Deserialize)]
436+
#[serde(rename_all = "kebab-case")]
437+
pub struct DistWorkspaceConfig {
438+
/// The `[workspace]` table.
439+
#[serde(skip_serializing_if = "Option::is_none")]
440+
pub workspace: Option<WorkspaceTable>,
441+
/// The `[dist]` table
442+
pub dist: TomlLayer,
443+
/// The `[package]` table.
444+
#[serde(skip_serializing_if = "Option::is_none")]
445+
pub package: Option<PackageTable>,
446+
}
447+
376448
/// The "raw" input from a toml file containing config
377449
#[derive(Debug, Clone, Serialize, Deserialize)]
378450
#[serde(rename_all = "kebab-case")]

cargo-dist/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ pub enum DistError {
7474
#[error(transparent)]
7575
TripleError(#[from] cargo_dist_schema::target_lexicon::ParseError),
7676

77+
/// error when using axoasset::toml::to_string() or similar
78+
#[error(transparent)]
79+
AxoassetTomlSerErr(#[from] axoasset::toml::ser::Error),
80+
7781
/// A problem with a jinja template, which is always a dist bug
7882
#[error("Failed to render template")]
7983
#[diagnostic(

cargo-dist/src/init.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use axoasset::{toml_edit, LocalAsset};
1+
use axoasset::{toml, toml_edit, LocalAsset};
22
use axoproject::{WorkspaceGraph, WorkspaceInfo, WorkspaceKind};
33
use camino::Utf8PathBuf;
44
use cargo_dist_schema::TripleNameRef;
@@ -199,11 +199,46 @@ fn do_migrate_from_dist_toml() -> DistResult<()> {
199199
Ok(())
200200
}
201201

202+
fn do_migrate_from_v0() -> DistResult<()> {
203+
let workspaces = config::get_project()?;
204+
let root_workspace = workspaces.root_workspace();
205+
let manifest_path = &root_workspace.manifest_path;
206+
207+
if config::load_config(manifest_path).is_ok() {
208+
// We're already on a V1 config, no need to migrate!
209+
return Ok(());
210+
}
211+
212+
// Load in the root workspace toml to edit and write back
213+
let Ok(dist_metadata) = config::load_generic_v0_config(manifest_path) else {
214+
// We don't have a valid v0 _or_ v1 config. No migration can be done.
215+
// It feels weird to return Ok(()) here, but I think it's right?
216+
return Ok(());
217+
};
218+
219+
let dist = dist_metadata.to_toml_layer(true);
220+
let workspace = Some(config::v1::WorkspaceTable { members: vec![] });
221+
let package = None;
222+
223+
let config = config::v1::DistWorkspaceConfig {
224+
dist,
225+
workspace,
226+
package,
227+
};
228+
229+
let workspace_toml_text = toml::to_string(&config)?;
230+
231+
// Write new config file.
232+
axoasset::LocalAsset::write_new(&workspace_toml_text, manifest_path)?;
233+
234+
Ok(())
235+
}
236+
202237
/// Run `dist migrate`
203238
pub fn do_migrate() -> DistResult<()> {
204239
do_migrate_from_rust_workspace()?;
205240
do_migrate_from_dist_toml()?;
206-
//do_migrate_from_v0()?;
241+
do_migrate_from_v0()?;
207242
Ok(())
208243
}
209244

0 commit comments

Comments
 (0)