config_definition.rs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![allow(clippy::field_reassign_with_default)]
  5. #[cfg(target_os = "linux")]
  6. use heck::ToKebabCase;
  7. use schemars::JsonSchema;
  8. use serde::{Deserialize, Serialize};
  9. use serde_json::Value as JsonValue;
  10. use serde_with::skip_serializing_none;
  11. use url::Url;
  12. use std::{collections::HashMap, path::PathBuf};
  13. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  14. #[serde(untagged)]
  15. pub enum BundleTarget {
  16. All(Vec<String>),
  17. One(String),
  18. }
  19. impl BundleTarget {
  20. #[allow(dead_code)]
  21. pub fn to_vec(&self) -> Vec<String> {
  22. match self {
  23. Self::All(list) => list.clone(),
  24. Self::One(i) => vec![i.clone()],
  25. }
  26. }
  27. }
  28. #[skip_serializing_none]
  29. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  30. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  31. pub struct DebConfig {
  32. /// The list of deb dependencies your application relies on.
  33. pub depends: Option<Vec<String>>,
  34. /// Enable the boostrapper script.
  35. #[serde(default)]
  36. pub use_bootstrapper: bool,
  37. /// The files to include on the package.
  38. #[serde(default)]
  39. pub files: HashMap<PathBuf, PathBuf>,
  40. }
  41. #[skip_serializing_none]
  42. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  43. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  44. pub struct MacConfig {
  45. /// A list of strings indicating any macOS X frameworks that need to be bundled with the application.
  46. ///
  47. /// If a name is used, ".framework" must be omitted and it will look for standard install locations. You may also use a path to a specific framework.
  48. pub frameworks: Option<Vec<String>>,
  49. /// A version string indicating the minimum macOS X version that the bundled application supports.
  50. pub minimum_system_version: Option<String>,
  51. /// Allows your application to communicate with the outside world.
  52. /// It should be a lowercase, without port and protocol domain name.
  53. pub exception_domain: Option<String>,
  54. /// The path to the license file to add to the DMG bundle.
  55. pub license: Option<String>,
  56. /// Enable the boostrapper script.
  57. #[serde(default)]
  58. pub use_bootstrapper: bool,
  59. /// Identity to use for code signing.
  60. pub signing_identity: Option<String>,
  61. /// Path to the entitlements file.
  62. pub entitlements: Option<String>,
  63. }
  64. fn default_language() -> String {
  65. "en-US".into()
  66. }
  67. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  68. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  69. pub struct WixConfig {
  70. /// The installer language. See https://docs.microsoft.com/en-us/windows/win32/msi/localizing-the-error-and-actiontext-tables.
  71. #[serde(default = "default_language")]
  72. pub language: String,
  73. /// A custom .wxs template to use.
  74. pub template: Option<PathBuf>,
  75. /// A list of paths to .wxs files with WiX fragments to use.
  76. #[serde(default)]
  77. pub fragment_paths: Vec<PathBuf>,
  78. /// The ComponentGroup element ids you want to reference from the fragments.
  79. #[serde(default)]
  80. pub component_group_refs: Vec<String>,
  81. /// The Component element ids you want to reference from the fragments.
  82. #[serde(default)]
  83. pub component_refs: Vec<String>,
  84. /// The FeatureGroup element ids you want to reference from the fragments.
  85. #[serde(default)]
  86. pub feature_group_refs: Vec<String>,
  87. /// The Feature element ids you want to reference from the fragments.
  88. #[serde(default)]
  89. pub feature_refs: Vec<String>,
  90. /// The Merge element ids you want to reference from the fragments.
  91. #[serde(default)]
  92. pub merge_refs: Vec<String>,
  93. /// Disables the Webview2 runtime installation after app install.
  94. #[serde(default)]
  95. pub skip_webview_install: bool,
  96. /// The path to the license file to render on the installer.
  97. ///
  98. /// Must be an RTF file, so if a different extension is provided, we convert it to the RTF format.
  99. pub license: Option<PathBuf>,
  100. #[serde(default)]
  101. pub enable_elevated_update_task: bool,
  102. /// Path to a bitmap file to use as the installation user interface banner.
  103. /// This bitmap will appear at the top of all but the first page of the installer.
  104. ///
  105. /// The required dimensions are 493px × 58px.
  106. pub banner_path: Option<PathBuf>,
  107. /// Path to a bitmap file to use on the installation user interface dialogs.
  108. /// It is used on the welcome and completion dialogs.
  109. /// The required dimensions are 493px × 312px.
  110. pub dialog_image_path: Option<PathBuf>,
  111. }
  112. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  113. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  114. pub struct WindowsConfig {
  115. /// Specifies the file digest algorithm to use for creating file signatures.
  116. /// Required for code signing. SHA-256 is recommended.
  117. pub digest_algorithm: Option<String>,
  118. /// Specifies the SHA1 hash of the signing certificate.
  119. pub certificate_thumbprint: Option<String>,
  120. /// Server to use during timestamping.
  121. pub timestamp_url: Option<String>,
  122. /// Configuration for the MSI generated with WiX.
  123. pub wix: Option<WixConfig>,
  124. }
  125. #[skip_serializing_none]
  126. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  127. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  128. pub struct PackageConfig {
  129. /// Application name. Automatically converted to kebab-case on Linux.
  130. pub product_name: Option<String>,
  131. /// Application version.
  132. pub version: Option<String>,
  133. }
  134. impl PackageConfig {
  135. #[allow(dead_code)]
  136. pub fn binary_name(&self) -> Option<String> {
  137. #[cfg(target_os = "linux")]
  138. {
  139. self.product_name.as_ref().map(|n| n.to_kebab_case())
  140. }
  141. #[cfg(not(target_os = "linux"))]
  142. {
  143. self.product_name.clone()
  144. }
  145. }
  146. }
  147. #[skip_serializing_none]
  148. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  149. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  150. pub struct BundleConfig {
  151. /// Whether we should build your app with tauri-bundler or plain `cargo build`
  152. #[serde(default)]
  153. pub active: bool,
  154. /// The bundle targets, currently supports ["deb", "app", "msi", "appimage", "dmg"] or "all"
  155. pub targets: Option<BundleTarget>,
  156. /// The app's identifier
  157. pub identifier: Option<String>,
  158. /// The app's icons
  159. pub icon: Option<Vec<String>>,
  160. /// App resources to bundle.
  161. /// Each resource is a path to a file or directory.
  162. /// Glob patterns are supported.
  163. pub resources: Option<Vec<String>>,
  164. /// A copyright string associated with your application.
  165. pub copyright: Option<String>,
  166. /// The application kind.
  167. pub category: Option<String>,
  168. /// A short description of your application.
  169. pub short_description: Option<String>,
  170. /// A longer, multi-line description of the application.
  171. pub long_description: Option<String>,
  172. /// Configuration for the Debian bundle.
  173. #[serde(default)]
  174. pub deb: DebConfig,
  175. /// Configuration for the macOS bundles.
  176. #[serde(rename = "macOS", default)]
  177. pub macos: MacConfig,
  178. /// A list of—either absolute or relative—paths to binaries to embed with your application.
  179. ///
  180. /// Note that Tauri will look for system-specific binaries following the pattern "binary-name{-target-triple}{.system-extension}".
  181. ///
  182. /// E.g. for the external binary "my-binary", Tauri looks for:
  183. ///
  184. /// - "my-binary-x86_64-pc-windows-msvc.exe" for Windows
  185. /// - "my-binary-x86_64-apple-darwin" for macOS
  186. /// - "my-binary-x86_64-unknown-linux-gnu" for Linux
  187. ///
  188. /// so don't forget to provide binaries for all targeted platforms.
  189. pub external_bin: Option<Vec<String>>,
  190. /// Configuration for the Windows bundle.
  191. #[serde(default)]
  192. pub windows: WindowsConfig,
  193. }
  194. /// A CLI argument definition
  195. #[skip_serializing_none]
  196. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  197. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  198. pub struct CliArg {
  199. /// The short version of the argument, without the preceding -.
  200. ///
  201. /// NOTE: Any leading - characters will be stripped, and only the first non - character will be used as the short version.
  202. pub short: Option<char>,
  203. /// The unique argument name
  204. pub name: String,
  205. /// The argument description which will be shown on the help information.
  206. /// Typically, this is a short (one line) description of the arg.
  207. pub description: Option<String>,
  208. /// The argument long description which will be shown on the help information.
  209. /// Typically this a more detailed (multi-line) message that describes the argument.
  210. pub long_description: Option<String>,
  211. /// Specifies that the argument takes a value at run time.
  212. ///
  213. /// NOTE: values for arguments may be specified in any of the following methods
  214. /// - Using a space such as -o value or --option value
  215. /// - Using an equals and no space such as -o=value or --option=value
  216. /// - Use a short and no space such as -ovalue
  217. pub takes_value: Option<bool>,
  218. /// Specifies that the argument may appear more than once.
  219. ///
  220. /// - For flags, this results in the number of occurrences of the flag being recorded.
  221. /// For example -ddd or -d -d -d would count as three occurrences.
  222. /// - For options there is a distinct difference in multiple occurrences vs multiple values.
  223. /// For example, --opt val1 val2 is one occurrence, but two values. Whereas --opt val1 --opt val2 is two occurrences.
  224. pub multiple: Option<bool>,
  225. /// specifies that the argument may appear more than once.
  226. pub multiple_occurrences: Option<bool>,
  227. ///
  228. pub number_of_values: Option<u64>,
  229. /// Specifies a list of possible values for this argument.
  230. /// At runtime, the CLI verifies that only one of the specified values was used, or fails with an error message.
  231. pub possible_values: Option<Vec<String>>,
  232. /// Specifies the minimum number of values for this argument.
  233. /// For example, if you had a -f <file> argument where you wanted at least 2 'files',
  234. /// you would set `minValues: 2`, and this argument would be satisfied if the user provided, 2 or more values.
  235. pub min_values: Option<u64>,
  236. /// Specifies the maximum number of values are for this argument.
  237. /// For example, if you had a -f <file> argument where you wanted up to 3 'files',
  238. /// you would set .max_values(3), and this argument would be satisfied if the user provided, 1, 2, or 3 values.
  239. pub max_values: Option<u64>,
  240. /// Sets whether or not the argument is required by default.
  241. ///
  242. /// - Required by default means it is required, when no other conflicting rules have been evaluated
  243. /// - Conflicting rules take precedence over being required.
  244. pub required: Option<bool>,
  245. /// Sets an arg that override this arg's required setting
  246. /// i.e. this arg will be required unless this other argument is present.
  247. pub required_unless_present: Option<String>,
  248. /// Sets args that override this arg's required setting
  249. /// i.e. this arg will be required unless all these other arguments are present.
  250. pub required_unless_present_all: Option<Vec<String>>,
  251. /// Sets args that override this arg's required setting
  252. /// i.e. this arg will be required unless at least one of these other arguments are present.
  253. pub required_unless_present_any: Option<Vec<String>>,
  254. /// Sets a conflicting argument by name
  255. /// i.e. when using this argument, the following argument can't be present and vice versa.
  256. pub conflicts_with: Option<String>,
  257. /// The same as conflictsWith but allows specifying multiple two-way conflicts per argument.
  258. pub conflicts_with_all: Option<Vec<String>>,
  259. /// Tets an argument by name that is required when this one is present
  260. /// i.e. when using this argument, the following argument must be present.
  261. pub requires: Option<String>,
  262. /// Sts multiple arguments by names that are required when this one is present
  263. /// i.e. when using this argument, the following arguments must be present.
  264. pub requires_all: Option<Vec<String>>,
  265. /// Allows a conditional requirement with the signature [arg, value]
  266. /// the requirement will only become valid if `arg`'s value equals `${value}`.
  267. pub requires_if: Option<Vec<String>>,
  268. /// Allows specifying that an argument is required conditionally with the signature [arg, value]
  269. /// the requirement will only become valid if the `arg`'s value equals `${value}`.
  270. pub required_if_eq: Option<Vec<String>>,
  271. /// Requires that options use the --option=val syntax
  272. /// i.e. an equals between the option and associated value.
  273. pub require_equals: Option<bool>,
  274. /// The positional argument index, starting at 1.
  275. ///
  276. /// The index refers to position according to other positional argument.
  277. /// It does not define position in the argument list as a whole. When utilized with multiple=true,
  278. /// only the last positional argument may be defined as multiple (i.e. the one with the highest index).
  279. pub index: Option<u64>,
  280. }
  281. /// describes a CLI configuration
  282. #[skip_serializing_none]
  283. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  284. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  285. pub struct CliConfig {
  286. /// command description which will be shown on the help information
  287. description: Option<String>,
  288. /// command long description which will be shown on the help information
  289. long_description: Option<String>,
  290. /// adds additional help information to be displayed in addition to auto-generated help
  291. /// this information is displayed before the auto-generated help information.
  292. /// this is often used for header information
  293. before_help: Option<String>,
  294. /// adds additional help information to be displayed in addition to auto-generated help
  295. /// this information is displayed after the auto-generated help information
  296. /// this is often used to describe how to use the arguments, or caveats to be noted.
  297. after_help: Option<String>,
  298. /// list of args for the command
  299. args: Option<Vec<CliArg>>,
  300. /// list of subcommands of this command.
  301. ///
  302. /// subcommands are effectively sub-apps, because they can contain their own arguments, subcommands, usage, etc.
  303. /// they also function just like the app command, in that they get their own auto generated help and usage
  304. subcommands: Option<HashMap<String, CliConfig>>,
  305. }
  306. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  307. #[serde(untagged)]
  308. pub enum Port {
  309. /// Port with a numeric value.
  310. Value(u16),
  311. /// Random port.
  312. Random,
  313. }
  314. /// The window configuration object.
  315. #[skip_serializing_none]
  316. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  317. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  318. pub struct WindowConfig {
  319. /// The window identifier.
  320. pub label: Option<String>,
  321. /// The window webview URL.
  322. pub url: Option<String>,
  323. /// Whether the file drop is enabled or not on the webview. By default it is enabled.
  324. ///
  325. /// Disabling it is required to use drag and drop on the frontend on Windows.
  326. #[serde(default = "default_file_drop_enabled")]
  327. pub file_drop_enabled: bool,
  328. /// Whether or not the window starts centered or not.
  329. #[serde(default)]
  330. pub center: bool,
  331. /// The horizontal position of the window's top left corner
  332. pub x: Option<f64>,
  333. /// The vertical position of the window's top left corner
  334. pub y: Option<f64>,
  335. /// The window width.
  336. pub width: Option<f64>,
  337. /// The window height.
  338. pub height: Option<f64>,
  339. /// The min window width.
  340. pub min_width: Option<f64>,
  341. /// The min window height.
  342. pub min_height: Option<f64>,
  343. /// The max window width.
  344. pub max_width: Option<f64>,
  345. /// The max window height.
  346. pub max_height: Option<f64>,
  347. /// Whether the window is resizable or not.
  348. #[serde(default)]
  349. pub resizable: bool,
  350. /// The window title.
  351. pub title: Option<String>,
  352. /// Whether the window starts as fullscreen or not.
  353. #[serde(default)]
  354. pub fullscreen: bool,
  355. /// Whether the window will be initially hidden or focused.
  356. #[serde(default = "default_focus")]
  357. pub focus: bool,
  358. /// Whether the window is transparent or not.
  359. #[serde(default)]
  360. pub transparent: bool,
  361. /// Whether the window is maximized or not.
  362. #[serde(default)]
  363. pub maximized: bool,
  364. /// Whether the window is visible or not.
  365. #[serde(default = "default_visible")]
  366. pub visible: bool,
  367. /// Whether the window should have borders and bars.
  368. #[serde(default = "default_decorations")]
  369. pub decorations: bool,
  370. /// Whether the window should always be on top of other windows.
  371. #[serde(default)]
  372. pub always_on_top: bool,
  373. /// Whether or not the window icon should be added to the taskbar.
  374. #[serde(default)]
  375. pub skip_taskbar: bool,
  376. }
  377. fn default_focus() -> bool {
  378. true
  379. }
  380. fn default_visible() -> bool {
  381. true
  382. }
  383. fn default_decorations() -> bool {
  384. true
  385. }
  386. fn default_file_drop_enabled() -> bool {
  387. true
  388. }
  389. #[skip_serializing_none]
  390. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  391. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  392. pub struct SecurityConfig {
  393. /// The Content Security Policy that will be injected on all HTML files.
  394. ///
  395. /// This is a really important part of the configuration since it helps you ensure your WebView is secured.
  396. /// See https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP.
  397. pub csp: Option<String>,
  398. }
  399. pub trait Allowlist {
  400. fn to_features(&self) -> Vec<&str>;
  401. }
  402. macro_rules! check_feature {
  403. ($self:ident, $features:ident, $flag:ident, $feature_name: expr) => {
  404. if $self.$flag {
  405. $features.push($feature_name)
  406. }
  407. };
  408. }
  409. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  410. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  411. pub struct FsAllowlistConfig {
  412. /// Use this flag to enable all file system API features.
  413. #[serde(default)]
  414. pub all: bool,
  415. /// Read text file from local filesystem.
  416. #[serde(default)]
  417. pub read_text_file: bool,
  418. /// Read binary file from local filesystem.
  419. #[serde(default)]
  420. pub read_binary_file: bool,
  421. /// Write text file to local filesystem.
  422. #[serde(default)]
  423. pub write_file: bool,
  424. /// Write binary file to local filesystem.
  425. #[serde(default)]
  426. pub write_binary_file: bool,
  427. /// Read directory from local filesystem.
  428. #[serde(default)]
  429. pub read_dir: bool,
  430. /// Copy file from local filesystem.
  431. #[serde(default)]
  432. pub copy_file: bool,
  433. /// Create directory from local filesystem.
  434. #[serde(default)]
  435. pub create_dir: bool,
  436. /// Remove directory from local filesystem.
  437. #[serde(default)]
  438. pub remove_dir: bool,
  439. /// Remove file from local filesystem.
  440. #[serde(default)]
  441. pub remove_file: bool,
  442. /// Rename file from local filesystem.
  443. #[serde(default)]
  444. pub rename_file: bool,
  445. }
  446. impl Allowlist for FsAllowlistConfig {
  447. fn to_features(&self) -> Vec<&str> {
  448. if self.all {
  449. vec!["fs-all"]
  450. } else {
  451. let mut features = Vec::new();
  452. check_feature!(self, features, read_text_file, "fs-read-text-file");
  453. check_feature!(self, features, read_binary_file, "fs-read-binary-file");
  454. check_feature!(self, features, write_file, "fs-write-file");
  455. check_feature!(self, features, write_binary_file, "fs-write-binary-file");
  456. check_feature!(self, features, read_dir, "fs-read-dir");
  457. check_feature!(self, features, copy_file, "fs-copy-file");
  458. check_feature!(self, features, create_dir, "fs-create-dir");
  459. check_feature!(self, features, remove_dir, "fs-remove-dir");
  460. check_feature!(self, features, remove_file, "fs-remove-file");
  461. check_feature!(self, features, rename_file, "fs-rename-file");
  462. features
  463. }
  464. }
  465. }
  466. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  467. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  468. pub struct WindowAllowlistConfig {
  469. /// Use this flag to enable all window API features.
  470. #[serde(default)]
  471. pub all: bool,
  472. /// Allows dynamic window creation.
  473. #[serde(default)]
  474. pub create: bool,
  475. }
  476. impl Allowlist for WindowAllowlistConfig {
  477. fn to_features(&self) -> Vec<&str> {
  478. if self.all {
  479. vec!["window-all"]
  480. } else {
  481. let mut features = Vec::new();
  482. check_feature!(self, features, create, "window-create");
  483. features
  484. }
  485. }
  486. }
  487. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  488. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  489. pub struct ShellAllowlistConfig {
  490. /// Use this flag to enable all shell API features.
  491. #[serde(default)]
  492. pub all: bool,
  493. /// Enable binary execution.
  494. #[serde(default)]
  495. pub execute: bool,
  496. /// Open URL with the user's default application.
  497. #[serde(default)]
  498. pub open: bool,
  499. }
  500. impl Allowlist for ShellAllowlistConfig {
  501. fn to_features(&self) -> Vec<&str> {
  502. if self.all {
  503. vec!["shell-all"]
  504. } else {
  505. let mut features = Vec::new();
  506. check_feature!(self, features, execute, "shell-execute");
  507. check_feature!(self, features, open, "shell-open");
  508. features
  509. }
  510. }
  511. }
  512. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  513. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  514. pub struct DialogAllowlistConfig {
  515. /// Use this flag to enable all dialog API features.
  516. #[serde(default)]
  517. pub all: bool,
  518. /// Open dialog window to pick files.
  519. #[serde(default)]
  520. pub open: bool,
  521. /// Open dialog window to pick where to save files.
  522. #[serde(default)]
  523. pub save: bool,
  524. }
  525. impl Allowlist for DialogAllowlistConfig {
  526. fn to_features(&self) -> Vec<&str> {
  527. if self.all {
  528. vec!["dialog-all"]
  529. } else {
  530. let mut features = Vec::new();
  531. check_feature!(self, features, open, "dialog-open");
  532. check_feature!(self, features, save, "dialog-save");
  533. features
  534. }
  535. }
  536. }
  537. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  538. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  539. pub struct HttpAllowlistConfig {
  540. /// Use this flag to enable all HTTP API features.
  541. #[serde(default)]
  542. pub all: bool,
  543. /// Allows making HTTP requests.
  544. #[serde(default)]
  545. pub request: bool,
  546. }
  547. impl Allowlist for HttpAllowlistConfig {
  548. fn to_features(&self) -> Vec<&str> {
  549. if self.all {
  550. vec!["http-all"]
  551. } else {
  552. let mut features = Vec::new();
  553. check_feature!(self, features, request, "http-request");
  554. features
  555. }
  556. }
  557. }
  558. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  559. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  560. pub struct NotificationAllowlistConfig {
  561. /// Use this flag to enable all notification API features.
  562. #[serde(default)]
  563. pub all: bool,
  564. }
  565. impl Allowlist for NotificationAllowlistConfig {
  566. fn to_features(&self) -> Vec<&str> {
  567. if self.all {
  568. vec!["notification-all"]
  569. } else {
  570. vec![]
  571. }
  572. }
  573. }
  574. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  575. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  576. pub struct GlobalShortcutAllowlistConfig {
  577. /// Use this flag to enable all global shortcut API features.
  578. #[serde(default)]
  579. pub all: bool,
  580. }
  581. impl Allowlist for GlobalShortcutAllowlistConfig {
  582. fn to_features(&self) -> Vec<&str> {
  583. if self.all {
  584. vec!["global-shortcut-all"]
  585. } else {
  586. vec![]
  587. }
  588. }
  589. }
  590. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  591. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  592. pub struct OsAllowlistConfig {
  593. /// Use this flag to enable all OS API features.
  594. #[serde(default)]
  595. pub all: bool,
  596. }
  597. impl Allowlist for OsAllowlistConfig {
  598. fn to_features(&self) -> Vec<&str> {
  599. if self.all {
  600. vec!["os-all"]
  601. } else {
  602. vec![]
  603. }
  604. }
  605. }
  606. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  607. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  608. pub struct PathAllowlistConfig {
  609. /// Use this flag to enable all path API features.
  610. #[serde(default)]
  611. pub all: bool,
  612. }
  613. impl Allowlist for PathAllowlistConfig {
  614. fn to_features(&self) -> Vec<&str> {
  615. if self.all {
  616. vec!["path-all"]
  617. } else {
  618. vec![]
  619. }
  620. }
  621. }
  622. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  623. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  624. pub struct AllowlistConfig {
  625. /// Use this flag to enable all API features.
  626. #[serde(default)]
  627. pub all: bool,
  628. /// File system API allowlist.
  629. #[serde(default)]
  630. pub fs: FsAllowlistConfig,
  631. /// Window API allowlist.
  632. #[serde(default)]
  633. pub window: WindowAllowlistConfig,
  634. /// Shell API allowlist.
  635. #[serde(default)]
  636. pub shell: ShellAllowlistConfig,
  637. /// Dialog API allowlist.
  638. #[serde(default)]
  639. pub dialog: DialogAllowlistConfig,
  640. /// HTTP API allowlist.
  641. #[serde(default)]
  642. pub http: HttpAllowlistConfig,
  643. /// Notification API allowlist.
  644. #[serde(default)]
  645. pub notification: NotificationAllowlistConfig,
  646. /// Global shortcut API allowlist.
  647. #[serde(default)]
  648. pub global_shortcut: GlobalShortcutAllowlistConfig,
  649. /// OS allowlist.
  650. #[serde(default)]
  651. pub os: OsAllowlistConfig,
  652. /// Path API allowlist.
  653. #[serde(default)]
  654. pub path: PathAllowlistConfig,
  655. }
  656. impl Allowlist for AllowlistConfig {
  657. fn to_features(&self) -> Vec<&str> {
  658. if self.all {
  659. vec!["api-all"]
  660. } else {
  661. let mut features = Vec::new();
  662. features.extend(self.fs.to_features());
  663. features.extend(self.window.to_features());
  664. features.extend(self.shell.to_features());
  665. features.extend(self.dialog.to_features());
  666. features.extend(self.http.to_features());
  667. features.extend(self.notification.to_features());
  668. features.extend(self.global_shortcut.to_features());
  669. features.extend(self.os.to_features());
  670. features.extend(self.path.to_features());
  671. features
  672. }
  673. }
  674. }
  675. /// The Tauri configuration object.
  676. #[skip_serializing_none]
  677. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  678. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  679. pub struct TauriConfig {
  680. /// The windows configuration.
  681. #[serde(default)]
  682. pub windows: Vec<WindowConfig>,
  683. /// The CLI configuration.
  684. pub cli: Option<CliConfig>,
  685. /// The bundler configuration.
  686. #[serde(default)]
  687. pub bundle: BundleConfig,
  688. /// The allowlist configuration.
  689. #[serde(default)]
  690. allowlist: AllowlistConfig,
  691. /// Security configuration.
  692. pub security: Option<SecurityConfig>,
  693. /// The updater configuration.
  694. #[serde(default = "default_updater")]
  695. pub updater: UpdaterConfig,
  696. /// Configuration for app system tray.
  697. pub system_tray: Option<SystemTrayConfig>,
  698. }
  699. impl TauriConfig {
  700. #[allow(dead_code)]
  701. pub fn features(&self) -> Vec<&str> {
  702. self.allowlist.to_features()
  703. }
  704. }
  705. #[skip_serializing_none]
  706. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  707. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  708. pub struct UpdaterConfig {
  709. /// Whether the updater is active or not.
  710. #[serde(default)]
  711. pub active: bool,
  712. /// Display built-in dialog or use event system if disabled.
  713. #[serde(default = "default_dialog")]
  714. pub dialog: Option<bool>,
  715. /// The updater endpoints.
  716. pub endpoints: Option<Vec<String>>,
  717. /// Optional pubkey.
  718. pub pubkey: Option<String>,
  719. }
  720. #[skip_serializing_none]
  721. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  722. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  723. pub struct SystemTrayConfig {
  724. /// Path to the icon to use on the system tray.
  725. ///
  726. /// It is forced to be a `.png` file on Linux and macOS, and a `.ico` file on Windows.
  727. pub icon_path: PathBuf,
  728. /// A Boolean value that determines whether the image represents a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) image on macOS.
  729. #[serde(default)]
  730. pub icon_as_template: bool,
  731. }
  732. // We enable the unnecessary_wraps because we need
  733. // to use an Option for dialog otherwise the CLI schema will mark
  734. // the dialog as a required field which is not as we default it to true.
  735. #[allow(clippy::unnecessary_wraps)]
  736. fn default_dialog() -> Option<bool> {
  737. Some(true)
  738. }
  739. /// The window webview URL options.
  740. #[derive(PartialEq, Debug, Clone, Deserialize, Serialize, JsonSchema)]
  741. #[serde(untagged)]
  742. #[non_exhaustive]
  743. pub enum WindowUrl {
  744. /// An external URL.
  745. External(Url),
  746. /// An app URL.
  747. App(PathBuf),
  748. }
  749. impl Default for WindowUrl {
  750. fn default() -> Self {
  751. Self::App("index.html".into())
  752. }
  753. }
  754. impl std::fmt::Display for WindowUrl {
  755. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  756. match self {
  757. Self::External(url) => write!(f, "{}", url),
  758. Self::App(path) => write!(f, "{}", path.display()),
  759. }
  760. }
  761. }
  762. /// The `dev_path` and `dist_dir` options.
  763. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  764. #[serde(untagged, deny_unknown_fields)]
  765. pub enum AppUrl {
  766. /// The app's external URL, or the path to the directory containing the app assets.
  767. Url(WindowUrl),
  768. /// An array of files to embed on the app.
  769. Files(Vec<PathBuf>),
  770. }
  771. impl std::fmt::Display for AppUrl {
  772. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  773. match self {
  774. Self::Url(url) => write!(f, "{}", url),
  775. Self::Files(files) => write!(f, "{}", serde_json::to_string(files).unwrap()),
  776. }
  777. }
  778. }
  779. /// The Build configuration object.
  780. #[skip_serializing_none]
  781. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  782. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  783. pub struct BuildConfig {
  784. /// The binary used to build and run the application.
  785. pub runner: Option<String>,
  786. /// The path or URL to use on development.
  787. #[serde(default = "default_dev_path")]
  788. pub dev_path: AppUrl,
  789. /// The path to the app's dist dir. This path must contain your index.html file.
  790. #[serde(default = "default_dist_dir")]
  791. pub dist_dir: AppUrl,
  792. /// A shell command to run before `tauri dev` kicks in.
  793. ///
  794. /// The PLATFORM, ARCH, FAMILY, PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.
  795. pub before_dev_command: Option<String>,
  796. /// A shell command to run before `tauri build` kicks in.
  797. ///
  798. /// The PLATFORM, ARCH, FAMILY, PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.
  799. pub before_build_command: Option<String>,
  800. /// Features passed to `cargo` commands.
  801. pub features: Option<Vec<String>>,
  802. /// Whether we should inject the Tauri API on `window.__TAURI__` or not.
  803. #[serde(default)]
  804. pub with_global_tauri: bool,
  805. }
  806. fn default_dev_path() -> AppUrl {
  807. AppUrl::Url(WindowUrl::External(
  808. Url::parse("http://localhost:8080").unwrap(),
  809. ))
  810. }
  811. fn default_dist_dir() -> AppUrl {
  812. AppUrl::Url(WindowUrl::App(PathBuf::from("../dist")))
  813. }
  814. /// The tauri.conf.json mapper.
  815. #[skip_serializing_none]
  816. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  817. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  818. pub struct Config {
  819. /// Package settings.
  820. #[serde(default)]
  821. pub package: PackageConfig,
  822. /// The Tauri configuration.
  823. #[serde(default)]
  824. pub tauri: TauriConfig,
  825. /// The build configuration.
  826. #[serde(default = "default_build")]
  827. pub build: BuildConfig,
  828. /// The plugins config.
  829. #[serde(default)]
  830. pub plugins: PluginConfig,
  831. }
  832. /// The plugin configs holds a HashMap mapping a plugin name to its configuration object.
  833. #[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize, JsonSchema)]
  834. pub struct PluginConfig(pub HashMap<String, JsonValue>);
  835. fn default_build() -> BuildConfig {
  836. BuildConfig {
  837. runner: None,
  838. dev_path: default_dev_path(),
  839. dist_dir: default_dist_dir(),
  840. before_dev_command: None,
  841. before_build_command: None,
  842. features: None,
  843. with_global_tauri: false,
  844. }
  845. }
  846. fn default_updater() -> UpdaterConfig {
  847. UpdaterConfig {
  848. active: false,
  849. dialog: Some(true),
  850. endpoints: None,
  851. pubkey: None,
  852. }
  853. }