random name for back alley

This commit is contained in:
nora 2024-01-21 21:16:24 +01:00
parent 9f9120dc27
commit 2d2c820510
6 changed files with 95 additions and 9 deletions

View file

@ -11,6 +11,7 @@ use color_eyre::{eyre::Context, Result};
use crate::Config;
pub fn assemble_website(
rng: &mut rand::rngs::StdRng,
config: &Config,
statics: &Path,
submodules: &Path,
@ -24,7 +25,7 @@ pub fn assemble_website(
)
.wrap_err("building slides")?;
statics::build(&config.slides, statics, dist).wrap_err("building root files")?;
statics::build(rng, &config.slides, statics, dist).wrap_err("building root files")?;
add_cname(dist)?;

View file

@ -4,6 +4,7 @@ use std::{fs, path::Path};
use askama::Template;
use color_eyre::{eyre::WrapErr, Result};
use rand::seq::SliceRandom;
use crate::{utils, SlidesConfig, Talk};
@ -13,9 +14,33 @@ struct Slides {
talks: Vec<Talk>,
}
pub fn build(config: &SlidesConfig, statics: &Path, dist: &Path) -> Result<()> {
pub fn build(
rng: &mut rand::rngs::StdRng,
config: &SlidesConfig,
statics: &Path,
dist: &Path,
) -> Result<()> {
let back_alley_name = b"abcdefghijklmnopqrstuvwxyz"
.choose_multiple(rng, 6)
.map(|&c| char::from_u32(c.into()).unwrap())
.collect::<String>();
let back_alley_name = format!("back-alley-{back_alley_name}.html");
utils::cp_content(&statics.join("root"), dist).wrap_err("copying root files")?;
let back_alley = dist.join("back-alley.html");
std::fs::copy(&back_alley, dist.join(&back_alley_name)).wrap_err("copying back-alley.html")?;
fs::remove_file(back_alley).wrap_err("deleting normal back-alley.html")?;
let index_html = dist.join("index.html");
let index = fs::read_to_string(&index_html).wrap_err("reading index.html")?;
fs::write(
index_html,
index.replace("back-alley.html", &back_alley_name),
)
.wrap_err("writing back index.html")?;
let slide_html = Slides {
talks: config.talks.clone(),
}

View file

@ -16,6 +16,7 @@ use color_eyre::{
Result,
};
use notify::{RecursiveMode, Watcher};
use rand::SeedableRng;
use serde::Deserialize;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
@ -74,14 +75,18 @@ fn main() -> Result<()> {
Ok(())
}
Some("watch") => watch(root),
Some("build") => build(root),
Some("build") => build(&mut rand::rngs::StdRng::from_entropy(), root),
Some(cmd) => bail!("invalid subcommand {cmd}"),
None => bail!("no subcommand provided"),
}
}
fn watch(root: &'static Path) -> Result<()> {
build(root).wrap_err("initial build")?;
let seed: u64 = rand::random();
let rng = move || rand::rngs::StdRng::seed_from_u64(seed);
build(&mut rng(), root).wrap_err("initial build")?;
let (send, recv) = std::sync::mpsc::sync_channel(1);
let mut watcher = notify::recommended_watcher(move |res| match res {
Ok(_) => {
@ -130,7 +135,7 @@ fn watch(root: &'static Path) -> Result<()> {
last = now;
info!("Received update, rebuilding");
if let Err(e) = build(root) {
if let Err(e) = build(&mut rng(), root) {
error!(?e);
}
}
@ -139,7 +144,7 @@ fn watch(root: &'static Path) -> Result<()> {
.map_err(|_| eyre!("build thread panicked"))
}
fn build(root: &Path) -> Result<()> {
fn build(rng: &mut rand::rngs::StdRng, root: &Path) -> Result<()> {
let config =
std::fs::read_to_string(root.join("config.toml")).wrap_err("reading config.toml")?;
let config = toml::from_str::<Config>(&config).wrap_err("parsing config.toml")?;
@ -152,7 +157,13 @@ fn build(root: &Path) -> Result<()> {
submodule::sync(&submodules_path, &sub_config).wrap_err("syncing subtrees")?;
let dist_path = root.join("dist");
build::assemble_website(&config, &root.join("static"), &submodules_path, &dist_path)?;
build::assemble_website(
rng,
&config,
&root.join("static"),
&submodules_path,
&dist_path,
)?;
Ok(())
}