new.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use super::PluginIosFramework;
  5. use crate::Result;
  6. use clap::Parser;
  7. use std::path::PathBuf;
  8. #[derive(Debug, Parser)]
  9. #[clap(about = "Initializes a new Tauri plugin project")]
  10. pub struct Options {
  11. /// Name of your Tauri plugin
  12. plugin_name: String,
  13. /// Initializes a Tauri plugin without the TypeScript API
  14. #[clap(long)]
  15. no_api: bool,
  16. /// Initialize without an example project.
  17. #[clap(long)]
  18. no_example: bool,
  19. /// Set target directory for init
  20. #[clap(short, long)]
  21. directory: Option<String>,
  22. /// Author name
  23. #[clap(short, long)]
  24. author: Option<String>,
  25. /// Whether to initialize an Android project for the plugin.
  26. #[clap(long)]
  27. android: bool,
  28. /// Whether to initialize an iOS project for the plugin.
  29. #[clap(long)]
  30. ios: bool,
  31. /// Whether to initialize Android and iOS projects for the plugin.
  32. #[clap(long)]
  33. mobile: bool,
  34. /// Type of framework to use for the iOS project.
  35. #[clap(long)]
  36. #[clap(default_value_t = PluginIosFramework::default())]
  37. pub(crate) ios_framework: PluginIosFramework,
  38. /// Generate github workflows
  39. #[clap(long)]
  40. github_workflows: bool,
  41. /// Initializes a Tauri core plugin (internal usage)
  42. #[clap(long, hide(true))]
  43. tauri: bool,
  44. /// Path of the Tauri project to use (relative to the cwd)
  45. #[clap(short, long)]
  46. tauri_path: Option<PathBuf>,
  47. }
  48. impl From<Options> for super::init::Options {
  49. fn from(o: Options) -> Self {
  50. Self {
  51. plugin_name: Some(o.plugin_name),
  52. no_api: o.no_api,
  53. no_example: o.no_example,
  54. directory: o.directory.unwrap(),
  55. author: o.author,
  56. android: o.android,
  57. ios: o.ios,
  58. mobile: o.mobile,
  59. ios_framework: o.ios_framework,
  60. github_workflows: o.github_workflows,
  61. tauri: o.tauri,
  62. tauri_path: o.tauri_path,
  63. }
  64. }
  65. }
  66. pub fn command(mut options: Options) -> Result<()> {
  67. let cwd = std::env::current_dir()?;
  68. if let Some(dir) = &options.directory {
  69. std::fs::create_dir_all(cwd.join(dir))?;
  70. } else {
  71. let target = cwd.join(format!("tauri-plugin-{}", options.plugin_name));
  72. std::fs::create_dir_all(&target)?;
  73. options.directory.replace(target.display().to_string());
  74. }
  75. super::init::command(options.into())
  76. }