Просмотр исходного кода

feat(cli): find development teams for iOS development (#5627)

Lucas Fernandes Nogueira 2 лет назад
Родитель
Сommit
8c576222ba

+ 1 - 1
tooling/cli/Cargo.lock

@@ -387,7 +387,7 @@ checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c"
 [[package]]
 name = "cargo-mobile"
 version = "0.1.0"
-source = "git+https://github.com/tauri-apps/cargo-mobile?branch=dev#bf876273afc0b3e5e1e2cdcc232cdfe81ff4428d"
+source = "git+https://github.com/tauri-apps/cargo-mobile?branch=dev#22337b1fbde028e8c66b00e9f9ac97f60146bbb5"
 dependencies = [
  "cocoa",
  "colored 1.9.3",

+ 23 - 0
tooling/cli/src/info.rs

@@ -885,6 +885,29 @@ pub fn command(_options: Options) -> Result<()> {
     }
   }
 
+  #[cfg(target_os = "macos")]
+  if tauri_dir.is_some() {
+    let p = tauri_dir.as_ref().unwrap();
+    if p.join("gen/apple").exists() {
+      let teams = cargo_mobile::apple::teams::find_development_teams().unwrap_or_default();
+      Section("iOS").display();
+      InfoBlock::new(
+        "Teams",
+        if teams.is_empty() {
+          "None".red().to_string()
+        } else {
+          teams
+            .iter()
+            .map(|t| format!("{} (ID: {})", t.name, t.id))
+            .collect::<Vec<String>>()
+            .join(", ")
+            .to_string()
+        },
+      )
+      .display();
+    }
+  }
+
   Ok(())
 }
 

+ 2 - 0
tooling/cli/src/mobile/init.rs

@@ -135,6 +135,8 @@ pub fn exec(
     #[cfg(target_os = "macos")]
     // Generate Xcode project
     Target::Ios => {
+      // the apple development team is not required on init
+      std::env::set_var(super::ios::APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME, "");
       let (app, config, metadata) =
         super::ios::get_config(Some(app), tauri_config_, &Default::default());
       map.insert("apple", &config);

+ 19 - 2
tooling/cli/src/mobile/ios.rs

@@ -11,6 +11,7 @@ use cargo_mobile::{
     device::Device,
     ios_deploy, simctl,
     target::Target,
+    teams::find_development_teams,
   },
   config::app::App,
   env::Env,
@@ -32,6 +33,7 @@ use crate::{
 };
 
 use std::{
+  process::exit,
   thread::{sleep, spawn},
   time::Duration,
 };
@@ -42,6 +44,8 @@ mod open;
 pub(crate) mod project;
 mod xcode_script;
 
+pub const APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME: &str = "TAURI_APPLE_DEVELOPMENT_TEAM";
+
 #[derive(Parser)]
 #[clap(
   author,
@@ -98,10 +102,23 @@ pub fn get_config(
   let ios_options = cli_options.clone();
 
   let raw = RawAppleConfig {
-    development_team: std::env::var("TAURI_APPLE_DEVELOPMENT_TEAM")
+    development_team: std::env::var(APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME)
         .ok()
         .or_else(|| config.tauri.bundle.ios.development_team.clone())
-        .expect("you must set `tauri > iOS > developmentTeam` config value or the `TAURI_APPLE_DEVELOPMENT_TEAM` environment variable"),
+        .unwrap_or_else(|| {
+          let teams = find_development_teams().unwrap_or_default();
+          match teams.len() {
+            0 => {
+              log::error!("No code signing certificates found. You must add one and set the certificate development team ID on the `tauri > iOS > developmentTeam` config value or the `{APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME}` environment variable. To list the available certificates, run `tauri info`.");
+              exit(1);
+            }
+            1 => teams.first().unwrap().id.clone(),
+            _ => {
+              log::error!("You must set the code signing certificate development team ID on  the `tauri > iOS > developmentTeam` config value or the `{APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME}` environment variable. Available certificates: {}", teams.iter().map(|t| format!("{} (ID: {})", t.name, t.id)).collect::<Vec<String>>().join(", "));
+              exit(1);
+            }
+          }
+        }),
     ios_features: ios_options.features.clone(),
     bundle_version: config.package.version.clone(),
     bundle_version_short: config.package.version.clone(),

+ 35 - 1
tooling/cli/src/mobile/ios/dev.rs

@@ -1,5 +1,6 @@
 use super::{
   device_prompt, ensure_init, env, init_dot_cargo, open_and_wait, with_config, MobileTarget,
+  APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME,
 };
 use crate::{
   helpers::{config::get as get_tauri_config, flock},
@@ -10,11 +11,14 @@ use crate::{
 use clap::{ArgAction, Parser};
 
 use cargo_mobile::{
-  apple::config::Config as AppleConfig,
+  apple::{config::Config as AppleConfig, teams::find_development_teams},
   config::app::App,
   env::Env,
   opts::{NoiseLevel, Profile},
 };
+use dialoguer::{theme::ColorfulTheme, Select};
+
+use std::env::{set_var, var_os};
 
 #[derive(Debug, Clone, Parser)]
 #[clap(about = "iOS dev")]
@@ -57,6 +61,36 @@ impl From<Options> for crate::dev::Options {
 }
 
 pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
+  if var_os(APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME).is_none() {
+    if let Ok(teams) = find_development_teams() {
+      let index = match teams.len() {
+        0 => None,
+        1 => Some(0),
+        _ => {
+          let index = Select::with_theme(&ColorfulTheme::default())
+            .items(
+              &teams
+                .iter()
+                .map(|t| format!("{} (ID: {})", t.name, t.id))
+                .collect::<Vec<String>>(),
+            )
+            .default(0)
+            .interact()?;
+          Some(index)
+        }
+      };
+      if let Some(index) = index {
+        let team = teams.get(index).unwrap();
+        log::info!(
+            "Using development team `{}`. To make this permanent, set the `{}` environment variable to `{}`",
+            team.name,
+            APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME,
+            team.id
+          );
+        set_var(APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME, &team.id);
+      }
+    }
+  }
   with_config(
     Some(Default::default()),
     |app, config, _metadata, _cli_options| {