main.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. use clap::{crate_version, load_yaml, App, AppSettings, ArgMatches};
  6. use dialoguer::Input;
  7. mod build;
  8. mod dev;
  9. mod helpers;
  10. mod info;
  11. mod init;
  12. mod sign;
  13. pub use helpers::Logger;
  14. macro_rules! value_or_prompt {
  15. ($init_runner: ident, $setter_fn: ident, $value: ident, $ci: ident, $prompt_message: expr) => {{
  16. let mut init_runner = $init_runner;
  17. if let Some(value) = $value {
  18. init_runner = init_runner.$setter_fn(value);
  19. } else if !$ci {
  20. let input = Input::<String>::new()
  21. .with_prompt($prompt_message)
  22. .interact_text()?;
  23. init_runner = init_runner.$setter_fn(input);
  24. }
  25. init_runner
  26. }};
  27. }
  28. fn init_command(matches: &ArgMatches) -> Result<()> {
  29. let force = matches.is_present("force");
  30. let directory = matches.value_of("directory");
  31. let tauri_path = matches.value_of("tauri-path");
  32. let app_name = matches.value_of("app-name");
  33. let window_title = matches.value_of("window-title");
  34. let dist_dir = matches.value_of("dist-dir");
  35. let dev_path = matches.value_of("dev-path");
  36. let ci = matches.is_present("ci") || std::env::var("CI").is_ok();
  37. let mut init_runner = init::Init::new();
  38. if force {
  39. init_runner = init_runner.force();
  40. }
  41. if let Some(directory) = directory {
  42. init_runner = init_runner.directory(directory);
  43. }
  44. if let Some(tauri_path) = tauri_path {
  45. init_runner = init_runner.tauri_path(tauri_path);
  46. }
  47. init_runner = value_or_prompt!(
  48. init_runner,
  49. app_name,
  50. app_name,
  51. ci,
  52. "What is your app name?"
  53. );
  54. init_runner = value_or_prompt!(
  55. init_runner,
  56. window_title,
  57. window_title,
  58. ci,
  59. "What should the window title be?"
  60. );
  61. init_runner = value_or_prompt!(
  62. init_runner,
  63. dist_dir,
  64. dist_dir,
  65. ci,
  66. r#"Where are your web assets (HTML/CSS/JS) located, relative to the "<current dir>/src-tauri" folder that will be created?"#
  67. );
  68. init_runner = value_or_prompt!(
  69. init_runner,
  70. dev_path,
  71. dev_path,
  72. ci,
  73. "What is the url of your dev server?"
  74. );
  75. init_runner.run()
  76. }
  77. fn dev_command(matches: &ArgMatches) -> Result<()> {
  78. let exit_on_panic = matches.is_present("exit-on-panic");
  79. let config = matches.value_of("config");
  80. let args: Vec<String> = matches
  81. .values_of("args")
  82. .map(|a| a.into_iter().map(|v| v.to_string()).collect())
  83. .unwrap_or_default();
  84. let mut dev_runner = dev::Dev::new().exit_on_panic(exit_on_panic).args(args);
  85. if let Some(config) = config {
  86. dev_runner = dev_runner.config(config.to_string());
  87. }
  88. dev_runner.run()
  89. }
  90. fn build_command(matches: &ArgMatches) -> Result<()> {
  91. let debug = matches.is_present("debug");
  92. let verbose = matches.is_present("verbose");
  93. let targets = matches.values_of_lossy("target");
  94. let config = matches.value_of("config");
  95. let mut build_runner = build::Build::new();
  96. if debug {
  97. build_runner = build_runner.debug();
  98. }
  99. if verbose {
  100. build_runner = build_runner.verbose();
  101. }
  102. if let Some(targets) = targets {
  103. build_runner = build_runner.targets(targets);
  104. }
  105. if let Some(config) = config {
  106. build_runner = build_runner.config(config.to_string());
  107. }
  108. build_runner.run()
  109. }
  110. fn info_command() -> Result<()> {
  111. info::Info::new().run()
  112. }
  113. fn sign_command(matches: &ArgMatches) -> Result<()> {
  114. let private_key = matches.value_of("private-key");
  115. let private_key_path = matches.value_of("private-key-path");
  116. let file = matches.value_of("sign-file");
  117. let password = matches.value_of("password");
  118. let no_password = matches.is_present("no-password");
  119. let write_keys = matches.value_of("write-keys");
  120. let force = matches.is_present("force");
  121. // generate keypair
  122. if matches.is_present("generate") {
  123. let mut keygen_runner = sign::KeyGenerator::new();
  124. if no_password {
  125. keygen_runner = keygen_runner.empty_password();
  126. }
  127. if force {
  128. keygen_runner = keygen_runner.force();
  129. }
  130. if let Some(write_keys) = write_keys {
  131. keygen_runner = keygen_runner.output_path(write_keys);
  132. }
  133. if let Some(password) = password {
  134. keygen_runner = keygen_runner.password(password);
  135. }
  136. return keygen_runner.generate_keys();
  137. }
  138. // sign our binary / archive
  139. let mut sign_runner = sign::Signer::new();
  140. if let Some(private_key) = private_key {
  141. sign_runner = sign_runner.private_key(private_key);
  142. }
  143. if let Some(private_key_path) = private_key_path {
  144. sign_runner = sign_runner.private_key_path(private_key_path);
  145. }
  146. if let Some(file) = file {
  147. sign_runner = sign_runner.file_to_sign(file);
  148. }
  149. if let Some(password) = password {
  150. sign_runner = sign_runner.password(password);
  151. }
  152. if no_password {
  153. sign_runner = sign_runner.empty_password();
  154. }
  155. sign_runner.run()
  156. }
  157. fn main() -> Result<()> {
  158. let yaml = load_yaml!("cli.yml");
  159. let app = App::from(yaml)
  160. .version(crate_version!())
  161. .setting(AppSettings::ArgRequiredElseHelp)
  162. .setting(AppSettings::GlobalVersion)
  163. .setting(AppSettings::SubcommandRequired);
  164. let app_matches = app.get_matches();
  165. let matches = app_matches.subcommand_matches("tauri").unwrap();
  166. if let Some(matches) = matches.subcommand_matches("init") {
  167. init_command(&matches)?;
  168. } else if let Some(matches) = matches.subcommand_matches("dev") {
  169. dev_command(&matches)?;
  170. } else if let Some(matches) = matches.subcommand_matches("build") {
  171. build_command(&matches)?;
  172. } else if matches.subcommand_matches("info").is_some() {
  173. info_command()?;
  174. } else if let Some(matches) = matches.subcommand_matches("sign") {
  175. sign_command(&matches)?;
  176. }
  177. Ok(())
  178. }