From a28fdf7ec71bf6db2498569004de83318b6d25ac Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Fri, 26 May 2023 05:36:06 +0300 Subject: [PATCH] feat(cli/mobile/init): skip installing already installed targets, closes #7044 (#7058) Co-authored-by: Lucas Fernandes Nogueira closes #7044 --- .changes/cli-skip-targets-install.md | 6 +++++ .github/workflows/check-license-header.yml | 2 +- tooling/cli/src/interface/rust.rs | 1 + .../cli/src/interface/rust/installation.rs | 26 +++++++++++++++++++ tooling/cli/src/mobile/android/project.rs | 18 +++++++++++-- tooling/cli/src/mobile/ios/project.rs | 18 +++++++++++-- 6 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 .changes/cli-skip-targets-install.md create mode 100644 tooling/cli/src/interface/rust/installation.rs diff --git a/.changes/cli-skip-targets-install.md b/.changes/cli-skip-targets-install.md new file mode 100644 index 000000000..0e84a5ba0 --- /dev/null +++ b/.changes/cli-skip-targets-install.md @@ -0,0 +1,6 @@ +--- +'cli.rs': 'patch' +'cli.js': 'patch' +--- + +Skip Rust target installation if they are already installed. diff --git a/.github/workflows/check-license-header.yml b/.github/workflows/check-license-header.yml index fb2055c10..c0d6da66b 100644 --- a/.github/workflows/check-license-header.yml +++ b/.github/workflows/check-license-header.yml @@ -2,7 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: MIT -name: Check generated files +name: check license header on: pull_request: diff --git a/tooling/cli/src/interface/rust.rs b/tooling/cli/src/interface/rust.rs index 3fc4a6a33..05ffd5e3b 100644 --- a/tooling/cli/src/interface/rust.rs +++ b/tooling/cli/src/interface/rust.rs @@ -36,6 +36,7 @@ use tauri_utils::display_path; mod cargo_config; mod desktop; +pub mod installation; pub mod manifest; use cargo_config::Config as CargoConfig; use manifest::{rewrite_manifest, Manifest}; diff --git a/tooling/cli/src/interface/rust/installation.rs b/tooling/cli/src/interface/rust/installation.rs new file mode 100644 index 000000000..c06d7feb5 --- /dev/null +++ b/tooling/cli/src/interface/rust/installation.rs @@ -0,0 +1,26 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +use crate::Result; + +use std::{fs::read_dir, path::PathBuf, process::Command}; + +pub fn installed_targets() -> Result> { + let output = Command::new("rustc") + .args(["--print", "sysroot"]) + .output()?; + let sysroot_path = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim().to_string()); + + let mut targets = Vec::new(); + for entry in read_dir(sysroot_path.join("lib").join("rustlib"))?.flatten() { + if entry.file_type().map(|t| t.is_dir()).unwrap_or_default() { + let name = entry.file_name(); + if name != "etc" && name != "src" { + targets.push(name.to_string_lossy().into_owned()); + } + } + } + + Ok(targets) +} diff --git a/tooling/cli/src/mobile/android/project.rs b/tooling/cli/src/mobile/android/project.rs index 62950088f..24c3d380c 100644 --- a/tooling/cli/src/mobile/android/project.rs +++ b/tooling/cli/src/mobile/android/project.rs @@ -35,8 +35,22 @@ pub fn gen( (handlebars, mut map): (Handlebars, template::JsonMap), wrapper: &TextWrapper, ) -> Result<()> { - println!("Installing Android toolchains..."); - Target::install_all().with_context(|| "failed to run rustup")?; + let installed_targets = + crate::interface::rust::installation::installed_targets().unwrap_or_default(); + let missing_targets = Target::all() + .values() + .filter(|t| !installed_targets.contains(&t.triple().into())) + .collect::>(); + + if !missing_targets.is_empty() { + println!("Installing Android Rust toolchains..."); + for target in missing_targets { + target + .install() + .context("failed to install target with rustup")?; + } + } + println!("Generating Android Studio project..."); let dest = config.project_dir(); let asset_packs = metadata.asset_packs().unwrap_or_default(); diff --git a/tooling/cli/src/mobile/ios/project.rs b/tooling/cli/src/mobile/ios/project.rs index 19b509d4b..8d87edc2b 100644 --- a/tooling/cli/src/mobile/ios/project.rs +++ b/tooling/cli/src/mobile/ios/project.rs @@ -34,8 +34,22 @@ pub fn gen( non_interactive: bool, reinstall_deps: bool, ) -> Result<()> { - println!("Installing iOS toolchains..."); - Target::install_all()?; + let installed_targets = + crate::interface::rust::installation::installed_targets().unwrap_or_default(); + let missing_targets = Target::all() + .values() + .filter(|t| !installed_targets.contains(&t.triple().into())) + .collect::>(); + + if !missing_targets.is_empty() { + println!("Installing iOS Rust toolchains..."); + for target in missing_targets { + target + .install() + .context("failed to install target with rustup")?; + } + } + rust_version_check(wrapper)?; deps::install_all(wrapper, non_interactive, true, reinstall_deps)