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

48
Cargo.lock generated
View file

@ -212,6 +212,17 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "getrandom"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.28.1" version = "0.28.1"
@ -458,6 +469,12 @@ version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.76" version = "1.0.76"
@ -476,6 +493,36 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.4.1" version = "0.4.1"
@ -765,6 +812,7 @@ dependencies = [
"color-eyre", "color-eyre",
"fs_extra", "fs_extra",
"notify", "notify",
"rand",
"serde", "serde",
"serde_derive", "serde_derive",
"toml", "toml",

View file

@ -10,6 +10,7 @@ askama = "0.12.1"
color-eyre = "0.6.2" color-eyre = "0.6.2"
fs_extra = "1.3.0" fs_extra = "1.3.0"
notify = "6.1.1" notify = "6.1.1"
rand = "0.8.5"
serde = { version = "1.0.195", features = ["derive"] } serde = { version = "1.0.195", features = ["derive"] }
serde_derive = "1.0.195" serde_derive = "1.0.195"
toml = "0.8.8" toml = "0.8.8"

View file

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

View file

@ -4,6 +4,7 @@ use std::{fs, path::Path};
use askama::Template; use askama::Template;
use color_eyre::{eyre::WrapErr, Result}; use color_eyre::{eyre::WrapErr, Result};
use rand::seq::SliceRandom;
use crate::{utils, SlidesConfig, Talk}; use crate::{utils, SlidesConfig, Talk};
@ -13,9 +14,33 @@ struct Slides {
talks: Vec<Talk>, 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")?; 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 { let slide_html = Slides {
talks: config.talks.clone(), talks: config.talks.clone(),
} }

View file

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

View file

@ -97,8 +97,8 @@
<p> <p>
in addition to all the other stuff mentioned above, i also have some random projects in addition to all the other stuff mentioned above, i also have some random projects
hosted on my server. they are pretty bad and i won't promote them this openly, but you hosted on my server. they are pretty bad and i won't promote them this openly, but you
will be able to find them if you really want to. some people have been saying will be able to find them if you really want to. some people have been saying that there
that there might be a secret <span role="button" class="hint">b</span>ack alley somewhere... might be a secret <span role="button" class="hint">b</span>ack alley somewhere...
</p> </p>
</div> </div>
</div> </div>