@@ -16,64 +16,81 @@ defmodule AdventOfCode.Helpers.Generator do
1616 - Test cases containing default tests for `run_1` and `run_2` at `test/<year>/day_<day>_test.exs` file
1717 """
1818 def run ( { year , day } ) do
19- # Write the input data at `priv/input_files`
20- input_file_path =
21- :advent_of_code
22- |> :code . priv_dir ( )
23- |> Path . join ( "input_files" )
24- |> Path . join ( "#{ year } _#{ day } .txt" )
19+ padded_day = zero_padded ( day )
2520
26- input_file =
27- input_file_path
28- |> create_input_file ( year , day )
21+ # Write the input data at `priv/input_files`
22+ input_path = "priv/input_files/ #{ year } _ #{ day } .txt"
23+ input_result = create_input_file ( input_path , year , day )
2924
3025 # Write code files at `lib/<year>/day_<day>.ex`
3126 code_content =
3227 @ code_template
3328 |> EEx . eval_file ( day: day , year: year , title: get_title ( year , day ) )
3429
35- code_file =
36- "lib/#{ year } /day_#{ zero_padded ( day ) } .ex"
37- |> create_file ( code_content )
30+ code_path = "lib/#{ year } /day_#{ padded_day } .ex"
31+ code_result = create_file ( code_path , code_content )
3832
3933 # Write test files at `test/<year>/day_<year>_test.exs`
4034 test_content =
4135 @ test_template
4236 |> EEx . eval_file ( day: day , year: year )
4337
44- test_file =
45- "test/#{ year } /day_#{ zero_padded ( day ) } _test.exs"
46- |> create_file ( test_content )
38+ test_path = "test/#{ year } /day_#{ padded_day } _test.exs"
39+ test_result = create_file ( test_path , test_content )
4740
48- "INPUT: #{ input_file } \t CODE: #{ code_file } \t TEST: #{ test_file } \n "
41+ [ input_result , code_result , test_result ]
4942 end
5043
5144 defp create_file ( path , content ) do
5245 if ! File . exists? ( path ) do
53- File . write ( path , content )
46+ path |> Path . dirname ( ) |> File . mkdir_p! ( )
47+
48+ case File . write ( path , content ) do
49+ :ok -> { :ok , path }
50+ { :error , reason } -> { :error , path , reason }
51+ end
52+ else
53+ { :exists , path }
5454 end
5555 end
5656
5757 defp fetch_cookie ( year , day ) do
58- HTTPoison . start ( )
59-
60- "https://adventofcode.com/#{ year } /day/#{ day } /input"
61- |> HTTPoison . get ( [ { "cookie" , "session=#{ System . get_env ( "COOKIE" , "" ) } " } ] )
62- |> then ( fn response ->
63- case response do
64- { :ok , % HTTPoison.Response { body: body } } -> { :ok , body }
65- _ -> nil
66- end
67- end )
58+ session_key = System . get_env ( "AOC_SESSION_KEY" )
59+
60+ if is_nil ( session_key ) or session_key == "" do
61+ { :error , "AOC_SESSION_KEY environment variable is not set" }
62+ else
63+ HTTPoison . start ( )
64+
65+ "https://adventofcode.com/#{ year } /day/#{ day } /input"
66+ |> HTTPoison . get ( [ { "cookie" , "session=#{ session_key } " } ] )
67+ |> then ( fn response ->
68+ case response do
69+ { :ok , % HTTPoison.Response { status_code: 200 , body: body } } -> { :ok , body }
70+ { :ok , % HTTPoison.Response { status_code: status } } -> { :error , "Received HTTP #{ status } " }
71+ { :error , % HTTPoison.Error { reason: reason } } -> { :error , reason }
72+ _ -> { :error , "unknown error" }
73+ end
74+ end )
75+ end
6876 end
6977
7078 defp create_input_file ( path , year , day ) do
7179 if ! File . exists? ( path ) do
72- with { :ok , data } <- fetch_cookie ( year , day ) ,
73- { :ok , file } <- File . open ( path , [ :write ] ) ,
74- :ok <- IO . write ( file , data ) do
75- File . close ( file )
80+ path |> Path . dirname ( ) |> File . mkdir_p! ( )
81+
82+ case fetch_cookie ( year , day ) do
83+ { :ok , data } ->
84+ case File . write ( path , data ) do
85+ :ok -> { :ok , path }
86+ { :error , reason } -> { :error , path , reason }
87+ end
88+
89+ { :error , reason } ->
90+ { :error , path , reason }
7691 end
92+ else
93+ { :exists , path }
7794 end
7895 end
7996
0 commit comments