Skip to content

Commit

Permalink
fix(core): clippy warnings, simplify embed_plist usage (#10844)
Browse files Browse the repository at this point in the history
* fix(core): clippy warnings

* fix test

* chore: simplify example
  • Loading branch information
lucasfernog authored Sep 2, 2024
1 parent 9c9644d commit 27d0183
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .changes/embed-plist-no-unit-val.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-codegen": patch:changes
"tauri": patch:changes
---

Changes how the Info.plist is embedded on macOS development to avoid a clippy warning.
9 changes: 5 additions & 4 deletions crates/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
};

#[cfg(target_os = "macos")]
let info_plist = if target == Target::MacOS && dev && !running_tests {
let maybe_embed_plist_block = if target == Target::MacOS && dev && !running_tests {
let info_plist_path = config_parent.join("Info.plist");
let mut info_plist = if info_plist_path.exists() {
plist::Value::from_file(&info_plist_path)
Expand Down Expand Up @@ -333,10 +333,10 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
tauri::embed_plist::embed_info_plist!(#plist);
})
} else {
quote!(())
quote!()
};
#[cfg(not(target_os = "macos"))]
let info_plist = quote!(());
let maybe_embed_plist_block = quote!();

let pattern = match &options.pattern {
PatternKind::Brownfield => quote!(#root::Pattern::Brownfield),
Expand Down Expand Up @@ -490,14 +490,15 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
};

let context = quote!({
#maybe_embed_plist_block

#[allow(unused_mut, clippy::let_and_return)]
let mut context = #root::Context::new(
#config,
::std::boxed::Box::new(#assets),
#default_window_icon,
#app_icon,
#package_info,
#info_plist,
#pattern,
#runtime_authority,
#plugin_global_api_script
Expand Down
34 changes: 22 additions & 12 deletions crates/tauri-utils/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

//! Platform helper functions.

use std::{
fmt::Display,
path::{Path, PathBuf, MAIN_SEPARATOR},
};
use std::{fmt::Display, path::PathBuf};

use serde::{Deserialize, Serialize};

use crate::{Env, PackageInfo};

mod starting_binary;

/// URI prefix of a Tauri asset.
///
/// This is referenced in the Tauri Android library,
/// which resolves these assets to a file descriptor.
#[cfg(target_os = "android")]
pub const ANDROID_ASSET_PROTOCOL_URI_PREFIX: &str = "asset://localhost/";

Expand Down Expand Up @@ -225,16 +226,16 @@ pub fn target_triple() -> crate::Result<String> {
Ok(format!("{arch}-{os}"))
}

#[cfg(not(test))]
fn is_cargo_output_directory(path: &Path) -> bool {
#[cfg(all(not(test), not(target_os = "android")))]
fn is_cargo_output_directory(path: &std::path::Path) -> bool {
path.join(".cargo-lock").exists()
}

#[cfg(test)]
const CARGO_OUTPUT_DIRECTORIES: &[&str] = &["debug", "release", "custom-profile"];

#[cfg(test)]
fn is_cargo_output_directory(path: &Path) -> bool {
fn is_cargo_output_directory(path: &std::path::Path) -> bool {
let last_component = path
.components()
.last()
Expand Down Expand Up @@ -265,21 +266,30 @@ fn is_cargo_output_directory(path: &Path) -> bool {
/// Android uses a special URI prefix that is resolved by the Tauri file system plugin `asset://localhost/`
pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> crate::Result<PathBuf> {
#[cfg(target_os = "android")]
return Ok(PathBuf::from(ANDROID_ASSET_PROTOCOL_URI_PREFIX));
let exe = current_exe()?;
resource_dir_from(exe, package_info, env)
return resource_dir_android(package_info, env);
#[cfg(not(target_os = "android"))]
{
let exe = current_exe()?;
resource_dir_from(exe, package_info, env)
}
}

#[cfg(target_os = "android")]
fn resource_dir_android(_package_info: &PackageInfo, _env: &Env) -> crate::Result<PathBuf> {
Ok(PathBuf::from(ANDROID_ASSET_PROTOCOL_URI_PREFIX))
}

#[cfg(not(target_os = "android"))]
#[allow(unused_variables)]
fn resource_dir_from<P: AsRef<Path>>(
fn resource_dir_from<P: AsRef<std::path::Path>>(
exe: P,
package_info: &PackageInfo,
env: &Env,
) -> crate::Result<PathBuf> {
let exe_dir = exe.as_ref().parent().expect("failed to get exe directory");
let curr_dir = exe_dir.display().to_string();

let parts: Vec<&str> = curr_dir.split(MAIN_SEPARATOR).collect();
let parts: Vec<&str> = curr_dir.split(std::path::MAIN_SEPARATOR).collect();
let len = parts.len();

// Check if running from the Cargo output directory, which means it's an executable in a development machine
Expand Down
3 changes: 0 additions & 3 deletions crates/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ pub struct Context<R: Runtime> {
#[cfg(all(desktop, feature = "tray-icon"))]
pub(crate) tray_icon: Option<image::Image<'static>>,
pub(crate) package_info: PackageInfo,
pub(crate) _info_plist: (),
pub(crate) pattern: Pattern,
pub(crate) runtime_authority: RuntimeAuthority,
pub(crate) plugin_global_api_scripts: Option<&'static [&'static str]>,
Expand Down Expand Up @@ -502,7 +501,6 @@ impl<R: Runtime> Context<R> {
default_window_icon: Option<image::Image<'static>>,
app_icon: Option<Vec<u8>>,
package_info: PackageInfo,
info_plist: (),
pattern: Pattern,
runtime_authority: RuntimeAuthority,
plugin_global_api_scripts: Option<&'static [&'static str]>,
Expand All @@ -517,7 +515,6 @@ impl<R: Runtime> Context<R> {
#[cfg(all(desktop, feature = "tray-icon"))]
tray_icon: None,
package_info,
_info_plist: info_plist,
pattern,
runtime_authority,
plugin_global_api_scripts,
Expand Down
1 change: 0 additions & 1 deletion crates/tauri/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ pub fn mock_context<R: Runtime, A: Assets<R>>(assets: A) -> crate::Context<R> {
description: "Tauri test",
crate_name: "test",
},
_info_plist: (),
pattern: Pattern::Brownfield,
runtime_authority: RuntimeAuthority::new(Default::default(), Resolved::default()),
plugin_global_api_scripts: None,
Expand Down
31 changes: 17 additions & 14 deletions crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,20 +505,6 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
self
}

/// Whether the window should be transparent. If this is true, writing colors
/// with alpha values different than `1.0` will produce a transparent window.
#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
#[cfg_attr(
docsrs,
doc(cfg(any(not(target_os = "macos"), feature = "macos-private-api")))
)]
#[must_use]
pub fn transparent(mut self, transparent: bool) -> Self {
self.window_builder = self.window_builder.transparent(transparent);
self.webview_builder = self.webview_builder.transparent(transparent);
self
}

/// Whether the window should have borders and bars.
#[must_use]
pub fn decorations(mut self, decorations: bool) -> Self {
Expand Down Expand Up @@ -859,6 +845,23 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
self
}

/// Whether the window should be transparent. If this is true, writing colors
/// with alpha values different than `1.0` will produce a transparent window.
#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
#[cfg_attr(
docsrs,
doc(cfg(any(not(target_os = "macos"), feature = "macos-private-api")))
)]
#[must_use]
pub fn transparent(mut self, transparent: bool) -> Self {
#[cfg(desktop)]
{
self.window_builder = self.window_builder.transparent(transparent);
}
self.webview_builder = self.webview_builder.transparent(transparent);
self
}

/// Whether page zooming by hotkeys is enabled
///
/// ## Platform-specific:
Expand Down
2 changes: 1 addition & 1 deletion examples/file-associations/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn handle_file_associations(app: AppHandle, files: Vec<PathBuf>) {

fn main() {
tauri::Builder::default()
.setup(|app| {
.setup(|#[allow(unused_variables)] app| {
#[cfg(any(windows, target_os = "linux"))]
{
let mut files = Vec::new();
Expand Down

0 comments on commit 27d0183

Please sign in to comment.