From f84fa1a508a789edc13403043fdaaa34d489ea37 Mon Sep 17 00:00:00 2001 From: Nicolas Mauri Date: Wed, 27 Dec 2023 10:18:02 +0100 Subject: [PATCH] xtask swift build-mocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a —build-mocks parameter to xtask swift build-framework --- xtask/src/swift.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/xtask/src/swift.rs b/xtask/src/swift.rs index 9d08f75db16..d8df058a1a7 100644 --- a/xtask/src/swift.rs +++ b/xtask/src/swift.rs @@ -2,7 +2,7 @@ use std::fs::{copy, create_dir_all, remove_dir_all, remove_file, rename}; use camino::{Utf8Path, Utf8PathBuf}; use clap::{Args, Subcommand}; -use uniffi_bindgen::{bindings::TargetLanguage, library_mode::generate_bindings}; +use uniffi_bindgen::{bindings::TargetLanguage, library_mode::generate_bindings, library_mode::generate_mocks}; use xshell::{cmd, pushd}; use crate::{workspace, Result}; @@ -33,6 +33,10 @@ enum SwiftCommand { #[clap(long)] only_target: Option, + /// Build mocks + #[clap(long)] + build_mocks: Option, + /// Move the generated xcframework and swift sources into the given /// components-folder #[clap(long)] @@ -44,6 +48,19 @@ enum SwiftCommand { #[clap(long)] sequentially: bool, }, + + /// Builds mock files for the SDK for Swift. + BuildMocks { + /// Library path to use for generating mocks + #[clap(long)] + library_path: Utf8PathBuf, + + /// Move the generated file source into the given + /// output-folder + /// Apple Silicon M1s + #[clap(long)] + output_directory: Option, + }, } impl SwiftArgs { @@ -57,11 +74,13 @@ impl SwiftArgs { profile, components_path, only_target, + build_mocks, sequentially, } => { let profile = profile.as_deref().unwrap_or(if release { "release" } else { "dev" }); - build_xcframework(profile, only_target, components_path, sequentially) + build_xcframework(profile, only_target, build_mocks.unwrap_or_else(|| false), components_path, sequentially) } + SwiftCommand::BuildMocks { library_path, output_directory } => build_mocks(&library_path, output_directory), } } } @@ -85,6 +104,7 @@ fn build_library() -> Result<()> { create_dir_all(swift_directory.as_path())?; generate_uniffi(&ffi_directory.join(FFI_LIBRARY_NAME), &ffi_directory)?; + generate_uniffi_mocks(&ffi_directory.join(FFI_LIBRARY_NAME), &ffi_directory)?; let module_map_file = ffi_directory.join("module.modulemap"); if module_map_file.exists() { @@ -118,6 +138,7 @@ fn build_for_target(target: &str, profile: &str) -> Result { fn build_xcframework( profile: &str, only_target: Option, + build_mocks: bool, components_path: Option, sequentially: bool, ) -> Result<()> { @@ -221,6 +242,11 @@ fn build_xcframework( println!("-- Generating uniffi files"); generate_uniffi(&uniffi_lib_path, &generated_dir)?; + if build_mocks { + println!("-- Generating uniffi mock files"); + generate_uniffi_mocks(&uniffi_lib_path, &generated_dir)?; + } + move_files("h", &generated_dir, &headers_dir)?; consolidate_modulemap_files(&generated_dir, &headers_dir)?; @@ -315,3 +341,20 @@ fn consolidate_modulemap_files(source: &Utf8PathBuf, destination: &Utf8PathBuf) std::fs::write(destination.join("module.modulemap"), modulemap)?; Ok(()) } + +fn build_mocks( + library_path: &Utf8Path, + output_directory: Option +) -> Result<()> { + let output_directory = output_directory.unwrap_or_else(|| library_path.parent().unwrap().into()); + + generate_uniffi_mocks(library_path, &output_directory)?; + + println!("Mocks file generated in {output_directory}"); + Ok(()) +} + +fn generate_uniffi_mocks(library_path: &Utf8Path, ffi_directory: &Utf8Path) -> Result<()> { + generate_mocks(library_path, None, &[TargetLanguage::Swift], None, ffi_directory, false)?; + Ok(()) +}