Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
Restructured opt module. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrisco authored Oct 14, 2021
1 parent 3541daf commit 9a5229f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 109 deletions.
10 changes: 1 addition & 9 deletions tools/cargo_bazel/src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,9 @@ fn cargo_meta_pkg_to_locked_pkg<'a>(
pkg: &Package,
lock_packages: &'a [cargo_lock::Package],
) -> Option<&'a cargo_lock::Package> {
match lock_packages
lock_packages
.iter()
.find(|lock_pkg| lock_pkg.name.as_str() == pkg.name && lock_pkg.version == pkg.version)
{
Some(s) => Some(s),
None => {
println!("{:?}", pkg);
println!("{:#?}", lock_packages);
None
}
}
}

#[cfg(test)]
Expand Down
49 changes: 48 additions & 1 deletion tools/cargo_bazel/src/cli/generate.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
//! TODO
use std::path::PathBuf;

use anyhow::{bail, Result};
use structopt::StructOpt;

use crate::annotation::Annotations;
use crate::cli::opt::GenerateOptions;
use crate::config::Config;
use crate::context::Context;
use crate::lockfile::{is_cargo_lockfile, write_lockfile, LockfileKind};
use crate::metadata::{Generator, MetadataGenerator};
use crate::rendering::{write_outputs, Renderer};

/// Command line options for the `generate` subcommand
#[derive(StructOpt, Debug)]
pub struct GenerateOptions {
/// The path to a Cargo binary to use for gathering metadata
#[structopt(long, env = "CARGO")]
pub cargo: Option<PathBuf>,

/// The path to a rustc binary for use with Cargo
#[structopt(long, env = "RUSTC")]
pub rustc: Option<PathBuf>,

/// The config file with information about the Bazel and Cargo workspace
#[structopt(long)]
pub config: PathBuf,

/// The path to either a Cargo or Bazel lockfile
#[structopt(long)]
pub lockfile: PathBuf,

/// The type of lockfile
#[structopt(long)]
pub lockfile_kind: LockfileKind,

/// The directory of the current repository rule
#[structopt(long)]
pub repository_dir: PathBuf,

/// A [Cargo config](https://doc.rust-lang.org/cargo/reference/config.html#configuration)
/// file to use when gathering metadata
#[structopt(long)]
pub cargo_config: Option<PathBuf>,

/// Whether or not to ignore the provided lockfile and re-generate one
#[structopt(long)]
pub repin: bool,

/// The root manifest used to generate metadata
#[structopt(long)]
pub manifest: Option<PathBuf>,

/// If true, outputs will be printed instead of written to disk.
#[structopt(long)]
pub dry_run: bool,
}

