fix-all command
This commit is contained in:
parent
6f849837c0
commit
8643d419ab
6 changed files with 105 additions and 29 deletions
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue