2021-09-28 02:15:17 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
helpers::{resolve_tauri_path, template, Logger},
|
|
|
|
VersionMetadata,
|
|
|
|
};
|
|
|
|
use anyhow::Context;
|
|
|
|
use handlebars::{to_json, Handlebars};
|
|
|
|
use heck::{KebabCase, SnakeCase};
|
|
|
|
use include_dir::{include_dir, Dir};
|
|
|
|
|
|
|
|
use std::{collections::BTreeMap, env::current_dir, fs::remove_dir_all, path::PathBuf};
|
|
|
|
|
|
|
|
const BACKEND_PLUGIN_DIR: Dir = include_dir!("templates/plugin/backend");
|
|
|
|
const API_PLUGIN_DIR: Dir = include_dir!("templates/plugin/with-api");
|
|
|
|
|
|
|
|
pub struct Plugin {
|
|
|
|
plugin_name: String,
|
|
|
|
api: bool,
|
2021-10-03 14:58:31 +03:00
|
|
|
tauri: bool,
|
2021-09-28 02:15:17 +03:00
|
|
|
directory: PathBuf,
|
|
|
|
tauri_path: Option<PathBuf>,
|
2021-10-03 14:58:31 +03:00
|
|
|
author: String,
|
2021-09-28 02:15:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Plugin {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
plugin_name: "".into(),
|
|
|
|
api: false,
|
2021-10-03 14:58:31 +03:00
|
|
|
tauri: false,
|
2021-09-28 02:15:17 +03:00
|
|
|
directory: current_dir().expect("failed to read cwd"),
|
|
|
|
tauri_path: None,
|
2021-10-03 14:58:31 +03:00
|
|
|
author: "".into(),
|
2021-09-28 02:15:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plugin_name(mut self, plugin_name: String) -> Self {
|
|
|
|
self.plugin_name = plugin_name;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn api(mut self) -> Self {
|
|
|
|
self.api = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-03 14:58:31 +03:00
|
|
|
pub fn tauri(mut self) -> Self {
|
|
|
|
self.tauri = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-28 02:15:17 +03:00
|
|
|
pub fn directory(mut self, directory: impl Into<PathBuf>) -> Self {
|
|
|
|
self.directory = directory.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tauri_path(mut self, tauri_path: impl Into<PathBuf>) -> Self {
|
|
|
|
self.tauri_path = Some(tauri_path.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-03 14:58:31 +03:00
|
|
|
pub fn author(mut self, author: String) -> Self {
|
|
|
|
self.author = author;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-28 02:15:17 +03:00
|
|
|
pub fn run(self) -> crate::Result<()> {
|
|
|
|
let logger = Logger::new("tauri:init:plugin");
|
|
|
|
let template_target_path = self.directory.join(&format!(
|
|
|
|
"tauri-plugin-{}",
|
|
|
|
self.plugin_name.to_kebab_case()
|
|
|
|
));
|
|
|
|
let metadata = serde_json::from_str::<VersionMetadata>(include_str!("../metadata.json"))?;
|
|
|
|
if template_target_path.exists() {
|
|
|
|
logger.warn(format!(
|
|
|
|
"Plugin dir ({:?}) not empty.",
|
|
|
|
template_target_path
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
let (tauri_dep, tauri_build_dep) = if let Some(tauri_path) = self.tauri_path {
|
|
|
|
(
|
|
|
|
format!(
|
2021-10-02 02:21:47 +03:00
|
|
|
r#"{{ path = {:?}, features = [ "api-all" ] }}"#,
|
2021-09-28 02:15:17 +03:00
|
|
|
resolve_tauri_path(&tauri_path, "core/tauri")
|
|
|
|
),
|
|
|
|
format!(
|
|
|
|
"{{ path = {:?} }}",
|
|
|
|
resolve_tauri_path(&tauri_path, "core/tauri-build")
|
|
|
|
),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
2021-10-01 19:17:02 +03:00
|
|
|
format!(
|
|
|
|
r#"{{ version = "{}", features = [ "api-all" ] }}"#,
|
|
|
|
metadata.tauri
|
|
|
|
),
|
2021-10-02 02:21:47 +03:00
|
|
|
format!(r#"{{ version = "{}" }}"#, metadata.tauri_build),
|
2021-09-28 02:15:17 +03:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = remove_dir_all(&template_target_path);
|
|
|
|
let handlebars = Handlebars::new();
|
|
|
|
|
|
|
|
let mut data = BTreeMap::new();
|
|
|
|
data.insert("plugin_name_original", to_json(&self.plugin_name));
|
|
|
|
data.insert("plugin_name", to_json(self.plugin_name.to_kebab_case()));
|
|
|
|
data.insert(
|
|
|
|
"plugin_name_snake_case",
|
|
|
|
to_json(self.plugin_name.to_snake_case()),
|
|
|
|
);
|
|
|
|
data.insert("tauri_dep", to_json(tauri_dep));
|
|
|
|
data.insert("tauri_build_dep", to_json(tauri_build_dep));
|
2021-10-03 14:58:31 +03:00
|
|
|
data.insert("author", to_json(self.author));
|
|
|
|
|
|
|
|
if self.tauri {
|
|
|
|
data.insert(
|
|
|
|
"license_template",
|
|
|
|
to_json(
|
|
|
|
"// Copyright {20\\d{2}(-20\\d{2})?} Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT\n\n"
|
|
|
|
.replace(" ", "")
|
|
|
|
.replace(" //", "//"),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
data.insert(
|
|
|
|
"license_header",
|
|
|
|
to_json(
|
|
|
|
"// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT\n\n"
|
|
|
|
.replace(" ", "")
|
|
|
|
.replace(" //", "//"),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-09-28 02:15:17 +03:00
|
|
|
|
|
|
|
template::render(
|
|
|
|
&handlebars,
|
|
|
|
&data,
|
|
|
|
if self.api {
|
|
|
|
&API_PLUGIN_DIR
|
|
|
|
} else {
|
|
|
|
&BACKEND_PLUGIN_DIR
|
|
|
|
},
|
|
|
|
&template_target_path,
|
|
|
|
)
|
|
|
|
.with_context(|| "failed to render Tauri template")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|