Skip to content

Commit

Permalink
get ready for v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Dec 6, 2024
1 parent 6f2b022 commit 801efa6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 60 deletions.
2 changes: 0 additions & 2 deletions .mise.toml

This file was deleted.

28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ _Note:_
Part 1: error: todo - unimplemented in module aoc_2024/day_1 in function pt_1 at line 2
```

## Fetching problem inputs

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.

Some things to note:
- The `AOC_COOKIE` environment variable must be set with your advent of code session cookie.
- 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.

## Reusable parse funtions

Expand Down Expand Up @@ -145,16 +152,21 @@ _Note_: gladvent will not compare your solution output against the expectations

### Why did you make this?

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

### Why does this not download the input from the advent of code website?

A few reasons:

1. I wanted to keep this utility as simple as possible to start with
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.
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.

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

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.
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!

### Why did you change your mind on fetching inputs?

I started to reflect a bit after gladvent's users kept asking for the feature...

While my initial rationale was twofold:
1. To encourage people to use the advent of code website, and I felt like fetching inputs somehow took away from that.
2. To minimise the risk that people would use a tool I made to spam the advent of code website with requests.

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):
- fetching the description of the daily problems
- submitting solutions
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "gladvent"
version = "2.0.2"
version = "2.1.0"
repository = { type = "github", user = "TanklesXL", repo = "gladvent" }
description = "An Advent Of Code runner for gleam"
licences = ["Apache-2.0"]
Expand Down
6 changes: 3 additions & 3 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ packages = [
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
{ 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" },
{ 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" },
{ name = "gleam_erlang", version = "0.30.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "760618870AE4A497B10C73548E6E44F43B76292A54F0207B3771CBB599C675B4" },
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" },
{ name = "gleam_http", version = "3.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "A9EE0722106FCCAB8AD3BF9D0A3EFF92BFE8561D59B83BAE96EB0BE1938D4E0F" },
{ 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" },
{ name = "gleam_json", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "0A57FB5666E695FD2BEE74C0428A98B0FC11A395D2C7B4CDF5E22C5DD32C74C6" },
{ name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" },
{ 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" },
{ name = "gleam_stdlib", version = "0.43.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "69EF22E78FDCA9097CBE7DF91C05B2A8B5436826D9F66680D879182C0860A747" },
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
{ name = "glearray", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "B99767A9BC63EF9CC8809F66C7276042E5EFEACAA5B25188B552D3691B91AC6D" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ 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" },
Expand All @@ -23,7 +23,7 @@ packages = [
{ name = "shellout", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "E2FCD18957F0E9F67E1F497FC9FF57393392F8A9BAEAEA4779541DE7A68DD7E0" },
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
{ name = "snag", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "08E9EB87C413457DB1DD66CD704C6878DACC9C93B418600F63873D0CD224E756" },
{ 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" },
{ 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" },
{ name = "tom", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "228E667239504B57AD05EC3C332C930391592F6C974D0EFECF32FFD0F3629A27" },
]

Expand Down
74 changes: 28 additions & 46 deletions src/gladvent/internal/cmd/new.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import gladvent/internal/cmd
import gladvent/internal/input
import gladvent/internal/parse.{type Day}
import gladvent/internal/util
import gleam/bool
import gleam/http
import gleam/http/request
import gleam/http/response
Expand Down Expand Up @@ -34,7 +35,7 @@ fn create_src_file(ctx: Context) -> fn() -> Result(String, Err) {

use _ <- result.try(
simplifile.create_file(gleam_src_path)
|> result.map_error(handle_file_open_failure(_, gleam_src_path)),
|> result.map_error(FailedToCreateFile(gleam_src_path, _)),
)

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

simplifile.write(gleam_src_path, file_data)
|> result.map_error(handle_file_open_failure(_, gleam_src_path))
|> result.map_error(FailedToWriteToFile(gleam_src_path, _))
|> result.replace(gleam_src_path)
}
}
Expand All @@ -54,32 +55,26 @@ fn create_input_file(
) -> fn() -> Result(String, Err) {
fn() {
let input_path = input.get_file_path(ctx.year, ctx.day, kind)
case kind {
input.Puzzle if ctx.fetch_input -> {
case simplifile.file_info(input_path) {
Error(_) -> {
use content <- result.try(download_input(ctx))
simplifile.write(input_path, content)
|> result.map_error(FailedToWriteToFile(_))
}
_ -> Error(FileAlreadyExists(input_path))
}
}
_ -> {
simplifile.create_file(input_path)
|> result.map_error(handle_file_open_failure(_, input_path))
}
}
use Nil <- result.try(
simplifile.create_file(input_path)
|> result.map_error(FailedToCreateFile(input_path, _)),
)
use <- bool.guard(
when: kind == input.Example || !ctx.fetch_input,
return: Ok(input_path),
)
use content <- result.try(download_input(ctx))
simplifile.write(input_path, content)
|> result.map_error(FailedToWriteToFile(input_path, _))
|> result.replace(input_path)
}
}

type Err {
CookieNotDefined
FailedToCreateDir(String)
FailedToCreateFile(String)
FailedToWriteToFile(simplifile.FileError)
FileAlreadyExists(String)
FailedToCreateDir(String, simplifile.FileError)
FailedToCreateFile(String, simplifile.FileError)
FailedToWriteToFile(String, simplifile.FileError)
HttpError(httpc.HttpError)
UnexpectedHttpResponse(response.Response(String))
}
Expand All @@ -88,11 +83,12 @@ fn err_to_string(e: Err) -> String {
case e {
CookieNotDefined ->
"'" <> aoc_cookie_name <> "' environment variable not defined"
FailedToCreateDir(d) -> "failed to create dir: " <> d
FailedToCreateFile(f) -> "failed to create file: " <> f
FailedToWriteToFile(e) ->
"failed to write to file:" <> simplifile.describe_error(e)
FileAlreadyExists(f) -> "file already exists: " <> f
FailedToCreateDir(d, e) ->
"failed to create dir '" <> d <> "': " <> simplifile.describe_error(e)
FailedToCreateFile(f, e) ->
"failed to create file '" <> f <> "': " <> simplifile.describe_error(e)
FailedToWriteToFile(f, e) ->
"failed to write to file '" <> f <> "': " <> simplifile.describe_error(e)
HttpError(e) ->
"HTTP error while fetching input file: " <> string.inspect(e)
UnexpectedHttpResponse(r) ->
Expand Down Expand Up @@ -121,28 +117,14 @@ fn handle_dir_open_res(
case res {
Ok(_) -> Ok(filename)
Error(simplifile.Eexist) -> Ok("")
_ ->
filename
|> FailedToCreateDir
|> Error
}
}

fn handle_file_open_failure(
reason: simplifile.FileError,
filename: String,
) -> Err {
case reason {
simplifile.Eexist -> FileAlreadyExists(filename)
_ -> FailedToCreateFile(filename)
Error(e) -> Error(FailedToCreateDir(filename, e))
}
}

fn get_cookie_value() -> Result(String, Err) {
case envoy.get(aoc_cookie_name) {
Ok(cookie) -> Ok(cookie)
_ -> Error(CookieNotDefined)
}
aoc_cookie_name
|> envoy.get()
|> result.replace_error(CookieNotDefined)
}

fn download_input(ctx: Context) -> Result(String, Err) {
Expand All @@ -161,7 +143,7 @@ fn download_input(ctx: Context) -> Result(String, Err) {
|> request.set_cookie("session", cookie)
|> request.set_header("user-agent", "github.com/TanklesXL/gladvent")
|> httpc.send()
|> result.map_error(HttpError(_)),
|> result.map_error(HttpError),
)
case resp.status {
200 -> Ok(resp.body)
Expand Down

0 comments on commit 801efa6

Please sign in to comment.