2022-08-28 18:13:21 +00:00
|
|
|
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
|
2021-12-09 15:21:33 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
use crate::Result;
|
|
|
|
|
use crate::{
|
2022-05-07 13:19:54 +00:00
|
|
|
helpers::{resolve_tauri_path, template},
|
2021-12-09 15:21:33 +00:00
|
|
|
VersionMetadata,
|
|
|
|
|
};
|
|
|
|
|
use anyhow::Context;
|
2022-02-27 20:35:43 +00:00
|
|
|
use clap::Parser;
|
2023-02-06 11:56:00 +00:00
|
|
|
use dialoguer::Input;
|
2021-12-09 15:21:33 +00:00
|
|
|
use handlebars::{to_json, Handlebars};
|
2022-07-11 19:07:39 +00:00
|
|
|
use heck::{AsKebabCase, ToKebabCase, ToSnakeCase};
|
2021-12-09 15:21:33 +00:00
|
|
|
use include_dir::{include_dir, Dir};
|
2022-05-07 13:19:54 +00:00
|
|
|
use log::warn;
|
2023-02-06 11:56:00 +00:00
|
|
|
use std::{
|
2023-02-13 13:54:48 +00:00
|
|
|
collections::BTreeMap,
|
|
|
|
|
env::current_dir,
|
|
|
|
|
ffi::OsStr,
|
|
|
|
|
fmt::Display,
|
|
|
|
|
fs::{create_dir_all, remove_dir_all, File, OpenOptions},
|
|
|
|
|
path::{Path, PathBuf},
|
2023-02-06 11:56:00 +00:00
|
|
|
str::FromStr,
|
|
|
|
|
};
|
2021-12-09 15:21:33 +00:00
|
|
|
|
|
|
|
|
const BACKEND_PLUGIN_DIR: Dir<'_> = include_dir!("templates/plugin/backend");
|
|
|
|
|
const API_PLUGIN_DIR: Dir<'_> = include_dir!("templates/plugin/with-api");
|
2023-02-06 11:56:00 +00:00
|
|
|
const ANDROID_PLUGIN_DIR: Dir<'_> = include_dir!("templates/plugin/android");
|
2023-02-11 13:30:44 +00:00
|
|
|
const IOS_PLUGIN_DIR: Dir<'_> = include_dir!("templates/plugin/ios");
|
2021-12-09 15:21:33 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
|
#[clap(about = "Initializes a Tauri plugin project")]
|
|
|
|
|
pub struct Options {
|
|
|
|
|
/// Name of your Tauri plugin
|
|
|
|
|
#[clap(short = 'n', long = "name")]
|
|
|
|
|
plugin_name: String,
|
|
|
|
|
/// Initializes a Tauri plugin with TypeScript API
|
2022-01-03 23:00:58 +00:00
|
|
|
#[clap(long)]
|
2021-12-09 15:21:33 +00:00
|
|
|
api: bool,
|
|
|
|
|
/// Initializes a Tauri core plugin (internal usage)
|
2022-02-08 16:13:46 +00:00
|
|
|
#[clap(long, hide(true))]
|
2021-12-09 15:21:33 +00:00
|
|
|
tauri: bool,
|
|
|
|
|
/// Set target directory for init
|
|
|
|
|
#[clap(short, long)]
|
|
|
|
|
#[clap(default_value_t = current_dir().expect("failed to read cwd").display().to_string())]
|
|
|
|
|
directory: String,
|
|
|
|
|
/// Path of the Tauri project to use (relative to the cwd)
|
|
|
|
|
#[clap(short, long)]
|
|
|
|
|
tauri_path: Option<PathBuf>,
|
|
|
|
|
/// Author name
|
|
|
|
|
#[clap(short, long)]
|
|
|
|
|
author: Option<String>,
|
2023-02-06 11:56:00 +00:00
|
|
|
/// Adds native Android support.
|
|
|
|
|
#[clap(long)]
|
|
|
|
|
android: bool,
|
2023-02-11 13:30:44 +00:00
|
|
|
/// Adds native iOS support.
|
|
|
|
|
#[clap(long)]
|
|
|
|
|
ios: bool,
|
2021-12-09 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Options {
|
|
|
|
|
fn load(&mut self) {
|
|
|
|
|
if self.author.is_none() {
|
|
|
|
|
self.author.replace(if self.tauri {
|
|
|
|
|
"Tauri Programme within The Commons Conservancy".into()
|
|
|
|
|
} else {
|
|
|
|
|
"You".into()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn command(mut options: Options) -> Result<()> {
|
|
|
|
|
options.load();
|
2022-12-15 20:56:23 +00:00
|
|
|
let template_target_path = PathBuf::from(options.directory).join(format!(
|
2021-12-09 15:21:33 +00:00
|
|
|
"tauri-plugin-{}",
|
2022-07-11 19:07:39 +00:00
|
|
|
AsKebabCase(&options.plugin_name)
|
2021-12-09 15:21:33 +00:00
|
|
|
));
|
|
|
|
|
let metadata = serde_json::from_str::<VersionMetadata>(include_str!("../../metadata.json"))?;
|
|
|
|
|
if template_target_path.exists() {
|
2022-05-07 13:19:54 +00:00
|
|
|
warn!("Plugin dir ({:?}) not empty.", template_target_path);
|
2021-12-09 15:21:33 +00:00
|
|
|
} else {
|
|
|
|
|
let (tauri_dep, tauri_example_dep, tauri_build_dep) =
|
|
|
|
|
if let Some(tauri_path) = options.tauri_path {
|
|
|
|
|
(
|
|
|
|
|
format!(
|
|
|
|
|
r#"{{ path = {:?} }}"#,
|
|
|
|
|
resolve_tauri_path(&tauri_path, "core/tauri")
|
|
|
|
|
),
|
|
|
|
|
format!(
|
2022-11-03 21:57:32 +00:00
|
|
|
r#"{{ path = {:?} }}"#,
|
2021-12-09 15:21:33 +00:00
|
|
|
resolve_tauri_path(&tauri_path, "core/tauri")
|
|
|
|
|
),
|
|
|
|
|
format!(
|
|
|
|
|
"{{ path = {:?} }}",
|
|
|
|
|
resolve_tauri_path(&tauri_path, "core/tauri-build")
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(
|
|
|
|
|
format!(r#"{{ version = "{}" }}"#, metadata.tauri),
|
2022-11-03 21:57:32 +00:00
|
|
|
format!(r#"{{ version = "{}" }}"#, metadata.tauri),
|
2021-12-09 15:21:33 +00:00
|
|
|
format!(r#"{{ version = "{}" }}"#, metadata.tauri_build),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let _ = remove_dir_all(&template_target_path);
|
|
|
|
|
let handlebars = Handlebars::new();
|
|
|
|
|
|
|
|
|
|
let mut data = BTreeMap::new();
|
|
|
|
|
data.insert("plugin_name_original", to_json(&options.plugin_name));
|
|
|
|
|
data.insert("plugin_name", to_json(options.plugin_name.to_kebab_case()));
|
|
|
|
|
data.insert(
|
|
|
|
|
"plugin_name_snake_case",
|
|
|
|
|
to_json(options.plugin_name.to_snake_case()),
|
|
|
|
|
);
|
|
|
|
|
data.insert("tauri_dep", to_json(tauri_dep));
|
|
|
|
|
data.insert("tauri_example_dep", to_json(tauri_example_dep));
|
|
|
|
|
data.insert("tauri_build_dep", to_json(tauri_build_dep));
|
|
|
|
|
data.insert("author", to_json(options.author));
|
|
|
|
|
|
|
|
|
|
if options.tauri {
|
|
|
|
|
data.insert(
|
|
|
|
|
"license_header",
|
|
|
|
|
to_json(
|
2022-08-28 18:13:21 +00:00
|
|
|
"// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
|
2021-12-09 15:21:33 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
// SPDX-License-Identifier: MIT\n\n"
|
|
|
|
|
.replace(" ", "")
|
|
|
|
|
.replace(" //", "//"),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template::render(
|
|
|
|
|
&handlebars,
|
|
|
|
|
&data,
|
|
|
|
|
if options.api {
|
|
|
|
|
&API_PLUGIN_DIR
|
|
|
|
|
} else {
|
|
|
|
|
&BACKEND_PLUGIN_DIR
|
|
|
|
|
},
|
|
|
|
|
&template_target_path,
|
|
|
|
|
)
|
|
|
|
|
.with_context(|| "failed to render Tauri template")?;
|
2023-02-06 11:56:00 +00:00
|
|
|
|
|
|
|
|
if options.android {
|
|
|
|
|
let plugin_id = request_input(
|
|
|
|
|
"What should be the Package ID for your plugin?",
|
|
|
|
|
Some(format!("com.plugin.{}", options.plugin_name)),
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
)?
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let mut data = BTreeMap::new();
|
|
|
|
|
data.insert("package_id", to_json(&plugin_id));
|
|
|
|
|
|
|
|
|
|
let mut created_dirs = Vec::new();
|
2023-02-13 13:54:48 +00:00
|
|
|
let dest = template_target_path.join("android");
|
2023-02-06 11:56:00 +00:00
|
|
|
template::render_with_generator(
|
|
|
|
|
&handlebars,
|
|
|
|
|
&data,
|
|
|
|
|
&ANDROID_PLUGIN_DIR,
|
2023-02-13 13:54:48 +00:00
|
|
|
&dest,
|
2023-02-06 11:56:00 +00:00
|
|
|
&mut |path| {
|
2023-02-13 13:54:48 +00:00
|
|
|
generate_android_out_file(path, &dest, &plugin_id.replace('.', "/"), &mut created_dirs)
|
2023-02-06 11:56:00 +00:00
|
|
|
},
|
|
|
|
|
)
|
2023-02-11 13:30:44 +00:00
|
|
|
.with_context(|| "failed to render plugin Android template")?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if options.ios {
|
|
|
|
|
template::render(
|
|
|
|
|
&handlebars,
|
|
|
|
|
&data,
|
|
|
|
|
&IOS_PLUGIN_DIR,
|
|
|
|
|
template_target_path.join("ios"),
|
|
|
|
|
)
|
|
|
|
|
.with_context(|| "failed to render plugin iOS template")?;
|
2023-02-06 11:56:00 +00:00
|
|
|
}
|
2021-12-09 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-02-06 11:56:00 +00:00
|
|
|
|
2023-02-13 13:54:48 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 11:56:00 +00:00
|
|
|
fn request_input<T>(
|
|
|
|
|
prompt: &str,
|
|
|
|
|
initial: Option<T>,
|
|
|
|
|
skip: bool,
|
|
|
|
|
allow_empty: bool,
|
|
|
|
|
) -> Result<Option<T>>
|
|
|
|
|
where
|
|
|
|
|
T: Clone + FromStr + Display + ToString,
|
|
|
|
|
T::Err: Display + std::fmt::Debug,
|
|
|
|
|
{
|
|
|
|
|
if skip {
|
|
|
|
|
Ok(initial)
|
|
|
|
|
} else {
|
|
|
|
|
let theme = dialoguer::theme::ColorfulTheme::default();
|
|
|
|
|
let mut builder = Input::with_theme(&theme);
|
|
|
|
|
builder.with_prompt(prompt);
|
|
|
|
|
builder.allow_empty(allow_empty);
|
|
|
|
|
|
|
|
|
|
if let Some(v) = initial {
|
|
|
|
|
builder.with_initial_text(v.to_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.interact_text().map(Some).map_err(Into::into)
|
|
|
|
|
}
|
|
|
|
|
}
|