Adds tauri build script (#68)

* tauri build script

* missed file
This commit is contained in:
Wyatt Verchere 2023-04-05 12:39:40 -07:00 committed by GitHub
parent b4a809362b
commit a3e377666e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 127 additions and 17 deletions

1
Cargo.lock generated
View File

@ -3574,6 +3574,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"daedalus", "daedalus",
"futures", "futures",
"regex",
"serde", "serde",
"serde_json", "serde_json",
"tauri", "tauri",

View File

@ -6,11 +6,13 @@ authors = ["you"]
license = "" license = ""
repository = "" repository = ""
edition = "2021" edition = "2021"
build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies] [build-dependencies]
tauri-build = { version = "1.2", features = [] } tauri-build = { version = "1.2", features = [] }
regex = "1.5"
[dependencies] [dependencies]
theseus = { path = "../../theseus" } theseus = { path = "../../theseus" }

View File

@ -1,3 +1,111 @@
use std::fs;
use regex::Regex;
fn main() { fn main() {
tauri_build::build() // Build the Tauri app
tauri_build::build();
// Check that all JavaScript 'invoke' Tauri functions have a corresponding tagged Rust function
// This is to prevent the app from crashing if a JavaScript function is invoked but the corresponding Rust function is not tagged
// This only allows simple functions, but functions in theseus_gui should be kept simple
check_invoke_sanity();
}
fn check_invoke_sanity() {
let js_files = read_js_files("../src/helpers");
let rust_files = read_rust_files("src");
let js_function_names = extract_js_function_names(&js_files);
let rust_function_names = extract_rust_function_names(&rust_files);
let mut missing_functions = Vec::new();
for js_fn_name in js_function_names {
if !rust_function_names.contains(&js_fn_name) {
missing_functions.push(js_fn_name);
}
}
if !missing_functions.is_empty() {
panic!(
"The following invoked Tauri functions do not have corresponding Rust functions with #[tauri::command] attribute :\n{}",
missing_functions.join("\n")
);
}
}
fn read_js_files(directory: &str) -> Vec<String> {
let mut files = Vec::new();
read_files_recursively(directory, "js", &mut files);
files
}
fn read_rust_files(directory: &str) -> Vec<String> {
let mut files = Vec::new();
read_files_recursively(directory, "rs", &mut files);
files
}
// Recursive in case we make the helpers directory more complex
fn read_files_recursively(
directory: &str,
extension: &str,
files: &mut Vec<String>,
) {
for entry in fs::read_dir(directory).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
read_files_recursively(&path.to_string_lossy(), extension, files);
} else if path.extension().map_or(false, |ext| ext == extension) {
let content = fs::read_to_string(path).unwrap();
files.push(content);
}
}
}
fn extract_rust_function_names(rust_files: &[String]) -> Vec<String> {
// Matches #[tauri::command] attribute
let re_tauri_command = Regex::new(r"(?m)#\[tauri::command\]").unwrap();
// Matches function name following the #[tauri::command] attribute
// Matches up to the first (, to allow for function arguments and comments in that area
let re_function_name =
Regex::new(r"fn\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(").unwrap();
let mut function_names = Vec::new();
for file in rust_files {
let mut start = 0;
while let Some(command_match) = re_tauri_command.find_at(file, start) {
if let Some(function_name_cap) =
re_function_name.captures(&file[command_match.end()..])
{
function_names.push(function_name_cap[1].to_string());
start = command_match.start() + 1;
} else {
break;
}
}
}
function_names
}
fn extract_js_function_names(js_files: &[String]) -> Vec<String> {
// Matches functions of the form: invoke('function_name', { ... }) (or invoke('function_name') )
let re_invoke = Regex::new(
r"(?m)invoke\(\s*'([a-zA-Z_][a-zA-Z0-9_]*)'\s*(?:,\s*\{.*?\})?\s*\)",
)
.unwrap();
let mut function_names = Vec::new();
for file in js_files {
let mut start = 0;
while let Some(invoke_match) = re_invoke.find_at(file, start) {
if let Some(captures) = re_invoke.captures(invoke_match.as_str()) {
function_names.push(captures[1].to_string());
}
start = invoke_match.start() + 1;
}
}
function_names
} }

