File size: 2,148 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{fmt::Display, path::Path};

use clap::{Parser, Subcommand, ValueEnum};

use crate::{
  error::{Context, ErrorExt},
  Result,
};

mod android;
mod init;
mod ios;
mod new;

#[derive(Debug, Clone, ValueEnum, Default)]
pub enum PluginIosFramework {
  /// Swift Package Manager project
  #[default]
  Spm,
  /// Xcode project
  Xcode,
}

impl Display for PluginIosFramework {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Spm => write!(f, "spm"),
      Self::Xcode => write!(f, "xcode"),
    }
  }
}

#[derive(Parser)]
#[clap(
  author,
  version,
  about = "Manage or create Tauri plugins",
  subcommand_required(true),
  arg_required_else_help(true)
)]
pub struct Cli {
  #[clap(subcommand)]
  command: Commands,
}

#[derive(Subcommand)]
enum Commands {
  New(new::Options),
  Init(init::Options),
  Android(android::Cli),
  Ios(ios::Cli),
}

pub fn command(cli: Cli) -> Result<()> {
  match cli.command {
    Commands::New(options) => new::command(options)?,
    Commands::Init(options) => init::command(options)?,
    Commands::Android(cli) => android::command(cli)?,
    Commands::Ios(cli) => ios::command(cli)?,
  }

  Ok(())
}

fn infer_plugin_name<P: AsRef<Path>>(directory: P) -> Result<String> {
  let dir = directory.as_ref();
  let cargo_toml_path = dir.join("Cargo.toml");
  let name = if cargo_toml_path.exists() {
    let contents = std::fs::read_to_string(&cargo_toml_path)
      .fs_context("failed to read Cargo manifest", cargo_toml_path)?;
    let cargo_toml: toml::Value =
      toml::from_str(&contents).context("failed to parse Cargo.toml")?;
    cargo_toml
      .get("package")
      .and_then(|v| v.get("name"))
      .map(|v| v.as_str().unwrap_or_default())
      .unwrap_or_default()
      .to_string()
  } else {
    dir
      .file_name()
      .unwrap_or_default()
      .to_string_lossy()
      .to_string()
  };
  Ok(
    name
      .strip_prefix("tauri-plugin-")
      .unwrap_or(&name)
      .to_string(),
  )
}