fix-all command

This commit is contained in:
liquidex 2023-08-27 21:36:44 +02:00
parent 6f849837c0
commit 8643d419ab
6 changed files with 105 additions and 29 deletions

View file

@ -10,6 +10,7 @@
% id = "01H89RFHCQEQ2RAWRTRN6HZ8A3"
- whatever's on my mind at the time that I wanna share with the world, I write down here
% id = "01H8W7VEVGGS29756T8C466GRY"
- doing my best to keep it approachable and readable by not just me, but _you_ too
% id = "01H89RFHCQY5EE9Y46PHJT9DFR"
@ -137,6 +138,7 @@
% id = "01H8VWEFJ1BGA21FBVHC4TFF3V"
- the "Choose Your Own Poem" lol
% id = "01H8W7VEVHCAEM03Z75QYC36SG"
- I understand if this form of writing can be daunting at first, but please, give it a shot. I'm doing my best to make it as accessible as possible.
% id = "01H89RFHCQAXJ0ST31TP1A104V"

View file

@ -1,52 +1,74 @@
% id = "01H8W7VEVNQ0XR4RDYRGMKS0E9"
- the emojis here are grouped by game, topic, or other thing for easier navigation
% id = "01H8W7VEVNAGEY9XFD2W1CJ1NS"
- most of them are stolen from other places online, but I did draw some myself too
% id = "01H8W7VEVNTVQ1E2Z9YWT2PQPX"
- ### A Hat in Time
% id = "emoji/hueh"
- :hueh: - snicker
% id = "01H8W7VEVNA4GH1W2XVZ6QB3TF"
+ stolen from the Hat in Time Discord
% id = "01H8W7VEVNNE5WKC4CSTNG4NAP"
- which is stolen from a piece of art that was posted there I think
% id = "01H8W7VEVNY6Q7VTWY0HK8F4AZ"
- also [check this out](https://hueh.ovh)
% id = "01H8W7VEVN64AMSVZ4Y21KDPFR"
- ### Deltarune
% id = "emoji/ralsei_dead"
- :ralsei_dead: - he dead (NOTE: not actually dead)
- :ralsei_dead: - he ded (NOTE: not actually ded)
% id = "01H8W7VEVNAPQTFC32NQTS9Y8F"
- (just lying face down on the floor)
% id = "01H8W7VEVNVTZSNW52XNGG521K"
- perhaps you could consider this the fluffy version of a facepalm
% id = "01H8W7VEVN2N1TQ5E8EWP9Z44Q"
+ but it isn't really one, because splatting on the floor like that usually happens through external means
% id = "01H8W7VEVNRTMR5QM5P6G8Y622"
- like someone jumping on you :hueh:
% id = "01H8W7VEVNVYSH9RQ42XNHAZV0"
- since this one's just an in-game sprite I will probably redraw it to be something more legible at the size it's displayed
% id = "emoji/ralsei_love"
- :ralsei_love: - happy fluffy boy wants to tell you he loves you
% id = "01H8W7VEVNHS54MG0CQWS14XYY"
- stolen from the Deltarune Discord
- I might replace this one with my own version at some point because I generally use hatless Ralsei as my online avatar
% id = "01H8W7VEVN63P6V2W7P4TYNBNJ"
- I might replace this one with my own version at some point because I generally use hatless rather than hatted Ralsei as my online avatar
% id = "emoji/ralsei_wave"
- :ralsei_wave: - hiii!! :heart:
% id = "01H8W7VEVN9J1CM9VRGGB92HPW"
- this one I drew myself
% id = "emoji/oh"
- :oh: - oh.
% id = "01H8W7VEVPG40KYGVK0RKGWRNW"
+ again stolen from the Deltarune Discord
% id = "01H8W7VEVPN91QA8DSBEQW1WGM"
- which steals from an in-game sprite, but the version on Discord is not pixelated
% id = "01H8W7VEVPSHDWDH58HFKBMGD6"
- but I don't wanna replace it because it's just too good
% id = "01H8W7VEVP9FHW7QKZFXDRPRGD"
- ### [Twemoji](https://github.com/twitter/twemoji)
% id = "01H8W7VEVPAT6NMJX9FG3KVYE3"
- yeah I stole a couple of those. but you can view those on [the *real* emojipedia](https://emojipedia.org) because they're pretty ubiquitous and easy to understand.

View file

@ -2,12 +2,13 @@ use std::ops::Range;
use anyhow::Context;
use treehouse_format::ast::Branch;
use walkdir::WalkDir;
use crate::state::{FileId, Treehouse};
use super::{
parse::{self, parse_toml_with_diagnostics, parse_tree_with_diagnostics},
FixArgs,
FixAllArgs, FixArgs, Paths,
};
struct Fix {
@ -149,3 +150,37 @@ pub fn fix_file_cli(fix_args: FixArgs) -> anyhow::Result<()> {
Ok(())
}
pub fn fix_all_cli(fix_all_args: FixAllArgs, paths: &Paths<'_>) -> anyhow::Result<()> {
for entry in WalkDir::new(paths.content_dir) {
let entry = entry?;
if entry.file_type().is_file() {
let file = std::fs::read_to_string(entry.path())
.with_context(|| format!("cannot read file to fix: {:?}", entry.path()))?;
let utf8_filename = entry.path().to_string_lossy();
let mut treehouse = Treehouse::new();
let file_id = treehouse.add_file(utf8_filename.into_owned(), None, file);
if let Ok(fixed) = fix_file(&mut treehouse, file_id) {
if fixed != treehouse.source(file_id) {
if fix_all_args.apply {
println!("fixing: {:?}", entry.path());
std::fs::write(entry.path(), fixed).with_context(|| {
format!("cannot overwrite original file: {:?}", entry.path())
})?;
} else {
println!("will fix: {:?}", entry.path());
}
}
} else {
treehouse.report_diagnostics()?;
}
}
}
if !fix_all_args.apply {
println!("run with `--apply` to apply changes");
}
Ok(())
}

View file

@ -27,6 +27,8 @@ use crate::{
use crate::state::{FileId, Treehouse};
use super::Paths;
#[derive(Default)]
struct Generator {
tree_files: Vec<PathBuf>,
@ -203,16 +205,6 @@ impl Generator {
}
}
#[derive(Debug, Clone, Copy)]
pub struct Paths<'a> {
pub target_dir: &'a Path,
pub static_dir: &'a Path,
pub template_dir: &'a Path,
pub content_dir: &'a Path,
pub config_file: &'a Path,
}
pub fn regenerate(paths: &Paths<'_>) -> anyhow::Result<()> {
let start = Instant::now();

View file

@ -2,7 +2,7 @@ pub mod fix;
pub mod generate;
mod parse;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use clap::{Args, Parser, Subcommand};
@ -19,6 +19,11 @@ pub enum Command {
/// Populate missing metadata in blocks.
Fix(#[clap(flatten)] FixArgs),
/// Populate missing metadata in blocks across all files.
///
/// By default only prints which files would be changed. To apply the changes, use `--apply`.
FixAll(#[clap(flatten)] FixAllArgs),
}
#[derive(Args)]
@ -43,3 +48,21 @@ pub struct FixArgs {
#[clap(long)]
pub backup: Option<PathBuf>,
}
#[derive(Args)]
pub struct FixAllArgs {
/// If you're happy with the suggested changes, specifying this will apply them to the file
/// (overwrite it in place.)
#[clap(long)]
pub apply: bool,
}
#[derive(Debug, Clone, Copy)]
pub struct Paths<'a> {
pub target_dir: &'a Path,
pub static_dir: &'a Path,
pub template_dir: &'a Path,
pub content_dir: &'a Path,
pub config_file: &'a Path,
}

View file

@ -2,9 +2,9 @@ use std::path::Path;
use clap::Parser;
use cli::{
fix::fix_file_cli,
generate::{self, regenerate_or_report_error, Paths},
Command, ProgramArgs,
fix::{fix_all_cli, fix_file_cli},
generate::{self, regenerate_or_report_error},
Command, Paths, ProgramArgs,
};
use log::{error, info};
@ -18,21 +18,22 @@ mod tree;
async fn fallible_main() -> anyhow::Result<()> {
let args = ProgramArgs::parse();
let paths = Paths {
target_dir: Path::new("target/site"),
config_file: Path::new("treehouse.toml"),
// NOTE: These are intentionally left unconfigurable from within treehouse.toml
// because this is is one of those things that should be consistent between sites.
static_dir: Path::new("static"),
template_dir: Path::new("template"),
content_dir: Path::new("content"),
};
match args.command {
Command::Generate(regenerate_args) => {
let dirs = Paths {
target_dir: Path::new("target/site"),
config_file: Path::new("treehouse.toml"),
info!("regenerating using directories: {paths:#?}");
// NOTE: These are intentionally left unconfigurable from within treehouse.toml
// because this is is one of those things that should be consistent between sites.
static_dir: Path::new("static"),
template_dir: Path::new("template"),
content_dir: Path::new("content"),
};
info!("regenerating using directories: {dirs:#?}");
regenerate_or_report_error(&dirs);
regenerate_or_report_error(&paths);
if regenerate_args.serve {
generate::web_server().await?;
@ -40,6 +41,7 @@ async fn fallible_main() -> anyhow::Result<()> {
}
Command::Fix(fix_args) => fix_file_cli(fix_args)?,
Command::FixAll(fix_args) => fix_all_cli(fix_args, &paths)?,
}
Ok(())