From ee2936dfc49ca25823d354959f404cb763de551a Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 24 Feb 2022 21:12:50 -0500 Subject: [PATCH] improved git commit fetching --- Cargo.toml | 1 + build.sh | 10 +--------- src/egui_app.rs | 34 +++++++++++++++++++++------------- src/lib.rs | 4 ++-- src/main.rs | 2 +- www/index.html | 4 +--- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 49346bf..e45a450 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ meval = { git = "https://github.com/Titaniumtown/meval-rs.git" } egui = "0.17.0" eframe = "0.17.0" instant = { version = "0.1.12", features = ["wasm-bindgen"] } +git-version = "0.3.5" [target.'cfg(target_arch = "wasm32")'.dependencies] console_error_panic_hook = "0.1.7" diff --git a/build.sh b/build.sh index 5716e8d..0536fe4 100755 --- a/build.sh +++ b/build.sh @@ -10,12 +10,4 @@ mkdir tmp tmp/pkg cp -r pkg/integral_site_bg.wasm pkg/integral_site.js tmp/pkg/ cp www/index.html www/style.css tmp/ -sed -i 's/\.\.\/pkg/\.\/pkg/g' tmp/index.html - -git ls-files --exclude-standard --others . >/dev/null 2>&1; ec=$? -if test "$ec" = 0; then - echo some untracked files -elif test "$ec" = 1; then - commit=$(git rev-parse HEAD) - sed -i "s/let commit = ''/let commit = '${commit}'/g" tmp/index.html -fi +sed -i 's/\.\.\/pkg/\.\/pkg/g' tmp/index.html \ No newline at end of file diff --git a/src/egui_app.rs b/src/egui_app.rs index a4b9417..f45a6f7 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -5,7 +5,11 @@ use crate::misc::{digits_precision, test_func, Cache}; use eframe::{egui, epi}; use egui::plot::{Line, Plot, Value, Values}; use egui::widgets::plot::{Bar, BarChart}; -use egui::{Color32, TextStyle, Vec2, Layout}; +use egui::Color32; +use git_version::git_version; + +// Grabs git version on compile time +const GIT_VERSION: &str = git_version!(); pub struct MathApp { func_str: String, @@ -16,11 +20,10 @@ pub struct MathApp { chart_manager: ChartManager, back_cache: Cache>, front_cache: Cache<(Vec, f64)>, - commit: String } -impl MathApp { - pub fn new(commit: String) -> Self { +impl Default for MathApp { + fn default() -> Self { let def_func = "x^2".to_string(); let def_min_x = -10.0; let def_max_x = 10.0; @@ -35,11 +38,12 @@ impl MathApp { resolution: def_resolution, chart_manager: ChartManager::new(def_func, def_min_x, def_max_x, def_interval, def_resolution), back_cache: Cache::new_empty(), - front_cache: Cache::new_empty(), - commit + front_cache: Cache::new_empty() } } +} +impl MathApp { #[inline] fn get_back(&mut self) -> Line { let data = if self.back_cache.is_valid() { @@ -101,8 +105,7 @@ impl epi::App for MathApp { resolution, chart_manager, back_cache, - front_cache, - commit, + front_cache } = self; // Note: This Instant implementation does not show microseconds when using wasm. @@ -155,14 +158,18 @@ impl epi::App for MathApp { "I'm Opensource! (and licensed under AGPLv3)", "https://github.com/Titaniumtown/integral_site", ); + + // Displays commit info ui.horizontal(|ui| { ui.with_layout(egui::Layout::top_down_justified(egui::Align::Min), |ui| { - if commit.is_empty() { - ui.label(format!("Current build is untracked!")); - } else { ui.label("Commit: "); - ui.hyperlink_to(commit.clone(), format!("https://github.com/Titaniumtown/integral_site/commit/{}", commit)); - } + + // Only include hyperlink if the build doesn't have untracked files + if !GIT_VERSION.contains("-modified") { + ui.hyperlink_to(GIT_VERSION, format!("https://github.com/Titaniumtown/integral_site/commit/{}", GIT_VERSION)); + } else { + ui.label(GIT_VERSION); + } }); }); }); @@ -170,6 +177,7 @@ impl epi::App for MathApp { if parse_error.is_empty() { let do_update = chart_manager.update(func_str.clone(), *min_x, *max_x, *num_interval, *resolution); + // Invalidates caches according to what settings were changed match do_update { UpdateType::Full => { back_cache.invalidate(); diff --git a/src/lib.rs b/src/lib.rs index 52a748e..c876304 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ extern "C" { #[cfg(target_arch = "wasm32")] #[wasm_bindgen] -pub fn start(canvas_id: &str, commit: &str) -> Result<(), wasm_bindgen::JsValue> { +pub fn start(canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> { log("Initializing..."); // See performance in browser profiler! @@ -39,6 +39,6 @@ pub fn start(canvas_id: &str, commit: &str) -> Result<(), wasm_bindgen::JsValue> log("Finished initializing!"); log("Starting App..."); - let app = egui_app::MathApp::new(commit.to_string()); + let app = egui_app::MathApp::default(); eframe::start_web(canvas_id, Box::new(app)) } diff --git a/src/main.rs b/src/main.rs index ce84c56..96df7d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ mod misc; // For running the program natively! (Because why not?) #[cfg(not(target_arch = "wasm32"))] fn main() { - let app = egui_app::MathApp::new("".to_string()); + let app = egui_app::MathApp::default(); let options = eframe::NativeOptions { // Let's show off that we support transparent windows transparent: true, diff --git a/www/index.html b/www/index.html index 3e30a60..d9d7daa 100644 --- a/www/index.html +++ b/www/index.html @@ -18,9 +18,7 @@ async function run() { await init(); - - let commit = ''; - start("canvas", commit); + start("canvas"); } run();