Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api.js): fix os.platform return on macos and windows, closes #2698 #2699

Merged
merged 3 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`.
2 changes: 1 addition & 1 deletion core/tauri/src/api/file/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn detect_archive_type(path: &path::Path) -> ArchiveFormat {
Some(extension) if extension == std::ffi::OsStr::new("tar") => ArchiveFormat::Tar(None),
Some(extension) if extension == std::ffi::OsStr::new("gz") => match path
.file_stem()
.map(|e| path::Path::new(e))
.map(path::Path::new)
.and_then(|f| f.extension())
{
Some(extension) if extension == std::ffi::OsStr::new("tar") => {
Expand Down
9 changes: 5 additions & 4 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,11 @@ impl<R: Runtime> Builder<R> {
T: Send + Sync + 'static,
{
let type_name = std::any::type_name::<T>();
if !self.state.set(state) {
panic!("state for type '{}' is already being managed", type_name);
}

assert!(
self.state.set(state),
"state for type '{}' is already being managed",
type_name
);
self
}

Expand Down
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();
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
}
#[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
4 changes: 2 additions & 2 deletions tooling/cli.rs/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Build {
logger.log(format!("Running `{}`", before_build));
#[cfg(target_os = "windows")]
execute_with_output(
&mut Command::new("cmd")
Command::new("cmd")
.arg("/C")
.arg(before_build)
.current_dir(app_dir())
Expand All @@ -96,7 +96,7 @@ impl Build {
.with_context(|| format!("failed to run `{}` with `cmd /C`", before_build))?;
#[cfg(not(target_os = "windows"))]
execute_with_output(
&mut Command::new("sh")
Command::new("sh")
.arg("-c")
.arg(before_build)
.current_dir(app_dir())
Expand Down