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
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::error::*;
use std::path::{Path, PathBuf};
use std::process::Command;

/// ## Deploy APKs to a connected device
/// After you generate a set of APKs, bundletool can deploy the right combination of APKs
/// from that set to a connected device For example, if you have a connected device
/// running Android 5.0 (API level 21) or higher, bundletool pushes the base APK,
/// feature module APKs, and configuration APKs required to run your app on that device.
/// Alternatively, if your connected device is running Android 4.4 (API level 20) or
/// lower, bundletool looks for a compatible multi-APK and deploys it to your device.
/// To deploy your app from an APK set, use the install-apks command and specify the path
/// of the APK set using the --apks=/path/to/apks flag, as shown below. (If you have
/// multiple devices connected, specify a target device by adding the
/// --device-id=serial-id flag.)
#[derive(Debug, Default)]
pub struct InstallApks {
    apks: PathBuf,
    local_testing: bool,
    device_id: Option<String>,
}

impl InstallApks {
    /// Specifies path to set of apks
    pub fn new(apks: &Path) -> Self {
        Self {
            apks: apks.to_owned(),
            ..Default::default()
        }
    }

    /// If you're using the --local-testing flag with the build-apks command, for local
    /// testing to work correctly, you need to use install-apks to install your APKs
    pub fn local_testing(&mut self, local_testing: bool) -> &mut Self {
        self.local_testing = local_testing;
        self
    }

    /// If you have multiple devices connected, specify a target device by adding the
    /// --device-id=serial-id flag
    pub fn device_id(&mut self, device_id: String) -> &mut Self {
        self.device_id = Some(device_id);
        self
    }

    pub fn run(&self) -> Result<()> {
        let mut install_apks = Command::new("java");
        install_apks.arg("-jar");
        if let Ok(bundletool_path) = std::env::var("BUNDLETOOL_PATH") {
            install_apks.arg(bundletool_path);
        } else {
            return Err(Error::BundletoolNotFound);
        }
        install_apks.arg("install-apks");
        install_apks.arg("--apks");
        install_apks.arg(&self.apks);
        if self.local_testing {
            install_apks.arg("--local-testing");
        }
        if let Some(device_id) = &self.device_id {
            install_apks.arg("--device-id").arg(device_id);
        }
        install_apks.output_err(true)?;
        Ok(())
    }
}

// #[cfg(test)]
// mod tests {
//     use super::*;

//     #[test]
//     fn new() {
//         // TODO: Test install_apks
// InstallApks::new(Path::new("\\target\\android\\debug\\threed.apks")).run().unwrap();
//     }
// }