pub fn generate(opt: GenerateOptions) -> Result<()> {
// Load the config
let config = Config::try_from_path(&opt.config)?;
Expand Down
100 changes: 3 additions & 97 deletions tools/cargo_bazel/src/cli/opt.rs
Original file line number Diff line number Diff line change
@@ -1,104 +1,10 @@
//! A collection of command line options
use std::path::PathBuf;

use structopt::StructOpt;

use crate::lockfile::LockfileKind;

/// Command line options for the `generate` subcommand
#[derive(StructOpt, Debug)]
pub struct GenerateOptions {
/// The path to a Cargo binary to use for gathering metadata
#[structopt(long, env = "CARGO")]
pub cargo: Option<PathBuf>,

/// The path to a rustc binary for use with Cargo
#[structopt(long, env = "RUSTC")]
pub rustc: Option<PathBuf>,

/// The config file with information about the Bazel and Cargo workspace
#[structopt(long)]
pub config: PathBuf,

/// The path to either a Cargo or Bazel lockfile
#[structopt(long)]
pub lockfile: PathBuf,

/// The type of lockfile
#[structopt(long)]
pub lockfile_kind: LockfileKind,

/// The directory of the current repository rule
#[structopt(long)]
pub repository_dir: PathBuf,

/// A [Cargo config](https://doc.rust-lang.org/cargo/reference/config.html#configuration)
/// file to use when gathering metadata
#[structopt(long)]
pub cargo_config: Option<PathBuf>,

/// Whether or not to ignore the provided lockfile and re-generate one
#[structopt(long)]
pub repin: bool,

/// The root manifest used to generate metadata
#[structopt(long)]
pub manifest: Option<PathBuf>,

/// If true, outputs will be printed instead of written to disk.
#[structopt(long)]
pub dry_run: bool,
}

/// Command line options for the `splice` subcommand
#[derive(StructOpt, Debug)]
pub struct SpliceOptions {
/// A generated manifest of splicing inputs
#[structopt(long)]
pub splicing_manifest: PathBuf,

/// A Cargo lockfile (Cargo.lock).
#[structopt(long)]
pub cargo_lockfile: Option<PathBuf>,

/// The directory in which to build the workspace. A `Cargo.toml` file
/// should always be produced within this directory.
#[structopt(long)]
pub workspace_dir: PathBuf,

/// If true, outputs will be printed instead of written to disk.
#[structopt(long)]
pub dry_run: bool,

/// The path to a Cargo binary to use for gathering metadata
#[structopt(long, env = "CARGO")]
pub cargo: PathBuf,

/// The path to a rustc binary for use with Cargo
#[structopt(long, env = "RUSTC")]
pub rustc: PathBuf,
}

/// Command line options for the `query` subcommand
#[derive(StructOpt, Debug)]
pub struct QueryOptions {
/// The lockfile path for reproducible Cargo->Bazel renderings
#[structopt(long)]
pub lockfile: PathBuf,

/// The config file with information about the Bazel and Cargo workspace
#[structopt(long)]
pub config: PathBuf,

/// The path to a Cargo binary to use for gathering metadata
#[structopt(long, env = "CARGO")]
pub cargo: PathBuf,

/// The path to a rustc binary for use with Cargo
#[structopt(long, env = "RUSTC")]
pub rustc: PathBuf,
}
use super::generate::GenerateOptions;
use super::query::QueryOptions;
use super::splice::SpliceOptions;

#[derive(StructOpt, Debug)]
#[structopt(name = "cargo-bazel")]
Expand Down
23 changes: 22 additions & 1 deletion tools/cargo_bazel/src/cli/query.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
//!
use std::fs;
use std::path::PathBuf;

use anyhow::Result;
use serde::Deserialize;
use structopt::StructOpt;

use crate::cli::opt::QueryOptions;
use crate::config::Config;
use crate::digest::Digest;

/// Command line options for the `query` subcommand
#[derive(StructOpt, Debug)]
pub struct QueryOptions {
/// The lockfile path for reproducible Cargo->Bazel renderings
#[structopt(long)]
pub lockfile: PathBuf,

/// The config file with information about the Bazel and Cargo workspace
#[structopt(long)]
pub config: PathBuf,

/// The path to a Cargo binary to use for gathering metadata
#[structopt(long, env = "CARGO")]
pub cargo: PathBuf,

/// The path to a rustc binary for use with Cargo
#[structopt(long, env = "RUSTC")]
pub rustc: PathBuf,
}

#[derive(Debug, Deserialize)]
struct Lockfile {
pub checksum: Option<Digest>,
Expand Down
33 changes: 32 additions & 1 deletion tools/cargo_bazel/src/cli/splice.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
//! TODO
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;

use crate::cli::opt::SpliceOptions;
use structopt::StructOpt;

use crate::cli::Result;
use crate::splicing::{Splicer, SplicingManifest};

/// Command line options for the `splice` subcommand
#[derive(StructOpt, Debug)]
pub struct SpliceOptions {
/// A generated manifest of splicing inputs
#[structopt(long)]
pub splicing_manifest: PathBuf,

/// A Cargo lockfile (Cargo.lock).
#[structopt(long)]
pub cargo_lockfile: Option<PathBuf>,

/// The directory in which to build the workspace. A `Cargo.toml` file
/// should always be produced within this directory.
#[structopt(long)]
pub workspace_dir: PathBuf,

/// If true, outputs will be printed instead of written to disk.
#[structopt(long)]
pub dry_run: bool,

/// The path to a Cargo binary to use for gathering metadata
#[structopt(long, env = "CARGO")]
pub cargo: PathBuf,

/// The path to a rustc binary for use with Cargo
#[structopt(long, env = "RUSTC")]
pub rustc: PathBuf,
}

/// Combine a set of disjoint manifests into a single workspace.
pub fn splice(opt: SpliceOptions) -> Result<()> {
// Load the "Splicing manifest"
Expand Down

0 comments on commit 9a5229f

Please sign in to comment.