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::error::*;
use std::{
    path::{Path, PathBuf},
    process::Command,
};

#[derive(Clone, Default)]
pub struct AdbPush {
    sync: Option<PathBuf>,
    n: bool,
    z_enable_compression: bool,
    z_disable_compression: bool,
}

impl AdbPush {
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }

    /// Only push files that are newer on the host than the device
    pub fn sync(&mut self, sync: &Path) -> &mut Self {
        self.sync = Some(sync.to_owned());
        self
    }

    /// Dry run: push files to device without storing to the filesystem
    pub fn n(&mut self, n: bool) -> &mut Self {
        self.n = n;
        self
    }

    /// Enable compression with a specified algorithm (any, none, brotli)
    pub fn z_enable_compression(&mut self, z_enable_compression: bool) -> &mut Self {
        self.z_enable_compression = z_enable_compression;
        self
    }

    /// Disable compression
    pub fn z_disable_compression(&mut self, z_disable_compression: bool) -> &mut Self {
        self.z_disable_compression = z_disable_compression;
        self
    }

    pub fn run(&self) -> Result<()> {
        let mut adb_push = Command::new("adb");
        adb_push.arg("push");
        if let Some(sync) = &self.sync {
            adb_push.arg("--sync").arg(sync);
        }
        if self.n {
            adb_push.arg("-n");
        }
        if self.z_enable_compression {
            adb_push.arg("-z");
        }
        if self.z_disable_compression {
            adb_push.arg("-Z");
        }
        adb_push.output_err(true)?;
        Ok(())
    }
}