Skip to content

Commit 05e679a

Browse files
authored
feat(api.js): add os module (#2299)
* feat(api.js): add `os` module * use correct endpoint for version * return version as a string * clippy * cleanup ?! * [skip ci] * [skip ci]
1 parent 71d687b commit 05e679a

File tree

12 files changed

+189
-33
lines changed

12 files changed

+189
-33
lines changed

.changes/api-os-module.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"api": patch
3+
---
4+
Add `os` module which exports `EOL`, `platform()`, `version()`, `type()`, `arch()`, `tempdir()`

core/tauri/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ os_pipe = { version = "0.9", optional = true }
6969
rfd = "0.4"
7070
raw-window-handle = { version = "0.3.3", optional = true }
7171
minisign-verify = { version = "0.1", optional = true }
72+
os_info = { version = "3.0.6", optional = true}
7273

7374
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
7475
gtk = { version = "0.9", features = [ "v3_16" ] }
@@ -97,6 +98,7 @@ api-all = [
9798
"notification-all",
9899
"global-shortcut-all",
99100
"shell-all",
101+
"os-all",
100102
"dialog-all",
101103
"updater"
102104
]
@@ -128,3 +130,4 @@ http-all = [ ]
128130
http-request = [ ]
129131
notification-all = [ "notify-rust" ]
130132
global-shortcut-all = [ ]
133+
os-all = [ "os_info" ]

core/tauri/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,8 @@ fn main() {
5151

5252
// global shortcut
5353
global_shortcut_all: { any(api_all, feature = "global_shortcut-all") },
54+
55+
// os
56+
os_all: { any(api_all, feature = "os-all") },
5457
}
5558
}

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/endpoints.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod global_shortcut;
2424
mod http;
2525
mod internal;
2626
mod notification;
27+
mod operating_system;
2728
mod process;
2829
mod shell;
2930
mod window;
@@ -47,6 +48,7 @@ enum Module {
4748
App(app::Cmd),
4849
Process(process::Cmd),
4950
Fs(file_system::Cmd),
51+
Os(operating_system::Cmd),
5052
Window(Box<window::Cmd>),
5153
Shell(shell::Cmd),
5254
Event(event::Cmd),
@@ -82,6 +84,8 @@ impl Module {
8284
.and_then(|r| r.json)
8385
.map_err(InvokeError::from)
8486
}),
87+
Self::Os(cmd) => resolver
88+
.respond_async(async move { cmd.run().and_then(|r| r.json).map_err(InvokeError::from) }),
8589
Self::Window(cmd) => resolver.respond_async(async move {
8690
cmd
8791
.run(window)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
use super::InvokeResponse;
6+
use serde::Deserialize;
7+
8+
/// The API descriptor.
9+
#[derive(Deserialize)]
10+
#[serde(tag = "cmd", rename_all = "camelCase")]
11+
pub enum Cmd {
12+
Platform,
13+
Version,
14+
Type,
15+
Arch,
16+
Tempdir,
17+
}
18+
19+
impl Cmd {
20+
#[allow(unused_variables)]
21+
pub fn run(self) -> crate::Result<InvokeResponse> {
22+
#[cfg(os_all)]
23+
return match self {
24+
Self::Platform => Ok(std::env::consts::OS.into()),
25+
Self::Version => Ok(os_info::get().version().to_string().into()),
26+
Self::Type => {
27+
#[cfg(target_os = "linux")]
28+
return Ok("Linux".into());
29+
#[cfg(target_os = "windows")]
30+
return Ok("Windows_NT".into());
31+
#[cfg(target_os = "macos")]
32+
return Ok("Darwing".into());
33+
}
34+
Self::Arch => Ok(std::env::consts::ARCH.into()),
35+
Self::Tempdir => Ok(std::env::temp_dir().into()),
36+
};
37+
#[cfg(not(os_all))]
38+
Err(crate::Error::ApiNotAllowlisted("os".into()))
39+
}
40+
}

tooling/api/rollup.config.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
// rollup.config.js
6+
import { readdirSync } from 'fs'
67
import { terser } from 'rollup-plugin-terser'
78
import resolve from '@rollup/plugin-node-resolve'
89
import commonjs from '@rollup/plugin-commonjs'
@@ -13,24 +14,13 @@ import pkg from './package.json'
1314

1415
export default [
1516
{
16-
input: {
17-
app: './src/app.ts',
18-
fs: './src/fs.ts',
19-
path: './src/path.ts',
20-
dialog: './src/dialog.ts',
21-
event: './src/event.ts',
22-
updater: './src/updater.ts',
23-
http: './src/http.ts',
24-
index: './src/index.ts',
25-
shell: './src/shell.ts',
26-
tauri: './src/tauri.ts',
27-
window: './src/window.ts',
28-
cli: './src/cli.ts',
29-
notification: './src/notification.ts',
30-
globalShortcut: './src/globalShortcut.ts',
31-
process: './src/process.ts',
32-
clipboard: './src/clipboard.ts'
33-
},
17+
input: (() => {
18+
let input = {}
19+
readdirSync('src')
20+
.filter((e) => e.endsWith('.ts') && e !== 'bundle.ts')
21+
.forEach((mod) => (input[`${mod.replace('.ts', '')}`] = `./src/${mod}`))
22+
return input
23+
})(),
3424
treeshake: true,
3525
perf: true,
3626
output: [

tooling/api/scripts/after-build.cjs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
const { readFileSync, readdirSync, writeFileSync, copyFileSync } = require('fs')
66
const { copySync } = require('fs-extra')
77

8-
/**
9-
* append our api modules to `exports` in `package.json` then write it to `./dist`
10-
*/
8+
// append our api modules to `exports` in `package.json` then write it to `./dist`
119
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
12-
const modules = readdirSync('src').map((mod) => mod.replace('.ts', ''))
13-
if (!pkg.exports) {
14-
pkg.exports = {}
15-
}
10+
const modules = readdirSync('src')
11+
.filter((e) => e !== 'helpers')
12+
.map((mod) => mod.replace('.ts', ''))
1613

1714
const outputPkg = {
1815
...pkg,
@@ -33,22 +30,18 @@ const outputPkg = {
3330
}),
3431
// if for some reason in the future we manually add something in the `exports` field
3532
// this will ensure it doesn't get overwritten by the logic above
36-
{ ...pkg.exports }
33+
{ ...(pkg.exports || {}) }
3734
)
3835
}
3936
writeFileSync('dist/package.json', JSON.stringify(outputPkg, undefined, 2))
4037

41-
/**
42-
* copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
43-
*/
38+
// copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
4439
const dir = readdirSync('.')
4540
const files = [
4641
...dir.filter((f) => f.startsWith('LICENSE')),
4742
...dir.filter((f) => f.endsWith('.md'))
4843
]
4944
files.forEach((f) => copyFileSync(f, `dist/${f}`))
5045

51-
/**
52-
* copy typescript src files to `./dist`
53-
*/
46+
// copy typescript src files to `./dist`
5447
copySync('src', 'dist')

tooling/api/src/bundle.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as shell from './shell'
2020
import * as tauri from './tauri'
2121
import * as updater from './updater'
2222
import * as window from './window'
23+
import * as os from './os'
2324
const invoke = tauri.invoke
2425

2526
export {
@@ -38,5 +39,6 @@ export {
3839
tauri,
3940
updater,
4041
window,
42+
os,
4143
invoke
4244
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
/** @ignore */
6+
7+
function isLinux(): boolean {
8+
return navigator.appVersion.includes('Linux')
9+
}
10+
11+
function isWindows(): boolean {
12+
return navigator.appVersion.includes('Win')
13+
}
14+
15+
function isMacOS(): boolean {
16+
return navigator.appVersion.includes('Mac')
17+
}
18+
19+
export { isLinux, isWindows, isMacOS }

0 commit comments

Comments
 (0)