-module(gladvent@internal@cmd@new). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new_command/0]). -export_type([context/0, success/0, err/0]). -type context() :: {context, integer(), integer(), boolean(), boolean(), boolean()}. -type success() :: {dir, binary()} | {file, binary()}. -type err() :: cookie_not_defined | {failed_to_create_dir, binary(), simplifile:file_error()} | {failed_to_create_file, binary(), simplifile:file_error()} | {failed_to_write_to_file, binary(), simplifile:file_error()} | {http_error, gleam@httpc:http_error()} | {unexpected_http_response, gleam@http@response:response(binary())}. -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 107). -spec gleam_src_path(integer(), integer()) -> binary(). gleam_src_path(Year, Day) -> filepath:join( gladvent@internal@cmd:src_dir(Year), <<<<"day_"/utf8, (erlang:integer_to_binary(Day))/binary>>/binary, ".gleam"/utf8>> ). -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 119). -spec handle_dir_open_res( {ok, any()} | {error, simplifile:file_error()}, binary() ) -> {ok, binary()} | {error, err()}. handle_dir_open_res(Res, Filename) -> case Res of {ok, _} -> {ok, Filename}; {error, eexist} -> {ok, <<""/utf8>>}; {error, E} -> {error, {failed_to_create_dir, Filename, E}} end. -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 111). -spec create_dir(binary()) -> fun(() -> {ok, success()} | {error, err()}). create_dir(Dir) -> fun() -> _pipe = simplifile:create_directory_all(Dir), _pipe@1 = handle_dir_open_res(_pipe, Dir), gleam@result:map(_pipe@1, fun(Field@0) -> {dir, Field@0} end) end. -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 230). -spec collect(integer(), {integer(), binary()}) -> binary(). collect(Year, X) -> Day = erlang:integer_to_binary(erlang:element(1, X)), Year@1 = erlang:integer_to_binary(Year), <<<<<<<<<<"initialized "/utf8, Year@1/binary>>/binary, " day "/utf8>>/binary, Day/binary>>/binary, "\n"/utf8>>/binary, (erlang:element(2, X))/binary>>. -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 219). -spec collect_async(integer(), {integer(), {ok, binary()} | {error, binary()}}) -> binary(). collect_async(Year, X) -> _pipe = fun(Res) -> case Res of {ok, Res@1} -> Res@1; {error, Err} -> Err end end, _pipe@1 = gleam@pair:map_second(X, _pipe), collect(Year, _pipe@1). -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 87). -spec err_to_string(err()) -> binary(). err_to_string(E) -> case E of cookie_not_defined -> <<<<"'"/utf8, "AOC_COOKIE"/utf8>>/binary, "' environment variable not defined"/utf8>>; {failed_to_create_dir, D, E@1} -> <<<<<<"failed to create dir '"/utf8, D/binary>>/binary, "': "/utf8>>/binary, (simplifile:describe_error(E@1))/binary>>; {failed_to_create_file, F, E@2} -> <<<<<<"failed to create file '"/utf8, F/binary>>/binary, "': "/utf8>>/binary, (simplifile:describe_error(E@2))/binary>>; {failed_to_write_to_file, F@1, E@3} -> <<<<<<"failed to write to file '"/utf8, F@1/binary>>/binary, "': "/utf8>>/binary, (simplifile:describe_error(E@3))/binary>>; {http_error, E@4} -> <<"HTTP error while fetching input file: "/utf8, (gleam@string:inspect(E@4))/binary>>; {unexpected_http_response, R} -> <<<<<<"unexpected HTTP response ("/utf8, (erlang:integer_to_binary(erlang:element(2, R)))/binary>>/binary, ") while fetching input file: "/utf8>>/binary, (erlang:element(4, R))/binary>> end. -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 130). -spec get_cookie_value() -> {ok, binary()} | {error, err()}. get_cookie_value() -> _pipe = <<"AOC_COOKIE"/utf8>>, _pipe@1 = envoy_ffi:get(_pipe), gleam@result:replace_error(_pipe@1, cookie_not_defined). -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 136). -spec download_input(context()) -> {ok, binary()} | {error, err()}. download_input(Ctx) -> gleam@result:'try'( get_cookie_value(), fun(Cookie) -> gleam@result:'try'( begin _pipe = gleam@http@request:new(), _pipe@1 = gleam@http@request:set_host( _pipe, <<"adventofcode.com"/utf8>> ), _pipe@2 = gleam@http@request:set_path( _pipe@1, <<<<<<<<"/"/utf8, (erlang:integer_to_binary( erlang:element(2, Ctx) ))/binary>>/binary, "/day/"/utf8>>/binary, (erlang:integer_to_binary( erlang:element(3, Ctx) ))/binary>>/binary, "/input"/utf8>> ), _pipe@3 = gleam@http@request:set_scheme(_pipe@2, https), _pipe@4 = gleam@http@request:set_cookie( _pipe@3, <<"session"/utf8>>, Cookie ), _pipe@5 = gleam@http@request:set_header( _pipe@4, <<"user-agent"/utf8>>, <<"github.com/TanklesXL/gladvent"/utf8>> ), _pipe@6 = gleam@httpc:send(_pipe@5), gleam@result:map_error( _pipe@6, fun(Field@0) -> {http_error, Field@0} end ) end, fun(Resp) -> case erlang:element(2, Resp) of 200 -> {ok, erlang:element(4, Resp)}; _ -> {error, {unexpected_http_response, Resp}} end end ) end ). -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 52). -spec create_input_file(context(), gladvent@internal@input:kind()) -> fun(() -> {ok, success()} | {error, err()}). create_input_file(Ctx, Kind) -> fun() -> Input_path = gladvent@internal@input:get_file_path( erlang:element(2, Ctx), erlang:element(3, Ctx), Kind ), gleam@result:'try'( begin _pipe = simplifile:create_file(Input_path), gleam@result:map_error( _pipe, fun(_capture) -> {failed_to_create_file, Input_path, _capture} end ) end, fun(_use0) -> nil = _use0, gleam@bool:guard( (Kind =:= example) orelse not erlang:element(6, Ctx), {ok, {file, Input_path}}, fun() -> gleam@result:'try'( download_input(Ctx), fun(Content) -> _pipe@1 = simplifile:write(Input_path, Content), _pipe@2 = gleam@result:map_error( _pipe@1, fun(_capture@1) -> {failed_to_write_to_file, Input_path, _capture@1} end ), gleam@result:replace( _pipe@2, {file, Input_path} ) end ) end ) end ) end. -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 32). -spec create_src_file(context()) -> fun(() -> {ok, success()} | {error, err()}). create_src_file(Ctx) -> fun() -> Gleam_src_path = gleam_src_path( erlang:element(2, Ctx), erlang:element(3, Ctx) ), gleam@result:'try'( begin _pipe = simplifile:create_file(Gleam_src_path), gleam@result:map_error( _pipe, fun(_capture) -> {failed_to_create_file, Gleam_src_path, _capture} end ) end, fun(_) -> File_data = case erlang:element(4, Ctx) of true -> <<<<"pub fn parse(input: String) -> String { todo as \"parse not implemented\" } "/utf8, "\n"/utf8>>/binary, "pub fn pt_1(input: String) { todo as \"part 1 not implemented\" } pub fn pt_2(input: String) { todo as \"part 2 not implemented\" } "/utf8>>; false -> <<"pub fn pt_1(input: String) { todo as \"part 1 not implemented\" } pub fn pt_2(input: String) { todo as \"part 2 not implemented\" } "/utf8>> end, _pipe@1 = simplifile:write(Gleam_src_path, File_data), _pipe@2 = gleam@result:map_error( _pipe@1, fun(_capture@1) -> {failed_to_write_to_file, Gleam_src_path, _capture@1} end ), gleam@result:replace(_pipe@2, {file, Gleam_src_path}) end ) end. -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 160). -spec do(context()) -> binary(). do(Ctx) -> Seq = [create_dir(gladvent@internal@input:dir(erlang:element(2, Ctx))), create_input_file(Ctx, puzzle), create_dir(gladvent@internal@cmd:src_dir(erlang:element(2, Ctx))), create_src_file(Ctx) | case erlang:element(5, Ctx) of true -> [create_input_file(Ctx, example)]; false -> [] end], Successes = fun(Good) -> case Good of <<""/utf8>> -> <<""/utf8>>; _ -> <<"created:"/utf8, Good/binary>> end end, Errors = fun(Errs) -> case Errs of <<""/utf8>> -> <<""/utf8>>; _ -> <<"errors:"/utf8, Errs/binary>> end end, Newline_tab = fun(A, B) -> <<<>/binary, B/binary>> end, {Good@1, Bad} = begin _pipe = (gleam@list:fold( Seq, {<<""/utf8>>, <<""/utf8>>}, fun(Acc, F) -> case F() of {ok, {dir, _}} -> Acc; {ok, {file, O}} -> gleam@pair:map_first( Acc, fun(_capture) -> Newline_tab(_capture, O) end ); {error, Err} -> gleam@pair:map_second( Acc, fun(_capture@1) -> Newline_tab(_capture@1, err_to_string(Err)) end ) end end )), _pipe@1 = gleam@pair:map_first(_pipe, Successes), gleam@pair:map_second(_pipe@1, Errors) end, _pipe@2 = [Good@1, Bad], _pipe@3 = gleam@list:filter(_pipe@2, fun(S) -> S /= <<""/utf8>> end), gleam@string:join(_pipe@3, <<"\n\n"/utf8>>). -file("/home/runner/work/gladvent/gladvent/src/gladvent/internal/cmd/new.gleam", 237). -spec new_command() -> glint:command({ok, list(binary())} | {error, snag:snag()}). new_command() -> glint:command_help( <<"Create .gleam and input files"/utf8>>, fun() -> glint:unnamed_args( {min_args, 1}, fun() -> glint:flag( begin _pipe = glint:bool_flag(<<"parse"/utf8>>), _pipe@1 = glint:flag_default(_pipe, false), glint:flag_help( _pipe@1, <<"Generate day runners with a parse function"/utf8>> ) end, fun(Parse_flag) -> glint:flag( begin _pipe@2 = glint:bool_flag( <<"example"/utf8>> ), _pipe@3 = glint:flag_default(_pipe@2, false), glint:flag_help( _pipe@3, <<"Generate example input files to run your solution against"/utf8>> ) end, fun(Example_flag) -> glint:flag( begin _pipe@4 = glint:bool_flag( <<"fetch"/utf8>> ), _pipe@5 = glint:flag_default( _pipe@4, false ), glint:flag_help( _pipe@5, <<<<"Fetch your own input from the AoC website. Needs to have your AoC cookie stored in the '"/utf8, "AOC_COOKIE"/utf8>>/binary, "' environment variable"/utf8>> ) end, fun(Fetch_flag) -> glint:command( fun(_, Args, Flags) -> gleam@result:map( gladvent@internal@parse:days( Args ), fun(Days) -> Days@1 = gladvent@internal@util:deduplicate_sort( Days ), _assert_subject = glint:get_flag( Flags, gladvent@internal@cmd:year_flag( ) ), {ok, Year} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error( #{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"gladvent/internal/cmd/new"/utf8>>, function => <<"new_command"/utf8>>, line => 262} ) end, _assert_subject@1 = Parse_flag( Flags ), {ok, Add_parse} = case _assert_subject@1 of {ok, _} -> _assert_subject@1; _assert_fail@1 -> erlang:error( #{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail@1, module => <<"gladvent/internal/cmd/new"/utf8>>, function => <<"new_command"/utf8>>, line => 263} ) end, _assert_subject@2 = Example_flag( Flags ), {ok, Create_example_file} = case _assert_subject@2 of {ok, _} -> _assert_subject@2; _assert_fail@2 -> erlang:error( #{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail@2, module => <<"gladvent/internal/cmd/new"/utf8>>, function => <<"new_command"/utf8>>, line => 264} ) end, _assert_subject@3 = Fetch_flag( Flags ), {ok, Fetch_input} = case _assert_subject@3 of {ok, _} -> _assert_subject@3; _assert_fail@3 -> erlang:error( #{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail@3, module => <<"gladvent/internal/cmd/new"/utf8>>, function => <<"new_command"/utf8>>, line => 265} ) end, gladvent@internal@cmd:exec( Days@1, endless, fun(Day) -> do( {context, Year, Day, Add_parse, Create_example_file, Fetch_input} ) end, fun(_capture) -> collect_async( Year, _capture ) end ) end ) end ) end ) end ) end ) end ) end ).