diff --git a/scaffold/src/args.rs b/scaffold/src/args.rs index f4f924f..4d55298 100644 --- a/scaffold/src/args.rs +++ b/scaffold/src/args.rs @@ -16,29 +16,6 @@ pub struct Args { pub subcommand: SubCommand, } -impl Args { - pub fn as_token_args(&self) -> &TokenArgs { - match &self.subcommand { - SubCommand::Token(args) => args, - _ => panic!("Expected token subcommand"), - } - } - - pub fn as_timer_args(&self) -> &TimerArgs { - match &self.subcommand { - SubCommand::Timer(args) => args, - _ => panic!("Expected timer subcommand"), - } - } - - pub fn as_init_args(&self) -> &InitArgs { - match &self.subcommand { - SubCommand::Init(args) => args, - _ => panic!("Expected init subcommand"), - } - } -} - #[derive(Parser, Debug)] pub enum SubCommand { /// Verify that the session token provided is still valid @@ -80,7 +57,7 @@ pub struct InitArgs { pub input_location: String, /// A formatter that will be used to get the path for the solution file. #[arg(short, long, default_value = "aoc_{{year}}/src/day_{{day:pad(2)}}.rs")] - solution_location: String, + pub solution_location: String, /// Location formatter of the file importing each solution module. #[arg(long, default_value = "aoc_{{year}}/src/lib.rs")] module_location: String, @@ -94,7 +71,7 @@ pub struct InitArgs { /// Path to a template file that will be used to create the solution file. /// If not provided, a default template will be used. #[arg(short = 't', long)] - solution_template: Option, + pub solution_template: Option, /// Don't create a solution file. /// Useful if you want to use this command with a different language or organization. #[arg(short, long)] diff --git a/scaffold/src/commands/init.rs b/scaffold/src/commands/init.rs index a555cc6..5c24c98 100644 --- a/scaffold/src/commands/init.rs +++ b/scaffold/src/commands/init.rs @@ -15,22 +15,36 @@ use crate::{ }; pub fn init(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> { + if !cmd.no_scaffold { + write_scaffold(cmd, args)?; + } + write_input(session, cmd, args)?; Ok(()) } +fn write_scaffold(cmd: &InitArgs, args: &Args) -> Result<()> { + let args: &[(&str, u16)] = &[("year", cmd.year), ("day", cmd.day as u16)]; + let file_location = Formatter::new(&cmd.solution_location)?.format(args)?; + let mut file = create_file(&Path::new(&file_location))?; + + println!("[*] Loading template"); + let template = match cmd.solution_template { + Some(ref path) => fs::read_to_string(path)?, + None => include_str!("../../template.txt").to_owned(), + }; + let template = Formatter::new(&template)?.format(args)?; + + file.write_all(template.as_bytes())?; + println!("[*] Wrote scaffold to {file_location}"); + Ok(()) +} + fn write_input(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> { let file_location = Formatter::new(&cmd.input_location)? .format::<&[_]>(&[("year", cmd.year), ("day", cmd.day as u16)])?; - let path = Path::new(&file_location); - if let Some(parent) = path.parent() { - if !parent.exists() { - fs::create_dir_all(parent)?; - } - } - - let mut file = File::create(path)?; + let mut file = create_file(&Path::new(&file_location))?; let input = fetch_input(session, &args.address, cmd.day, Some(cmd.year))?; file.write_all(input.as_bytes())?; println!("[*] Wrote input to {file_location}"); @@ -49,3 +63,13 @@ fn fetch_input(session: &Session, base: &Url, day: u8, year: Option) -> Res Ok(body) } + +fn create_file(path: &Path) -> Result { + if let Some(parent) = path.parent() { + if !parent.exists() { + fs::create_dir_all(parent)?; + } + } + + Ok(File::create(path)?) +} diff --git a/scaffold/src/formatter.rs b/scaffold/src/formatter.rs index d489050..0f82f76 100644 --- a/scaffold/src/formatter.rs +++ b/scaffold/src/formatter.rs @@ -93,7 +93,7 @@ impl Arguments for &[(&str, T)] { } mod tokenize { - use anyhow::{bail, Result}; + use anyhow::Result; use super::{Component, Processor}; diff --git a/scaffold/todo.md b/scaffold/todo.md index f14af7c..f5055f8 100644 --- a/scaffold/todo.md +++ b/scaffold/todo.md @@ -3,6 +3,9 @@ - [ ] Formatter - [ ] Commands - [ ] Init + - [x] Scaffold + - [ ] Module + - [x] Input - [x] Verify - [x] Token - [x] Timer