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
78
#[cfg(feature = "android")]
pub mod bundletool;
#[cfg(feature = "android")]
pub mod command_line_tools;
#[cfg(feature = "android")]
pub mod sdkmanager;
use crate::error::Result;
use clap::Parser;
use crossbundle_tools::types::Config;
#[cfg(feature = "android")]
use self::{
bundletool::BundletoolInstallCommand, command_line_tools::CommandLineToolsInstallCommand,
sdkmanager::SdkManagerInstallCommand,
};
#[derive(Parser, Clone, Debug)]
pub enum InstallCommand {
#[cfg(feature = "android")]
Bundletool(BundletoolInstallCommand),
#[cfg(feature = "android")]
CommandLineTools(CommandLineToolsInstallCommand),
#[cfg(feature = "android")]
SdkManager(SdkManagerInstallCommand),
}
impl InstallCommand {
pub fn handle_command(&self, config: &Config) -> Result<()> {
#[cfg(feature = "android")]
match self {
#[cfg(feature = "android")]
InstallCommand::Bundletool(cmd) => cmd.install(config)?,
#[cfg(feature = "android")]
InstallCommand::CommandLineTools(cmd) => cmd.install(config)?,
#[cfg(feature = "android")]
InstallCommand::SdkManager(cmd) => cmd.run(config)?,
}
Ok(())
}
}
pub fn download_to_file(
download_url: &str,
file_path: &std::path::Path,
) -> crate::error::Result<()> {
let response = ureq::get(download_url)
.call()
.map_err(crate::error::Error::DownloadFailed)?;
let mut out = std::fs::File::create(file_path).map_err(|cause| {
crate::error::Error::JarFileCreationFailed {
path: file_path.to_path_buf(),
cause,
}
})?;
std::io::copy(&mut response.into_reader(), &mut out).map_err(|cause| {
crate::error::Error::CopyToFileFailed {
path: file_path.to_path_buf(),
cause,
}
})?;
Ok(())
}
pub fn default_file_path(file_name: String) -> crate::error::Result<std::path::PathBuf> {
let default_file_path = dirs::home_dir()
.ok_or(crate::error::Error::HomeDirNotFound)?
.join(file_name);
Ok(default_file_path)
}