tauri_build/
lib.rs

1// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5#![cfg_attr(doc_cfg, feature(doc_cfg))]
6
7pub use anyhow::Result;
8
9use std::path::{Path, PathBuf};
10
11#[cfg(feature = "codegen")]
12mod codegen;
13
14#[cfg(feature = "codegen")]
15#[cfg_attr(doc_cfg, doc(cfg(feature = "codegen")))]
16pub use codegen::context::CodegenContext;
17
18/// Attributes used on Windows.
19#[allow(dead_code)]
20#[derive(Debug)]
21pub struct WindowsAttributes {
22  window_icon_path: PathBuf,
23}
24
25impl Default for WindowsAttributes {
26  fn default() -> Self {
27    Self {
28      window_icon_path: PathBuf::from("icons/icon.ico"),
29    }
30  }
31}
32
33impl WindowsAttributes {
34  /// Creates the default attribute set.
35  pub fn new() -> Self {
36    Self::default()
37  }
38
39  /// Sets the icon to use on the window. Currently only used on Windows.
40  /// It must be in `ico` format. Defaults to `icons/icon.ico`.
41  pub fn window_icon_path<P: AsRef<Path>>(mut self, window_icon_path: P) -> Self {
42    self.window_icon_path = window_icon_path.as_ref().into();
43    self
44  }
45}
46
47/// The attributes used on the build.
48#[derive(Debug, Default)]
49pub struct Attributes {
50  #[allow(dead_code)]
51  windows_attributes: WindowsAttributes,
52}
53
54impl Attributes {
55  /// Creates the default attribute set.
56  pub fn new() -> Self {
57    Self::default()
58  }
59
60  /// Sets the icon to use on the window. Currently only used on Windows.
61  pub fn windows_attributes(mut self, windows_attributes: WindowsAttributes) -> Self {
62    self.windows_attributes = windows_attributes;
63    self
64  }
65}
66
67/// Run all build time helpers for your Tauri Application.
68///
69/// The current helpers include the following:
70/// * Generates a Windows Resource file when targeting Windows.
71///
72/// # Platforms
73///
74/// [`build()`] should be called inside of `build.rs` regardless of the platform:
75/// * New helpers may target more platforms in the future.
76/// * Platform specific code is handled by the helpers automatically.
77/// * A build script is required in order to activate some cargo environmental variables that are
78///   used when generating code and embedding assets - so [`build()`] may as well be called.
79///
80/// In short, this is saying don't put the call to [`build()`] behind a `#[cfg(windows)]`.
81///
82/// # Panics
83///
84/// If any of the build time helpers fail, they will [`std::panic!`] with the related error message.
85/// This is typically desirable when running inside a build script; see [`try_build`] for no panics.
86pub fn build() {
87  if let Err(error) = try_build(Attributes::default()) {
88    panic!("error found during tauri-build: {}", error);
89  }
90}
91
92/// Non-panicking [`build()`].
93#[allow(unused_variables)]
94pub fn try_build(attributes: Attributes) -> Result<()> {
95  #[cfg(windows)]
96  {
97    use anyhow::{anyhow, Context};
98    use std::fs::read_to_string;
99    use tauri_utils::config::Config;
100    use winres::WindowsResource;
101
102    let config: Config = serde_json::from_str(
103      &read_to_string("tauri.conf.json").expect("failed to read tauri.conf.json"),
104    )
105    .expect("failed to parse tauri.conf.json");
106
107    let icon_path_string = attributes
108      .windows_attributes
109      .window_icon_path
110      .to_string_lossy()
111      .into_owned();
112
113    if attributes.windows_attributes.window_icon_path.exists() {
114      let mut res = WindowsResource::new();
115      if let Some(version) = &config.package.version {
116        res.set("FileVersion", version);
117        res.set("ProductVersion", version);
118      }
119      if let Some(product_name) = &config.package.product_name {
120        res.set("ProductName", product_name);
121        res.set("FileDescription", product_name);
122      }
123      res.set_icon_with_id(&icon_path_string, "32512");
124      res.compile().with_context(|| {
125        format!(
126          "failed to compile `{}` into a Windows Resource file during tauri-build",
127          icon_path_string
128        )
129      })?;
130    } else {
131      return Err(anyhow!(format!(
132        "`{}` not found; required for generating a Windows Resource file during tauri-build",
133        icon_path_string
134      )));
135    }
136  }
137
138  Ok(())
139}