Skip to content

Commit

Permalink
publish: Read dependencies metadata from embedded Cargo.toml file
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Oct 4, 2023
1 parent 963d83b commit 08a7a89
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ axum = { version = "=0.6.20", features = ["headers", "macros", "matched-path"] }
axum-extra = { version = "=0.8.0", features = ["cookie-signed"] }
base64 = "=0.21.4"
bigdecimal = "=0.4.1"
cargo-manifest = "=0.12.0"
crates_io_index = { path = "crates_io_index" }
crates_io_markdown = { path = "crates_io_markdown" }
crates_io_tarball = { path = "crates_io_tarball" }
Expand Down Expand Up @@ -98,7 +99,6 @@ url = "=2.4.1"

[dev-dependencies]
bytes = "=1.5.0"
cargo-manifest = "=0.12.0"
crates_io_index = { path = "crates_io_index", features = ["testing"] }
crates_io_tarball = { path = "crates_io_tarball", features = ["builder"] }
claims = "=0.7.1"
Expand Down
82 changes: 80 additions & 2 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::auth::AuthCheck;
use crate::background_jobs::{Job, PRIORITY_RENDER_README};
use axum::body::Bytes;
use cargo_manifest::{Dependency, DepsSet, TargetDepsSet};
use crates_io_tarball::{process_tarball, TarballError};
use diesel::connection::DefaultLoadingMode;
use diesel::dsl::{exists, select};
Expand Down Expand Up @@ -305,12 +306,19 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
VersionAction::Publish,
)?;

for dep in &metadata.deps {
let deps = convert_dependencies(
tarball_info.manifest.dependencies.as_ref(),
tarball_info.manifest.dev_dependencies.as_ref(),
tarball_info.manifest.build_dependencies.as_ref(),
tarball_info.manifest.target.as_ref()
);

for dep in &deps {
validate_dependency(dep)?;
}

// Link this new version to all dependencies
add_dependencies(conn, &metadata.deps, version.id)?;
add_dependencies(conn, &deps, version.id)?;

// Update all keywords for this crate
Keyword::update_crate(conn, &krate, &keywords)?;
Expand Down Expand Up @@ -455,6 +463,76 @@ fn missing_metadata_error_message(missing: &[&str]) -> String {
)
}

fn convert_dependencies(
normal_deps: Option<&DepsSet>,
dev_deps: Option<&DepsSet>,
build_deps: Option<&DepsSet>,
targets: Option<&TargetDepsSet>,
) -> Vec<EncodableCrateDependency> {
use DependencyKind as Kind;

let mut result = vec![];

let mut add = |deps_set: &DepsSet, kind: Kind, target: Option<&str>| {
for (name, dep) in deps_set {
result.push(convert_dependency(name, dep, kind, target));
}
};

if let Some(deps) = normal_deps {
add(deps, Kind::Normal, None);
}
if let Some(deps) = dev_deps {
add(deps, Kind::Dev, None);
}
if let Some(deps_set) = build_deps {
add(deps_set, Kind::Build, None);
}
if let Some(target_deps_set) = targets {
for (target, deps) in target_deps_set {
add(&deps.dependencies, Kind::Normal, Some(target));
add(&deps.dev_dependencies, Kind::Dev, Some(target));
add(&deps.build_dependencies, Kind::Build, Some(target));
}
}

result
}

fn convert_dependency(
name: &str,
dep: &Dependency,
kind: DependencyKind,
target: Option<&str>,
) -> EncodableCrateDependency {
let details = dep.detail();
let req = dep.req();

let (crate_name, explicit_name_in_toml) = match details.and_then(|it| it.package.clone()) {
None => (name.to_string(), None),
Some(package) => (package, Some(name.to_string())),
};

let optional = details.and_then(|it| it.optional).unwrap_or(false);
let default_features = details.and_then(|it| it.default_features).unwrap_or(true);
let features = details
.and_then(|it| it.features.clone())
.unwrap_or_default();
let registry = details.and_then(|it| it.registry.clone());

EncodableCrateDependency {
name: crate_name,
version_req: req.to_string(),
optional,
default_features,
features,
target: target.map(ToString::to_string),
kind: Some(kind),
explicit_name_in_toml,
registry,
}
}

pub fn validate_dependency(dep: &EncodableCrateDependency) -> AppResult<()> {
if !Crate::valid_name(&dep.name) {
return Err(cargo_err(&format_args!(
Expand Down
1 change: 0 additions & 1 deletion src/tests/builders/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ impl PublishBuilder {
let metadata = u::PublishMetadata {
name: self.krate_name.clone(),
vers: self.version.to_string(),
deps: self.deps.clone(),
readme: self.readme,
readme_file: None,
};
Expand Down
3 changes: 1 addition & 2 deletions src/views/krate_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ use crate::models::DependencyKind;
pub struct PublishMetadata {
pub name: String,
pub vers: String,
pub deps: Vec<EncodableCrateDependency>,
pub readme: Option<String>,
pub readme_file: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Debug)]
pub struct EncodableCrateDependency {
pub optional: bool,
pub default_features: bool,
Expand Down

0 comments on commit 08a7a89

Please sign in to comment.