Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate dist.toml to dist-workspace.toml #1656

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions axoproject/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,17 @@ fn workspace_from(manifest_path: &Utf8Path) -> Result<WorkspaceStructure> {
let source = SourceFile::new(manifest_path.as_str(), String::new());
let span = source.span_for_line_col(1, 1);

if manifest.workspace.is_some() && manifest.package.is_some() {
Err(AxoassetError::Toml {
source,
span,
details: axoasset::toml::de::Error::custom(
"dist-workspace.toml can't have both [workspace] and [package]",
),
})?
} else if let Some(workspace) = manifest.workspace {
process_virtual_workspace(manifest_path, workspace)
if let Some(workspace) = manifest.workspace {
// dist-workspace.toml for generic projects with
// both [workspace] and [package]
if let Some(package) = manifest.package {
let package = process_package(manifest_path, package, false)?;
upgrade_package_to_workspace(package)
} else {
process_virtual_workspace(manifest_path, workspace)
}
} else if let Some(package) = manifest.package {
let package = process_package(manifest_path, package)?;
let package = process_package(manifest_path, package, true)?;
upgrade_package_to_workspace(package)
} else {
Err(AxoassetError::Toml {
Expand Down Expand Up @@ -281,7 +280,7 @@ fn single_package_workspace_from(manifest_path: &Utf8Path) -> Result<WorkspaceSt
),
})?
} else if let Some(package) = manifest.package {
let package = process_package(manifest_path, package)?;
let package = process_package(manifest_path, package, true)?;
upgrade_package_to_workspace(package)
} else {
Err(AxoassetError::Toml {
Expand Down Expand Up @@ -322,10 +321,14 @@ fn raw_package_from(manifest_path: &Utf8Path) -> Result<Package> {
// Load and process a dist.toml
fn package_from(manifest_path: &Utf8Path) -> Result<PackageInfo> {
let package = raw_package_from(manifest_path)?;
process_package(manifest_path, package)
process_package(manifest_path, package, true)
}

fn process_package(manifest_path: &Utf8Path, package: Package) -> Result<PackageInfo> {
fn process_package(
manifest_path: &Utf8Path,
package: Package,
use_workspace_manifest: bool,
) -> Result<PackageInfo> {
use serde::de::Error;

let version = package.version.map(Version::Generic);
Expand All @@ -350,11 +353,13 @@ fn process_package(manifest_path: &Utf8Path, package: Package) -> Result<Package
})?;
};

let dist_manifest_path = use_workspace_manifest.then(|| manifest_path.clone());

let mut info = PackageInfo {
true_name: name.clone(),
true_version: version.clone(),
manifest_path: manifest_path.clone(),
dist_manifest_path: Some(manifest_path.clone()),
dist_manifest_path,
package_root: manifest_path.parent().unwrap().to_owned(),
name,
version,
Expand Down
87 changes: 83 additions & 4 deletions cargo-dist/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axoasset::toml_edit;
use axoasset::{toml_edit, LocalAsset};
use axoproject::{WorkspaceGraph, WorkspaceInfo, WorkspaceKind};
use camino::Utf8PathBuf;
use cargo_dist_schema::TripleNameRef;
Expand Down Expand Up @@ -76,7 +76,7 @@ fn prune_cargo_workspace_metadata_dist(workspace: &mut toml_edit::DocumentMut) {
}

/// Create a toml-edit document set up for a cargo workspace.
fn new_workspace() -> toml_edit::DocumentMut {
fn new_cargo_workspace() -> toml_edit::DocumentMut {
let mut new_workspace = toml_edit::DocumentMut::new();

// Write generic workspace config
Expand All @@ -91,6 +91,22 @@ fn new_workspace() -> toml_edit::DocumentMut {
new_workspace
}

/// Create a toml-edit document set up for a cargo workspace.
fn new_generic_workspace() -> toml_edit::DocumentMut {
let mut new_workspace = toml_edit::DocumentMut::new();

// Write generic workspace config
let mut table = toml_edit::table();
if let Some(t) = table.as_table_mut() {
let mut array = toml_edit::Array::new();
array.push("dist:.");
t["members"] = toml_edit::value(array);
}
new_workspace.insert("workspace", table);

new_workspace
}

fn do_migrate_from_rust_workspace() -> DistResult<()> {
let workspaces = config::get_project()?;
let root_workspace = workspaces.root_workspace();
Expand All @@ -111,7 +127,7 @@ fn do_migrate_from_rust_workspace() -> DistResult<()> {
let mut original_workspace_toml = workspace_toml.clone();

// Generate a new workspace, then populate it using config from Cargo.toml.
let mut new_workspace_toml = new_workspace();
let mut new_workspace_toml = new_cargo_workspace();
copy_cargo_workspace_metadata_dist(&mut new_workspace_toml, workspace_toml);

// Determine config file location.
Expand All @@ -129,9 +145,64 @@ fn do_migrate_from_rust_workspace() -> DistResult<()> {
Ok(())
}

fn do_migrate_from_dist_toml() -> DistResult<()> {
let workspaces = config::get_project()?;
let root_workspace = workspaces.root_workspace();
let initted = has_metadata_table(root_workspace);

if !initted {
return Ok(());
}

if root_workspace.kind != WorkspaceKind::Generic
&& root_workspace.manifest_path.file_name() != Some("dist.toml")
{
return Ok(());
}

// OK, now we know we have a root-level dist.toml. Time to fix that.
let workspace_toml = config::load_toml(&root_workspace.manifest_path)?;

eprintln!("Migrating tables");
// Init a generic workspace with the appropriate members
let mut new_workspace_toml = new_generic_workspace();
// First copy the [package] section
if let Some(package) = workspace_toml.get("package") {
let mut package = package.clone();
// Ensures we have whitespace between the end of [workspace] and
// the start of [package]
if let Some(table) = package.as_table_mut() {
let decor = table.decor_mut();
// Try to keep existing comments if we can
if let Some(desc) = decor.prefix().and_then(|p| p.as_str()) {
if !desc.starts_with('\n') {
decor.set_prefix(&format!("\n{desc}"));
}
} else {
decor.set_prefix("\n");
}
}
new_workspace_toml.insert("package", package.to_owned());
}
// ...then copy the [dist] section
if let Some(dist) = workspace_toml.get("dist") {
new_workspace_toml.insert("dist", dist.to_owned());
}

// Finally, write out the new config...
let filename = "dist-workspace.toml";
let destination = root_workspace.workspace_dir.join(filename);
config::write_toml(&destination, new_workspace_toml)?;
// ...and delete the old config
LocalAsset::remove_file(&root_workspace.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()?;
Ok(())
}
Expand Down Expand Up @@ -185,6 +256,14 @@ pub fn do_init(cfg: &Config, args: &InitArgs) -> DistResult<()> {
let workspace_toml = config::load_toml(&root_workspace.manifest_path)?;
let initted = has_metadata_table(root_workspace);

if root_workspace.kind == WorkspaceKind::Generic
&& initted
&& root_workspace.manifest_path.file_name() == Some("dist.toml")
{
do_migrate()?;
return do_init(cfg, args);
}

// Already-initted users should be asked whether to migrate.
if root_workspace.kind == WorkspaceKind::Rust && initted && !args.yes {
let prompt = r#"Would you like to opt in to the new configuration format?
Expand Down Expand Up @@ -235,7 +314,7 @@ pub fn do_init(cfg: &Config, args: &InitArgs) -> DistResult<()> {
// generic workspace specification, and will have some
// extraneous cargo-specific stuff that we don't want.
let mut workspace_toml = if newly_initted_generic {
new_workspace()
new_cargo_workspace()
} else {
workspace_toml
};
Expand Down
Loading