From 7e5e0504274b363bbed0fdc7dc9e18285f802370 Mon Sep 17 00:00:00 2001 From: David Ackerman Date: Mon, 29 Aug 2022 13:05:44 +0200 Subject: [PATCH] Add preferred flag --- crossbundle/cli/Cargo.toml | 2 +- .../cli/src/commands/install/bundletool.rs | 6 +- .../commands/install/command_line_tools.rs | 14 ++--- crossbundle/cli/src/commands/install/mod.rs | 62 +++++++++++++------ .../cli/src/commands/install/sdkmanager.rs | 30 ++++----- crossbundle/cli/src/commands/mod.rs | 1 - crossbundle/tools/Cargo.toml | 2 +- docs/src/crossbundle/command-install.md | 6 +- docs/src/install/android-linux.md | 2 +- docs/src/install/android-macos.md | 2 +- docs/src/install/android-windows.md | 2 +- 11 files changed, 75 insertions(+), 54 deletions(-) diff --git a/crossbundle/cli/Cargo.toml b/crossbundle/cli/Cargo.toml index d1f315d9..e4504c9e 100644 --- a/crossbundle/cli/Cargo.toml +++ b/crossbundle/cli/Cargo.toml @@ -20,7 +20,7 @@ path = "src/main.rs" [dependencies] crossbow = { path = "../../", version = "0.2.1", default-features = false, features = ["update-manifest"] } crossbundle-tools = { path = "../tools", version = "0.2.1", default-features = false } -android-tools = { version = "0.2.10", optional = true } +android-tools = { version = "0.2.11", optional = true } clap = { version = "3.2", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } diff --git a/crossbundle/cli/src/commands/install/bundletool.rs b/crossbundle/cli/src/commands/install/bundletool.rs index 30f6d545..db32ef64 100644 --- a/crossbundle/cli/src/commands/install/bundletool.rs +++ b/crossbundle/cli/src/commands/install/bundletool.rs @@ -11,14 +11,14 @@ pub struct BundletoolInstallCommand { /// Required. Version of download bundletool. For example: /// --version 1.8.2 #[clap(long, short, default_value = "1.8.2")] - version: String, + pub version: String, /// Path to install bundletool. By default bundletool will be downloaded and saved in /// home directory #[clap(long, short)] - path: Option, + pub path: Option, /// Force install bundletool even if found. #[clap(long, short)] - force: bool, + pub force: bool, } impl BundletoolInstallCommand { diff --git a/crossbundle/cli/src/commands/install/command_line_tools.rs b/crossbundle/cli/src/commands/install/command_line_tools.rs index 24ce808c..016da473 100644 --- a/crossbundle/cli/src/commands/install/command_line_tools.rs +++ b/crossbundle/cli/src/commands/install/command_line_tools.rs @@ -1,6 +1,7 @@ use super::*; +use android_tools::sdk_install_path; use clap::Parser; -use crossbundle_tools::{commands::android::*, types::AndroidSdk, types::Config}; +use crossbundle_tools::{commands::android::*, types::Config}; use std::path::{Path, PathBuf}; #[cfg(target_os = "windows")] @@ -44,9 +45,6 @@ impl CommandLineToolsInstallCommand { )?; self.download_and_save_file(command_line_tools_download_url, &file_path)?; - let sdk = AndroidSdk::from_env()?; - let sdk_path = sdk.sdk_path(); - if let Some(path) = &self.install_path { config.status_message( "Extracting zip archive contents into", @@ -54,6 +52,7 @@ impl CommandLineToolsInstallCommand { )?; extract_archive(&file_path, path)?; } else { + let sdk_path = sdk_install_path()?; config.status_message( "Extracting zip archive contents into", &sdk_path.to_str().unwrap(), @@ -61,7 +60,7 @@ impl CommandLineToolsInstallCommand { extract_archive(&file_path, Path::new(&sdk_path))?; } - config.status("Deleting zip archive was left after installation")?; + config.status("Deleting zip archive thaw was left after installation")?; remove(vec![file_path])?; Ok(()) } @@ -78,8 +77,9 @@ impl CommandLineToolsInstallCommand { download_url: PathBuf, file_path: &Path, ) -> crate::error::Result<()> { - for sdkmanager in std::fs::read_dir(file_path.parent().unwrap())? { - let zip_path = sdkmanager?.path(); + remove(vec![file_path.to_path_buf()])?; + for dir in std::fs::read_dir(file_path.parent().unwrap())? { + let zip_path = dir?.path(); if zip_path.ends_with(self.file_name()) { return Ok(()); } diff --git a/crossbundle/cli/src/commands/install/mod.rs b/crossbundle/cli/src/commands/install/mod.rs index ad69d0bc..f34d0b1d 100644 --- a/crossbundle/cli/src/commands/install/mod.rs +++ b/crossbundle/cli/src/commands/install/mod.rs @@ -5,7 +5,7 @@ pub mod command_line_tools; #[cfg(feature = "android")] pub mod sdkmanager; -use crate::error::Result; +use crate::error::*; use clap::Parser; use crossbundle_tools::types::Config; @@ -16,7 +16,17 @@ use self::{ }; #[derive(Parser, Clone, Debug)] -pub enum InstallCommand { +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, +} + +#[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")] @@ -28,40 +38,52 @@ pub enum InstallCommand { CommandLineTools(CommandLineToolsInstallCommand), /// Allows you to view, install, update, and uninstall packages for the Android SDK #[cfg(feature = "android")] - SdkManager(SdkManagerInstallCommand), + Sdkmanager(SdkManagerInstallCommand), } impl InstallCommand { pub fn handle_command(&self, config: &Config) -> Result<()> { - #[cfg(feature = "android")] - match self { + if self.preferred { + config.status("Installing all preferred tools")?; #[cfg(feature = "android")] - InstallCommand::Bundletool(cmd) => cmd.install(config)?, + CommandLineToolsInstallCommand::default().install(config)?; #[cfg(feature = "android")] - InstallCommand::CommandLineTools(cmd) => cmd.install(config)?, + BundletoolInstallCommand::default().install(config)?; #[cfg(feature = "android")] - InstallCommand::SdkManager(cmd) => cmd.run(config)?, + 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, -) -> crate::error::Result<()> { +pub fn download_to_file(download_url: &str, file_path: &std::path::Path) -> 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 { + .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| { - crate::error::Error::CopyToFileFailed { + Error::CopyToFileFailed { path: file_path.to_path_buf(), cause, } @@ -70,9 +92,9 @@ pub fn download_to_file( } /// Using default file path related on $HOME path for all installed commands -pub fn default_file_path(file_name: String) -> crate::error::Result { +pub fn default_file_path(file_name: String) -> Result { let default_file_path = dirs::home_dir() - .ok_or(crate::error::Error::HomeDirNotFound)? + .ok_or(Error::HomeDirNotFound)? .join(file_name); Ok(default_file_path) } diff --git a/crossbundle/cli/src/commands/install/sdkmanager.rs b/crossbundle/cli/src/commands/install/sdkmanager.rs index 87270afc..bbd1ae6d 100644 --- a/crossbundle/cli/src/commands/install/sdkmanager.rs +++ b/crossbundle/cli/src/commands/install/sdkmanager.rs @@ -9,49 +9,49 @@ pub struct SdkManagerInstallCommand { /// Install all preferred tools for correct crossbundle work. It will install /// build-tools;31.0.0, ndk;23.1.7779620 and platforms;android-31 #[clap(long, short)] - preferred_tools: bool, + pub preferred_tools: bool, /// List installed and available packages. Use the channel option to include a package /// from a channel up to and including channel_id. For example, specify the canary /// channel to list packages from all channels #[clap(long, short)] - list: bool, + pub list: bool, /// Install package. To see all available packages use --list. - /// Example: crossbundle install sdk-manager "ndk;23.1.7779620" + /// Example: crossbundle install sdkmanager "ndk;23.1.7779620" #[clap(long, short, multiple_values = true)] - install: Option>, + pub install: Option>, /// Android package that needs to be uninstalled #[clap(long)] - uninstall: Option, + pub uninstall: Option, /// Update all installed packages #[clap(long)] - update: bool, + pub update: bool, /// Use the specified SDK path instead of the SDK containing this tool #[clap(long, short)] - sdk_root: Option, + pub sdk_root: Option, /// Include packages in channels up to and including channel_id. Available channels /// are: 0 (Stable), 1 (Beta), 2 (Dev), and 3 (Canary) #[clap(long, short)] - channel: Option, + pub channel: Option, /// Include obsolete packages in the package listing or package updates. For use with /// --list and --update only #[clap(long)] - include_obsolete: bool, + pub include_obsolete: bool, /// Force all connections to use HTTP rather than HTTPS #[clap(long, short)] - no_https: bool, + pub no_https: bool, /// Verbose output mode. Errors, warnings and informational messages are printed #[clap(long, short)] - verbose: bool, + pub verbose: bool, /// Connect via a proxy of the given type: either http for high level protocols such /// as HTTP or FTP, or socks for a SOCKS (V4 or V5) proxy #[clap(long)] - proxy: Option, + pub proxy: Option, /// IP or DNS address of the proxy to use #[clap(long)] - proxy_host: Option, + pub proxy_host: Option, /// Proxy port number to connect to #[clap(long)] - proxy_port: Option, + pub proxy_port: Option, } impl SdkManagerInstallCommand { @@ -71,7 +71,7 @@ impl SdkManagerInstallCommand { } /// Install package. To see all available packages use --list. - /// Example: crossbundle install sdk-manager "ndk;23.1.7779620" + /// Example: crossbundle install sdkmanager "ndk;23.1.7779620" pub fn install(&mut self, install: Vec) -> &mut Self { self.install = Some(install); self diff --git a/crossbundle/cli/src/commands/mod.rs b/crossbundle/cli/src/commands/mod.rs index 1a407fc9..f538c4e2 100644 --- a/crossbundle/cli/src/commands/mod.rs +++ b/crossbundle/cli/src/commands/mod.rs @@ -20,7 +20,6 @@ pub enum Commands { /// with `crossbundle` New(new::NewCommand), /// Installs bundletool and Android Studio's sdkmanager - #[clap(subcommand)] Install(install::InstallCommand), } diff --git a/crossbundle/tools/Cargo.toml b/crossbundle/tools/Cargo.toml index a4e4d98f..2b814698 100644 --- a/crossbundle/tools/Cargo.toml +++ b/crossbundle/tools/Cargo.toml @@ -16,7 +16,7 @@ apple-bundle = { version = "0.1.4", optional = true } simctl = { version = "0.1.1", package = "creator-simctl", optional = true } # Android crates android-manifest = { version = "0.1.10", optional = true } -android-tools = { version = "0.2.10", optional = true } +android-tools = { version = "0.2.11", optional = true } serde = { version = "1.0", features = ["derive"] } serde_plain = "1.0" diff --git a/docs/src/crossbundle/command-install.md b/docs/src/crossbundle/command-install.md index 6515f6fc..f75cde3d 100644 --- a/docs/src/crossbundle/command-install.md +++ b/docs/src/crossbundle/command-install.md @@ -30,19 +30,19 @@ The [sdkmanager](https://developer.android.com/studio/command-line/sdkmanager) i To install packages use the command below. We prefer to use --preferred-tools flag to install minimal required tools needed for crossbundle correct working. This command will setup build-tools, android-ndk and android platforms: ```sh -crossbundle install sdk-manager --preferred-tools +crossbundle install sdkmanager --preferred-tools ``` Also you can install packages manually. To see all available tools use the -h flag. List installed and available packages: ```sh -crossbundle install sdk-manager --list +crossbundle install sdkmanager --list ``` And then enter the command. ```sh -crossbundle install sdk-manager --install "build-tools;31.0.0" "ndk;23.1.7779620" "platforms;android-31" +crossbundle install sdkmanager --install "build-tools;31.0.0" "ndk;23.1.7779620" "platforms;android-31" ``` The command will install packages into `$HOME\AppData\Local\Android\Sdk\` for Windows, `$HOME/Library/Android/sdk/` for macOS, and `$HOME/Android/sdk/` for Linux. diff --git a/docs/src/install/android-linux.md b/docs/src/install/android-linux.md index 78fdf278..e1b46f29 100644 --- a/docs/src/install/android-linux.md +++ b/docs/src/install/android-linux.md @@ -69,7 +69,7 @@ To prepare to run and test your Crossbow app on the Android emulator, follow the ```sh # Run following command to install System Image for Android SDK 31 -crossbundle install sdk-manager --install "system-images;android-31;google_apis;x86_64" +crossbundle install sdkmanager --install "system-images;android-31;google_apis;x86_64" # Run this command to create a new emulator avdmanager create avd -n Phone -k "system-images;android-31;google_apis;x86_64" # And finally run this command to start the emulator diff --git a/docs/src/install/android-macos.md b/docs/src/install/android-macos.md index b9dd6480..25ff9cd8 100644 --- a/docs/src/install/android-macos.md +++ b/docs/src/install/android-macos.md @@ -67,7 +67,7 @@ To prepare to run and test your Crossbow app on the Android emulator, follow the ```sh # Run following command to install System Image for Android SDK 31 -crossbundle install sdk-manager --install "system-images;android-31;google_apis;x86_64" +crossbundle install sdkmanager --install "system-images;android-31;google_apis;x86_64" # Run this command to create a new emulator avdmanager create avd -n Phone -k "system-images;android-31;google_apis;x86_64" # And finally run this command to start the emulator diff --git a/docs/src/install/android-windows.md b/docs/src/install/android-windows.md index f7fa4848..c21b1c5d 100644 --- a/docs/src/install/android-windows.md +++ b/docs/src/install/android-windows.md @@ -80,7 +80,7 @@ To prepare to run and test your Crossbow app on the Android emulator, follow the ```sh # Run following command to install System Image for Android SDK 31 -crossbundle install sdk-manager --install "system-images;android-31;google_apis;x86_64" +crossbundle install sdkmanager --install "system-images;android-31;google_apis;x86_64" # Run this command to create a new emulator avdmanager create avd -n Phone -k "system-images;android-31;google_apis;x86_64" # And finally run this command to start the emulator