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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#[cfg(feature = "android")]
pub mod bundletool;
#[cfg(feature = "android")]
pub mod command_line_tools;
#[cfg(feature = "android")]
pub mod sdkmanager;

use crate::error::*;
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 struct InstallCommand {
    /// This flag installs all necessary tools, sdk, and ndk.
    /// Also, if specified - has higher priority than subcommand.
    #[clap(long)]
    pub preferred: bool,
    #[clap(subcommand)]
    pub subcommand: Option<InstallCommandSubcommand>,
}

#[derive(Parser, Clone, Debug)]
pub enum InstallCommandSubcommand {
    /// Install bundletool. You can specify version of bundletool. By default, we have
    /// 1.8.2 bundletool version in usage
    #[cfg(feature = "android")]
    Bundletool(BundletoolInstallCommand),
    /// Download the basic Android command line tools below. You can use the included
    /// sdkmanager to download other SDK packages. These tools are included in Android
    /// Studio
    #[cfg(feature = "android")]
    CommandLineTools(CommandLineToolsInstallCommand),
    /// Allows you to view, install, update, and uninstall packages for the Android SDK
    #[cfg(feature = "android")]
    Sdkmanager(SdkManagerInstallCommand),
}

impl InstallCommand {
    pub fn handle_command(&self, config: &Config) -> Result<()> {
        if self.preferred {
            config.status("Installing all preferred tools")?;
            #[cfg(feature = "android")]
            CommandLineToolsInstallCommand::default().install(config)?;
            #[cfg(feature = "android")]
            BundletoolInstallCommand {
                version: String::from("1.8.2"),
                ..Default::default()
            }
            .install(config)?;
            #[cfg(feature = "android")]
            SdkManagerInstallCommand {
                preferred_tools: true,
                ..Default::default()
            }
            .run(config)?;
            return Ok(());
        }
        if let Some(subcommand) = &self.subcommand {
            #[cfg(feature = "android")]
            match subcommand {
                #[cfg(feature = "android")]
                InstallCommandSubcommand::Bundletool(cmd) => cmd.install(config)?,
                #[cfg(feature = "android")]
                InstallCommandSubcommand::CommandLineTools(cmd) => cmd.install(config)?,
                #[cfg(feature = "android")]
                InstallCommandSubcommand::Sdkmanager(cmd) => cmd.run(config)?,
            }
        }
        Ok(())
    }
}

/// Download from url and saves it in specified file
pub fn download_to_file(download_url: &str, file_path: &std::path::Path) -> Result<()> {
    let response = ureq::get(download_url)
        .call()
        .map_err(Error::DownloadFailed)?;
    let mut out =
        std::fs::File::create(file_path).map_err(|cause| Error::JarFileCreationFailed {
            path: file_path.to_path_buf(),
            cause,
        })?;
    std::io::copy(&mut response.into_reader(), &mut out).map_err(|cause| {
        Error::CopyToFileFailed {
            path: file_path.to_path_buf(),
            cause,
        }
    })?;
    Ok(())
}

/// Using default file path related on $HOME path for all installed commands
pub fn default_file_path(file_name: String) -> Result<std::path::PathBuf> {
    let default_file_path = dirs::home_dir()
        .ok_or(Error::HomeDirNotFound)?
        .join(file_name);
    Ok(default_file_path)
}