فهرست منبع

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

Co-authored-by: FabianLars <fabianlars@fabianlars.de>
Amr Bashir 3 سال پیش
والد
کامیت
3924c3d853

+ 5 - 0
.changes/api-fix-os-platform-return.md

@@ -0,0 +1,5 @@
+---
+"api": patch
+---
+
+Fix `os.platform` returning `macos` and `windows` instead of `darwin` and `win32`.

+ 1 - 1
core/tauri/src/api/file/extract.rs

@@ -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") => {

+ 5 - 4
core/tauri/src/app.rs

@@ -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
   }
 

+ 21 - 9
core/tauri/src/endpoints/operating_system.rs

@@ -21,16 +21,9 @@ 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()),
     };
@@ -38,3 +31,22 @@ impl Cmd {
     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 "Darwin".into();
+}
+#[cfg(os_all)]
+fn os_platform() -> String {
+  match std::env::consts::OS {
+    "windows" => "win32",
+    "macos" => "darwin",
+    _ => std::env::consts::OS,
+  }
+  .into()
+}

+ 38 - 30
tooling/api/src/os.ts

@@ -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'
@@ -64,13 +73,13 @@ 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'
@@ -78,26 +87,26 @@ async function type(): Promise<OsType> {
   })
 }
 
-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'
@@ -118,4 +127,3 @@ async function tempdir(): Promise<string> {
 }
 
 export { EOL, platform, version, type, arch, tempdir }
-export type { Platform, OsType, Arch }

+ 2 - 2
tooling/cli.rs/src/build.rs

@@ -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())
@@ -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())