Skip to content

Commit

Permalink
refactor(cli): use windows-sys instead of winapi (#9603)
Browse files Browse the repository at this point in the history
* refactor(cli): use `windows-sys` instead of `winapi`

* Update cli-windows-sys.md
  • Loading branch information
amrbashir authored Apr 29, 2024
1 parent 477bb8c commit 3f08054
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 64 deletions.
5 changes: 5 additions & 0 deletions .changes/cli-windows-sys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-cli": "patch:bug"
---

Use `windows-sys` crate instead of `winapi` which fixes installing the published cli from crates.io using `cargo install tauri-cli --version "^2.0.0-beta"`.
74 changes: 40 additions & 34 deletions tooling/cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions tooling/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ resvg = "0.40.0"
dunce = "1"
glob = "0.3"

[target."cfg(windows)".dependencies]
winapi = { version = "0.3", features = [ "handleapi", "processenv", "winbase", "wincon", "winnt" ] }
cc = "1"
[target."cfg(windows)".dependencies.windows-sys]
version = "0.52"
features = [
"Win32_Storage_FileSystem",
"Win32_System_IO",
"Win32_System_Console"
]

[target."cfg(unix)".dependencies]
libc = "0.2"
Expand Down
44 changes: 25 additions & 19 deletions tooling/cli/src/helpers/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ mod sys {
use std::mem;
use std::os::windows::io::AsRawHandle;

use winapi::shared::minwindef::DWORD;
use winapi::shared::winerror::{ERROR_INVALID_FUNCTION, ERROR_LOCK_VIOLATION};
use winapi::um::fileapi::{LockFileEx, UnlockFile};
use winapi::um::minwinbase::{LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY};
use windows_sys::Win32::Foundation::{ERROR_INVALID_FUNCTION, ERROR_LOCK_VIOLATION, HANDLE};
use windows_sys::Win32::Storage::FileSystem::{
LockFileEx, UnlockFile, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, LOCK_FILE_FLAGS,
};

pub(super) fn lock_shared(file: &File) -> Result<()> {
lock_file(file, 0)
Expand Down Expand Up @@ -339,25 +339,31 @@ mod sys {
}

pub(super) fn unlock(file: &File) -> Result<()> {
unsafe {
let ret = UnlockFile(file.as_raw_handle(), 0, 0, !0, !0);
if ret == 0 {
Err(Error::last_os_error())
} else {
Ok(())
}
let ret = unsafe { UnlockFile(file.as_raw_handle() as HANDLE, 0, 0, !0, !0) };
if ret == 0 {
Err(Error::last_os_error())
} else {
Ok(())
}
}

fn lock_file(file: &File, flags: DWORD) -> Result<()> {
unsafe {
fn lock_file(file: &File, flags: LOCK_FILE_FLAGS) -> Result<()> {
let ret = unsafe {
let mut overlapped = mem::zeroed();
let ret = LockFileEx(file.as_raw_handle(), flags, 0, !0, !0, &mut overlapped);
if ret == 0 {
Err(Error::last_os_error())
} else {
Ok(())
}
LockFileEx(
file.as_raw_handle() as HANDLE,
flags,
0,
!0,
!0,
&mut overlapped,
)
};

if ret == 0 {
Err(Error::last_os_error())
} else {
Ok(())
}
}
}
18 changes: 10 additions & 8 deletions tooling/cli/src/interface/rust/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,14 @@ mod terminal {
#[cfg(windows)]
mod terminal {
use std::{cmp, mem, ptr};
use winapi::um::fileapi::*;
use winapi::um::handleapi::*;
use winapi::um::processenv::*;
use winapi::um::winbase::*;
use winapi::um::wincon::*;
use winapi::um::winnt::*;

use windows_sys::Win32::{
Foundation::{CloseHandle, GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE},
Storage::FileSystem::{CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING},
System::Console::{
GetConsoleScreenBufferInfo, GetStdHandle, CONSOLE_SCREEN_BUFFER_INFO, STD_ERROR_HANDLE,
},
};

pub fn stderr_width() -> Option<usize> {
unsafe {
Expand All @@ -465,13 +467,13 @@ mod terminal {
// INVALID_HANDLE_VALUE. Use an alternate method which works
// in that case as well.
let h = CreateFileA(
"CONOUT$\0".as_ptr() as *const CHAR,
"CONOUT$\0".as_ptr(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
ptr::null_mut(),
OPEN_EXISTING,
0,
ptr::null_mut(),
0,
);
if h == INVALID_HANDLE_VALUE {
return None;
Expand Down

0 comments on commit 3f08054

Please sign in to comment.