WIP cargo aoc bench on webhook

This commit is contained in:
2022-12-03 14:31:58 -08:00
commit 6248844714
6 changed files with 2653 additions and 0 deletions

117
src/main.rs Normal file
View File

@@ -0,0 +1,117 @@
#[macro_use]
extern crate rocket;
use std::{
fs, io,
path::{Path, PathBuf},
process::Command,
};
use glog::Flags;
use log::info;
use rocket::{fairing::AdHoc, response::Responder, State};
use serde::Deserialize;
use thiserror::Error;
#[derive(Error, Debug, Responder)]
pub enum SyncError {
#[error("IO error")]
IoError(#[from] io::Error),
}
// For testing
#[get("/-/reload/<repo>")]
fn get_reload(config: &State<Config>, repo: &str) -> Result<String, SyncError> {
reload(config, repo)
}
#[post("/-/reload/<repo>")]
fn post_reload(config: &State<Config>, repo: &str) -> Result<String, SyncError> {
reload(config, repo)
}
fn reload(config: &State<Config>, repo: &str) -> Result<String, SyncError> {
info!("Need to reload '{}': {:?}", repo, config);
let source_path = config.source_root.join(repo);
let build_path = config.build_root.join(repo);
dbg!(&build_path);
dbg!(&source_path);
let needs_clone = false;
let mut output = String::new();
if needs_clone {
output += &format!(
"{:?}",
Command::new("/run/current-system/sw/bin/git")
.current_dir(&config.build_root)
.arg("clone")
.arg(&source_path)
.arg(&build_path)
.output()?
);
}
// Make sure buildable clone is up to date
output += &format!(
"{:?}",
Command::new("/run/current-system/sw/bin/git")
.current_dir(&build_path)
.arg("pull")
.output()?
);
// Run `cargo aoc bench`
output += &format!(
"{:?}",
Command::new("cargo")
.current_dir(&build_path)
.arg("aoc")
.arg("bench")
.output()?
);
// Copy files from `target/` to serving directory
let bench_path = build_path.join("target/aoc/aoc-autobench/target/criterion");
copy_dir_all(bench_path, config.www_root.join(repo))?;
let response = format!("{:?}", output);
info!("{}", response);
Ok(response)
}
// From https://stackoverflow.com/questions/26958489/how-to-copy-a-folder-recursively-in-rust
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
#[get("/")]
fn index() -> &'static str {
info!("Hello world");
"Hello, world!"
}
#[derive(Debug, Deserialize)]
struct Config {
source_root: PathBuf,
build_root: PathBuf,
www_root: PathBuf,
}
#[launch]
fn rocket() -> _ {
glog::new()
.init(Flags {
colorlogtostderr: true,
//alsologtostderr: true, // use logtostderr to only write to stderr and not to files
logtostderr: true,
..Default::default()
})
.unwrap();
rocket::build()
.mount("/", routes![index, get_reload, post_reload])
.attach(AdHoc::config::<Config>())
}