View File

@ -5,38 +5,37 @@ use theseus::tags::{
/// Gets cached category tags from the database /// Gets cached category tags from the database
#[tauri::command] #[tauri::command]
pub async fn tags_get_category_tags() -> Result<Vec<Category>> { pub async fn tags_get_categories() -> Result<Vec<Category>> {
Ok(theseus::tags::get_category_tags().await?) Ok(theseus::tags::get_category_tags().await?)
} }
/// Gets cached report type tags from the database /// Gets cached report type tags from the database
#[tauri::command] #[tauri::command]
pub async fn tags_get_report_type_tags() -> Result<Vec<String>> { pub async fn tags_get_report_types() -> Result<Vec<String>> {
Ok(theseus::tags::get_report_type_tags().await?) Ok(theseus::tags::get_report_type_tags().await?)
} }
/// Gets cached loader tags from the database /// Gets cached loader tags from the database
#[tauri::command] #[tauri::command]
pub async fn tags_get_loader_tags() -> Result<Vec<Loader>> { pub async fn tags_get_loaders() -> Result<Vec<Loader>> {
Ok(theseus::tags::get_loader_tags().await?) Ok(theseus::tags::get_loader_tags().await?)
} }
/// Gets cached game version tags from the database /// Gets cached game version tags from the database
#[tauri::command] #[tauri::command]
pub async fn tags_get_game_version_tags() -> Result<Vec<GameVersion>> { pub async fn tags_get_game_versions() -> Result<Vec<GameVersion>> {
Ok(theseus::tags::get_game_version_tags().await?) Ok(theseus::tags::get_game_version_tags().await?)
} }
/// Gets cached license tags from the database /// Gets cached license tags from the database
#[tauri::command] #[tauri::command]
pub async fn tags_get_license_tags() -> Result<Vec<License>> { pub async fn tags_get_licenses() -> Result<Vec<License>> {
Ok(theseus::tags::get_license_tags().await?) Ok(theseus::tags::get_license_tags().await?)
} }
/// Gets cached donation platform tags from the database /// Gets cached donation platform tags from the database
#[tauri::command] #[tauri::command]
pub async fn tags_get_donation_platform_tags() -> Result<Vec<DonationPlatform>> pub async fn tags_get_donation_platforms() -> Result<Vec<DonationPlatform>> {
{
Ok(theseus::tags::get_donation_platform_tags().await?) Ok(theseus::tags::get_donation_platform_tags().await?)
} }

View File

@ -36,12 +36,12 @@ fn main() {
api::auth::auth_has_user, api::auth::auth_has_user,
api::auth::auth_users, api::auth::auth_users,
api::auth::auth_get_user, api::auth::auth_get_user,
api::tags::tags_get_category_tags, api::tags::tags_get_categories,
api::tags::tags_get_donation_platform_tags, api::tags::tags_get_donation_platforms,
api::tags::tags_get_game_version_tags, api::tags::tags_get_game_versions,
api::tags::tags_get_loader_tags, api::tags::tags_get_loaders,
api::tags::tags_get_license_tags, api::tags::tags_get_licenses,
api::tags::tags_get_report_type_tags, api::tags::tags_get_report_types,
api::tags::tags_get_tag_bundle, api::tags::tags_get_tag_bundle,
api::settings::settings_get, api::settings::settings_get,
api::settings::settings_set, api::settings::settings_set,

View File

@ -12,17 +12,17 @@ export async function get_tag_bundle() {
// Gets cached category tags // Gets cached category tags
export async function get_categories() { export async function get_categories() {
return await invoke('tags_get_category_tags') return await invoke('tags_get_categories')
} }
// Gets cached loaders tags // Gets cached loaders tags
export async function get_loaders() { export async function get_loaders() {
return await invoke('tags_get_loader_tags') return await invoke('tags_get_loaders')
} }
// Gets cached game_versions tags // Gets cached game_versions tags
export async function get_game_versions() { export async function get_game_versions() {
return await invoke('tags_get_game_version_tags') return await invoke('tags_get_game_versions')
} }
// Gets cached licenses tags // Gets cached licenses tags