Skip to content

Commit

Permalink
fixup! devices/adb: Find adb executable in $ANDROID_HOME/`$ANDROID_…
Browse files Browse the repository at this point in the history
…SDK_ROOT`
  • Loading branch information
MarijnS95 committed Aug 17, 2023
1 parent ef610fa commit 2317648
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions xbuild/src/command/doctor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anyhow::Result;
use anyhow::{bail, Result};
use std::path::PathBuf;
use std::process::Command;

#[derive(Clone, Debug)]
use crate::devices::adb::Adb;

#[derive(Debug)]
pub struct Doctor {
groups: Vec<Group>,
}
Expand Down Expand Up @@ -35,7 +37,11 @@ impl Default for Doctor {
Group {
name: "android",
checks: vec![
Check::new("adb", Some(VersionCheck::new("--version", 0, 4))),
Check::with_path(
"adb",
Adb::which(),
Some(VersionCheck::new("--version", 0, 4)),
),
Check::new("javac", Some(VersionCheck::new("--version", 0, 1))),
Check::new("java", Some(VersionCheck::new("--version", 0, 1))),
Check::new("kotlin", Some(VersionCheck::new("-version", 0, 2))),
Expand Down Expand Up @@ -77,7 +83,7 @@ impl std::fmt::Display for Doctor {
}
}

#[derive(Clone, Debug)]
#[derive(Debug)]
struct Group {
name: &'static str,
checks: Vec<Check>,
Expand Down Expand Up @@ -105,15 +111,32 @@ impl std::fmt::Display for Group {
}
}

#[derive(Clone, Copy, Debug)]
#[derive(Debug)]
struct Check {
name: &'static str,
location: Option<Result<PathBuf>>,
version: Option<VersionCheck>,
}

impl Check {
pub const fn new(name: &'static str, version: Option<VersionCheck>) -> Self {
Self { name, version }
Self {
name,
location: None,
version,
}
}

pub const fn with_path(
name: &'static str,
path: Result<PathBuf>,
version: Option<VersionCheck>,
) -> Self {
Self {
name,
location: Some(path),
version,
}
}
}

Expand All @@ -131,22 +154,27 @@ impl VersionCheck {
}

impl Check {
fn name(self) -> &'static str {
fn name(&self) -> &'static str {
self.name
}

fn path(self) -> Result<PathBuf> {
Ok(which::which(self.name)?)
fn path(&self) -> Result<PathBuf> {
Ok(match &self.location {
Some(Ok(path)) => path.clone(),
// Cannot clone the error:
Some(Err(e)) => bail!("{:?}", e),
None => which::which(self.name)?,
})
}

fn version(self) -> Result<Option<String>> {
fn version(&self) -> Result<Option<String>> {
if let Some(version) = self.version {
let output = Command::new(self.name)
let output = Command::new(self.path()?)
.args(version.arg.split(' '))
.output()?;
anyhow::ensure!(output.status.success(), "failed to run {}", self.name);
let output = std::str::from_utf8(&output.stdout)?;
if let Some(line) = output.split('\n').nth(version.row as _) {
if let Some(line) = output.lines().nth(version.row as _) {
let mut col = version.col as usize;
if line.starts_with("Apple ") || line.starts_with("Homebrew ") {
col += 1;
Expand Down

0 comments on commit 2317648

Please # to comment.