Skip to content

Commit 1a384f0

Browse files
pixlwavestefanceriu
authored andcommitted
xtask: Workaround UniFFI's noHandle generation for Swift.
mozilla/uniffi-rs#2717
1 parent 781df55 commit 1a384f0

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xtask/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cargo_metadata = "0.19.0"
1717
camino = "1.0.8"
1818
clap = { version = "4.0.18", features = ["derive"] }
1919
fs_extra = "1"
20+
regex = "1"
2021
serde = { workspace = true, features = ["derive"] }
2122
serde_json.workspace = true
2223
uniffi_bindgen.workspace = true

xtask/src/swift.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ fn build_xcframework(
250250
consolidate_modulemap_files(&generated_dir, &headers_module_dir)?;
251251

252252
move_files("swift", &generated_dir, &swift_dir)?;
253+
apply_uniffi_mocks_workaround(&swift_dir)?;
253254

254255
println!("-- Generating MatrixSDKFFI.xcframework framework");
255256
let xcframework_path = generated_dir.join("MatrixSDKFFI.xcframework");
@@ -431,3 +432,26 @@ fn consolidate_modulemap_files(source: &Utf8Path, destination: &Utf8Path) -> Res
431432
std::fs::write(destination.join("module.modulemap"), modulemap)?;
432433
Ok(())
433434
}
435+
436+
// Temporary workaround for https://github.com/mozilla/uniffi-rs/issues/2717
437+
fn apply_uniffi_mocks_workaround(swift_dir: &Utf8Path) -> Result<()> {
438+
let regex = regex::Regex::new(r#"(deinit\s*\{\n)(\s*try!\s*rustCall)"#)?;
439+
440+
for entry in swift_dir.read_dir_utf8()? {
441+
let entry = entry?;
442+
if entry.file_type()?.is_file() {
443+
let path = entry.path();
444+
if path.extension() == Some("swift") {
445+
let mut contents = std::fs::read_to_string(path)?;
446+
let new_contents =
447+
regex.replace_all(&contents, "$1 guard handle != 0 else { return }\n$2");
448+
if new_contents != contents {
449+
contents = new_contents.to_string();
450+
std::fs::write(path, contents)?;
451+
}
452+
}
453+
}
454+
}
455+
456+
Ok(())
457+
}

0 commit comments

Comments
 (0)