From 3200bd5a93f6390ec84cb1751684df1e5c447316 Mon Sep 17 00:00:00 2001 From: nanoqsh Date: Tue, 31 May 2022 03:11:25 +0600 Subject: [PATCH] Update builder --- README.md | 23 ++++++- http/.gitignore | 1 - src/info.rs | 4 +- src/main.rs | 99 ++++++++++++++++++++++++++++++- {http => web}/static/favicon.svg | 0 web/{ => static}/images/1.jpg | Bin web/{ => static}/index.html | 0 web/{ => static}/style.css | 0 8 files changed, 119 insertions(+), 8 deletions(-) delete mode 100644 http/.gitignore rename {http => web}/static/favicon.svg (100%) rename web/{ => static}/images/1.jpg (100%) rename web/{ => static}/index.html (100%) rename web/{ => static}/style.css (100%) diff --git a/README.md b/README.md index ebcd487..b289d43 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,21 @@ -# voki -The chat app written in Rust +# Voki +Chat app written in Rust + +## Build +To build entire application make: +``` +cargo run +``` + +To build application with `release` profile make: +``` +cargo run release +``` + +That runs a build script which creates `dock` directory with a ready to start `Dockerfile`. Build a container with: +``` +docker build -t voki ./dock +``` + +## Run +todo!() diff --git a/http/.gitignore b/http/.gitignore deleted file mode 100644 index 7b4d4ba..0000000 --- a/http/.gitignore +++ /dev/null @@ -1 +0,0 @@ -static diff --git a/src/info.rs b/src/info.rs index 5f6064c..3e467c2 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,5 +1,5 @@ macro_rules! info { - ($l:expr, $e:expr) => { + ($l:expr, $e:expr) => {{ use termion::{color, style}; println!( @@ -11,7 +11,7 @@ macro_rules! info { style::Reset, $e ); - }; + }}; } pub(crate) use info; diff --git a/src/main.rs b/src/main.rs index adc8e20..f12d070 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,78 @@ mod info; mod tools; use self::{info::*, tools::*}; -use std::{env, process::Command}; +use std::{ + env, fs, io, + path::Path, + process::{Command, ExitCode}, +}; -fn main() { +fn update(from: &str, to: &str) -> io::Result<()> { + fn update_dir(from: &Path, to: &Path) -> io::Result<()> { + loop { + match fs::metadata(to) { + Ok(meta) if !meta.file_type().is_dir() => { + fs::remove_file(to)?; + } + Ok(_) => break, + Err(_) => break fs::create_dir_all(to)?, + } + } + + for entry in fs::read_dir(from)? { + let entry = entry?; + let file_type = entry.file_type()?; + let path = entry.path(); + + if file_type.is_dir() { + let name = path.file_name().expect("is a dir"); + update_dir(&path, &to.join(name))?; + } else if file_type.is_file() { + let name = path.file_name().expect("is a file"); + let to = to.join(name); + if let Err(_) = update_file(&path, &to) { + println!("error: from {path:?} to {to:?}"); + } + } + } + + Ok(()) + } + + struct FileNotFound; + + fn update_file(from: &Path, to: &Path) -> Result<(), FileNotFound> { + let from_last = fs::metadata(from) + .map(|meta| meta.modified().expect("should support")) + .map_err(|_| FileNotFound)?; + + fs::metadata(to) + .map(|meta| meta.modified().expect("should support")) + .ok() + .map(|to_last| to_last < from_last) + .unwrap_or(true) + .then(|| fs::copy(from, to)) + .transpose() + .map_err(|_| FileNotFound)?; + + Ok(()) + } + + let from = from.as_ref(); + let to = to.as_ref(); + + if fs::metadata(from)?.file_type().is_file() { + if let Err(_) = update_file(from, to) { + println!("error: from {from:?} to {to:?}"); + } + + Ok(()) + } else { + update_dir(from, to) + } +} + +fn main() -> ExitCode { let mut release = false; for arg in env::args() { if arg == "release" { @@ -41,7 +110,31 @@ fn main() { if !status.success() { error!("building failed"); - break; + return ExitCode::FAILURE; + } + } + + let sub = release.then(|| "release").unwrap_or("debug"); + let server_target_dir = format!("./server/target/{sub}/server"); + let http_target_dir = format!("./http/target/{sub}/http"); + let dirs = [ + ("./web/static", "./dock/voki/static"), + ("./web/pkg", "./dock/voki/static/pkg"), + (&server_target_dir, "./dock/voki/server"), + (&http_target_dir, "./dock/voki/http"), + ("./http/Rocket.toml", "./dock/voki/Rocket.toml"), + ]; + + for (from, to) in dirs { + info!("Update", to); + + if let Err(err) = update(from, to) { + let message = format!("{err:?}"); + error!(message); + return ExitCode::FAILURE; } } + + info!("Done", "Docker container is ready at ./dock"); + ExitCode::SUCCESS } diff --git a/http/static/favicon.svg b/web/static/favicon.svg similarity index 100% rename from http/static/favicon.svg rename to web/static/favicon.svg diff --git a/web/images/1.jpg b/web/static/images/1.jpg similarity index 100% rename from web/images/1.jpg rename to web/static/images/1.jpg diff --git a/web/index.html b/web/static/index.html similarity index 100% rename from web/index.html rename to web/static/index.html diff --git a/web/style.css b/web/static/style.css similarity index 100% rename from web/style.css rename to web/static/style.css