123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
- // SPDX-License-Identifier: Apache-2.0
- // SPDX-License-Identifier: MIT
- pub use anyhow::Result;
- mod build;
- mod dev;
- mod helpers;
- mod info;
- mod init;
- mod interface;
- mod plugin;
- mod signer;
- use clap::{FromArgMatches, IntoApp, Parser, Subcommand};
- use std::ffi::OsString;
- const TARGET: Option<&str> = option_env!("TARGET");
- pub(crate) trait CommandExt {
- fn pipe(&mut self) -> Result<&mut Self>;
- }
- impl CommandExt for std::process::Command {
- fn pipe(&mut self) -> Result<&mut Self> {
- self.stdout(os_pipe::dup_stdout()?);
- self.stderr(os_pipe::dup_stderr()?);
- Ok(self)
- }
- }
- #[derive(serde::Deserialize)]
- pub struct VersionMetadata {
- tauri: String,
- #[serde(rename = "tauri-build")]
- tauri_build: String,
- }
- #[derive(Parser)]
- #[clap(
- author,
- version,
- about,
- bin_name("cargo-tauri"),
- subcommand_required(true),
- arg_required_else_help(true),
- propagate_version(true),
- no_binary_name(true)
- )]
- struct Cli {
- #[clap(subcommand)]
- command: Commands,
- }
- #[derive(Subcommand)]
- enum Commands {
- Build(build::Options),
- Dev(dev::Options),
- Info(info::Options),
- Init(init::Options),
- Plugin(plugin::Cli),
- Signer(signer::Cli),
- }
- fn format_error<I: IntoApp>(err: clap::Error) -> clap::Error {
- let mut app = I::command();
- err.format(&mut app)
- }
- /// Run the Tauri CLI with the passed arguments.
- ///
- /// The passed arguments should have the binary argument(s) stripped out before being passed.
- ///
- /// e.g.
- /// 1. `tauri-cli 1 2 3` -> `1 2 3`
- /// 2. `cargo tauri 1 2 3` -> `1 2 3`
- /// 3. `node tauri.js 1 2 3` -> `1 2 3`
- ///
- /// The passed `bin_name` parameter should be how you want the help messages to display the command.
- /// This defaults to `cargo-tauri`, but should be set to how the program was called, such as
- /// `cargo tauri`.
- pub fn run<I, A>(args: I, bin_name: Option<String>) -> Result<()>
- where
- I: IntoIterator<Item = A>,
- A: Into<OsString> + Clone,
- {
- let matches = match bin_name {
- Some(bin_name) => Cli::command().bin_name(bin_name),
- None => Cli::command(),
- }
- .get_matches_from(args);
- let res = Cli::from_arg_matches(&matches).map_err(format_error::<Cli>);
- let cli = match res {
- Ok(s) => s,
- Err(e) => e.exit(),
- };
- match cli.command {
- Commands::Build(options) => build::command(options)?,
- Commands::Dev(options) => dev::command(options)?,
- Commands::Info(options) => info::command(options)?,
- Commands::Init(options) => init::command(options)?,
- Commands::Plugin(cli) => plugin::command(cli)?,
- Commands::Signer(cli) => signer::command(cli)?,
- }
- Ok(())
- }
|