Skip to content

Commit

Permalink
Add preferred flag
Browse files Browse the repository at this point in the history
  • Loading branch information
enfipy committed Aug 29, 2022
1 parent 36595cf commit 7e5e050
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 54 deletions.
2 changes: 1 addition & 1 deletion crossbundle/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
6 changes: 3 additions & 3 deletions crossbundle/cli/src/commands/install/bundletool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
pub path: Option<PathBuf>,
/// Force install bundletool even if found.
#[clap(long, short)]
force: bool,
pub force: bool,
}

impl BundletoolInstallCommand {
Expand Down
14 changes: 7 additions & 7 deletions crossbundle/cli/src/commands/install/command_line_tools.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -44,24 +45,22 @@ 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",
path.to_str().unwrap(),
)?;
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(),
)?;
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(())
}
Expand All @@ -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(());
}
Expand Down
62 changes: 42 additions & 20 deletions crossbundle/cli/src/commands/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<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")]
Expand All @@ -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,
}
Expand All @@ -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<std::path::PathBuf> {
pub fn default_file_path(file_name: String) -> Result<std::path::PathBuf> {
let default_file_path = dirs::home_dir()
.ok_or(crate::error::Error::HomeDirNotFound)?
.ok_or(Error::HomeDirNotFound)?
.join(file_name);
Ok(default_file_path)
}
30 changes: 15 additions & 15 deletions crossbundle/cli/src/commands/install/sdkmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,
pub install: Option<Vec<String>>,
/// Android package that needs to be uninstalled
#[clap(long)]
uninstall: Option<String>,
pub uninstall: Option<String>,
/// 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<std::path::PathBuf>,
pub sdk_root: Option<std::path::PathBuf>,
/// 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<u32>,
pub channel: Option<u32>,
/// 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<String>,
pub proxy: Option<String>,
/// IP or DNS address of the proxy to use
#[clap(long)]
proxy_host: Option<String>,
pub proxy_host: Option<String>,
/// Proxy port number to connect to
#[clap(long)]
proxy_port: Option<String>,
pub proxy_port: Option<String>,
}

impl SdkManagerInstallCommand {
Expand All @@ -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<String>) -> &mut Self {
self.install = Some(install);
self
Expand Down
1 change: 0 additions & 1 deletion crossbundle/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub enum Commands {
/// with `crossbundle`
New(new::NewCommand),
/// Installs bundletool and Android Studio's sdkmanager
#[clap(subcommand)]
Install(install::InstallCommand),
}

Expand Down
2 changes: 1 addition & 1 deletion crossbundle/tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions docs/src/crossbundle/command-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/install/android-linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/src/install/android-macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/src/install/android-windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7e5e050

Please sign in to comment.