Skip to content

Commit

Permalink
Update builder
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed May 30, 2022
1 parent 890137e commit 3200bd5
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 8 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!()
1 change: 0 additions & 1 deletion http/.gitignore

This file was deleted.

4 changes: 2 additions & 2 deletions src/info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
macro_rules! info {
($l:expr, $e:expr) => {
($l:expr, $e:expr) => {{
use termion::{color, style};

println!(
Expand All @@ -11,7 +11,7 @@ macro_rules! info {
style::Reset,
$e
);
};
}};
}

pub(crate) use info;
Expand Down
99 changes: 96 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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
}
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.

0 comments on commit 3200bd5

Please sign in to comment.