cargo_glab/
lib.rs

1mod rust_toolchain;
2use std::{
3    fs,
4    path::{Path, PathBuf},
5    process::Command,
6};
7
8use anyhow::{bail, Result};
9use clap::{Args, Parser};
10use toml;
11
12use crate::rust_toolchain::RustToolchainFile;
13
14#[derive(Parser, Debug)]
15#[command(name = "cargo", bin_name = "cargo")]
16pub enum CargoArgs {
17    Glab(GlabArgs),
18}
19
20#[derive(Args, Debug)]
21#[command(author, version, about, long_about = None)]
22pub struct GlabArgs {
23    /// Include this arg to actually perform the release.
24    ///
25    /// This includes publishing compressed artifacts to the
26    /// package registry, creating permalinks for the release,
27    /// and publishing a release.
28    #[arg(long, short)]
29    pub execute: bool,
30
31    /// Force replace a release, binaries, and permalinks.
32    ///
33    /// Command will fail otherwise if resources already exist.
34    #[arg(long, short)]
35    pub force: bool,
36}
37
38pub fn get_targets() -> Result<Vec<String>> {
39    if Path::new("rust-toolchain.toml").exists() {
40        let rust_toolchain: RustToolchainFile =
41            toml::from_str(fs::read_to_string("rust-toolchain.toml")?.as_str())?;
42        Ok(rust_toolchain.toolchain.targets)
43    } else {
44        let output = Command::new("rustc")
45            .args(&["--version", "--verbose"])
46            .output()?;
47
48        let output = String::from_utf8(output.stdout)?;
49
50        Ok(output
51            .lines()
52            .find(|l| l.starts_with("host: "))
53            .map(|l| l.trim_start_matches("host: ").to_string())
54            .into_iter()
55            .collect())
56    }
57}
58
59pub fn build_targets(targets: &Vec<String>) -> Result<()> {
60    for target in targets {
61        if PathBuf::from("src/lib.rs").exists() {
62            let mut lib_build = Command::new("cargo")
63                .arg("build")
64                .arg(format!("--target={target}"))
65                .arg("--release")
66                .spawn()?;
67            let lib_status = lib_build.wait()?;
68            eprintln!(
69                "Lib build for target {target} finished building with status of {lib_status}"
70            );
71
72            if !lib_status.success() {
73                bail!("Build failed for {target}")
74            }
75        }
76        let mut build = Command::new("cargo")
77            .arg("build")
78            .arg(format!("--target={target}"))
79            .arg("--release")
80            .spawn()?;
81
82        let status = build.wait()?;
83
84        eprintln!("Build for target {target} finished building with status of {status}");
85
86        if !status.success() {
87            bail!("Build failed for {target}")
88        }
89    }
90
91    Ok(())
92}