radstudio 0.8.0

A utility library for the Embarcadero RAD Studio IDE
use crate::Platform;
use std::{
    fs::File,
    io::{BufRead, BufReader},
    os::windows::process::CommandExt,
    path::{Path, PathBuf},
    process::{Command, ExitStatus},
};
use tempfile::NamedTempFile;

pub struct Bds {
    path: PathBuf,
}

impl Bds {
    pub(crate) fn new(path: PathBuf) -> Self {
        Self { path }
    }

    pub fn build(&self, file: impl AsRef<Path>, no_splash: bool) -> crate::Result<ExitStatus> {
        let output = NamedTempFile::new()?.into_temp_path();
        let mut cmd = Command::new(&self.path);
        cmd.arg(file.as_ref())
            .arg("-b")
            .raw_arg(format!(r#"-o"{}""#, output.display()));
        if no_splash {
            cmd.arg("-ns");
        };

        println!("Starting bds.exe and building...");
        let status = cmd.status()?;

        let mut indent = false;
        let reader = BufReader::new(File::open(output)?);
        for line in reader.lines() {
            let line = line?;
            if line.starts_with("  ") {
                indent = true;
                print!("{}", line.trim_start());
            } else {
                if indent {
                    println!("\n")
                };
                indent = false;
                println!("{}", line.trim_start_matches('\u{FEFF}'))
            }
        }

        Ok(status)
    }
}

impl crate::msbuild::Execute for Bds {
    fn execute(
        &self,
        platform: &Option<Platform>,
        options: &crate::msbuild::Options,
    ) -> crate::Result<ExitStatus> {
        let file = crate::msbuild::FileInfo::from(&options.file);
        let temps = crate::msbuild::patch_project_file(&file, platform, options)?;
        let input: &Path = match &temps {
            Some(temps) => &temps[0],
            None => &options.file,
        };
        self.build(input, options.no_logo)
    }
}