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

Remove remaining dependencies on Cargo #156

Merged
merged 1 commit into from
May 14, 2020
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
1,973 changes: 471 additions & 1,502 deletions impl/Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ name = "cargo-raze"
path = "src/bin/cargo-raze.rs"

[dependencies]
failure = "0.1.5"
anyhow = "1.0.30"
docopt = "1.0.2"
cargo = "0.42.0"
cargo-lock = "4.0.1"
cargo-platform = "0.1.0"
cargo_metadata = "0.9.1"
Expand Down
9 changes: 5 additions & 4 deletions impl/src/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use cargo::CargoResult;
use anyhow::Result;

use tera::{self, Context, Tera};

use crate::{
Expand Down Expand Up @@ -136,7 +137,7 @@ impl BazelRenderer {
fn include_additional_build_file(
package: &CrateContext,
existing_contents: String,
) -> CargoResult<String> {
) -> Result<String> {
match &package.raze_settings.additional_build_file {
Some(file_path) => {
let additional_content =
Expand All @@ -160,7 +161,7 @@ impl BuildRenderer for BazelRenderer {
&mut self,
render_details: &RenderDetails,
planned_build: &PlannedBuild,
) -> CargoResult<Vec<FileOutputs>> {
) -> Result<Vec<FileOutputs>> {
let &RenderDetails {
ref path_prefix,
ref buildfile_suffix,
Expand Down Expand Up @@ -210,7 +211,7 @@ impl BuildRenderer for BazelRenderer {
&mut self,
render_details: &RenderDetails,
planned_build: &PlannedBuild,
) -> CargoResult<Vec<FileOutputs>> {
) -> Result<Vec<FileOutputs>> {
let &RenderDetails {
ref path_prefix,
ref buildfile_suffix,
Expand Down
35 changes: 7 additions & 28 deletions impl/src/bin/cargo-raze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
path::{Path, PathBuf},
};

use cargo::{util::Config, CargoResult, CliResult};
use anyhow::Result;

use docopt::Docopt;

Expand Down Expand Up @@ -62,32 +62,11 @@ Options:
--cargo-bin-path=<PATH> Path to the cargo binary to be used for loading workspace metadata
"#;

fn main() {
let mut config = Config::default().unwrap();

let options = Docopt::new(USAGE)
fn main() -> Result<()> {
let options: Options = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());

let result = real_main(&options, &mut config);

if let Err(e) = result {
cargo::exit_with_error(e, &mut *config.shell());
}
}

fn real_main(options: &Options, cargo_config: &mut Config) -> CliResult {
cargo_config.configure(
options.flag_verbose,
options.flag_quiet,
&options.flag_color,
/* frozen = */ false,
/* locked = */ false,
/* offline */ false,
/* target_dir = */ &None,
&[],
)?;

let mut settings = load_settings("Cargo.toml")?;
println!("Loaded override settings: {:#?}", settings);

Expand Down Expand Up @@ -120,7 +99,7 @@ fn real_main(options: &Options, cargo_config: &mut Config) -> CliResult {
GenMode::Remote => {
// Create "remote/" if it doesn't exist
if fs::metadata("remote/").is_err() {
fs::create_dir("remote/").map_err(failure::Error::from)?;
fs::create_dir("remote/")?;
}

bazel_renderer.render_remote_planned_build(&render_details, &planned_build)?
Expand All @@ -140,7 +119,7 @@ fn real_main(options: &Options, cargo_config: &mut Config) -> CliResult {
}

/** Verifies that the provided settings make sense. */
fn validate_settings(settings: &mut RazeSettings) -> CargoResult<()> {
fn validate_settings(settings: &mut RazeSettings) -> Result<()> {
if !settings.workspace_path.starts_with("//") {
return Err(
RazeError::Config {
Expand All @@ -162,13 +141,13 @@ fn validate_settings(settings: &mut RazeSettings) -> CargoResult<()> {
Ok(())
}

fn write_to_file_loudly(path: &str, contents: &str) -> CargoResult<()> {
fn write_to_file_loudly(path: &str, contents: &str) -> Result<()> {
File::create(&path).and_then(|mut f| f.write_all(contents.as_bytes()))?;
println!("Generated {} successfully", path);
Ok(())
}

fn load_settings<T: AsRef<Path>>(cargo_toml_path: T) -> CargoResult<RazeSettings> {
fn load_settings<T: AsRef<Path>>(cargo_toml_path: T) -> Result<RazeSettings> {
let path = cargo_toml_path.as_ref();
let mut toml = File::open(path)?;
let mut toml_contents = String::new();
Expand Down
10 changes: 5 additions & 5 deletions impl/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use cargo::CargoResult;
use std::{fs, path::PathBuf};

use anyhow::Result;

use cargo_metadata::MetadataCommand;
pub use cargo_metadata::{DependencyKind, Metadata, Node, Package, PackageId};

use std::{fs, path::PathBuf};

use tempdir::TempDir;

const SYSTEM_CARGO_BIN_PATH: &str = "cargo";
Expand All @@ -30,7 +30,7 @@ const SYSTEM_CARGO_BIN_PATH: &str = "cargo";
* <https://github.com/rust-lang/cargo/pull/5122>
*/
pub trait MetadataFetcher {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> CargoResult<Metadata>;
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> Result<Metadata>;
}

/** The local Cargo workspace files to be used for build planning .*/
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Default for CargoMetadataFetcher {
}

impl MetadataFetcher for CargoMetadataFetcher {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> CargoResult<Metadata> {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> Result<Metadata> {
assert!(files.toml_path.is_file());
assert!(files.lock_path_opt.as_ref().map_or(true, |p| p.is_file()));

Expand Down
52 changes: 29 additions & 23 deletions impl/src/planning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use std::{
str::{self, FromStr},
};

use cargo::{core::SourceId, CargoResult};
use anyhow::Result;

use cargo_lock::lockfile::Lockfile;
use cargo_lock::SourceId;
use cargo_platform::Platform;

use itertools::Itertools;
Expand Down Expand Up @@ -50,7 +52,7 @@ pub trait BuildPlanner {
settings: &RazeSettings,
files: CargoWorkspaceFiles,
platform_details: PlatformDetails,
) -> CargoResult<PlannedBuild>;
) -> Result<PlannedBuild>;
}

/** A set of named dependencies (without version) derived from a package manifest. */
Expand Down Expand Up @@ -252,7 +254,7 @@ impl CrateCatalogEntry {

impl CrateCatalog {
/** Produces a CrateCatalog using the package entries from a metadata blob.*/
pub fn new(metadata: &Metadata) -> CargoResult<Self> {
pub fn new(metadata: &Metadata) -> Result<Self> {
let resolve = metadata
.resolve
.as_ref()
Expand Down Expand Up @@ -330,7 +332,7 @@ impl<'fetcher> BuildPlanner for BuildPlannerImpl<'fetcher> {
settings: &RazeSettings,
files: CargoWorkspaceFiles,
platform_details: PlatformDetails,
) -> CargoResult<PlannedBuild> {
) -> Result<PlannedBuild> {
let metadata = self.metadata_fetcher.fetch_metadata(&files)?;
let crate_catalog = CrateCatalog::new(&metadata)?;

Expand All @@ -354,7 +356,7 @@ impl<'fetcher> BuildPlannerImpl<'fetcher> {

impl<'planner> WorkspaceSubplanner<'planner> {
/** Produces a planned build using internal state. */
pub fn produce_planned_build(&self) -> CargoResult<PlannedBuild> {
pub fn produce_planned_build(&self) -> Result<PlannedBuild> {
checks::check_resolve_matches_packages(&self.metadata)?;

if self.settings.genmode != GenMode::Remote {
Expand Down Expand Up @@ -382,7 +384,7 @@ impl<'planner> WorkspaceSubplanner<'planner> {
}

/** Produces a crate context for each declared crate and dependency. */
fn produce_crate_contexts(&self) -> CargoResult<Vec<CrateContext>> {
fn produce_crate_contexts(&self) -> Result<Vec<CrateContext>> {
// Gather the checksums for all packages in the lockfile
// which have them.
//
Expand Down Expand Up @@ -464,7 +466,7 @@ impl<'planner> WorkspaceSubplanner<'planner> {

impl<'planner> CrateSubplanner<'planner> {
/** Builds a crate context from internal state. */
fn produce_context(&self) -> CargoResult<CrateContext> {
fn produce_context(&self) -> Result<CrateContext> {
let DependencySet {
build_deps,
dev_deps,
Expand Down Expand Up @@ -517,7 +519,7 @@ impl<'planner> CrateSubplanner<'planner> {
}

/** Generates the set of dependencies for the contained crate. */
fn produce_deps(&self) -> CargoResult<DependencySet> {
fn produce_deps(&self) -> Result<DependencySet> {
let DependencyNames {
build_dep_names,
dev_dep_names,
Expand Down Expand Up @@ -585,7 +587,7 @@ impl<'planner> CrateSubplanner<'planner> {
}

/** Yields the list of dependencies as described by the manifest (without version). */
fn identify_named_deps(&self) -> CargoResult<DependencyNames> {
fn identify_named_deps(&self) -> Result<DependencyNames> {
// Resolve dependencies into types
let mut build_dep_names = Vec::new();
let mut dev_dep_names = Vec::new();
Expand Down Expand Up @@ -634,10 +636,14 @@ impl<'planner> CrateSubplanner<'planner> {
/** Generates source details for internal crate. */
fn produce_source_details(&self) -> SourceDetails {
SourceDetails {
git_data: self.source_id.filter(|id| id.is_git()).map(|id| GitRepo {
remote: id.url().to_string(),
commit: id.precise().unwrap().to_owned(),
}),
git_data: self
.source_id
.as_ref()
.filter(|id| id.is_git())
.map(|id| GitRepo {
remote: id.url().to_string(),
commit: id.precise().unwrap().to_owned(),
}),
}
}

Expand Down Expand Up @@ -667,7 +673,7 @@ impl<'planner> CrateSubplanner<'planner> {
* This function may access the file system. See #find_package_root_for_manifest for more
* details.
*/
fn produce_targets(&self) -> CargoResult<Vec<BuildableTarget>> {
fn produce_targets(&self) -> Result<Vec<BuildableTarget>> {
let mut targets = Vec::new();
let package = self.crate_catalog_entry.package();
for target in &package.targets {
Expand Down Expand Up @@ -712,9 +718,9 @@ impl<'planner> CrateSubplanner<'planner> {
* often aren't solely the crate of interest, but rather a repository that contains the crate of
* interest among others.
*/
fn find_package_root_for_manifest(&self, manifest_path: &PathBuf) -> CargoResult<PathBuf> {
fn find_package_root_for_manifest(&self, manifest_path: &PathBuf) -> Result<PathBuf> {
let has_git_repo_root = {
let is_git = self.source_id.map_or(false, SourceId::is_git);
let is_git = self.source_id.as_ref().map_or(false, SourceId::is_git);
is_git && self.settings.genmode == GenMode::Remote
};

Expand Down Expand Up @@ -777,7 +783,7 @@ mod checks {
env, fs,
};

use cargo::util::CargoResult;
use anyhow::Result;

use crate::{
metadata::{Metadata, Package, PackageId},
Expand All @@ -791,7 +797,7 @@ mod checks {
const MAX_DISPLAYED_MISSING_RESOLVE_PACKAGES: usize = 5;

// Verifies that all provided packages are vendored (in VENDOR_DIR relative to CWD)
pub fn check_all_vendored(crate_catalog_entries: &[CrateCatalogEntry]) -> CargoResult<()> {
pub fn check_all_vendored(crate_catalog_entries: &[CrateCatalogEntry]) -> Result<()> {
let missing_package_ident_iter = crate_catalog_entries
.iter()
// Root does not need to be vendored -- usually it is a wrapper package.
Expand Down Expand Up @@ -823,7 +829,7 @@ mod checks {
}.into())
}

pub fn check_resolve_matches_packages(metadata: &Metadata) -> CargoResult<()> {
pub fn check_resolve_matches_packages(metadata: &Metadata) -> Result<()> {
let known_package_ids = metadata
.packages
.iter()
Expand Down Expand Up @@ -1021,7 +1027,7 @@ dependencies = [
}

impl MetadataFetcher for ResolveDroppingMetadataFetcher {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> CargoResult<Metadata> {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> Result<Metadata> {
let mut metadata = self.fetcher.fetch_metadata(&files)?;
assert!(metadata.resolve.is_some());
metadata.resolve = None;
Expand Down Expand Up @@ -1050,7 +1056,7 @@ dependencies = [
}

impl MetadataFetcher for PackageDroppingMetadataFetcher {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> CargoResult<Metadata> {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> Result<Metadata> {
let mut metadata = self.fetcher.fetch_metadata(&files)?;
metadata.packages.clear();
Ok(metadata)
Expand Down Expand Up @@ -1095,7 +1101,7 @@ dependencies = [
}

impl MetadataFetcher for DependencyInjectingMetadataFetcher {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> CargoResult<Metadata> {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> Result<Metadata> {
let mut metadata = self.fetcher.fetch_metadata(&files)?;

// Phase 1: Add a dummy dependency to the dependency graph.
Expand Down Expand Up @@ -1188,7 +1194,7 @@ dependencies = [
}

impl MetadataFetcher for WorkspaceCrateMetadataFetcher {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> CargoResult<Metadata> {
fn fetch_metadata(&mut self, files: &CargoWorkspaceFiles) -> Result<Metadata> {
let mut metadata = self.fetcher.fetch_metadata(&files)?;

// Phase 1: Create a workspace package, add it to the packages list.
Expand Down
6 changes: 3 additions & 3 deletions impl/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::planning::PlannedBuild;
use cargo::util::CargoResult;
use anyhow::Result;

/**
* An object that can convert a prepared build plan into a series of files for a Bazel-like build
Expand All @@ -24,12 +24,12 @@ pub trait BuildRenderer {
&mut self,
render_details: &RenderDetails,
planned_build: &PlannedBuild,
) -> CargoResult<Vec<FileOutputs>>;
) -> Result<Vec<FileOutputs>>;
fn render_remote_planned_build(
&mut self,
render_details: &RenderDetails,
planned_build: &PlannedBuild,
) -> CargoResult<Vec<FileOutputs>>;
) -> Result<Vec<FileOutputs>>;
}

#[derive(Debug, Clone)]
Expand Down
Loading