|
| 1 | +use crate::config::ConfigInfo; |
| 2 | +use crate::utils::{git_clone, run_command_with_output}; |
| 3 | + |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | + |
| 6 | +fn show_usage() { |
| 7 | + println!( |
| 8 | + r#" |
| 9 | +`clone-gcc` command help: |
| 10 | +
|
| 11 | + --out-path : Location where the GCC repository will be cloned (default: `./gcc`)"# |
| 12 | + ); |
| 13 | + ConfigInfo::show_usage(); |
| 14 | + println!(" --help : Show this help"); |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Default)] |
| 18 | +struct Args { |
| 19 | + out_path: PathBuf, |
| 20 | + config_info: ConfigInfo, |
| 21 | +} |
| 22 | + |
| 23 | +impl Args { |
| 24 | + fn new() -> Result<Option<Self>, String> { |
| 25 | + let mut command_args = Self::default(); |
| 26 | + |
| 27 | + let mut out_path = None; |
| 28 | + |
| 29 | + // We skip binary name and the `clone-gcc` command. |
| 30 | + let mut args = std::env::args().skip(2); |
| 31 | + |
| 32 | + while let Some(arg) = args.next() { |
| 33 | + match arg.as_str() { |
| 34 | + "--out-path" => match args.next() { |
| 35 | + Some(path) if !path.is_empty() => out_path = Some(path), |
| 36 | + _ => { |
| 37 | + return Err("Expected an argument after `--out-path`, found nothing".into()) |
| 38 | + } |
| 39 | + }, |
| 40 | + "--help" => { |
| 41 | + show_usage(); |
| 42 | + return Ok(None); |
| 43 | + } |
| 44 | + arg => { |
| 45 | + if !command_args.config_info.parse_argument(arg, &mut args)? { |
| 46 | + return Err(format!("Unknown option {}", arg)); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + command_args.out_path = match out_path { |
| 52 | + Some(p) => p.into(), |
| 53 | + None => PathBuf::from("./gcc"), |
| 54 | + }; |
| 55 | + return Ok(Some(command_args)); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +pub fn run() -> Result<(), String> { |
| 60 | + let Some(args) = Args::new()? else { |
| 61 | + return Ok(()); |
| 62 | + }; |
| 63 | + |
| 64 | + let result = git_clone("https://github.com/antoyo/gcc", Some(&args.out_path), false)?; |
| 65 | + if result.ran_clone { |
| 66 | + let gcc_commit = args.config_info.get_gcc_commit()?; |
| 67 | + println!("Checking out GCC commit `{}`...", gcc_commit); |
| 68 | + run_command_with_output( |
| 69 | + &[&"git", &"checkout", &gcc_commit], |
| 70 | + Some(Path::new(&result.repo_dir)), |
| 71 | + )?; |
| 72 | + } else { |
| 73 | + println!( |
| 74 | + "There is already a GCC folder in `{}`, leaving things as is...", |
| 75 | + args.out_path.display() |
| 76 | + ); |
| 77 | + } |
| 78 | + Ok(()) |
| 79 | +} |
0 commit comments