Skip to content

Commit

Permalink
fix(apple): allow skipping signing on build and archive (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Sep 1, 2024
1 parent 4151b32 commit 64d3e6f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changes/archive-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-mobile2": minor
---

Added an `ArchiveConfig` parameter to `apple::Target::archive`.
7 changes: 7 additions & 0 deletions .changes/fix-ios-automatic-signing-multiple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"cargo-mobile2": minor
---

Allow skipping code signing on `Apple::Target` `build` and `archive` methods,
which fixes an issue in CI where automatic signing only works on the first execution,
and following runs errors with `Revoke certificate: Your account already has a signing certificate for this machine but it is not present in your keychain`.
12 changes: 10 additions & 2 deletions src/apple/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{
device::{self, Device, RunError},
rust_version_check,
target::{
ArchiveError, BuildConfig, BuildError, CheckError, CompileLibError, ExportError, Target,
ArchiveConfig, ArchiveError, BuildConfig, BuildError, CheckError, CompileLibError,
ExportError, Target,
},
NAME,
},
Expand Down Expand Up @@ -355,7 +356,14 @@ impl Exec for Input {
)
.map_err(Error::BuildFailed)?;
target
.archive(config, &env, noise_level, profile, Some(app_version))
.archive(
config,
&env,
noise_level,
profile,
Some(app_version),
ArchiveConfig::new().allow_provisioning_updates(),
)
.map_err(Error::ArchiveFailed)
},
)
Expand Down
11 changes: 9 additions & 2 deletions src/apple/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
target::{ArchiveError, BuildError, ExportError, Target},
};
use crate::{
apple::target::{BuildConfig, ExportConfig},
apple::target::{ArchiveConfig, BuildConfig, ExportConfig},
env::{Env, ExplicitEnv as _},
opts,
util::cli::{Report, Reportable},
Expand Down Expand Up @@ -138,7 +138,14 @@ impl<'a> Device<'a> {
.map_err(RunError::BuildFailed)?;
println!("Archiving app...");
self.target
.archive(config, env, noise_level, profile, None)
.archive(
config,
env,
noise_level,
profile,
None,
ArchiveConfig::new(),
)
.map_err(RunError::ArchiveFailed)?;

match self.kind {
Expand Down
108 changes: 81 additions & 27 deletions src/apple/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use once_cell_regex::exports::once_cell::sync::OnceCell;
use std::{
collections::{BTreeMap, HashMap},
ffi::{OsStr, OsString},
process::Command,
};
use thiserror::Error;

Expand Down Expand Up @@ -135,31 +136,62 @@ impl Reportable for ExportError {
}

#[derive(Default)]
pub struct ExportConfig {
pub struct XcodebuildOptions {
allow_provisioning_updates: bool,
skip_codesign: bool,
authentication_credentials: Option<AuthCredentials>,
}

impl XcodebuildOptions {
fn args_for(&self, cmd: &mut Command) {
if self.skip_codesign {
cmd.args([
"CODE_SIGNING_REQUIRED=NO",
"CODE_SIGNING_ALLOWED=NO",
"CODE_SIGN_IDENTITY=\"\"",
"CODE_SIGN_ENTITLEMENTS=\"\"",
]);
}

if self.allow_provisioning_updates {
cmd.arg("-allowProvisioningUpdates");
}

if let Some(credentials) = &self.authentication_credentials {
cmd.args(["-authenticationKeyID", &credentials.key_id])
.arg("-authenticationKeyPath")
.arg(&credentials.key_path)
.args(["-authenticationKeyIssuerID", &credentials.key_issuer_id]);
}
}
}

#[derive(Default)]
pub struct ExportConfig {
xcodebuild_options: XcodebuildOptions,
}

impl ExportConfig {
pub fn new() -> Self {
Self::default()
}

pub fn allow_provisioning_updates(mut self) -> Self {
self.allow_provisioning_updates = true;
self.xcodebuild_options.allow_provisioning_updates = true;
self
}

pub fn authentication_credentials(mut self, credentials: AuthCredentials) -> Self {
self.authentication_credentials.replace(credentials);
self.xcodebuild_options
.authentication_credentials
.replace(credentials);
self
}
}

#[derive(Default)]
pub struct BuildConfig {
allow_provisioning_updates: bool,
authentication_credentials: Option<AuthCredentials>,
xcodebuild_options: XcodebuildOptions,
}

impl BuildConfig {
Expand All @@ -168,12 +200,47 @@ impl BuildConfig {
}

pub fn allow_provisioning_updates(mut self) -> Self {
self.allow_provisioning_updates = true;
self.xcodebuild_options.allow_provisioning_updates = true;
self
}

pub fn skip_codesign(mut self) -> Self {
self.xcodebuild_options.skip_codesign = true;
self
}

pub fn authentication_credentials(mut self, credentials: AuthCredentials) -> Self {
self.xcodebuild_options
.authentication_credentials
.replace(credentials);
self
}
}

#[derive(Default)]
pub struct ArchiveConfig {
xcodebuild_options: XcodebuildOptions,
}

impl ArchiveConfig {
pub fn new() -> Self {
Self::default()
}

pub fn allow_provisioning_updates(mut self) -> Self {
self.xcodebuild_options.allow_provisioning_updates = true;
self
}

pub fn skip_codesign(mut self) -> Self {
self.xcodebuild_options.skip_codesign = true;
self
}

pub fn authentication_credentials(mut self, credentials: AuthCredentials) -> Self {
self.authentication_credentials.replace(credentials);
self.xcodebuild_options
.authentication_credentials
.replace(credentials);
self
}
}
Expand Down Expand Up @@ -377,23 +444,15 @@ impl<'a> Target<'a> {
.full_env(env.explicit_env())
.env("FORCE_COLOR", "--force-color")
.before_spawn(move |cmd| {
build_config.xcodebuild_options.args_for(cmd);

if let Some(v) = verbosity(noise_level) {
cmd.arg(v);
}
if let Some(a) = &arch {
cmd.args(["-arch", a]);
}

if build_config.allow_provisioning_updates {
cmd.arg("-allowProvisioningUpdates");
}
if let Some(credentials) = &build_config.authentication_credentials {
cmd.args(["-authenticationKeyID", &credentials.key_id])
.arg("-authenticationKeyPath")
.arg(&credentials.key_path)
.args(["-authenticationKeyIssuerID", &credentials.key_issuer_id]);
}

cmd.args(["-scheme", &scheme])
.arg("-workspace")
.arg(&workspace_path)
Expand All @@ -415,6 +474,7 @@ impl<'a> Target<'a> {
noise_level: opts::NoiseLevel,
profile: opts::Profile,
build_number: Option<VersionNumber>,
archive_config: ArchiveConfig,
) -> Result<(), ArchiveError> {
if let Some(build_number) = build_number {
util::with_working_dir(config.project_dir(), || {
Expand Down Expand Up @@ -442,6 +502,8 @@ impl<'a> Target<'a> {
duct::cmd("xcodebuild", args)
.full_env(env.explicit_env())
.before_spawn(move |cmd| {
archive_config.xcodebuild_options.args_for(cmd);

if let Some(v) = verbosity(noise_level) {
cmd.arg(v);
}
Expand Down Expand Up @@ -484,6 +546,8 @@ impl<'a> Target<'a> {
duct::cmd("xcodebuild", args)
.full_env(env.explicit_env())
.before_spawn(move |cmd| {
export_config.xcodebuild_options.args_for(cmd);

if let Some(v) = verbosity(noise_level) {
cmd.arg(v);
}
Expand All @@ -495,16 +559,6 @@ impl<'a> Target<'a> {
.arg("-exportPath")
.arg(&export_dir);

if export_config.allow_provisioning_updates {
cmd.arg("-allowProvisioningUpdates");
}
if let Some(credentials) = &export_config.authentication_credentials {
cmd.args(["-authenticationKeyID", &credentials.key_id])
.arg("-authenticationKeyPath")
.arg(&credentials.key_path)
.args(["-authenticationKeyIssuerID", &credentials.key_issuer_id]);
}

Ok(())
})
.dup_stdio()
Expand Down

0 comments on commit 64d3e6f

Please sign in to comment.