Skip to content

Commit

Permalink
fix(api.js): fix os.platform return on macos and windows, closes #2698
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Oct 2, 2021
1 parent a0bea43 commit d2293fc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changes/api-fix-os-platform-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": patch
---

Fix `os.platform` returning `macos` and `windows` instead of `darwin` and `win32`.
30 changes: 21 additions & 9 deletions core/tauri/src/endpoints/operating_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,32 @@ impl Cmd {
pub fn run(self) -> crate::Result<InvokeResponse> {
#[cfg(os_all)]
return match self {
Self::Platform => Ok(std::env::consts::OS.into()),
Self::Platform => Ok(os_platform().into()),
Self::Version => Ok(os_info::get().version().to_string().into()),
Self::Type => {
#[cfg(target_os = "linux")]
return Ok("Linux".into());
#[cfg(target_os = "windows")]
return Ok("Windows_NT".into());
#[cfg(target_os = "macos")]
return Ok("Darwing".into());
}
Self::Type => Ok(os_type().into()),
Self::Arch => Ok(std::env::consts::ARCH.into()),
Self::Tempdir => Ok(std::env::temp_dir().into()),
};
#[cfg(not(os_all))]
Err(crate::Error::ApiNotAllowlisted("os".into()))
}
}

#[cfg(os_all)]
fn os_type() -> String {
#[cfg(target_os = "linux")]
return "Linux".into();
#[cfg(target_os = "windows")]
return "Windows_NT".into();
#[cfg(target_os = "macos")]
return "Darwing".into();
}
#[cfg(os_all)]
fn os_platform() -> String {
match std::env::consts::OS {
"windows" => "win32",
"macos" => "darwin",
_ => std::env::consts::OS,
}
.into()
}
68 changes: 38 additions & 30 deletions tooling/api/src/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,26 @@ import { invokeTauriCommand } from './helpers/tauri'
* */
const EOL = isWindows() ? '\r\n' : '\n'

type Platform = LiteralUnion<
'aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32',
string
>

/**
* Returns a string identifying the operating system platform.
* The value is set at compile time. Possible values are `'aix'`, `'darwin'`, `'freebsd'`, `'linux'`, `'openbsd'`, `'sunos'`, and `'win32'`.
* The value is set at compile time. Possible values are `'linux'`, `'darwin'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'win32'`
*/
async function platform(): Promise<Platform> {
return invokeTauriCommand<Platform>({
async function platform(): Promise<
LiteralUnion<
| 'linux'
| 'darwin'
| 'ios'
| 'freebsd'
| 'dragonfly'
| 'netbsd'
| 'openbsd'
| 'solaris'
| 'android'
| 'win32',
string
>
> {
return invokeTauriCommand<string>({
__tauriModule: 'Os',
message: {
cmd: 'platform'
Expand All @@ -64,40 +73,40 @@ async function version(): Promise<string> {
})
}

type OsType = LiteralUnion<'Linux' | 'Darwin' | 'Windows_NT', string>

/**
* Returns `'Linux'` on Linux, `'Darwin'` on macOS, and `'Windows_NT'` on Windows.
*/
async function type(): Promise<OsType> {
return invokeTauriCommand<OsType>({
async function type(): Promise<
LiteralUnion<'Linux' | 'Darwin' | 'Windows_NT', string>
> {
return invokeTauriCommand<string>({
__tauriModule: 'Os',
message: {
cmd: 'type'
}
})
}

type Arch = LiteralUnion<
| 'x86'
| 'x86_64'
| 'arm'
| 'aarch64'
| 'mips'
| 'mips64'
| 'powerpc'
| 'powerpc64'
| 'riscv64'
| 's390x'
| 'sparc64',
string
>

/**
* Returns the operating system CPU architecture for which the tauri app was compiled. Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`
*/
async function arch(): Promise<Arch> {
return invokeTauriCommand<Arch>({
async function arch(): Promise<
LiteralUnion<
| 'x86'
| 'x86_64'
| 'arm'
| 'aarch64'
| 'mips'
| 'mips64'
| 'powerpc'
| 'powerpc64'
| 'riscv64'
| 's390x'
| 'sparc64',
string
>
> {
return invokeTauriCommand<string>({
__tauriModule: 'Os',
message: {
cmd: 'arch'
Expand All @@ -118,4 +127,3 @@ async function tempdir(): Promise<string> {
}

export { EOL, platform, version, type, arch, tempdir }
export type { Platform, OsType, Arch }
20 changes: 10 additions & 10 deletions tooling/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,12 @@ class WindowManager extends WebviewWindowHandle {
type: 'setMinSize',
payload: size
? {
type: size.type,
data: {
width: size.width,
height: size.height
type: size.type,
data: {
width: size.width,
height: size.height
}
}
}
: null
}
}
Expand Down Expand Up @@ -903,12 +903,12 @@ class WindowManager extends WebviewWindowHandle {
type: 'setMaxSize',
payload: size
? {
type: size.type,
data: {
width: size.width,
height: size.height
type: size.type,
data: {
width: size.width,
height: size.height
}
}
}
: null
}
}
Expand Down

0 comments on commit d2293fc

Please sign in to comment.