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

Add SHA-256 to the local_repositories #47

Merged
merged 4 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
871 changes: 461 additions & 410 deletions impl/Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions impl/src/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ mod tests {
additional_flags: Vec::new(),
extra_aliased_targets: Vec::new(),
data_attr: None,
sha256: None,
}
}

Expand Down Expand Up @@ -337,6 +338,7 @@ mod tests {
additional_flags: Vec::new(),
extra_aliased_targets: Vec::new(),
data_attr: None,
sha256: None,
}
}

Expand Down
1 change: 1 addition & 0 deletions impl/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct CrateContext {
pub additional_flags: Vec<String>,
pub extra_aliased_targets: Vec<String>,
pub data_attr: Option<String>,
pub sha256: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
Expand Down
7 changes: 7 additions & 0 deletions impl/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct Package {
pub targets: Vec<Target>,
pub features: HashMap<String, Vec<FeatureOrDependency>>,
pub manifest_path: String,
pub sha256: Option<String>,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
Expand Down Expand Up @@ -272,6 +273,9 @@ impl<'config> MetadataFetcher for CargoInternalsMetadataFetcher<'config> {
features.insert(feature.clone(), features_or_dependencies.clone());
}

// Cargo use SHA256 for checksum so we can use them directly
let sha256 = package.manifest().summary().checksum().and_then(|x| Some(x.to_string()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two Nits:

s/and_then(|x| Some(x.to_string()))/map(ToString::to_string)

The and_then is only necessary when the mapping function yields an option. map is similar but without the option. ToString::to_string is the direct reference to the method (though |x| x.to_string() is totally fine as well).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


packages.push(Package {
name: package.name().to_string(),
version: package.version().to_string(),
Expand All @@ -284,6 +288,7 @@ impl<'config> MetadataFetcher for CargoInternalsMetadataFetcher<'config> {
targets: targets,
features: features,
manifest_path: package.manifest_path().display().to_string(),
sha256: sha256,
});
}

Expand Down Expand Up @@ -321,6 +326,7 @@ fn default_dependency_field_use_default_features() -> bool {
true
}

#[cfg(test)]
pub mod testing {
use super::*;

Expand Down Expand Up @@ -353,6 +359,7 @@ pub mod testing {
targets: Vec::new(),
features: HashMap::new(),
manifest_path: String::new(),
sha256: None,
}
}

Expand Down
10 changes: 2 additions & 8 deletions impl/src/planning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl<'fetcher> BuildPlannerImpl<'fetcher> {
let licenses = load_and_dedup_licenses(license_str);

let data_attr = possible_crate_settings.and_then(|s| s.data_attr.clone());

crate_contexts.push(CrateContext {
pkg_name: own_package.name.clone(),
pkg_version: own_package.version.clone(),
Expand All @@ -328,16 +328,14 @@ impl<'fetcher> BuildPlannerImpl<'fetcher> {
additional_flags: additional_flags,
extra_aliased_targets: extra_aliased_targets,
data_attr: data_attr,
sha256: own_package.sha256.clone(),
})
}

Ok(crate_contexts)
}

fn produce_targets(&self, package: &Package) -> CargoResult<Vec<BuildTarget>> {
let full_name = format!("{}-{}", package.name, package.version);
let partial_path = format!("{}/", full_name);
let partial_path_byte_length = partial_path.as_bytes().len();
let mut targets = Vec::new();
for target in package.targets.iter() {
let manifest_pathbuf = PathBuf::from(&package.manifest_path);
Expand Down Expand Up @@ -416,14 +414,10 @@ fn load_and_dedup_licenses(licenses: &str) -> Vec<LicenseData> {
mod tests {
use super::*;
use metadata::Metadata;
use metadata::MetadataFetcher;
use metadata::ResolveNode;
use metadata::testing::StubMetadataFetcher;
use metadata::testing as metadata_testing;
use settings::testing as settings_testing;
use hamcrest::core::expect;
use hamcrest::prelude::*;
use settings::RazeSettings;

const ROOT_NODE_IDX: usize = 0;

Expand Down
1 change: 1 addition & 0 deletions impl/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub enum GenMode {
Remote,
}

#[cfg(test)]
pub mod testing {
use super::*;

Expand Down
5 changes: 4 additions & 1 deletion impl/src/templates/remote_crates.bzl.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def {{workspace.gen_workspace_prefix}}_fetch_remote_crates():
name = "{{workspace.gen_workspace_prefix}}__{{crate.pkg_name | slugify | replace(from="-", to="_")}}__{{crate.pkg_version | slugify | replace(from="-", to="_")}}",
url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/{{crate.pkg_name}}/{{crate.pkg_name}}-{{crate.pkg_version}}.crate",
type = "tar.gz",
strip_prefix = "{{crate.pkg_name}}-{{crate.pkg_version}}",
{% if crate.sha256
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use "{%-" to accomplish the same thing as you're accomplishing by splitting the tag.

Example: https://github.com/google/cargo-raze/blob/master/impl/src/templates/crate.BUILD.template#L23

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer. Done!

%}sha256 = "{{crate.sha256}}",
{% endif
%}strip_prefix = "{{crate.pkg_name}}-{{crate.pkg_version}}",
build_file = "{{workspace.workspace_path}}/remote:{{crate.pkg_name}}-{{crate.pkg_version}}.BUILD"
)
{% endfor %}