Skip to content

Commit

Permalink
refactor(core): generate TauriActivity on build script (#6783)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 24, 2023
1 parent ecc9ac9 commit 9422490
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/generate-tauri-activity.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Generate `TauriActivity` Kotlin class on the build script.
3 changes: 2 additions & 1 deletion .github/workflows/test-android.yml
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: MIT

name: test mobile
name: test android

on:
pull_request:
Expand All @@ -14,6 +14,7 @@ on:
- '!tooling/cli/src/mobile/ios/**'
- 'core/tauri-build/src/mobile.rs'
- 'core/tauri/mobile/android/**'
- 'core/tauri/mobile/android-codegen/**'
workflow_dispatch:

concurrency:
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-runtime-wry/Cargo.toml
Expand Up @@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ]
readme = "README.md"

[dependencies]
wry = { version = "0.28.1", default-features = false, features = [ "file-drop", "protocol" ] }
wry = { version = "0.28.2", default-features = false, features = [ "file-drop", "protocol" ] }
tauri-runtime = { version = "0.13.0-alpha.4", path = "../tauri-runtime" }
tauri-utils = { version = "2.0.0-alpha.4", path = "../tauri-utils" }
uuid = { version = "1", features = [ "v4" ] }
Expand Down
44 changes: 43 additions & 1 deletion core/tauri/build.rs
Expand Up @@ -9,6 +9,9 @@ use heck::ToSnakeCase;
use once_cell::sync::OnceCell;

use std::env::var_os;
use std::fs::read_dir;
use std::fs::read_to_string;
use std::fs::write;
use std::{
env::var,
path::{Path, PathBuf},
Expand Down Expand Up @@ -150,7 +153,46 @@ fn main() {
}

if target_os == "android" {
if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
if let Ok(kotlin_out_dir) = std::env::var("WRY_ANDROID_KOTLIN_FILES_OUT_DIR") {
fn env_var(var: &str) -> String {
std::env::var(var).unwrap_or_else(|_| {
panic!(
"`{}` is not set, which is needed to generate the kotlin files for android.",
var
)
})
}

let package = env_var("WRY_ANDROID_PACKAGE");
let library = env_var("WRY_ANDROID_LIBRARY");

let kotlin_out_dir = PathBuf::from(&kotlin_out_dir)
.canonicalize()
.unwrap_or_else(move |_| {
panic!("Failed to canonicalize `WRY_ANDROID_KOTLIN_FILES_OUT_DIR` path {kotlin_out_dir}")
});

let kotlin_files_path =
PathBuf::from(env_var("CARGO_MANIFEST_DIR")).join("mobile/android-codegen");
println!("cargo:rerun-if-changed={}", kotlin_files_path.display());
let kotlin_files =
read_dir(kotlin_files_path).expect("failed to read Android codegen directory");

for file in kotlin_files {
let file = file.unwrap();

let content = read_to_string(file.path())
.expect("failed to read kotlin file as string")
.replace("{{package}}", &package)
.replace("{{library}}", &library);

let out_path = kotlin_out_dir.join(file.file_name());
write(&out_path, content).expect("Failed to write kotlin file");
println!("cargo:rerun-if-changed={}", out_path.display());
}
}

if let Some(project_dir) = var_os("WRY_ANDROID_PROJECT_PATH").map(PathBuf::from) {
let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace(
"$PACKAGE",
&var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"),
Expand Down
@@ -1,4 +1,10 @@
package {{reverse-domain app.domain}}.{{snake-case app.name}}
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

/* THIS FILE IS AUTO-GENERATED. DO NOT MODIFY!! */

package {{package}}

import android.os.Bundle
import app.tauri.plugin.PluginManager
Expand Down
4 changes: 2 additions & 2 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9422490

Please sign in to comment.