Skip to content

Commit

Permalink
feat: 内嵌文件, 并分别通过js、rust读取
Browse files Browse the repository at this point in the history
  • Loading branch information
nICEnnnnnnnLee committed Jan 3, 2023
1 parent 3eb5d57 commit 58c7470
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "MasterLee",
"name": "master-lee",
"private": true,
"version": "0.0.0",
"type": "module",
Expand Down
20 changes: 10 additions & 10 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "MasterLee"
name = "master_lee"
version = "0.0.0"
description = "A Tauri App"
authors = ["you"]
Expand All @@ -16,7 +16,7 @@ tauri-build = { version = "1.2", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2", features = ["shell-open"] }
tauri = { version = "1.2", features = ["fs-all", "path-all", "shell-open"] }

[features]
# by default Tauri runs in production mode
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/resources/config/config.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
15 changes: 14 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}

#[tauri::command]
fn read_conf(handle: tauri::AppHandle) -> String {
let resource_path = handle
.path_resolver()
.resolve_resource("resources/config/config.default.json")
.expect("failed to resolve resource");

let file = std::fs::File::open(&resource_path).unwrap();
let foo: serde_json::Value = serde_json::from_reader(file).unwrap();

foo.get("foo").unwrap().to_string()
}

fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.invoke_handler(tauri::generate_handler![greet, read_conf])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
21 changes: 16 additions & 5 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
"shell": {
"all": false,
"open": true
},
"fs": {
"all": true,
"scope": [
"$RESOURCE/**/*"
]
},
"path": {
"all": true
}
},
"bundle": {
Expand All @@ -33,17 +42,19 @@
"icons/icon.icns",
"icons/icon.ico"
],
"identifier": "com.tauri.dev",
"longDescription": "",
"identifier": "io.github.nICEnnnnnnnLee.master-lee",
"longDescription": "长一点的介绍",
"macOS": {
"entitlements": null,
"exceptionDomain": "",
"frameworks": [],
"providerShortName": null,
"signingIdentity": null
},
"resources": [],
"shortDescription": "",
"resources": [
"resources/*"
],
"shortDescription": "简短的介绍",
"targets": "all",
"windows": {
"certificateThumbprint": null,
Expand All @@ -67,4 +78,4 @@
}
]
}
}
}
26 changes: 26 additions & 0 deletions src/components/Greet.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<script setup>
import { ref } from "vue";
import { invoke } from "@tauri-apps/api/tauri";
import { resolveResource } from '@tauri-apps/api/path'
// alternatively, use `window.__TAURI__.path.resolveResource`
import { readTextFile } from '@tauri-apps/api/fs'
// alternatively, use `window.__TAURI__.fs.readTextFile`
const greetMsg = ref("");
const name = ref("");
Expand All @@ -9,13 +13,35 @@ async function greet() {
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
greetMsg.value = await invoke("greet", { name: name.value });
}
async function readConf() {
// `config.default.json` is the value specified on `tauri.conf.json > tauri > bundle > resources`
const resourcePath = await resolveResource('resources/config/config.default.json');
const conf = JSON.parse(await readTextFile(resourcePath));
console.log(conf);
greetMsg.value = "read using js: " + conf.foo;
}
async function readConfFromRust() {
greetMsg.value = "read using rust: " + await invoke("read_conf");
}
</script>

<style>
.btn {
margin-top: 5px;
}
</style>
<template>
<div class="card">
<input id="greet-input" v-model="name" placeholder="Enter a name..." />
<button type="button" @click="greet()">Greet</button>
</div>
<div class="btn" >
<button type="button" @click="readConf()">ReadConf</button>
<button type="button" @click="readConfFromRust()">ReadConf2</button>
</div>

<p>{{ greetMsg }}</p>
</template>

0 comments on commit 58c7470

Please sign in to comment.