1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::cmd::{Binary, Command, Runnable};
use crate::tools::Tool;
use crate::{Toolchain, Workspace};
use failure::Error;
use std::path::PathBuf;

pub(crate) struct BinaryCrate {
    pub(super) crate_name: &'static str,
    pub(super) binary: &'static str,
    pub(super) cargo_subcommand: Option<&'static str>,
}

impl BinaryCrate {
    pub(crate) fn binary_path(&self, workspace: &Workspace) -> PathBuf {
        Tool::binary_path(self, workspace)
    }
}

impl Runnable for BinaryCrate {
    fn name(&self) -> Binary {
        Binary::ManagedByRustwide(if self.cargo_subcommand.is_some() {
            "cargo".into()
        } else {
            self.binary.into()
        })
    }

    fn prepare_command<'w, 'pl>(&self, mut cmd: Command<'w, 'pl>) -> Command<'w, 'pl> {
        if let Some(subcommand) = self.cargo_subcommand {
            cmd = cmd.args(&[subcommand]);
        }
        cmd
    }
}

impl Tool for BinaryCrate {
    fn name(&self) -> &'static str {
        self.binary
    }

    fn is_installed(&self, workspace: &Workspace) -> Result<bool, Error> {
        let path = self.binary_path(workspace);
        if !path.is_file() {
            return Ok(false);
        }

        Ok(crate::native::is_executable(path)?)
    }

    fn install(&self, workspace: &Workspace, fast_install: bool) -> Result<(), Error> {
        let mut cmd = Command::new(workspace, &Toolchain::MAIN.cargo())
            .args(&["install", self.crate_name])
            .timeout(None);
        if fast_install {
            cmd = cmd.args(&["--debug"]);
        }
        cmd.run()?;
        Ok(())
    }

    fn update(&self, workspace: &Workspace, fast_install: bool) -> Result<(), Error> {
        self.install(workspace, fast_install)
    }
}