config_definition.rs 32 KB

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