create-basicjs 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/bin/create-basicjs.js +27 -1
- package/package.json +1 -1
- package/payload/projects/tauri/.github/workflows/build.yml +86 -0
- package/payload/projects/tauri/.github/workflows/tests.yml +87 -0
- package/payload/projects/tauri/AGENTS.md +128 -0
- package/payload/projects/tauri/CLAUDE.md +1 -0
- package/payload/projects/tauri/LICENSE +201 -0
- package/payload/projects/tauri/NOTICE +8 -0
- package/payload/projects/tauri/README.md +269 -0
- package/payload/projects/tauri/checks/window-check.js +189 -0
- package/payload/projects/tauri/gitignore +8 -0
- package/payload/projects/tauri/handbook/01-getting-started.md +188 -0
- package/payload/projects/tauri/handbook/02-the-project.md +264 -0
- package/payload/projects/tauri/handbook/03-the-page.md +260 -0
- package/payload/projects/tauri/handbook/04-commands.md +265 -0
- package/payload/projects/tauri/handbook/05-events-and-state.md +275 -0
- package/payload/projects/tauri/handbook/06-plugins.md +282 -0
- package/payload/projects/tauri/handbook/07-the-window-and-the-app.md +431 -0
- package/payload/projects/tauri/handbook/08-security.md +374 -0
- package/payload/projects/tauri/handbook/09-tests-and-checks.md +266 -0
- package/payload/projects/tauri/handbook/10-building-and-releasing.md +238 -0
- package/payload/projects/tauri/handbook/11-troubleshooting.md +259 -0
- package/payload/projects/tauri/handbook/README.md +125 -0
- package/payload/projects/tauri/handbook/turkce/01-getting-started.md +193 -0
- package/payload/projects/tauri/handbook/turkce/02-the-project.md +272 -0
- package/payload/projects/tauri/handbook/turkce/03-the-page.md +272 -0
- package/payload/projects/tauri/handbook/turkce/04-commands.md +271 -0
- package/payload/projects/tauri/handbook/turkce/05-events-and-state.md +284 -0
- package/payload/projects/tauri/handbook/turkce/06-plugins.md +286 -0
- package/payload/projects/tauri/handbook/turkce/07-the-window-and-the-app.md +433 -0
- package/payload/projects/tauri/handbook/turkce/08-security.md +384 -0
- package/payload/projects/tauri/handbook/turkce/09-tests-and-checks.md +275 -0
- package/payload/projects/tauri/handbook/turkce/10-building-and-releasing.md +241 -0
- package/payload/projects/tauri/handbook/turkce/11-troubleshooting.md +264 -0
- package/payload/projects/tauri/handbook/turkce/README.md +128 -0
- package/payload/projects/tauri/images/app.png +0 -0
- package/payload/projects/tauri/src-tauri/Cargo.lock +4389 -0
- package/payload/projects/tauri/src-tauri/Cargo.toml +31 -0
- package/payload/projects/tauri/src-tauri/app-icon.svg +16 -0
- package/payload/projects/tauri/src-tauri/build.rs +15 -0
- package/payload/projects/tauri/src-tauri/capabilities/default.json +7 -0
- package/payload/projects/tauri/src-tauri/gitignore +5 -0
- package/payload/projects/tauri/src-tauri/icons/128x128.png +0 -0
- package/payload/projects/tauri/src-tauri/icons/128x128@2x.png +0 -0
- package/payload/projects/tauri/src-tauri/icons/32x32.png +0 -0
- package/payload/projects/tauri/src-tauri/icons/icon.icns +0 -0
- package/payload/projects/tauri/src-tauri/icons/icon.ico +0 -0
- package/payload/projects/tauri/src-tauri/src/check.rs +144 -0
- package/payload/projects/tauri/src-tauri/src/files.rs +255 -0
- package/payload/projects/tauri/src-tauri/src/main.rs +45 -0
- package/payload/projects/tauri/src-tauri/src/sizes.rs +214 -0
- package/payload/projects/tauri/src-tauri/tauri.conf.json +44 -0
- package/payload/projects/tauri/src-tauri/tests/project.rs +243 -0
- package/payload/projects/tauri/ui/app.js +459 -0
- package/payload/projects/tauri/ui/backend.js +196 -0
- package/payload/projects/tauri/ui/basic/LICENSE +14 -0
- package/payload/projects/tauri/ui/basic/basic.min.css +1 -0
- package/payload/projects/tauri/ui/basic/basic.min.js +78 -0
- package/payload/projects/tauri/ui/basic/font/open-sans/LICENSE.txt +206 -0
- package/payload/projects/tauri/ui/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- package/payload/projects/tauri/ui/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- package/payload/projects/tauri/ui/basic/scroll-bar.min.js +3 -0
- package/payload/projects/tauri/ui/comp/_sources.txt +9 -0
- package/payload/projects/tauri/ui/comp/components.min.js +21 -0
- package/payload/projects/tauri/ui/index.html +28 -0
- package/payload/projects/tauri/ui/sizes.js +104 -0
- package/payload/starter/_basic/VERSION +1 -1
- package/payload/starter/_basic/comp/bottom-bar.js +31 -3
- package/payload/starter/_basic/comp/code-editor.js +219 -1
- package/payload/starter/_basic/comp/context-menu.js +6 -1
- package/payload/starter/_basic/comp/markdown-view.js +4 -2
- package/payload/starter/_basic/comp/property-panel.js +1 -1
- package/payload/starter/_basic/comp/slider.js +27 -12
- package/payload/starter/_basic/comp/smart-button.js +5 -1
- package/payload/starter/_basic/comp/smart-table.js +8 -1
- package/payload/starter/_basic/comp/stepper.js +29 -3
- package/payload/starter/_basic/comp/toggle.js +2 -1
- package/payload/starter/_basic/docs/component-index.md +53 -53
- package/payload/starter/_basic/examples/code-editor.htm +48 -1
- package/payload/starter/_basic/examples/gallery-styled.htm +896 -0
- package/payload/starter/_basic/examples/search-input.htm +1 -1
- package/payload/starter/_basic/examples/select-box-2-item-view.htm +304 -0
- package/payload/starter/_basic/examples/{slider-2-classic.htm → slider-2-split.htm} +36 -2
- package/payload/starter/_basic/examples/stepper-2.htm +22 -6
- package/payload/starter/_basic/min/bottom-bar.min.js +1 -1
- package/payload/starter/_basic/min/code-editor.min.js +1 -1
- package/payload/starter/_basic/min/context-menu.min.js +1 -1
- package/payload/starter/_basic/min/markdown-view.min.js +1 -1
- package/payload/starter/_basic/min/slider.min.js +1 -1
- package/payload/starter/_basic/min/smart-button.min.js +1 -1
- package/payload/starter/_basic/min/smart-table.min.js +1 -1
- package/payload/starter/_basic/min/stepper.min.js +1 -1
- package/payload/starter/_basic/min/toggle.min.js +1 -1
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// Bismillah
|
|
2
|
+
//
|
|
3
|
+
// sizes.rs - Tauri Starter v0.1 (OzdenUI for Tauri)
|
|
4
|
+
// THE SECOND EXAMPLE: a long job in Rust that tells the page how it goes, with events.
|
|
5
|
+
// The page starts it (ui/sizes.js):
|
|
6
|
+
// window.__TAURI__.core.invoke("measure_folder", { path: "/Users/me", job: 1 })
|
|
7
|
+
// and listens to its events (job: the number the page gave, so it knows the events of its last job):
|
|
8
|
+
// "measure-progress" { job, files, bytes } at most every PROGRESS_INTERVAL
|
|
9
|
+
// "measure-folder" { job, path, files, bytes } a folder inside is measured (its row shows its size)
|
|
10
|
+
// "measure-done" { job, files, bytes, millis, stopped } the end
|
|
11
|
+
// invoke("stop_measure") stops it.
|
|
12
|
+
//
|
|
13
|
+
// Started Date: November 2026
|
|
14
|
+
// Webpage: https://bug7a.github.io/ozdenui-tauri-template/
|
|
15
|
+
//
|
|
16
|
+
// Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
17
|
+
// Licensed under the Apache License, Version 2.0
|
|
18
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
19
|
+
|
|
20
|
+
use serde::Serialize;
|
|
21
|
+
use std::fs;
|
|
22
|
+
use std::path::{Path, PathBuf};
|
|
23
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
24
|
+
use std::sync::{Arc, Mutex};
|
|
25
|
+
use std::time::{Duration, Instant};
|
|
26
|
+
use tauri::{AppHandle, Emitter, State};
|
|
27
|
+
|
|
28
|
+
// WHY: a big folder has a million files; an event for each would flood the page
|
|
29
|
+
const PROGRESS_INTERVAL: Duration = Duration::from_millis(100);
|
|
30
|
+
|
|
31
|
+
/// The running job: kept by Tauri (.manage() in main.rs), every command can read it
|
|
32
|
+
#[derive(Default)]
|
|
33
|
+
pub struct Measure {
|
|
34
|
+
stop_flag: Mutex<Option<Arc<AtomicBool>>>, // the stop flag of the running job
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// The files and the bytes counted
|
|
38
|
+
#[derive(Clone, Copy, Default)]
|
|
39
|
+
struct Total {
|
|
40
|
+
files: u64,
|
|
41
|
+
bytes: u64,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#[derive(Clone, Serialize)]
|
|
45
|
+
struct Progress {
|
|
46
|
+
job: u32,
|
|
47
|
+
files: u64,
|
|
48
|
+
bytes: u64,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#[derive(Clone, Serialize)]
|
|
52
|
+
struct FolderSize {
|
|
53
|
+
job: u32,
|
|
54
|
+
path: String,
|
|
55
|
+
files: u64,
|
|
56
|
+
bytes: u64,
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#[derive(Clone, Serialize)]
|
|
60
|
+
struct Done {
|
|
61
|
+
job: u32,
|
|
62
|
+
files: u64,
|
|
63
|
+
bytes: u64,
|
|
64
|
+
millis: f64,
|
|
65
|
+
stopped: bool,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// Starts to measure a folder and answers at once: the work goes on in a thread.
|
|
69
|
+
/// One job at a time: a job that is still running is stopped.
|
|
70
|
+
// WHY the page gives the job number: an event of a small folder can come before the answer of this command
|
|
71
|
+
#[tauri::command]
|
|
72
|
+
pub fn measure_folder(app: AppHandle, measure: State<Measure>, path: String, job: u32) {
|
|
73
|
+
let stop = Arc::new(AtomicBool::new(false));
|
|
74
|
+
|
|
75
|
+
let old_stop = measure.stop_flag.lock().unwrap().replace(stop.clone());
|
|
76
|
+
if let Some(old_stop) = old_stop {
|
|
77
|
+
old_stop.store(true, Ordering::SeqCst);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
std::thread::spawn(move || run_job(&app, job, &PathBuf::from(path), &stop));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// Stops the running job. Its "measure-done" event comes with stopped: true.
|
|
84
|
+
#[tauri::command]
|
|
85
|
+
pub fn stop_measure(measure: State<Measure>) {
|
|
86
|
+
if let Some(stop) = measure.stop_flag.lock().unwrap().as_ref() {
|
|
87
|
+
stop.store(true, Ordering::SeqCst);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn run_job(app: &AppHandle, job: u32, folder: &Path, stop: &AtomicBool) {
|
|
92
|
+
let started = Instant::now();
|
|
93
|
+
let mut total = Total::default();
|
|
94
|
+
let mut last_progress = Instant::now();
|
|
95
|
+
// WHY let _ =: an event can only fail when the window is closing; the job has nothing to do then
|
|
96
|
+
let mut report = |total: &Total| {
|
|
97
|
+
if last_progress.elapsed() < PROGRESS_INTERVAL {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
last_progress = Instant::now();
|
|
101
|
+
let progress = Progress {
|
|
102
|
+
job,
|
|
103
|
+
files: total.files,
|
|
104
|
+
bytes: total.bytes,
|
|
105
|
+
};
|
|
106
|
+
let _ = app.emit("measure-progress", progress);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// WHY one by one: the page shows the size of every folder inside as soon as it is known
|
|
110
|
+
for item in fs::read_dir(folder).into_iter().flatten().flatten() {
|
|
111
|
+
if stop.load(Ordering::Relaxed) {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
let Ok(kind) = item.file_type() else { continue };
|
|
115
|
+
if !kind.is_dir() {
|
|
116
|
+
add_file(&item, &mut total);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
let before = total;
|
|
120
|
+
add_folder(&item.path(), &mut total, stop, &mut report);
|
|
121
|
+
if stop.load(Ordering::Relaxed) {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
let folder_size = FolderSize {
|
|
125
|
+
job,
|
|
126
|
+
path: item.path().display().to_string(),
|
|
127
|
+
files: total.files - before.files,
|
|
128
|
+
bytes: total.bytes - before.bytes,
|
|
129
|
+
};
|
|
130
|
+
let _ = app.emit("measure-folder", folder_size);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
let done = Done {
|
|
134
|
+
job,
|
|
135
|
+
files: total.files,
|
|
136
|
+
bytes: total.bytes,
|
|
137
|
+
millis: started.elapsed().as_secs_f64() * 1000.0,
|
|
138
|
+
stopped: stop.load(Ordering::Relaxed),
|
|
139
|
+
};
|
|
140
|
+
let _ = app.emit("measure-done", done);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/// Adds the files of a folder and of every folder in it. A link is counted as a file, not followed.
|
|
144
|
+
/// A folder that can not be read is skipped (Ex: no permission).
|
|
145
|
+
fn add_folder(
|
|
146
|
+
folder: &Path,
|
|
147
|
+
total: &mut Total,
|
|
148
|
+
stop: &AtomicBool,
|
|
149
|
+
report: &mut impl FnMut(&Total),
|
|
150
|
+
) {
|
|
151
|
+
let mut folders = vec![folder.to_path_buf()];
|
|
152
|
+
while let Some(next) = folders.pop() {
|
|
153
|
+
if stop.load(Ordering::Relaxed) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
let Ok(items) = fs::read_dir(&next) else {
|
|
157
|
+
continue;
|
|
158
|
+
};
|
|
159
|
+
for item in items.flatten() {
|
|
160
|
+
match item.file_type() {
|
|
161
|
+
Ok(kind) if kind.is_dir() => folders.push(item.path()),
|
|
162
|
+
Ok(_) => add_file(&item, total),
|
|
163
|
+
Err(_) => {}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
report(total);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
fn add_file(item: &fs::DirEntry, total: &mut Total) {
|
|
171
|
+
// WHY DirEntry::metadata: it does not follow a link, so a link is not counted twice
|
|
172
|
+
if let Ok(metadata) = item.metadata() {
|
|
173
|
+
total.files += 1;
|
|
174
|
+
total.bytes += metadata.len();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
#[cfg(test)]
|
|
179
|
+
mod tests {
|
|
180
|
+
use super::*;
|
|
181
|
+
|
|
182
|
+
#[test]
|
|
183
|
+
fn counts_the_files_of_every_folder_inside() {
|
|
184
|
+
let folder =
|
|
185
|
+
std::env::temp_dir().join(format!("tauri-starter-sizes-{}", std::process::id()));
|
|
186
|
+
fs::create_dir_all(folder.join("a/b")).unwrap();
|
|
187
|
+
fs::write(folder.join("one.txt"), "12345").unwrap();
|
|
188
|
+
fs::write(folder.join("a/two.txt"), "123").unwrap();
|
|
189
|
+
fs::write(folder.join("a/b/three.txt"), "12").unwrap();
|
|
190
|
+
|
|
191
|
+
let mut total = Total::default();
|
|
192
|
+
let mut reports = 0;
|
|
193
|
+
add_folder(&folder, &mut total, &AtomicBool::new(false), &mut |_| {
|
|
194
|
+
reports += 1
|
|
195
|
+
});
|
|
196
|
+
assert_eq!(total.files, 3);
|
|
197
|
+
assert_eq!(total.bytes, 10);
|
|
198
|
+
assert_eq!(reports, 3); // one for each folder
|
|
199
|
+
|
|
200
|
+
fs::remove_dir_all(&folder).unwrap();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
#[test]
|
|
204
|
+
fn a_stopped_job_counts_nothing_more() {
|
|
205
|
+
let mut total = Total::default();
|
|
206
|
+
add_folder(
|
|
207
|
+
Path::new("/"),
|
|
208
|
+
&mut total,
|
|
209
|
+
&AtomicBool::new(true),
|
|
210
|
+
&mut |_| {},
|
|
211
|
+
);
|
|
212
|
+
assert_eq!(total.files, 0);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://schema.tauri.app/config/2",
|
|
3
|
+
"productName": "Tauri Starter",
|
|
4
|
+
"identifier": "io.github.bug7a.tauri-starter",
|
|
5
|
+
"build": {
|
|
6
|
+
"frontendDist": "../ui"
|
|
7
|
+
},
|
|
8
|
+
"app": {
|
|
9
|
+
"withGlobalTauri": true,
|
|
10
|
+
"windows": [
|
|
11
|
+
{
|
|
12
|
+
"title": "Tauri Starter",
|
|
13
|
+
"width": 960,
|
|
14
|
+
"height": 640,
|
|
15
|
+
"minWidth": 760,
|
|
16
|
+
"minHeight": 420,
|
|
17
|
+
"theme": "Light"
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
"security": {
|
|
21
|
+
"csp": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self' ipc: http://ipc.localhost"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"bundle": {
|
|
25
|
+
"active": true,
|
|
26
|
+
"targets": "all",
|
|
27
|
+
"publisher": "Bugra Ozden",
|
|
28
|
+
"copyright": "Copyright 2020-2026 Bugra Ozden",
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"homepage": "https://bug7a.github.io/ozdenui-tauri-template/",
|
|
31
|
+
"category": "Utility",
|
|
32
|
+
"shortDescription": "A desktop app with a Rust side and a basic.js interface",
|
|
33
|
+
"macOS": {
|
|
34
|
+
"signingIdentity": "-"
|
|
35
|
+
},
|
|
36
|
+
"icon": [
|
|
37
|
+
"icons/32x32.png",
|
|
38
|
+
"icons/128x128.png",
|
|
39
|
+
"icons/128x128@2x.png",
|
|
40
|
+
"icons/icon.icns",
|
|
41
|
+
"icons/icon.ico"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// Bismillah
|
|
2
|
+
//
|
|
3
|
+
// project.rs - Tauri Starter v0.1 (OzdenUI for Tauri)
|
|
4
|
+
// THE TESTS OF THE PROJECT: the page (ui/*.js) and the Rust side (src/) are written in two languages, so the compiler
|
|
5
|
+
// can not see a name that is in one of them only. These tests read the files and tell it before the app runs:
|
|
6
|
+
// a command the page calls that Rust does not have, a command that is not in generate_handler!, a plugin without
|
|
7
|
+
// its permission, a script written into a page of ui/ or loaded from the web. Run them in src-tauri/: cargo test
|
|
8
|
+
//
|
|
9
|
+
// Started Date: November 2026
|
|
10
|
+
// Webpage: https://bug7a.github.io/ozdenui-tauri-template/
|
|
11
|
+
//
|
|
12
|
+
// Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
13
|
+
// Licensed under the Apache License, Version 2.0
|
|
14
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
15
|
+
|
|
16
|
+
use std::fs;
|
|
17
|
+
use std::path::{Path, PathBuf};
|
|
18
|
+
|
|
19
|
+
// The parts of window.__TAURI__ that Tauri itself brings; every other one is a plugin
|
|
20
|
+
const CORE_PARTS: [&str; 12] = [
|
|
21
|
+
"app",
|
|
22
|
+
"core",
|
|
23
|
+
"dpi",
|
|
24
|
+
"event",
|
|
25
|
+
"image",
|
|
26
|
+
"menu",
|
|
27
|
+
"mocks",
|
|
28
|
+
"path",
|
|
29
|
+
"tray",
|
|
30
|
+
"webview",
|
|
31
|
+
"webviewWindow",
|
|
32
|
+
"window",
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
fn project_folder() -> PathBuf {
|
|
36
|
+
Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// The texts of the files of a folder that end with the extension (not of the folders inside it)
|
|
40
|
+
fn texts_of(folder: &Path, extension: &str) -> Vec<String> {
|
|
41
|
+
let mut paths: Vec<_> = fs::read_dir(folder)
|
|
42
|
+
.unwrap()
|
|
43
|
+
.flatten()
|
|
44
|
+
.map(|item| item.path())
|
|
45
|
+
.collect();
|
|
46
|
+
// WHY: read_dir has no order (Linux gives another one than macOS): the same text on every system
|
|
47
|
+
paths.sort();
|
|
48
|
+
let mut texts = Vec::new();
|
|
49
|
+
for path in paths {
|
|
50
|
+
let is_wanted = path.extension().is_some_and(|found| found == extension);
|
|
51
|
+
if path.is_file() && is_wanted {
|
|
52
|
+
texts.push(fs::read_to_string(&path).unwrap());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
texts
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// The code of the page: ui/*.js (the library and the components are in folders of ui/, so they are not read)
|
|
59
|
+
fn page_code() -> String {
|
|
60
|
+
without_comments(&texts_of(&project_folder().join("../ui"), "js").join("\n"))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// WHY: a comment may show a command that is not written yet (the "A NEW COMMAND" note of ui/backend.js)
|
|
64
|
+
fn without_comments(code: &str) -> String {
|
|
65
|
+
let mut kept = String::new();
|
|
66
|
+
let mut is_in_block = false;
|
|
67
|
+
for line in code.lines() {
|
|
68
|
+
let trimmed = line.trim_start();
|
|
69
|
+
if is_in_block {
|
|
70
|
+
is_in_block = !trimmed.contains("*/");
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if trimmed.starts_with("/*") {
|
|
74
|
+
is_in_block = !trimmed.contains("*/");
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if trimmed.starts_with("//") {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
kept.push_str(line);
|
|
81
|
+
kept.push('\n');
|
|
82
|
+
}
|
|
83
|
+
kept
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// WHY without comments: "write it with #[tauri::command]" in a comment would take the next fn as a command
|
|
87
|
+
fn rust_code() -> String {
|
|
88
|
+
without_comments(&texts_of(&project_folder().join("src"), "rs").join("\n"))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// The words that come right after a text: words_after("a.b(\"x\") a.b(\"y\")", "a.b(\"") gives ["x", "y"]
|
|
92
|
+
fn words_after(text: &str, before: &str) -> Vec<String> {
|
|
93
|
+
let mut words = Vec::new();
|
|
94
|
+
for part in text.split(before).skip(1) {
|
|
95
|
+
let word: String = part
|
|
96
|
+
.chars()
|
|
97
|
+
.take_while(|letter| letter.is_alphanumeric() || *letter == '_')
|
|
98
|
+
.collect();
|
|
99
|
+
if !word.is_empty() && !words.contains(&word) {
|
|
100
|
+
words.push(word);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
words
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// The commands of tauri::generate_handler![files::list_folder, ...]: ["list_folder", ...]
|
|
107
|
+
fn commands_of_the_app() -> Vec<String> {
|
|
108
|
+
let code = rust_code();
|
|
109
|
+
let list = code
|
|
110
|
+
.split("generate_handler![")
|
|
111
|
+
.nth(1)
|
|
112
|
+
.and_then(|after| after.split(']').next())
|
|
113
|
+
.expect("src/main.rs has no tauri::generate_handler![...]");
|
|
114
|
+
list.split(',')
|
|
115
|
+
.map(|command| command.trim().rsplit("::").next().unwrap().to_string())
|
|
116
|
+
.filter(|command| !command.is_empty())
|
|
117
|
+
.collect()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// The functions written under #[tauri::command]
|
|
121
|
+
fn commands_of_rust() -> Vec<String> {
|
|
122
|
+
let code = rust_code();
|
|
123
|
+
let mut commands = Vec::new();
|
|
124
|
+
for part in code.split("#[tauri::command").skip(1) {
|
|
125
|
+
let name = part.split("fn ").nth(1).map(|after| {
|
|
126
|
+
after
|
|
127
|
+
.chars()
|
|
128
|
+
.take_while(|letter| letter.is_alphanumeric() || *letter == '_')
|
|
129
|
+
.collect::<String>()
|
|
130
|
+
});
|
|
131
|
+
commands.extend(name);
|
|
132
|
+
}
|
|
133
|
+
commands
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// The opening <script ...> tags of the pages of ui/ (ui/*.html); a tag in an HTML comment is left out
|
|
137
|
+
fn script_tags() -> Vec<String> {
|
|
138
|
+
let mut tags = Vec::new();
|
|
139
|
+
for page in texts_of(&project_folder().join("../ui"), "html") {
|
|
140
|
+
let mut without_comments = String::new();
|
|
141
|
+
for (index, part) in page.split("<!--").enumerate() {
|
|
142
|
+
let after_comment = if index == 0 {
|
|
143
|
+
part
|
|
144
|
+
} else {
|
|
145
|
+
part.split_once("-->").map_or("", |(_, after)| after)
|
|
146
|
+
};
|
|
147
|
+
without_comments.push_str(after_comment);
|
|
148
|
+
}
|
|
149
|
+
for part in without_comments.split("<script").skip(1) {
|
|
150
|
+
tags.push(format!("<script{}>", part.split('>').next().unwrap_or("")));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
tags
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// "clipboardManager" (the name in the page) is "clipboard-manager" in Cargo.toml and in the permissions
|
|
157
|
+
fn kebab_case(name: &str) -> String {
|
|
158
|
+
let mut kebab = String::new();
|
|
159
|
+
for letter in name.chars() {
|
|
160
|
+
if letter.is_uppercase() {
|
|
161
|
+
kebab.push('-');
|
|
162
|
+
}
|
|
163
|
+
kebab.extend(letter.to_lowercase());
|
|
164
|
+
}
|
|
165
|
+
kebab
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
#[test]
|
|
169
|
+
fn every_command_the_page_calls_is_a_command_of_the_app() {
|
|
170
|
+
let page = page_code();
|
|
171
|
+
let mut called = words_after(&page, "invoke(\"");
|
|
172
|
+
called.extend(words_after(&page, "invoke('"));
|
|
173
|
+
let registered = commands_of_the_app();
|
|
174
|
+
|
|
175
|
+
assert!(
|
|
176
|
+
!called.is_empty(),
|
|
177
|
+
"no invoke(\"...\") was found in ui/*.js"
|
|
178
|
+
);
|
|
179
|
+
for command in called {
|
|
180
|
+
assert!(
|
|
181
|
+
registered.contains(&command),
|
|
182
|
+
"the page calls invoke(\"{command}\"), but generate_handler! of src/main.rs has no {command}"
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#[test]
|
|
188
|
+
fn every_command_of_rust_is_in_generate_handler() {
|
|
189
|
+
let registered = commands_of_the_app();
|
|
190
|
+
|
|
191
|
+
for command in commands_of_rust() {
|
|
192
|
+
assert!(
|
|
193
|
+
registered.contains(&command),
|
|
194
|
+
"{command} is written with #[tauri::command], but it is not in generate_handler! of src/main.rs: \
|
|
195
|
+
the page can not call it"
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#[test]
|
|
201
|
+
fn every_plugin_the_page_uses_is_added_and_has_its_permission() {
|
|
202
|
+
let cargo_toml = fs::read_to_string(project_folder().join("Cargo.toml")).unwrap();
|
|
203
|
+
let capabilities = texts_of(&project_folder().join("capabilities"), "json").join("\n");
|
|
204
|
+
|
|
205
|
+
for part in words_after(&page_code(), "__TAURI__.") {
|
|
206
|
+
if CORE_PARTS.contains(&part.as_str()) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
let plugin = kebab_case(&part);
|
|
210
|
+
assert!(
|
|
211
|
+
cargo_toml.contains(&format!("tauri-plugin-{plugin}")),
|
|
212
|
+
"the page uses window.__TAURI__.{part}, but Cargo.toml has no tauri-plugin-{plugin}: \
|
|
213
|
+
run \"cargo tauri add {plugin}\" in src-tauri/"
|
|
214
|
+
);
|
|
215
|
+
assert!(
|
|
216
|
+
capabilities.contains(&format!("\"{plugin}:")),
|
|
217
|
+
"the page uses window.__TAURI__.{part}, but capabilities/ gives no \"{plugin}:...\" permission"
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// WHY a test: the security policy does not stop these two. When the app is compiled, Tauri adds the hash of a script
|
|
223
|
+
// written into the page and a nonce of a <script src="https://..."> to the policy, so both run in the window.
|
|
224
|
+
#[test]
|
|
225
|
+
fn the_pages_load_scripts_only_from_the_files_of_the_app() {
|
|
226
|
+
for tag in script_tags() {
|
|
227
|
+
let Some(source) = tag.split("src=").nth(1) else {
|
|
228
|
+
panic!(
|
|
229
|
+
"{tag} in ui/*.html: a script written into the page; put its code into a .js file of ui/ and \
|
|
230
|
+
load it with <script src=\"...\">"
|
|
231
|
+
);
|
|
232
|
+
};
|
|
233
|
+
let address = source.trim_start_matches(['"', '\'']).to_lowercase();
|
|
234
|
+
let is_from_the_web = address.starts_with("http:")
|
|
235
|
+
|| address.starts_with("https:")
|
|
236
|
+
|| address.starts_with("//");
|
|
237
|
+
assert!(
|
|
238
|
+
!is_from_the_web,
|
|
239
|
+
"{tag} in ui/*.html: a script from the web would run in the app with all that the page may do; \
|
|
240
|
+
save a copy of it into ui/ and load that"
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|