lib.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. pub use anyhow::Result;
  5. mod build;
  6. mod dev;
  7. mod helpers;
  8. mod info;
  9. mod init;
  10. mod interface;
  11. mod plugin;
  12. mod signer;
  13. use clap::{FromArgMatches, IntoApp, Parser, Subcommand};
  14. use std::ffi::OsString;
  15. const TARGET: Option<&str> = option_env!("TARGET");
  16. pub(crate) trait CommandExt {
  17. fn pipe(&mut self) -> Result<&mut Self>;
  18. }
  19. impl CommandExt for std::process::Command {
  20. fn pipe(&mut self) -> Result<&mut Self> {
  21. self.stdout(os_pipe::dup_stdout()?);
  22. self.stderr(os_pipe::dup_stderr()?);
  23. Ok(self)
  24. }
  25. }
  26. #[derive(serde::Deserialize)]
  27. pub struct VersionMetadata {
  28. tauri: String,
  29. #[serde(rename = "tauri-build")]
  30. tauri_build: String,
  31. }
  32. #[derive(Parser)]
  33. #[clap(
  34. author,
  35. version,
  36. about,
  37. bin_name("cargo-tauri"),
  38. subcommand_required(true),
  39. arg_required_else_help(true),
  40. propagate_version(true),
  41. no_binary_name(true)
  42. )]
  43. struct Cli {
  44. #[clap(subcommand)]
  45. command: Commands,
  46. }
  47. #[derive(Subcommand)]
  48. enum Commands {
  49. Build(build::Options),
  50. Dev(dev::Options),
  51. Info(info::Options),
  52. Init(init::Options),
  53. Plugin(plugin::Cli),
  54. Signer(signer::Cli),
  55. }
  56. fn format_error<I: IntoApp>(err: clap::Error) -> clap::Error {
  57. let mut app = I::command();
  58. err.format(&mut app)
  59. }
  60. /// Run the Tauri CLI with the passed arguments.
  61. ///
  62. /// The passed arguments should have the binary argument(s) stripped out before being passed.
  63. ///
  64. /// e.g.
  65. /// 1. `tauri-cli 1 2 3` -> `1 2 3`
  66. /// 2. `cargo tauri 1 2 3` -> `1 2 3`
  67. /// 3. `node tauri.js 1 2 3` -> `1 2 3`
  68. ///
  69. /// The passed `bin_name` parameter should be how you want the help messages to display the command.
  70. /// This defaults to `cargo-tauri`, but should be set to how the program was called, such as
  71. /// `cargo tauri`.
  72. pub fn run<I, A>(args: I, bin_name: Option<String>) -> Result<()>
  73. where
  74. I: IntoIterator<Item = A>,
  75. A: Into<OsString> + Clone,
  76. {
  77. let matches = match bin_name {
  78. Some(bin_name) => Cli::command().bin_name(bin_name),
  79. None => Cli::command(),
  80. }
  81. .get_matches_from(args);
  82. let res = Cli::from_arg_matches(&matches).map_err(format_error::<Cli>);
  83. let cli = match res {
  84. Ok(s) => s,
  85. Err(e) => e.exit(),
  86. };
  87. match cli.command {
  88. Commands::Build(options) => build::command(options)?,
  89. Commands::Dev(options) => dev::command(options)?,
  90. Commands::Info(options) => info::command(options)?,
  91. Commands::Init(options) => init::command(options)?,
  92. Commands::Plugin(cli) => plugin::command(cli)?,
  93. Commands::Signer(cli) => signer::command(cli)?,
  94. }
  95. Ok(())
  96. }