Skip to content

Commit 9c91a6c

Browse files
committed
get ready for v2.1.0
1 parent 6f2b022 commit 9c91a6c

File tree

5 files changed

+59
-66
lines changed

5 files changed

+59
-66
lines changed

.mise.toml

-2
This file was deleted.

README.md

+27-14
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ _Note:_
7474
Part 1: error: todo - unimplemented in module aoc_2024/day_1 in function pt_1 at line 2
7575
```
7676

77+
## Fetching problem inputs
78+
79+
When stubbing out a new day's solution with the `new` command, you can use the `--fetch` flag to tell gladvent to fetch your problem input from the advent of code website.
80+
81+
Some things to note:
82+
- The `AOC_COOKIE` environment variable must be set with your advent of code session cookie.
83+
- Gladvent will only attempt to fetch your input if the input file for the day being requested does not exist. This is to prevent accidental and redundant calls to the advent of code website, there should be no reason to fetch input data for the same day more than once.
7784

7885
## Reusable parse funtions
7986

@@ -110,17 +117,18 @@ Gladvent makes it easy for you to define expected outputs in your `gleam.toml` f
110117
Defining expectations is as simple as adding sections to your `gleam.toml` in the following format:
111118

112119
```toml
113-
[gladvent.<year as int>.<day as int>]
114-
pt_1 = <int or string>
115-
pt_2 = <int or string>
120+
[gladvent.<year as int>]
121+
1 = { pt_1 = <int or string>, pt_2 = <int or string> }
122+
2 = { pt_1 = <int or string>, pt_2 = <int or string> }
123+
3 = { pt_1 = <int or string>, pt_2 = <int or string> }
124+
...
116125
```
117126

118127
For example, to set the expectations for Dec 1st 2024 (2024 day 1) you would add something like:
119128

120129
```toml
121-
[gladvent.2024.1]
122-
pt_1 = 1
123-
pt_2 = 2
130+
[gladvent.2024]
131+
1 = { pt_1 = 1, pt_2 = 2 }
124132
```
125133

126134
When running, gladvent will detect whether a specific day has it's expectations set and if so will print out the result for you.
@@ -145,16 +153,21 @@ _Note_: gladvent will not compare your solution output against the expectations
145153

146154
### Why did you make this?
147155

148-
It seemed fun, I like small command line utilities and I wanted a way to get advent of code done in gleam without having the additional overhead of lots of copy-pasting and connecting things to get it to run
149-
150-
### Why does this not download the input from the advent of code website?
151-
152-
A few reasons:
153-
154-
1. I wanted to keep this utility as simple as possible to start with
155-
2. I like the advent of code website and I felt like it was a shame to circumvent visiting it, especially since you should access it to read the daily challenge. On top of that, I would like to avoid spamming the `advent of code` api if possible.
156+
It seemed fun, I like small command line utilities and I wanted a way to get advent of code done in gleam without having the overhead of lots of copy-pasting and connecting things to get it to run.
156157

157158
### Why run as a command line utility and not just use unit tests?
158159

159160
I thought a lot about that and I just prefer the overall interactivity of a CLI better, as well as allowing for endless runs or runs with configurable timeouts.
160161
Having it run as part of `eunit` doesnt provide as much flexibility as I would like. Other testing frameworks have been popping up but I leave the decision to use them up to you!
162+
163+
### Why did you change your mind on fetching inputs?
164+
165+
I started to reflect a bit after gladvent's users kept asking for the feature...
166+
167+
While my initial rationale was twofold:
168+
1. To encourage people to use the advent of code website, and I felt like fetching inputs somehow took away from that.
169+
2. To minimise the risk that people would use a tool I made to spam the advent of code website with requests.
170+
171+
Fetching inputs in a smart way (only ever if your input file does not already exist, so you should only need to do it once per day) still requires users to visit the advent of code website for the following (things gladvent will never do):
172+
- fetching the description of the daily problems
173+
- submitting solutions

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "gladvent"
2-
version = "2.0.2"
2+
version = "2.1.0"
33
repository = { type = "github", user = "TanklesXL", repo = "gladvent" }
44
description = "An Advent Of Code runner for gleam"
55
licences = ["Apache-2.0"]

manifest.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ packages = [
88
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
99
{ name = "gleam_community_ansi", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "4CD513FC62523053E62ED7BAC2F36136EC17D6A8942728250A9A00A15E340E4B" },
1010
{ name = "gleam_community_colour", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "386CB9B01B33371538672EEA8A6375A0A0ADEF41F17C86DDCB81C92AD00DA610" },
11-
{ name = "gleam_erlang", version = "0.30.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "760618870AE4A497B10C73548E6E44F43B76292A54F0207B3771CBB599C675B4" },
11+
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" },
1212
{ name = "gleam_http", version = "3.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "A9EE0722106FCCAB8AD3BF9D0A3EFF92BFE8561D59B83BAE96EB0BE1938D4E0F" },
1313
{ name = "gleam_httpc", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "76FEEC99473E568EBA34336A37CF3D54629ACE77712950DC9BB097B5FD664664" },
1414
{ name = "gleam_json", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "0A57FB5666E695FD2BEE74C0428A98B0FC11A395D2C7B4CDF5E22C5DD32C74C6" },
1515
{ name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" },
1616
{ name = "gleam_package_interface", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "CF3BFC5D0997750D9550D8D73A90F4B8D71C6C081B20ED4E70FFBE1E99AFC3C2" },
17-
{ name = "gleam_stdlib", version = "0.43.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "69EF22E78FDCA9097CBE7DF91C05B2A8B5436826D9F66680D879182C0860A747" },
17+
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
1818
{ name = "glearray", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "B99767A9BC63EF9CC8809F66C7276042E5EFEACAA5B25188B552D3691B91AC6D" },
1919
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
2020
{ name = "glint", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "5F6720081150AED8023131B0F3A35F9B0D6426A96CE02BEC52AD7018DF70566A" },
@@ -23,7 +23,7 @@ packages = [
2323
{ name = "shellout", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "E2FCD18957F0E9F67E1F497FC9FF57393392F8A9BAEAEA4779541DE7A68DD7E0" },
2424
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
2525
{ name = "snag", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "08E9EB87C413457DB1DD66CD704C6878DACC9C93B418600F63873D0CD224E756" },
26-
{ name = "spinner", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib", "glearray", "repeatedly"], otp_app = "spinner", source = "hex", outer_checksum = "9EE43AA33BE2DA5731B8F3F170AAB59AF1A815AFA5BF615F12C1B91F3B03F157" },
26+
{ name = "spinner", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib", "glearray", "repeatedly"], otp_app = "spinner", source = "hex", outer_checksum = "B824C4CFDA6AC912D14365BF365F2A52C4DA63EF2D768D2A1C46D9BF7AF669E7" },
2727
{ name = "tom", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "228E667239504B57AD05EC3C332C930391592F6C974D0EFECF32FFD0F3629A27" },
2828
]
2929

src/gladvent/internal/cmd/new.gleam

+28-46
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import gladvent/internal/cmd
44
import gladvent/internal/input
55
import gladvent/internal/parse.{type Day}
66
import gladvent/internal/util
7+
import gleam/bool
78
import gleam/http
89
import gleam/http/request
910
import gleam/http/response
@@ -34,7 +35,7 @@ fn create_src_file(ctx: Context) -> fn() -> Result(String, Err) {
3435

3536
use _ <- result.try(
3637
simplifile.create_file(gleam_src_path)
37-
|> result.map_error(handle_file_open_failure(_, gleam_src_path)),
38+
|> result.map_error(FailedToCreateFile(gleam_src_path, _)),
3839
)
3940

4041
let file_data = case ctx.add_parse {
@@ -43,7 +44,7 @@ fn create_src_file(ctx: Context) -> fn() -> Result(String, Err) {
4344
}
4445

4546
simplifile.write(gleam_src_path, file_data)
46-
|> result.map_error(handle_file_open_failure(_, gleam_src_path))
47+
|> result.map_error(FailedToWriteToFile(gleam_src_path, _))
4748
|> result.replace(gleam_src_path)
4849
}
4950
}
@@ -54,32 +55,26 @@ fn create_input_file(
5455
) -> fn() -> Result(String, Err) {
5556
fn() {
5657
let input_path = input.get_file_path(ctx.year, ctx.day, kind)
57-
case kind {
58-
input.Puzzle if ctx.fetch_input -> {
59-
case simplifile.file_info(input_path) {
60-
Error(_) -> {
61-
use content <- result.try(download_input(ctx))
62-
simplifile.write(input_path, content)
63-
|> result.map_error(FailedToWriteToFile(_))
64-
}
65-
_ -> Error(FileAlreadyExists(input_path))
66-
}
67-
}
68-
_ -> {
69-
simplifile.create_file(input_path)
70-
|> result.map_error(handle_file_open_failure(_, input_path))
71-
}
72-
}
58+
use Nil <- result.try(
59+
simplifile.create_file(input_path)
60+
|> result.map_error(FailedToCreateFile(input_path, _)),
61+
)
62+
use <- bool.guard(
63+
when: kind == input.Example || !ctx.fetch_input,
64+
return: Ok(input_path),
65+
)
66+
use content <- result.try(download_input(ctx))
67+
simplifile.write(input_path, content)
68+
|> result.map_error(FailedToWriteToFile(input_path, _))
7369
|> result.replace(input_path)
7470
}
7571
}
7672

7773
type Err {
7874
CookieNotDefined
79-
FailedToCreateDir(String)
80-
FailedToCreateFile(String)
81-
FailedToWriteToFile(simplifile.FileError)
82-
FileAlreadyExists(String)
75+
FailedToCreateDir(String, simplifile.FileError)
76+
FailedToCreateFile(String, simplifile.FileError)
77+
FailedToWriteToFile(String, simplifile.FileError)
8378
HttpError(httpc.HttpError)
8479
UnexpectedHttpResponse(response.Response(String))
8580
}
@@ -88,11 +83,12 @@ fn err_to_string(e: Err) -> String {
8883
case e {
8984
CookieNotDefined ->
9085
"'" <> aoc_cookie_name <> "' environment variable not defined"
91-
FailedToCreateDir(d) -> "failed to create dir: " <> d
92-
FailedToCreateFile(f) -> "failed to create file: " <> f
93-
FailedToWriteToFile(e) ->
94-
"failed to write to file:" <> simplifile.describe_error(e)
95-
FileAlreadyExists(f) -> "file already exists: " <> f
86+
FailedToCreateDir(d, e) ->
87+
"failed to create dir '" <> d <> "': " <> simplifile.describe_error(e)
88+
FailedToCreateFile(f, e) ->
89+
"failed to create file '" <> f <> "': " <> simplifile.describe_error(e)
90+
FailedToWriteToFile(f, e) ->
91+
"failed to write to file '" <> f <> "': " <> simplifile.describe_error(e)
9692
HttpError(e) ->
9793
"HTTP error while fetching input file: " <> string.inspect(e)
9894
UnexpectedHttpResponse(r) ->
@@ -121,28 +117,14 @@ fn handle_dir_open_res(
121117
case res {
122118
Ok(_) -> Ok(filename)
123119
Error(simplifile.Eexist) -> Ok("")
124-
_ ->
125-
filename
126-
|> FailedToCreateDir
127-
|> Error
128-
}
129-
}
130-
131-
fn handle_file_open_failure(
132-
reason: simplifile.FileError,
133-
filename: String,
134-
) -> Err {
135-
case reason {
136-
simplifile.Eexist -> FileAlreadyExists(filename)
137-
_ -> FailedToCreateFile(filename)
120+
Error(e) -> Error(FailedToCreateDir(filename, e))
138121
}
139122
}
140123

141124
fn get_cookie_value() -> Result(String, Err) {
142-
case envoy.get(aoc_cookie_name) {
143-
Ok(cookie) -> Ok(cookie)
144-
_ -> Error(CookieNotDefined)
145-
}
125+
aoc_cookie_name
126+
|> envoy.get()
127+
|> result.replace_error(CookieNotDefined)
146128
}
147129

148130
fn download_input(ctx: Context) -> Result(String, Err) {
@@ -161,7 +143,7 @@ fn download_input(ctx: Context) -> Result(String, Err) {
161143
|> request.set_cookie("session", cookie)
162144
|> request.set_header("user-agent", "github.com/TanklesXL/gladvent")
163145
|> httpc.send()
164-
|> result.map_error(HttpError(_)),
146+
|> result.map_error(HttpError),
165147
)
166148
case resp.status {
167149
200 -> Ok(resp.body)

0 commit comments

Comments
 (0)