tools/corrosion/generator/src/main.rs
branchtransitional_engine
changeset 16038 d903f8d2395a
parent 16036 7b8d96fc8799
equal deleted inserted replaced
16036:7b8d96fc8799 16038:d903f8d2395a
     1 use std::path::PathBuf;
       
     2 
       
     3 use cargo_metadata::Metadata;
       
     4 use clap::{App, Arg};
       
     5 
       
     6 mod subcommands {
       
     7     pub mod gen_cmake;
       
     8 }
       
     9 
       
    10 use subcommands::*;
       
    11 
       
    12 // common options
       
    13 const MANIFEST_PATH: &str = "manifest-path";
       
    14 const CARGO_EXECUTABLE: &str = "cargo-executable";
       
    15 const VERBOSE: &str = "verbose";
       
    16 const LOCKED: &str = "locked";
       
    17 const FROZEN: &str = "frozen";
       
    18 
       
    19 pub struct GeneratorSharedArgs {
       
    20     pub manifest_path: PathBuf,
       
    21     pub cargo_executable: PathBuf,
       
    22     pub metadata: Metadata,
       
    23     pub verbose: bool,
       
    24 }
       
    25 
       
    26 fn main() -> Result<(), Box<dyn std::error::Error>> {
       
    27     let matches = App::new("CMake Generator for Cargo")
       
    28         .version("0.1")
       
    29         .author("Andrew Gaspar <andrew.gaspar@outlook.com>")
       
    30         .about("Generates CMake files for Cargo projects")
       
    31         .arg(
       
    32             Arg::with_name(MANIFEST_PATH)
       
    33                 .long("manifest-path")
       
    34                 .value_name("Cargo.toml")
       
    35                 .help("Specifies the target Cargo project")
       
    36                 .required(true)
       
    37                 .takes_value(true),
       
    38         )
       
    39         .arg(
       
    40             Arg::with_name(CARGO_EXECUTABLE)
       
    41                 .long("cargo")
       
    42                 .value_name("EXECUTABLE")
       
    43                 .required(true)
       
    44                 .help("Path to the cargo executable to use"),
       
    45         )
       
    46         .arg(
       
    47             Arg::with_name(VERBOSE)
       
    48                 .long("verbose")
       
    49                 .help("Request verbose output"),
       
    50         )
       
    51         .arg(
       
    52             Arg::with_name(LOCKED)
       
    53                 .long("locked")
       
    54                 .help("Pass --locked to cargo invocations"),
       
    55         )
       
    56         .arg(
       
    57             Arg::with_name(FROZEN)
       
    58                 .long("frozen")
       
    59                 .help("Pass --frozen to cargo invocations"),
       
    60         )
       
    61         .subcommand(gen_cmake::subcommand())
       
    62         .get_matches();
       
    63 
       
    64     let mut cmd = cargo_metadata::MetadataCommand::new();
       
    65     cmd.no_deps();
       
    66     if matches.is_present(LOCKED) {
       
    67         cmd.other_options(["--locked".into()]);
       
    68     }
       
    69     if matches.is_present(FROZEN) {
       
    70         cmd.other_options(["--frozen".into()]);
       
    71     }
       
    72 
       
    73     let manifest_path = matches.value_of(MANIFEST_PATH).unwrap();
       
    74     let cargo_executable = matches.value_of(CARGO_EXECUTABLE).unwrap();
       
    75 
       
    76     cmd.manifest_path(manifest_path);
       
    77     cmd.cargo_path(cargo_executable);
       
    78 
       
    79     let metadata = cmd.exec()?;
       
    80 
       
    81     let shared_args = GeneratorSharedArgs {
       
    82         manifest_path: manifest_path.into(),
       
    83         cargo_executable: cargo_executable.into(),
       
    84         metadata,
       
    85         verbose: matches.is_present(VERBOSE),
       
    86     };
       
    87 
       
    88     match matches.subcommand() {
       
    89         (gen_cmake::GEN_CMAKE, Some(matches)) => gen_cmake::invoke(&shared_args, matches)?,
       
    90         _ => unreachable!(),
       
    91     };
       
    92 
       
    93     // We should never reach this statement
       
    94     std::process::exit(1);
       
    95 }