cargo_mobile2/android/emulator/
avd_list.rs

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
use super::Emulator;
use crate::{android::env::Env, env::ExplicitEnv, DuctExpressionExt};
use std::{collections::BTreeSet, path::PathBuf};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("Failed to run `adb devices`: {0}")]
    ListAvdsFailed(std::io::Error),
}

pub fn avd_list(env: &Env) -> Result<BTreeSet<Emulator>, Error> {
    duct::cmd(
        PathBuf::from(env.android_home()).join("emulator/emulator"),
        ["-list-avds"],
    )
    .vars(env.explicit_env())
    .stderr_capture()
    .read()
    .map(|raw_list| {
        raw_list
            .split('\n')
            .filter_map(|name| {
                if name.is_empty() || is_emulator_log_line(name) {
                    None
                } else {
                    Some(Emulator::new(name.trim().into()))
                }
            })
            .collect::<BTreeSet<Emulator>>()
    })
    .map_err(Error::ListAvdsFailed)
}

fn is_emulator_log_line(name: &str) -> bool {
    ["INFO    |", "WARNING |", "ERROR   |"]
        .iter()
        .any(|prefix| name.starts_with(prefix))
}