feat(cli.rs): add tauri and author options to the init plugin cmd

This commit is contained in:
Lucas Nogueira 2021-10-03 08:58:31 -03:00
parent 62b6b5b1aa
commit 9c9dead8a0
No known key found for this signature in database
GPG Key ID: 2714B66BCFB01F7F
15 changed files with 111 additions and 8 deletions

View File

@ -195,3 +195,12 @@ subcommands:
short: a
long: api
about: Initializes a Tauri plugin with TypeScript API.
- author:
long: author
about: Author name.
takes_value: true
- tauri:
short: t
long: tauri
about: Initializes a Tauri core plugin (internal usage).
setting: Hidden

View File

@ -69,12 +69,28 @@ fn plugin_command(matches: &ArgMatches) -> Result<()> {
let plugin_name = matches.value_of("name").expect("name is required");
let directory = matches.value_of("directory");
let tauri_path = matches.value_of("tauri-path");
let tauri = matches.is_present("tauri");
let author = matches
.value_of("author")
.map(|p| p.to_string())
.unwrap_or_else(|| {
if tauri {
"Tauri Programme within The Commons Conservancy".into()
} else {
"You".into()
}
});
let mut plugin_runner = plugin::Plugin::new().plugin_name(plugin_name.to_string());
let mut plugin_runner = plugin::Plugin::new()
.plugin_name(plugin_name.to_string())
.author(author);
if api {
plugin_runner = plugin_runner.api();
}
if tauri {
plugin_runner = plugin_runner.tauri();
}
if let Some(directory) = directory {
plugin_runner = plugin_runner.directory(directory);
}

View File

@ -19,8 +19,10 @@ const API_PLUGIN_DIR: Dir = include_dir!("templates/plugin/with-api");
pub struct Plugin {
plugin_name: String,
api: bool,
tauri: bool,
directory: PathBuf,
tauri_path: Option<PathBuf>,
author: String,
}
impl Default for Plugin {
@ -28,8 +30,10 @@ impl Default for Plugin {
Self {
plugin_name: "".into(),
api: false,
tauri: false,
directory: current_dir().expect("failed to read cwd"),
tauri_path: None,
author: "".into(),
}
}
}
@ -49,6 +53,11 @@ impl Plugin {
self
}
pub fn tauri(mut self) -> Self {
self.tauri = true;
self
}
pub fn directory(mut self, directory: impl Into<PathBuf>) -> Self {
self.directory = directory.into();
self
@ -59,6 +68,11 @@ impl Plugin {
self
}
pub fn author(mut self, author: String) -> Self {
self.author = author;
self
}
pub fn run(self) -> crate::Result<()> {
let logger = Logger::new("tauri:init:plugin");
let template_target_path = self.directory.join(&format!(
@ -105,6 +119,30 @@ impl Plugin {
);
data.insert("tauri_dep", to_json(tauri_dep));
data.insert("tauri_build_dep", to_json(tauri_build_dep));
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(" //", "//"),
),
);
}
template::render(
&handlebars,

View File

@ -0,0 +1 @@
{{ license_template }}

View File

@ -1,7 +1,7 @@
[package]
name = "tauri-plugin-{{ plugin_name }}"
version = "0.0.0"
authors = [ "You" ]
authors = [ "{{ author }}" ]
description = ""
edition = "2018"
exclude = ["/examples"]

View File

@ -2,7 +2,7 @@
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = [ "You" ]
authors = [ "{{ author }}" ]
repository = ""
edition = "2018"

View File

@ -0,0 +1,15 @@
max_width = 100
hard_tabs = false
tab_spaces = 2
newline_style = "Unix"
use_small_heuristics = "Default"
reorder_imports = true
reorder_modules = true
remove_nested_parens = true
edition = "2018"
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
normalize_doc_attributes = true
license_template_path = ".license_template"

View File

@ -1,3 +1,6 @@
{{#if license_header}}
{{ license_header }}
{{/if}}
use tauri::{plugin::Plugin, Runtime};
#[derive(Default)]

View File

@ -0,0 +1 @@
{{ license_template }}

View File

@ -1,7 +1,7 @@
[package]
name = "tauri-plugin-{{ plugin_name }}"
version = "0.0.0"
authors = [ "You" ]
authors = [ "{{ author }}" ]
description = ""
edition = "2018"
exclude = ["/examples", "/webview-dist", "/webview-src", "node_modules"]

View File

@ -2,7 +2,7 @@
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = [ "You" ]
authors = [ "{{ author }}" ]
repository = ""
edition = "2018"

View File

@ -1,9 +1,7 @@
{
"name": "tauri-plugin-{{ plugin_name }}-api",
"version": "0.0.0",
"authors": [
"You"
],
"author": "{{ author }}",
"description": "",
"browser": "webview-dist/index.js",
"main": "webview-dist/index.js",

View File

@ -0,0 +1,15 @@
max_width = 100
hard_tabs = false
tab_spaces = 2
newline_style = "Unix"
use_small_heuristics = "Default"
reorder_imports = true
reorder_modules = true
remove_nested_parens = true
edition = "2018"
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
normalize_doc_attributes = true
license_template_path = ".license_template"

View File

@ -1,3 +1,7 @@
{{#if license_header}}
{{ license_header }}
{{/if}}
use serde::{ser::Serializer, Serialize};
use serde_json::Value as JsonValue;
use tauri::{command, plugin::Plugin, AppHandle, Invoke, Manager, Runtime, State, Window};

View File

@ -1,3 +1,6 @@
{{#if license_header}}
{{ license_header }}
{{/if}}
import { invoke } from '@tauri-apps/api/tauri'
export async function execute() {