move submodules in-tree

This commit is contained in:
nora 2025-08-13 20:47:44 +02:00
parent 57c4a239da
commit 830045ee2f
238 changed files with 41955 additions and 187 deletions

View file

@ -14,26 +14,19 @@ pub fn assemble_website(
rng: &mut rand::rngs::StdRng,
config: &Config,
statics: &Path,
submodules: &Path,
root: &Path,
dist: &Path,
) -> Result<()> {
blog::build(&submodules.join("blog"), &dist.join("blog")).wrap_err("building blog")?;
blog::build(&root.join("blog"), &dist.join("blog")).wrap_err("building blog")?;
slides::build(
&config.slides,
&submodules.join("slides"),
&root.join("slides"),
&dist.join("slides"),
)
.wrap_err("building slides")?;
statics::build(rng, &config.slides, statics, dist).wrap_err("building root files")?;
add_cname(dist)?;
Ok(())
}
fn add_cname(dist: &Path) -> Result<()> {
let cname = "nilstrieb.dev\n";
std::fs::write(dist.join("CNAME"), cname).wrap_err("writing cname")?;
Ok(())
}

View file

@ -1,5 +1,4 @@
mod build;
mod submodule;
mod utils;
#[macro_use]
@ -164,21 +163,8 @@ fn build(rng: &mut rand::rngs::StdRng, root: &Path) -> Result<()> {
talk.dir_name = talk.dir_name();
});
let sub_config = std::fs::read_to_string(root.join("submodules.toml"))
.wrap_err("reading submodules.toml")?;
let sub_config =
submodule::Submodules::parse(&sub_config).wrap_err("invalid submodules.toml")?;
let submodules_path = root.join("submodules");
submodule::sync(&submodules_path, &sub_config).wrap_err("syncing submodules")?;
let dist_path = root.join("dist");
build::assemble_website(
rng,
&config,
&root.join("static"),
&submodules_path,
&dist_path,
)?;
build::assemble_website(rng, &config, &root.join("static"), &root, &dist_path)?;
Ok(())
}

View file

@ -1,139 +0,0 @@
//! Implements a simplistic form of git submodules.
//!
//! The config format is as follows:
//!
//! A list of
//! ```toml
//! [[submodule]]
//! name = ""
//! url = ""
//! commit = ""
//! ```
//!
//! For example,
//! ```toml
//! [[submodule]]
//! name = "nixos"
//! url = "https://github.com/Noratrieb/nixos.git"
//! commit = "c5b2fc10b9266b105d792d958b8f13479866a7bd"
//! ```
//!
//! This module will check them out into a directory called `submodules` in the current directory.
//! Make sure to put this directory into `.gitignore`.
use std::{path::Path, process};
use color_eyre::{
eyre::{Context, OptionExt},
Result,
};
use crate::utils::{self, cp_r};
pub struct Submodules {
configs: Vec<SyncConfig>,
}
pub struct SyncConfig {
name: String,
url: String,
commit: String,
}
impl Submodules {
pub fn parse(s: &str) -> Result<Submodules> {
let doc = s.parse::<toml::Table>().wrap_err("invalid toml")?;
let subs = doc
.get("submodule")
.ok_or_eyre("no top-level submodule tables")?;
let mods = subs.as_array().ok_or_eyre("submodule is not an array")?;
let mut configs = Vec::new();
for module in mods {
let map = module.as_table().ok_or_eyre("submodule is not a table")?;
let get_str = |name| -> Result<String> {
Ok(map
.get(name)
.ok_or_eyre(format!("{name} is missing"))?
.as_str()
.ok_or_eyre(format!("{name} is not a string"))?
.into())
};
configs.push(SyncConfig {
name: get_str("name")?,
url: get_str("url")?,
commit: get_str("commit")?,
});
}
Ok(Self { configs })
}
}
pub fn sync(path: &Path, config: &Submodules) -> color_eyre::Result<()> {
info!("Syncing submodules...");
utils::create_dir_if_not_exist(path)?;
for sync in &config.configs {
let name = &sync.name;
let url = sync.url.as_str();
let span = info_span!("Syncing submodule", ?name, ?url);
let _span = span.enter();
let sub_path = path.join(name);
if let Ok(path) = std::env::var(format!("SUBMODULE_DIR_{}", name.to_uppercase())) {
info!(?name, ?path, "Taking submodule from hardcoded path");
cp_r(Path::new(&path), &sub_path).wrap_err("copying path from SUBMODULE_DIR")?;
continue;
}
if !sub_path.exists() {
info!(?name, ?url, "Cloning");
let mut cmd = process::Command::new("git");
cmd.args(["clone", url, sub_path.to_str().unwrap()]);
utils::run_process(&mut cmd)?;
} else {
debug!(?name, ?url, "Repo already exists");
}
let current_commit = utils::run_process(
process::Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(&sub_path),
)
.wrap_err("running git rev-parse HEAD")?;
debug!(?current_commit, "Current commit");
if current_commit.trim() != sync.commit {
info!("Need to change commit");
let commit_exists = utils::run_process(
process::Command::new("git")
.args(["cat-file", "-t", sync.commit.as_str()])
.current_dir(&sub_path),
);
if !commit_exists.is_ok_and(|typ| typ == *"commit\n") {
info!("Must fetch commit");
utils::run_process(process::Command::new("git").current_dir(&sub_path).args([
"fetch",
"origin",
sync.commit.as_str(),
]))?;
}
utils::run_process(process::Command::new("git").current_dir(&sub_path).args([
"reset",
"--hard",
sync.commit.as_str(),
]))?;
}
}
Ok(())
}

View file

@ -2,7 +2,12 @@ use color_eyre::{
eyre::{bail, Context},
Result,
};
use std::{fs, io, os::unix::ffi::OsStrExt, path::{Path, PathBuf}, process::Command};
use std::{
fs, io,
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
process::Command,
};
pub fn run_process(cmd: &mut Command) -> Result<String> {
fn run_process_inner(cmd: &mut Command) -> Result<String> {
@ -31,15 +36,6 @@ pub fn run_process(cmd: &mut Command) -> Result<String> {
))
}
pub fn create_dir_if_not_exist(p: &Path) -> Result<()> {
match std::fs::create_dir(p) {
Ok(()) => debug!(?p, "Created directory"),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
e => return e.wrap_err("failed to create submodules"),
}
Ok(())
}
pub fn cp_r(from: &Path, to: &Path) -> Result<()> {
copy_fn(from, to, |content, _, _| Ok(content))
}