Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Run All Scripts In A Directory Recursively In Rust

I'm expermenting with an idea of including executable scripts in the main content directory for my Neopolitan implementation. The idea is to use them for preflight by running them before each build. That allows functionality to be added without having to mess with the main binary.

This is the code I'm using to loop down the direcotry structure and run the files:

Code

use std::path::PathBuf;
use std::process::Command;
use walkdir::Error;
use walkdir::WalkDir;

pub fn run_preflight() -> Result<(), Error> {
    for entry in WalkDir::new("./site/content").sort_by_file_name() {
        let path = PathBuf::from(entry?.path());
        if let Some(ext) = path.extension() {
            if ext == "py" {
                let path_string = path.display().to_string();
                let args: Vec<&str> = vec![path_string.as_str()];
                let cmd_output = Command::new("/opt/homebrew/bin/python3")
                    .args(args)
                    .output()
                    .unwrap();
            }
        }
    }
    Ok(())
}

Run through a directory. Run all the things