diff --git a/Cargo.lock b/Cargo.lock index 7f43131..1fd69ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1156,6 +1156,7 @@ dependencies = [ "markdown", "rfd", "serde", + "tempfile", "thiserror 2.0.16", "tinyrand", "tokio", @@ -3128,9 +3129,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.21.0" +version = "3.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15b61f8f20e3a6f7e0649d825294eaf317edce30f82cf6026e7e4cb9222a7d1e" +checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16" dependencies = [ "fastrand", "getrandom", diff --git a/Cargo.toml b/Cargo.toml index 7e11b65..4775d8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ axum = "0.8.4" tokio = { version = "1.47.1", features = ["full"] } tinyrand = "0.5.0" clipboard-rs = "0.3.0" +tempfile = "3.23.0" [package.metadata.bundle] identifier = "org.geekfu.fleen" diff --git a/src/fleen_app.rs b/src/fleen_app.rs index 8cfc3ce..dd342c2 100644 --- a/src/fleen_app.rs +++ b/src/fleen_app.rs @@ -32,7 +32,11 @@ pub enum FleenError { #[error("Target dir is invalid (can't contain the app dir or vice versa)")] TargetDir, #[error("IO error: {0}")] - Io(#[from] io::Error) + Io(#[from] io::Error), + #[error("Deploy script missing! Create _scripts/deploy.sh")] + ScriptMissing, + #[error("Deploy script error:\n\n{0}")] + DeployError(String) } #[derive(Clone, Debug)] @@ -279,6 +283,32 @@ impl FleenApp { } Ok(()) } + + pub fn build_and_deploy(&self) -> Result { + let output_dir = tempfile::tempdir().map_err(|_| TargetDir)?; + self.build_site(&output_dir.path())?; // Attempt to build the site somewhere + + let deploy_script_path = self.root.join("_scripts/deploy.sh"); + if !deploy_script_path.exists() { + Err(FleenError::ScriptMissing) + } else { + let mut command = Command::new(deploy_script_path); + command.current_dir(output_dir.path()); // don't consume dir! + match command.output() { + Ok(output) => { + let output = String::from_utf8(output.stdout).unwrap_or("Error reading deploy script output".to_string()); + if command.status().unwrap().success() { + Ok(output) + } else { + Err(FleenError::DeployError(output)) + } + }, + Err(e) => { + Err(FleenError::DeployError(e.to_string())) + } + } + } + } } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index 4c06def..c2b2da3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,7 @@ struct TempMessage { struct FleenUi { app: Option, error: Option, + message: Option, selected_file: Option, dialog_mode: Option, server_handle: Option>, @@ -51,6 +52,7 @@ impl Default for FleenUi { Self { app: None, error: None, + message: None, selected_file: None, dialog_mode: None, server_handle: None, @@ -103,11 +105,17 @@ impl FleenUi { }); ui.column(width, |ui| self.server_controls(ui)); ui.column(width, |ui| { - if ui.add_fill_width(Button::green("Build site...")).clicked() && + if ui.add_fill_width(Button::green("Build and deploy")).clicked() { + match self.app.as_ref().unwrap().build_and_deploy() { + Ok(output) => { self.message = Some(format!("Site built and deployed:\n\n{}", output)) } + Err(err) => { self.error = Some(err) } + } + } + + if ui.add_fill_width(Button::blue("Build site...")).clicked() && let Some(path) = rfd::FileDialog::new().pick_folder() { match self.app.as_ref().unwrap().build_site(&path) { - // TODO some kind of temporary notification thing - Ok(()) => { println!("Yay!") } + Ok(()) => { self.message = Some("Site built successfully".to_string()) } Err(err) => { self.error = Some(err) } } } @@ -358,6 +366,16 @@ impl eframe::App for FleenUi { }); } + if let Some(message) = &self.message { + let message = message.clone(); + egui::Window::new("FYI").collapsible(false).resizable(false).show(ctx, |ui| { + ui.label(message); + if ui.button("Thanks!").clicked() { + self.message = None + } + }); + } + if let Some(TempMessage { created, .. }) = &self.image_message && *created < Instant::now() - Duration::from_secs(2) { self.image_message = None diff --git a/templates/deploy.sh b/templates/deploy.sh new file mode 100644 index 0000000..c0f87cf --- /dev/null +++ b/templates/deploy.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Make this be a script that will deploy your site. Assume it'll be run from the +# path of the built site, that the build succeeded, and that anything written to +# stdout will be displayed in a dialog. + +# Example: +# rsync -r . root@example.com:/var/www/html 2>&1 && echo "Site deployed!" + +echo "Edit _scripts/deploy.sh to set up deploy script" \ No newline at end of file