Skip to content

Commit 3f08054

Browse files
authored
refactor(cli): use windows-sys instead of winapi (#9603)
* refactor(cli): use `windows-sys` instead of `winapi` * Update cli-windows-sys.md
1 parent 477bb8c commit 3f08054

File tree

5 files changed

+87
-64
lines changed

5 files changed

+87
-64
lines changed

.changes/cli-windows-sys.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-cli": "patch:bug"
3+
---
4+
5+
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"`.

tooling/cli/Cargo.lock

Lines changed: 40 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ resvg = "0.40.0"
9696
dunce = "1"
9797
glob = "0.3"
9898

99-
[target."cfg(windows)".dependencies]
100-
winapi = { version = "0.3", features = [ "handleapi", "processenv", "winbase", "wincon", "winnt" ] }
101-
cc = "1"
99+
[target."cfg(windows)".dependencies.windows-sys]
100+
version = "0.52"
101+
features = [
102+
"Win32_Storage_FileSystem",
103+
"Win32_System_IO",
104+
"Win32_System_Console"
105+
]
102106

103107
[target."cfg(unix)".dependencies]
104108
libc = "0.2"

tooling/cli/src/helpers/flock.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,10 @@ mod sys {
305305
use std::mem;
306306
use std::os::windows::io::AsRawHandle;
307307

308-
use winapi::shared::minwindef::DWORD;
309-
use winapi::shared::winerror::{ERROR_INVALID_FUNCTION, ERROR_LOCK_VIOLATION};
310-
use winapi::um::fileapi::{LockFileEx, UnlockFile};
311-
use winapi::um::minwinbase::{LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY};
308+
use windows_sys::Win32::Foundation::{ERROR_INVALID_FUNCTION, ERROR_LOCK_VIOLATION, HANDLE};
309+
use windows_sys::Win32::Storage::FileSystem::{
310+
LockFileEx, UnlockFile, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, LOCK_FILE_FLAGS,
311+
};
312312

313313
pub(super) fn lock_shared(file: &File) -> Result<()> {
314314
lock_file(file, 0)
@@ -339,25 +339,31 @@ mod sys {
339339
}
340340

341341
pub(super) fn unlock(file: &File) -> Result<()> {
342-
unsafe {
343-
let ret = UnlockFile(file.as_raw_handle(), 0, 0, !0, !0);
344-
if ret == 0 {
345-
Err(Error::last_os_error())
346-
} else {
347-
Ok(())
348-
}
342+
let ret = unsafe { UnlockFile(file.as_raw_handle() as HANDLE, 0, 0, !0, !0) };
343+
if ret == 0 {
344+
Err(Error::last_os_error())
345+
} else {
346+
Ok(())
349347
}
350348
}
351349

352-
fn lock_file(file: &File, flags: DWORD) -> Result<()> {
353-
unsafe {
350+
fn lock_file(file: &File, flags: LOCK_FILE_FLAGS) -> Result<()> {
351+
let ret = unsafe {
354352
let mut overlapped = mem::zeroed();
355-
let ret = LockFileEx(file.as_raw_handle(), flags, 0, !0, !0, &mut overlapped);
356-
if ret == 0 {
357-
Err(Error::last_os_error())
358-
} else {
359-
Ok(())
360-
}
353+
LockFileEx(
354+
file.as_raw_handle() as HANDLE,
355+
flags,
356+
0,
357+
!0,
358+
!0,
359+
&mut overlapped,
360+
)
361+
};
362+
363+
if ret == 0 {
364+
Err(Error::last_os_error())
365+
} else {
366+
Ok(())
361367
}
362368
}
363369
}

tooling/cli/src/interface/rust/desktop.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,14 @@ mod terminal {
446446
#[cfg(windows)]
447447
mod terminal {
448448
use std::{cmp, mem, ptr};
449-
use winapi::um::fileapi::*;
450-
use winapi::um::handleapi::*;
451-
use winapi::um::processenv::*;
452-
use winapi::um::winbase::*;
453-
use winapi::um::wincon::*;
454-
use winapi::um::winnt::*;
449+
450+
use windows_sys::Win32::{
451+
Foundation::{CloseHandle, GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE},
452+
Storage::FileSystem::{CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING},
453+
System::Console::{
454+
GetConsoleScreenBufferInfo, GetStdHandle, CONSOLE_SCREEN_BUFFER_INFO, STD_ERROR_HANDLE,
455+
},
456+
};
455457

456458
pub fn stderr_width() -> Option<usize> {
457459
unsafe {
@@ -465,13 +467,13 @@ mod terminal {
465467
// INVALID_HANDLE_VALUE. Use an alternate method which works
466468
// in that case as well.
467469
let h = CreateFileA(
468-
"CONOUT$\0".as_ptr() as *const CHAR,
470+
"CONOUT$\0".as_ptr(),
469471
GENERIC_READ | GENERIC_WRITE,
470472
FILE_SHARE_READ | FILE_SHARE_WRITE,
471473
ptr::null_mut(),
472474
OPEN_EXISTING,
473475
0,
474-
ptr::null_mut(),
476+
0,
475477
);
476478
if h == INVALID_HANDLE_VALUE {
477479
return None;

0 commit comments

Comments
 (0)