Skip to content

Commit

Permalink
Implement normalization of manifests for publishing
Browse files Browse the repository at this point in the history
commit-id:fa87cc89
  • Loading branch information
mkaput committed Sep 26, 2023
1 parent f0290af commit d516f3e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 5 deletions.
1 change: 1 addition & 0 deletions scarb/src/core/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use toml::Value;

pub use compiler_config::*;
pub use dependency::*;
pub use maybe_workspace::*;
pub use scripts::*;
pub use summary::*;
pub use target::*;
Expand Down
1 change: 1 addition & 0 deletions scarb/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod dirs;
pub mod errors;
pub(crate) mod manifest;
pub(crate) mod package;
pub(crate) mod publishing;
pub(crate) mod registry;
pub(crate) mod resolver;
pub(crate) mod source;
Expand Down
101 changes: 101 additions & 0 deletions scarb/src/core/publishing/manifest_normalization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::collections::BTreeMap;

use anyhow::Result;
use itertools::Itertools;

use crate::core::{
MaybeWorkspace, Package, PathOrBool, TomlExternalTargetParams, TomlManifest, TomlPackage,
TomlTarget, Workspace,
};

pub fn prepare_manifest_for_publish(pkg: &Package, _ws: &Workspace<'_>) -> Result<TomlManifest> {
let manifest = &pkg.manifest;
let summary = &manifest.summary;
let metadata = &manifest.metadata;

let package = Some(Box::new(TomlPackage {
name: summary.package_id.name.clone(),
version: MaybeWorkspace::Defined(summary.package_id.version.clone()),
authors: metadata.authors.clone().map(MaybeWorkspace::Defined),
urls: metadata.urls.clone(),
description: metadata.description.clone().map(MaybeWorkspace::Defined),
documentation: metadata.documentation.clone().map(MaybeWorkspace::Defined),
homepage: metadata.homepage.clone().map(MaybeWorkspace::Defined),
keywords: metadata.keywords.clone().map(MaybeWorkspace::Defined),
license: metadata.license.clone().map(MaybeWorkspace::Defined),
// TODO(mkaput): Normalize this the same way as readme is.
license_file: metadata.license_file.clone().map(MaybeWorkspace::Defined),
readme: match &metadata.readme {
None => None,
Some(path) => {
// NOTE: We assume here that the packaging logic will put the readme next
// to the generated Scarb.toml.
let file_name = path
.file_name()
.expect("Readme path must have a file name.")
.into();
Some(MaybeWorkspace::Defined(PathOrBool::Path(file_name)))
}
},
repository: metadata.repository.clone().map(MaybeWorkspace::Defined),
no_core: summary.no_core.then_some(true),
cairo_version: metadata.cairo_version.clone().map(MaybeWorkspace::Defined),
}));

let dependencies = todo!();

let target = Some(BTreeMap::from_iter(
manifest
.targets
.iter()
.map(|target| {
let kind = target.kind.clone();

let name = Some(target.name.clone());
let source_path = Some(
target
.source_path
.strip_prefix(pkg.root())
.expect("Source paths should always be within package root directory.")
.to_path_buf(),
);

let params = target
.params
.clone()
.try_into::<TomlExternalTargetParams>()
.expect(
"Internally stored target params should always be \
a string-keyed map-like structure.",
);

let toml_target = TomlTarget {
name,
source_path,
params,
};
(kind, toml_target)
})
.into_group_map(),
));

let tool = metadata.tool_metadata.clone().map(|m| {
m.into_iter()
.map(|(k, v)| (k, MaybeWorkspace::Defined(v)))
.collect()
});

Ok(TomlManifest {
package,
workspace: None,
dependencies,
lib: None,
cairo_plugin: None,
test: None,
target,
cairo: None,
profile: None,
scripts: None,
tool,
})
}
1 change: 1 addition & 0 deletions scarb/src/core/publishing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod manifest_normalization;
41 changes: 36 additions & 5 deletions scarb/src/ops/package.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{Seek, SeekFrom};
use std::io::{Seek, SeekFrom, Write};

use anyhow::{ensure, Context, Result};
use camino::Utf8PathBuf;
use indoc::writedoc;

use scarb_ui::components::Status;
use scarb_ui::{HumanBytes, HumanCount};

use crate::core::publishing::manifest_normalization::prepare_manifest_for_publish;
use crate::core::{Package, PackageId, PackageName, Workspace};
use crate::flock::FileLockGuard;
use crate::internal::fsx;
Expand All @@ -30,7 +32,6 @@ type ArchiveRecipe<'a> = Vec<ArchiveFile<'a>>;
struct ArchiveFile<'a> {
/// The relative path in the archive (not including top-level package name directory).
path: Utf8PathBuf,
#[allow(dead_code)]
/// The contents of the file.
contents: ArchiveFileContents<'a>,
}
Expand Down Expand Up @@ -232,9 +233,39 @@ fn check_no_reserved_files(recipe: &ArchiveRecipe<'_>) -> Result<()> {
Ok(())
}

fn normalize_manifest(_pkg: &Package, _ws: &Workspace<'_>) -> Result<Vec<u8>> {
// TODO(mkaput): Implement this properly.
Ok("[package]".to_string().into_bytes())
fn normalize_manifest(pkg: &Package, ws: &Workspace<'_>) -> Result<Vec<u8>> {
let mut buf = Vec::new();

writedoc!(
&mut buf,
r##"
# Code generated by scarb package -p {package_name}; DO NOT EDIT.
#
# When uploading packages to the registry Scarb will automatically
# "normalize" {toml} files for maximal compatibility
# with all versions of Scarb and also rewrite `path` dependencies
# to registry dependencies.
#
# If you are reading this file be aware that the original {toml}
# will likely look very different (and much more reasonable).
# See {orig} for the original contents.
"##,
package_name = pkg.id.name,
toml = MANIFEST_FILE_NAME,
orig = ORIGINAL_MANIFEST_FILE_NAME,
)?;
writeln!(&mut buf)?;

let manifest = prepare_manifest_for_publish(pkg, ws)?;
let toml = toml::to_string_pretty(&manifest)?;
writeln!(&mut buf, "{toml}")?;

unsafe {
println!("{}", String::from_utf8_unchecked(buf));
}
todo!();

Ok(buf)
}

/// Compress and package the recipe, and write it into the given file.
Expand Down

0 comments on commit d516f3e

Please sign in to comment.