Explorar el Código

feat(examples): add `resources` example (#2001)

Lucas Fernandes Nogueira hace 4 años
padre
commit
1c97a151c1

+ 1 - 0
Cargo.toml

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

+ 3 - 0
examples/resources/README.md

@@ -0,0 +1,3 @@
+# Resource example
+
+This example demonstrates the Tauri bundle resources functionality. The example adds `src-tauri/assets/index.js` as a resource (defined on `tauri.conf.json > tauri > bundle > resources`) and executes it using `Node.js`, locating the JavaScript file using the `tauri::api::path::resolve_path` API.

+ 23 - 0
examples/resources/index.html

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

+ 7 - 0
examples/resources/package.json

@@ -0,0 +1,7 @@
+{
+  "name": "resources",
+  "version": "1.0.0",
+  "scripts": {
+    "tauri": "node ../../tooling/cli.js/bin/tauri"
+  }
+}

+ 10 - 0
examples/resources/src-tauri/.gitignore

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

+ 3 - 0
examples/resources/src-tauri/.license_template

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

+ 17 - 0
examples/resources/src-tauri/Cargo.toml

@@ -0,0 +1,17 @@
+[package]
+name = "resources"
+version = "0.1.0"
+description = "A Tauri application that uses Node.js with app resources"
+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" ]

+ 3 - 0
examples/resources/src-tauri/assets/index.js

@@ -0,0 +1,3 @@
+setInterval(() => {
+  console.log(`[${new Date().toLocaleTimeString()}] new message`) 
+}, 500)

+ 14 - 0
examples/resources/src-tauri/build.rs

@@ -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);
+  }
+}

+ 50 - 0
examples/resources/src-tauri/src/main.rs

@@ -0,0 +1,50 @@
+// 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::{
+    path::{resolve_path, BaseDirectory},
+    process::{Command, CommandEvent},
+  },
+  Manager,
+};
+
+fn main() {
+  let context = tauri::generate_context!();
+  let script_path = resolve_path(
+    context.config(),
+    context.package_info(),
+    "assets/index.js",
+    Some(BaseDirectory::Resource),
+  )
+  .unwrap();
+  tauri::Builder::default()
+    .setup(move |app| {
+      let window = app.get_window("main").unwrap();
+      let script_path = script_path.to_string_lossy().to_string();
+      tauri::async_runtime::spawn(async move {
+        let (mut rx, _child) = Command::new("node")
+          .args(&[script_path])
+          .spawn()
+          .expect("Failed to spawn node");
+
+        while let Some(event) = rx.recv().await {
+          if let CommandEvent::Stdout(line) = event {
+            window
+              .emit("message", Some(format!("'{}'", line)))
+              .expect("failed to emit event");
+          }
+        }
+      });
+
+      Ok(())
+    })
+    .run(context)
+    .expect("error while running tauri application");
+}

+ 64 - 0
examples/resources/src-tauri/tauri.conf.json

@@ -0,0 +1,64 @@
+{
+  "build": {
+    "distDir": [
+      "../index.html"
+    ],
+    "devPath": [
+      "../index.html"
+    ],
+    "beforeDevCommand": "",
+    "beforeBuildCommand": "",
+    "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": ["assets/*"],
+      "externalBin": [],
+      "copyright": "",
+      "category": "DeveloperTool",
+      "shortDescription": "",
+      "longDescription": "",
+      "deb": {
+        "depends": [],
+        "useBootstrapper": false
+      },
+      "macOS": {
+        "frameworks": [],
+        "minimumSystemVersion": "",
+        "useBootstrapper": false,
+        "exceptionDomain": ""
+      }
+    },
+    "allowlist": {
+      "all": false,
+      "shell": {
+        "execute": true
+      }
+    },
+    "windows": [
+      {
+        "title": "Welcome to Tauri!",
+        "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
+    }
+  }
+}