Explorar o código

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

Lucas Nogueira %!s(int64=3) %!d(string=hai) anos
pai
achega
9c9dead8a0

+ 9 - 0
tooling/cli.rs/src/cli.yml

@@ -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

+ 17 - 1
tooling/cli.rs/src/main.rs

@@ -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);
   }

+ 38 - 0
tooling/cli.rs/src/plugin.rs

@@ -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,

+ 1 - 0
tooling/cli.rs/templates/plugin/backend/.license_template

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

+ 1 - 1
tooling/cli.rs/templates/plugin/backend/Cargo.crate-manifest

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

+ 1 - 1
tooling/cli.rs/templates/plugin/backend/examples/vanilla/src-tauri/Cargo.crate-manifest

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

+ 15 - 0
tooling/cli.rs/templates/plugin/backend/rustfmt.toml

@@ -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"

+ 3 - 0
tooling/cli.rs/templates/plugin/backend/src/lib.rs

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

+ 1 - 0
tooling/cli.rs/templates/plugin/with-api/.license_template

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

+ 1 - 1
tooling/cli.rs/templates/plugin/with-api/Cargo.crate-manifest

@@ -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"]

+ 1 - 1
tooling/cli.rs/templates/plugin/with-api/examples/svelte-app/src-tauri/Cargo.crate-manifest

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

+ 1 - 3
tooling/cli.rs/templates/plugin/with-api/package.json

@@ -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",

+ 15 - 0
tooling/cli.rs/templates/plugin/with-api/rustfmt.toml

@@ -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"

+ 4 - 0
tooling/cli.rs/templates/plugin/with-api/src/lib.rs

@@ -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};

+ 3 - 0
tooling/cli.rs/templates/plugin/with-api/webview-src/index.ts

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