fix(cli): adjust plugin template to use correct package name (#6258)

This commit is contained in:
Lucas Fernandes Nogueira
2023-02-13 05:54:48 -08:00
committed by GitHub
parent bef4ef51bc
commit 65e487f344
13 changed files with 67 additions and 26 deletions

View File

@@ -4,7 +4,7 @@ plugins {
}
android {
namespace = "com.plugin.test"
namespace = "com.plugin.sample"
compileSdk = 32
defaultConfig {
@@ -42,4 +42,4 @@ dependencies {
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation(project(":tauri-android"))
}
}

View File

@@ -1,4 +1,4 @@
package com.plugin.test
package com.plugin.sample
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -19,6 +19,6 @@ class ExampleInstrumentedTest {
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.plugin.test.test", appContext.packageName)
assertEquals("com.plugin.sample", appContext.packageName)
}
}
}

View File

@@ -1,4 +1,4 @@
package com.plugin.test
package com.plugin.sample
import android.app.Activity
import app.tauri.plugin.JSObject

View File

@@ -1,4 +1,4 @@
package com.plugin.test
package com.plugin.sample
import org.junit.Test
@@ -14,4 +14,4 @@ class ExampleUnitTest {
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
}

View File

@@ -5,7 +5,7 @@ use tauri::{
const PLUGIN_NAME: &str = "sample";
#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "com.plugin.test";
const PLUGIN_IDENTIFIER: &str = "com.plugin.sample";
#[cfg(target_os = "ios")]
extern "C" {

View File

@@ -146,7 +146,7 @@ pub fn gen(
Ok(())
}
pub(crate) fn generate_out_file(
fn generate_out_file(
path: &Path,
dest: &Path,
package_path: &str,

View File

@@ -15,7 +15,12 @@ use heck::{AsKebabCase, ToKebabCase, ToSnakeCase};
use include_dir::{include_dir, Dir};
use log::warn;
use std::{
collections::BTreeMap, env::current_dir, fmt::Display, fs::remove_dir_all, path::PathBuf,
collections::BTreeMap,
env::current_dir,
ffi::OsStr,
fmt::Display,
fs::{create_dir_all, remove_dir_all, File, OpenOptions},
path::{Path, PathBuf},
str::FromStr,
};
@@ -153,18 +158,14 @@ pub fn command(mut options: Options) -> Result<()> {
data.insert("package_id", to_json(&plugin_id));
let mut created_dirs = Vec::new();
let dest = template_target_path.join("android");
template::render_with_generator(
&handlebars,
&data,
&ANDROID_PLUGIN_DIR,
&template_target_path,
&dest,
&mut |path| {
crate::mobile::android::project::generate_out_file(
path,
&template_target_path.join("android"),
&plugin_id.replace('.', "/"),
&mut created_dirs,
)
generate_android_out_file(path, &dest, &plugin_id.replace('.', "/"), &mut created_dirs)
},
)
.with_context(|| "failed to render plugin Android template")?;
@@ -183,6 +184,46 @@ pub fn command(mut options: Options) -> Result<()> {
Ok(())
}
fn generate_android_out_file(
path: &Path,
dest: &Path,
package_path: &str,
created_dirs: &mut Vec<PathBuf>,
) -> std::io::Result<Option<File>> {
let mut iter = path.iter();
let root = iter.next().unwrap().to_str().unwrap();
let path = match (root, path.extension().and_then(|o| o.to_str())) {
("src", Some("kt")) => {
let parent = path.parent().unwrap();
let file_name = path.file_name().unwrap();
let out_dir = dest.join(parent).join(package_path);
out_dir.join(file_name)
}
_ => dest.join(path),
};
let parent = path.parent().unwrap().to_path_buf();
if !created_dirs.contains(&parent) {
create_dir_all(&parent)?;
created_dirs.push(parent);
}
let mut options = OpenOptions::new();
options.write(true);
#[cfg(unix)]
if path.file_name().unwrap() == OsStr::new("gradlew") {
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o755);
}
if path.file_name().unwrap() == OsStr::new("BuildTask.kt") || !path.exists() {
options.create(true).open(path).map(Some)
} else {
Ok(None)
}
}
fn request_input<T>(
prompt: &str,
initial: Option<T>,

View File

@@ -4,7 +4,7 @@ plugins {
}
android {
namespace = "com.plugin.test"
namespace = "{{package_id}}"
compileSdk = 32
defaultConfig {
@@ -42,4 +42,4 @@ dependencies {
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation(project(":tauri-android"))
}
}

View File

@@ -1,4 +1,4 @@
package com.plugin.test
package {{package_id}}
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -19,6 +19,6 @@ class ExampleInstrumentedTest {
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.plugin.test.test", appContext.packageName)
assertEquals("{{package_id}}", appContext.packageName)
}
}
}

View File

@@ -1,4 +1,4 @@
package com.plugin.test
package {{package_id}}
import org.junit.Test
@@ -14,4 +14,4 @@ class ExampleUnitTest {
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
}