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

feat(api.js): add os module #2299

Merged
merged 7 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .changes/api-os-module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"api": patch
---
Add `os` module which exports `EOL`, `platform()`, `version()`, `type()`, `arch()`, `tempdir()`
3 changes: 3 additions & 0 deletions core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ os_pipe = { version = "0.9", optional = true }
rfd = "0.4"
raw-window-handle = { version = "0.3.3", optional = true }
minisign-verify = { version = "0.1", optional = true }
os_info = { version = "3.0.6", optional = true}

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
gtk = { version = "0.9", features = [ "v3_16" ] }
Expand Down Expand Up @@ -97,6 +98,7 @@ api-all = [
"notification-all",
"global-shortcut-all",
"shell-all",
"os-all",
"dialog-all",
"updater"
]
Expand Down Expand Up @@ -128,3 +130,4 @@ http-all = [ ]
http-request = [ ]
notification-all = [ "notify-rust" ]
global-shortcut-all = [ ]
os-all = [ "os_info" ]
3 changes: 3 additions & 0 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ fn main() {

// global shortcut
global_shortcut_all: { any(api_all, feature = "global_shortcut-all") },

// os
os_all: { any(api_all, feature = "os-all") },
}
}
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions core/tauri/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod global_shortcut;
mod http;
mod internal;
mod notification;
mod operating_system;
mod process;
mod shell;
mod window;
Expand All @@ -47,6 +48,7 @@ enum Module {
App(app::Cmd),
Process(process::Cmd),
Fs(file_system::Cmd),
Os(operating_system::Cmd),
Window(Box<window::Cmd>),
Shell(shell::Cmd),
Event(event::Cmd),
Expand Down Expand Up @@ -82,6 +84,8 @@ impl Module {
.and_then(|r| r.json)
.map_err(InvokeError::from)
}),
Self::Os(cmd) => resolver
.respond_async(async move { cmd.run().and_then(|r| r.json).map_err(InvokeError::from) }),
Self::Window(cmd) => resolver.respond_async(async move {
cmd
.run(window)
Expand Down
40 changes: 40 additions & 0 deletions core/tauri/src/endpoints/operating_system.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use super::InvokeResponse;
use serde::Deserialize;

/// The API descriptor.
#[derive(Deserialize)]
#[serde(tag = "cmd", rename_all = "camelCase")]
pub enum Cmd {
Platform,
Version,
Type,
Arch,
Tempdir,
}

impl Cmd {
#[allow(unused_variables)]
pub fn run(self) -> crate::Result<InvokeResponse> {
#[cfg(os_all)]
return match self {
Self::Platform => Ok(std::env::consts::OS.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::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()))
}
}
26 changes: 8 additions & 18 deletions tooling/api/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: MIT

// rollup.config.js
import { readdirSync } from 'fs'
import { terser } from 'rollup-plugin-terser'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
Expand All @@ -13,24 +14,13 @@ import pkg from './package.json'

export default [
{
input: {
app: './src/app.ts',
fs: './src/fs.ts',
path: './src/path.ts',
dialog: './src/dialog.ts',
event: './src/event.ts',
updater: './src/updater.ts',
http: './src/http.ts',
index: './src/index.ts',
shell: './src/shell.ts',
tauri: './src/tauri.ts',
window: './src/window.ts',
cli: './src/cli.ts',
notification: './src/notification.ts',
globalShortcut: './src/globalShortcut.ts',
process: './src/process.ts',
clipboard: './src/clipboard.ts'
},
input: (() => {
let input = {}
readdirSync('src')
.filter((e) => e.endsWith('.ts') && e !== 'bundle.ts')
.forEach((mod) => (input[`${mod.replace('.ts', '')}`] = `./src/${mod}`))
return input
})(),
treeshake: true,
perf: true,
output: [
Expand Down
21 changes: 7 additions & 14 deletions tooling/api/scripts/after-build.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
const { readFileSync, readdirSync, writeFileSync, copyFileSync } = require('fs')
const { copySync } = require('fs-extra')

/**
* append our api modules to `exports` in `package.json` then write it to `./dist`
*/
// append our api modules to `exports` in `package.json` then write it to `./dist`
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
const modules = readdirSync('src').map((mod) => mod.replace('.ts', ''))
if (!pkg.exports) {
pkg.exports = {}
}
const modules = readdirSync('src')
.filter((e) => e !== 'helpers')
.map((mod) => mod.replace('.ts', ''))

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

/**
* copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
*/
// copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
const dir = readdirSync('.')
const files = [
...dir.filter((f) => f.startsWith('LICENSE')),
...dir.filter((f) => f.endsWith('.md'))
]
files.forEach((f) => copyFileSync(f, `dist/${f}`))

/**
* copy typescript src files to `./dist`
*/
// copy typescript src files to `./dist`
copySync('src', 'dist')
2 changes: 2 additions & 0 deletions tooling/api/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as shell from './shell'
import * as tauri from './tauri'
import * as updater from './updater'
import * as window from './window'
import * as os from './os'
const invoke = tauri.invoke

export {
Expand All @@ -38,5 +39,6 @@ export {
tauri,
updater,
window,
os,
invoke
}
19 changes: 19 additions & 0 deletions tooling/api/src/helpers/os-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

/** @ignore */

function isLinux(): boolean {
return navigator.appVersion.includes('Linux')
}

function isWindows(): boolean {
return navigator.appVersion.includes('Win')
}

function isMacOS(): boolean {
return navigator.appVersion.includes('Mac')
}

export { isLinux, isWindows, isMacOS }
1 change: 1 addition & 0 deletions tooling/api/src/helpers/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { invoke } from '../tauri'
type TauriModule =
| 'App'
| 'Fs'
| 'Os'
| 'Window'
| 'Shell'
| 'Event'
Expand Down
97 changes: 97 additions & 0 deletions tooling/api/src/os.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

/**
* Provides operating system-related utility methods and properties.
*
* This package is also accessible with `window.__TAURI__.fs` when `tauri.conf.json > build > withGlobalTauri` is set to true.
*
* The APIs must be allowlisted on `tauri.conf.json`:
* ```json
* {
* "tauri": {
* "allowlist": {
* "os": {
* "all": true, // enable all Os APIs
* }
* }
* }
* }
* ```
* It is recommended to allowlist only the APIs you use for optimal bundle size and security.
* @module
*/

import { isWindows } from './helpers/os-check'
import { invokeTauriCommand } from './helpers/tauri'

/**
* The operating system-specific end-of-line marker.
* - `\n` on POSIX
* - `\r\n` on Windows
* */
const EOL = isWindows() ? '\r\n' : '\n'

/**
* 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'`.
*/
async function platform(): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'Os',
message: {
cmd: 'platform'
}
})
}

/**
* Returns a string identifying the kernel version.
*/
async function version(): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'Os',
message: {
cmd: 'version'
}
})
}

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

/**
* 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<string> {
return invokeTauriCommand<string>({
__tauriModule: 'Os',
message: {
cmd: 'arch'
}
})
}

/**
* Returns the operating system's default directory for temporary files as a string.
*/
async function tempdir(): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'Os',
message: {
cmd: 'tempdir'
}
})
}

export { EOL, platform, version, type, arch, tempdir }