Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tauri build script #68

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions theseus_gui/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ authors = ["you"]
license = ""
repository = ""
edition = "2021"
build = "build.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tauri-build = { version = "1.2", features = [] }
regex = "1.5"

[dependencies]
theseus = { path = "../../theseus" }
Expand Down
110 changes: 109 additions & 1 deletion theseus_gui/src-tauri/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,111 @@
use std::fs;

use regex::Regex;

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
}
2 changes: 1 addition & 1 deletion theseus_gui/src-tauri/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use thiserror::Error;
pub mod auth;
pub mod profile;
pub mod profile_create;
pub mod tags;
pub mod settings;
pub mod tags;

pub type Result<T> = std::result::Result<T, TheseusGuiError>;

Expand Down
13 changes: 6 additions & 7 deletions theseus_gui/src-tauri/src/api/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,37 @@ use theseus::tags::{

/// Gets cached category tags from the database
#[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?)
}

/// Gets cached report type tags from the database
#[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?)
}

/// Gets cached loader tags from the database
#[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?)
}

/// Gets cached game version tags from the database
#[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?)
}

/// Gets cached license tags from the database
#[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?)
}

/// Gets cached donation platform tags from the database
#[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?)
}

Expand Down
12 changes: 6 additions & 6 deletions theseus_gui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ fn main() {
api::auth::auth_has_user,
api::auth::auth_users,
api::auth::auth_get_user,
api::tags::tags_get_category_tags,
api::tags::tags_get_donation_platform_tags,
api::tags::tags_get_game_version_tags,
api::tags::tags_get_loader_tags,
api::tags::tags_get_license_tags,
api::tags::tags_get_report_type_tags,
api::tags::tags_get_categories,
api::tags::tags_get_donation_platforms,
api::tags::tags_get_game_versions,
api::tags::tags_get_loaders,
api::tags::tags_get_licenses,
api::tags::tags_get_report_types,
api::tags::tags_get_tag_bundle,
api::settings::settings_get,
api::settings::settings_set,
Expand Down
6 changes: 3 additions & 3 deletions theseus_gui/src/helpers/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export async function get_tag_bundle() {

// Gets cached category tags
export async function get_categories() {
return await invoke('tags_get_category_tags')
return await invoke('tags_get_categories')
}

// Gets cached loaders tags
export async function get_loaders() {
return await invoke('tags_get_loader_tags')
return await invoke('tags_get_loaders')
}

// Gets cached game_versions tags
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
Expand Down