feat(examples): add sidecar example (#1999)

This commit is contained in:
Lucas Fernandes Nogueira
2021-06-16 22:18:24 -03:00
committed by GitHub
parent ab3eb44bac
commit ec88e30617
27 changed files with 1310 additions and 13 deletions

View File

@@ -18,6 +18,7 @@ members = [
"examples/params/src-tauri",
"examples/splashscreen/src-tauri",
"examples/state/src-tauri",
"examples/sidecar/src-tauri",
# used to build updater artifacts
"examples/updater/src-tauri",
]

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -57,4 +57,4 @@
"active": false
}
}
}
}

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -0,0 +1,3 @@
# Sidecar example
This example demonstrates how to use the Tauri sidecar feature. It uses [pkg](https://github.com/vercel/pkg) to compile a Node.js application and bundle it on the Tauri application.

View File

@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sidecar</title>
</head>
<body>
<div></div>
<script>
const div = document.querySelector('div')
window.__TAURI__.event.listen('message', event => {
const p = document.createElement('p')
p.innerHTML = event.payload
div.appendChild(p)
})
</script>
</body>
</html>

View File

@@ -0,0 +1,17 @@
{
"name": "sidecar",
"version": "1.0.0",
"bin": "src/index.js",
"pkg": {
"assets": [
"src/**/*"
]
},
"scripts": {
"tauri": "node ../../tooling/cli.js/bin/tauri",
"package": "pkg package.json --output src-tauri/binaries/app && node scripts/move-binary.js"
},
"devDependencies": {
"pkg": "5.2.1"
}
}

View File

@@ -0,0 +1,32 @@
/**
* This script is used to rename the binary with the platform specific postfix.
* When `tauri build` is ran, it looks for the binary name appended with the platform specific postfix.
*/
const execa = require('execa')
const fs = require('fs')
async function main() {
const rustTargetInfo = JSON.parse(
(
await execa(
'rustc',
['-Z', 'unstable-options', '--print', 'target-spec-json'],
{
env: {
RUSTC_BOOTSTRAP: 1
}
}
)
).stdout
)
const platformPostfix = rustTargetInfo['llvm-target']
fs.renameSync(
'src-tauri/binaries/app',
`src-tauri/binaries/app-${platformPostfix}`
)
}
main().catch((e) => {
throw e
})

11
examples/sidecar/src-tauri/.gitignore vendored Normal file
View File

@@ -0,0 +1,11 @@
# Generated by Cargo
# will have compiled files and executables
/target/
binaries/
WixTools
# These are backup files generated by rustfmt
**/*.rs.bk
config.json
bundle.json

View File

@@ -0,0 +1,3 @@
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -0,0 +1,17 @@
[package]
name = "sidecar"
version = "0.1.0"
description = "A Tauri application with a sidecar binary"
edition = "2018"
[build-dependencies]
tauri-build = { path = "../../../core/tauri-build", features = [ "codegen" ] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
tauri = { path = "../../../core/tauri", features = ["shell-execute"] }
[features]
default = [ "custom-protocol" ]
custom-protocol = [ "tauri/custom-protocol" ]

View File

@@ -0,0 +1,14 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use tauri_build::{try_build, Attributes, WindowsAttributes};
fn main() {
if let Err(error) = try_build(
Attributes::new()
.windows_attributes(WindowsAttributes::new().window_icon_path("../../.icons/icon.ico")),
) {
panic!("error found during tauri-build: {}", error);
}
}

View File

@@ -0,0 +1,44 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use tauri::{
api::process::{Command, CommandEvent},
Manager,
};
fn main() {
tauri::Builder::default()
.setup(|app| {
let window = app.get_window("main").unwrap();
tauri::async_runtime::spawn(async move {
let (mut rx, mut child) = Command::new_sidecar("app")
.expect("failed to setup `app` sidecar")
.spawn()
.expect("Failed to spawn packaged node");
let mut i = 0;
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
window
.emit("message", Some(format!("'{}'", line)))
.expect("failed to emit event");
i += 1;
if i == 4 {
child.write("message from Rust\n".as_bytes()).unwrap();
i = 0;
}
}
}
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@@ -0,0 +1,64 @@
{
"build": {
"distDir": [
"../index.html"
],
"devPath": [
"../index.html"
],
"beforeDevCommand": "yarn package",
"beforeBuildCommand": "yarn package",
"withGlobalTauri": true
},
"tauri": {
"bundle": {
"active": true,
"targets": "all",
"identifier": "com.tauri.dev",
"icon": [
"../../.icons/32x32.png",
"../../.icons/128x128.png",
"../../.icons/128x128@2x.png",
"../../.icons/icon.icns",
"../../.icons/icon.ico"
],
"resources": [],
"externalBin": ["binaries/app"],
"copyright": "",
"category": "DeveloperTool",
"shortDescription": "",
"longDescription": "",
"deb": {
"depends": [],
"useBootstrapper": false
},
"macOS": {
"frameworks": [],
"minimumSystemVersion": "",
"useBootstrapper": false,
"exceptionDomain": ""
}
},
"allowlist": {
"all": false,
"shell": {
"execute": true
}
},
"windows": [
{
"title": "Sidecar",
"width": 800,
"height": 600,
"resizable": true,
"fullscreen": false
}
],
"security": {
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'"
},
"updater": {
"active": false
}
}
}

View File

@@ -0,0 +1,18 @@
const readline = require('readline')
module.exports = {
onMessage(cb) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
})
rl.on('line', function(line){
cb(line)
})
},
write(message) {
console.log(message)
}
}

View File

@@ -0,0 +1,9 @@
const { write, onMessage } = require('./communication')
onMessage(line => {
write(`read ${line}`)
})
setInterval(() => {
write(`[${new Date().toLocaleTimeString()}] new message`)
}, 500)

1017
examples/sidecar/yarn.lock Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

View File

@@ -1 +1,3 @@
../../../.license_template
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT