From d9a48403e744159958e8067d9c15aac152e1be6e Mon Sep 17 00:00:00 2001 From: bucsi <11545252+bucsi@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:32:55 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20add=20ability=20to=20use=20?= =?UTF-8?q?example=20files=20(#12)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ add ability to use example files * 🐛 Reverse use_example flag to input kind resolution to be correct * ♻️ Use shorter flag name Co-authored-by: Robert Attard * ♻️ Use shorter flag name * 🚧 add example flag to the new command TODO: configure flag in `gladvent.gleam` * 📝 change string description of example flag * ♻️ use example flag for new command correctly * ♻️ use list spread instead of iterator append when preparing task list for new command * 🐛 Remove example creation step from default new command sequence * ♻️ move run/example flag to command level instead of group level * ♻️ move remaining inputfile-related functions to the input module * 🐛 Fix helper text of run --example flag Co-authored-by: Robert Attard --------- Co-authored-by: Robert Attard --- src/gladvent/internal/cmd.gleam | 8 ------ src/gladvent/internal/cmd/new.gleam | 40 +++++++++++++++++++++-------- src/gladvent/internal/cmd/run.gleam | 30 ++++++++++++++++++---- src/gladvent/internal/input.gleam | 24 +++++++++++++++++ 4 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 src/gladvent/internal/input.gleam diff --git a/src/gladvent/internal/cmd.gleam b/src/gladvent/internal/cmd.gleam index f5abc09..42a1cab 100644 --- a/src/gladvent/internal/cmd.gleam +++ b/src/gladvent/internal/cmd.gleam @@ -23,14 +23,6 @@ fn find_root(path: String) -> String { } } -pub fn input_root() { - filepath.join(root(), "input") -} - -pub fn input_dir(year) { - filepath.join(input_root(), int.to_string(year)) -} - pub fn src_root() { filepath.join(root(), "src") } diff --git a/src/gladvent/internal/cmd/new.gleam b/src/gladvent/internal/cmd/new.gleam index 930e07b..44f5c8a 100644 --- a/src/gladvent/internal/cmd/new.gleam +++ b/src/gladvent/internal/cmd/new.gleam @@ -3,6 +3,7 @@ import file_streams/file_stream import file_streams/file_stream_error import filepath import gladvent/internal/cmd +import gladvent/internal/input import gladvent/internal/parse.{type Day} import gladvent/internal/util import gleam/int @@ -14,7 +15,7 @@ import glint import simplifile type Context { - Context(year: Int, day: Day, add_parse: Bool) + Context(year: Int, day: Day, add_parse: Bool, create_example_file: Bool) } fn create_src_dir(ctx: Context) { @@ -39,18 +40,26 @@ fn create_src_file(ctx: Context) { } fn create_input_root(_ctx: Context) { - cmd.input_root() + input.root() |> create_dir } fn create_input_dir(ctx: Context) { ctx.year - |> cmd.input_dir + |> input.dir |> create_dir } fn create_input_file(ctx: Context) { - let input_path = input_path(ctx.year, ctx.day) + let input_path = input.get_file_path(ctx.year, ctx.day, input.Puzzle) + + do_exclusive(input_path, fn(_) { Nil }) + |> result.map_error(handle_file_open_failure(_, input_path)) + |> result.replace(input_path) +} + +fn create_input_example_file(ctx: Context) { + let input_path = input.get_file_path(ctx.year, ctx.day, input.Example) do_exclusive(input_path, fn(_) { Nil }) |> result.map_error(handle_file_open_failure(_, input_path)) @@ -71,10 +80,6 @@ fn err_to_string(e: Err) -> String { } } -fn input_path(year: Int, day: Day) -> String { - filepath.join(cmd.input_dir(year), int.to_string(day) <> ".txt") -} - fn gleam_src_path(year: Int, day: Day) -> String { filepath.join(cmd.src_dir(year), "day_" <> int.to_string(day) <> ".gleam") } @@ -115,6 +120,10 @@ fn do(ctx: Context) -> String { create_input_file, create_src_dir, create_src_file, + ..case ctx.create_example_file { + True -> [create_input_example_file] + False -> [] + } ] let successes = fn(good) { @@ -185,19 +194,28 @@ fn collect(year: Int, x: #(Day, String)) -> String { pub fn new_command() { use <- glint.command_help("Create .gleam and input files") use <- glint.unnamed_args(glint.MinArgs(1)) - use parse <- glint.flag( + use parse_flag <- glint.flag( glint.bool_flag("parse") |> glint.flag_default(False) |> glint.flag_help("Generate day runners with a parse function"), ) + use example_flag <- glint.flag( + glint.bool_flag("example") + |> glint.flag_default(False) + |> glint.flag_help( + "Generate example input files to run your solution against", + ), + ) use _, args, flags <- glint.command() use days <- result.map(parse.days(args)) let assert Ok(year) = glint.get_flag(flags, cmd.year_flag()) - let assert Ok(parse) = parse(flags) + let assert Ok(parse) = parse_flag(flags) + let assert Ok(create_example) = example_flag(flags) + cmd.exec( days, cmd.Endless, - fn(day) { do(Context(year, day, parse)) }, + fn(day) { do(Context(year, day, parse, create_example)) }, collect_async(year, _), ) } diff --git a/src/gladvent/internal/cmd/run.gleam b/src/gladvent/internal/cmd/run.gleam index 4b49de2..4ea141b 100644 --- a/src/gladvent/internal/cmd/run.gleam +++ b/src/gladvent/internal/cmd/run.gleam @@ -1,6 +1,6 @@ import decode -import filepath import gladvent/internal/cmd.{Ending, Endless} +import gladvent/internal/input import gladvent/internal/parse.{type Day} import gladvent/internal/runners import gladvent/internal/util @@ -73,14 +73,14 @@ fn do( day: Day, package: package_interface.Package, allow_crash: Bool, + input_kind: input.Kind, ) -> RunResult { use #(pt_1, pt_2, parse) <- result.try( runners.get_day(package, year, day) |> result.map_error(FailedToGetRunner), ) - let input_path = - filepath.join(cmd.input_dir(year), int.to_string(day) <> ".txt") + let input_path = input.get_file_path(year, day, input_kind) use input <- result.try( input_path @@ -270,10 +270,22 @@ pub fn allow_crash_flag() { pub fn run_command() -> glint.Command(Result(List(String))) { use <- glint.command_help("Run the specified days") use <- glint.unnamed_args(glint.MinArgs(1)) + use example_flag <- glint.flag( + glint.bool_flag("example") + |> glint.flag_default(False) + |> glint.flag_help( + "Run solutions against example inputs (found at input//.example.txt)", + ), + ) use _, args, flags <- glint.command() use days <- result.then(parse.days(args)) let assert Ok(year) = glint.get_flag(flags, cmd.year_flag()) let assert Ok(allow_crash) = glint.get_flag(flags, allow_crash_flag()) + let assert Ok(use_example) = case example_flag(flags) { + Error(a) -> Error(a) + Ok(True) -> Ok(input.Example) + _ -> Ok(input.Puzzle) + } let spinner = spinner.new( @@ -297,7 +309,11 @@ pub fn run_command() -> glint.Command(Result(List(String))) { ) days - |> cmd.exec(timing, do(year, _, package, allow_crash), collect_async(year, _)) + |> cmd.exec( + timing, + do(year, _, package, allow_crash, use_example), + collect_async(year, _), + ) } pub fn run_all_command() -> glint.Command(Result(List(String))) { @@ -336,5 +352,9 @@ pub fn run_all_command() -> glint.Command(Result(List(String))) { |> result.replace_error(Nil) }) |> list.sort(int.compare) - |> cmd.exec(timing, do(year, _, package, allow_crash), collect_async(year, _)) + |> cmd.exec( + timing, + do(year, _, package, allow_crash, input.Puzzle), + collect_async(year, _), + ) } diff --git a/src/gladvent/internal/input.gleam b/src/gladvent/internal/input.gleam new file mode 100644 index 0000000..3beaec8 --- /dev/null +++ b/src/gladvent/internal/input.gleam @@ -0,0 +1,24 @@ +import filepath +import gladvent/internal/cmd +import gleam/int + +pub type Kind { + Example + Puzzle +} + +pub fn get_file_path(year: Int, day: Int, input_kind: Kind) -> String { + filepath.join(dir(year), int.to_string(day)) + <> case input_kind { + Example -> ".example.txt" + Puzzle -> ".txt" + } +} + +pub fn root() { + filepath.join(cmd.root(), "input") +} + +pub fn dir(year) { + filepath.join(root(), int.to_string(year)) +}