Skip to content

Commit

Permalink
use the new gleam package interface generator for type checking parse…
Browse files Browse the repository at this point in the history
… and run functions (#10)

- use package interface functionality
- upgrade to gleam 1.0
- update CI to use relaxed version constraints
  • Loading branch information
TanklesXL committed Mar 6, 2024
1 parent a7616ae commit 726533c
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 136 deletions.
9 changes: 7 additions & 2 deletions .github/actions/test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ inputs:
gleam-version:
description: gleam version
required: false
default: 1.0.0-rc2
default: "~> 1.0"
erlang-version:
description: erlang-otp version
required: false
default: 26.2.2
default: "~> 26.2"
rebar3-version:
description: rebar3 version
required: false
default: "~> 3.18"

runs:
using: composite
Expand All @@ -19,6 +23,7 @@ runs:
with:
otp-version: ${{ inputs.erlang-version }}
gleam-version: ${{ inputs.gleam-version }}
rebar3-version: ${{ inputs.rebar3-version }}
- uses: actions/setup-node@v2
with:
node-version: ${{ inputs.node-version }}
Expand Down
7 changes: 1 addition & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
erlang: ["26.2.2"]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: ./.github/actions/test
with:
erlang-version: ${{ matrix.erlang }}
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Release
uses: softprops/action-gh-release@v1
hexpm-publish:
runs-on: ubuntu-latest
environment: publish
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: ./.github/actions/test
- name: publish to hex
env:
Expand Down
16 changes: 10 additions & 6 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ version = "0.6.2"
repository = { type = "github", user = "TanklesXL", repo = "gladvent" }
description = "An Advent Of Code runner for gleam"
licences = ["Apache-2.0"]
internal_modules = ["gladvent/internal/*", "aoc_*"]
gleam = "~> 0.34 or ~> 1.0"
internal_modules = ["gladvent/internal/*"]
gleam = "~> 1.0"

[dependencies]
gleam_stdlib = "~> 0.34 or ~> 1.0"
glint = "~> 0.17"
simplifile = "~> 0.3"
argv = "~> 1.0"
shellout = "~> 1.6"
gleam_package_interface = "~> 1.0"
gleam_json = "~> 1.0"
spinner = "~> 1.1"
gleam_stdlib = "~> 0.36 or ~> 1.0"
gleam_otp = "~> 0.4"
gleam_erlang = "~> 0.7"
snag = "~> 0.2"
glint = " ~> 0.16.0"
simplifile = "~> 0.3"
argv = "~> 1.0"

[dev-dependencies]
gleeunit = "~> 1.0"
19 changes: 15 additions & 4 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@ packages = [
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "A49A5E3AE8B637A5ACBA80ECB9B1AFE89FD3D5351FF6410A42B84F666D40D7D5" },
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" },
{ name = "gleam_stdlib", version = "0.35.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5443EEB74708454B65650FEBBB1EF5175057D1DEC62AEA9D7C6D96F41DA79152" },
{ name = "gleam_package_interface", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "52A721BCA972C8099BB881195D821AAA64B9F2655BECC102165D5A1097731F01" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "glearray", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "908154F695D330E06A37FAB2C04119E8F315D643206F8F32B6A6C14A8709FFF4" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "glint", version = "0.16.0-rc1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "AF9A7CCAD9FC8CDFF2606F97BC52C2D98559F27EB28CD3B8A11A35641740AE2F" },
{ name = "glint", version = "0.17.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "DF094D7A97C54F4A23F670A7109A188DCBBDD17E003B3E650566835BDBA6BFDF" },
{ name = "repeatedly", version = "2.1.1", build_tools = ["gleam"], requirements = [], otp_app = "repeatedly", source = "hex", outer_checksum = "38808C3EC382B0CD981336D5879C24ECB37FCB9C1D1BD128F7A80B0F74404D79" },
{ name = "shellout", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "E2FCD18957F0E9F67E1F497FC9FF57393392F8A9BAEAEA4779541DE7A68DD7E0" },
{ name = "simplifile", version = "0.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "8F3C94B83F691CCFACD784A4D7C1F7E5A0437D93341549B908EE3B32E3477447" },
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
{ name = "spinner", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "glearray", "repeatedly"], otp_app = "spinner", source = "hex", outer_checksum = "200BA3D4A04D468898E63C0D316E23F526E02514BC46454091975CB5BAE41E8F" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]

[requirements]
argv = { version = "~> 1.0" }
gleam_erlang = { version = "~> 0.7" }
gleam_json = { version = "~> 1.0" }
gleam_otp = { version = "~> 0.4" }
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleam_package_interface = { version = "~> 1.0" }
gleam_stdlib = { version = "~> 0.36 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
glint = { version = " ~> 0.16.0-rc1" }
glint = { version = "~> 0.17" }
shellout = { version = "~> 1.6" }
simplifile = { version = "~> 0.3" }
snag = { version = "~> 0.2" }
spinner = { version = "~> 1.1" }
4 changes: 2 additions & 2 deletions src/gladvent/internal/cmd/new.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ fn do(ctx: Context) -> String {
}

const gleam_starter = "pub fn pt_1(input: String) {
todo
todo as \"part 1 not implemented\"
}
pub fn pt_2(input: String) {
todo
todo as \"part 2 not implemented\"
}
"

Expand Down
67 changes: 43 additions & 24 deletions src/gladvent/internal/cmd/run.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import gladvent/internal/cmd.{Ending, Endless}
import glint
import glint/flag
import gleam
import gladvent/internal/runners.{type RunnerMap}
import gladvent/internal/runners
import gleam/dynamic.{type Dynamic}
import gleam/option.{type Option, None}
import gleam/package_interface

type AsyncResult =
gleam.Result(RunResult, String)

type RunErr {
FailedToReadInput(String)
FailedToParseInput(String)
FailedToGetRunner(snag.Snag)
Unregistered(Day)
Other(String)
}
Expand All @@ -41,12 +43,13 @@ type SolveResult =
fn run_err_to_snag(err: RunErr) -> Snag {
case err {
Unregistered(day) ->
"day" <> " " <> int.to_string(day) <> " " <> "unregistered"
FailedToReadInput(input_path) -> "failed to read input file: " <> input_path
FailedToParseInput(err) -> "failed to parse input: " <> err
Other(s) -> s
snag.new("day" <> " " <> int.to_string(day) <> " " <> "unregistered")
FailedToReadInput(input_path) ->
snag.new("failed to read input file: " <> input_path)
FailedToParseInput(err) -> snag.new("failed to parse input: " <> err)
FailedToGetRunner(s) -> snag.layer(s, "failed to get runner")
Other(s) -> snag.new(s)
}
|> snag.new
}

type Direction {
Expand All @@ -62,11 +65,15 @@ fn string_trim(s: String, dir: Direction, sub: String) -> String {
@external(erlang, "string", "trim")
fn do_trim(a: String, b: Direction, c: Charlist) -> String

fn do(year: Int, day: Day, runners: RunnerMap, allow_crash: Bool) -> RunResult {
fn do(
year: Int,
day: Day,
package: package_interface.Package,
allow_crash: Bool,
) -> RunResult {
use #(pt_1, pt_2, parse) <- result.try(
runners
|> map.get(day)
|> result.replace_error(Unregistered(day)),
runners.get_day(package, year, day)
|> result.map_error(FailedToGetRunner),
)

let input_path =
Expand Down Expand Up @@ -251,14 +258,18 @@ pub fn run_command() -> glint.Command(Result(List(String))) {
{
use input <- glint.command()
let assert Ok(year) = flag.get_int(input.flags, cmd.year)
use runners <- result.then(runners.build_from_days_dir(year))
use allow_crash <- result.try(flag.get_bool(input.flags, allow_crash))
let assert Ok(allow_crash) = flag.get_bool(input.flags, allow_crash)

use days <- result.then(parse.days(input.args))
use package <- result.then(
runners.pkg_interface()
|> snag.context("failed to generate package interface"),
)

days
|> cmd.exec(
timing(input.flags),
do(year, _, runners, allow_crash),
do(year, _, package, allow_crash),
collect_async(year, _),
)
|> Ok
Expand All @@ -272,15 +283,29 @@ pub fn run_command() -> glint.Command(Result(List(String))) {
pub fn run_all_command() -> glint.Command(Result(List(String))) {
{
use input <- glint.command()
use allow_crash <- result.then(flag.get_bool(input.flags, allow_crash))
let assert Ok(allow_crash) = flag.get_bool(input.flags, allow_crash)
let assert Ok(year) = flag.get_int(input.flags, cmd.year)
use runners <- result.then(runners.build_from_days_dir(year))

runners
|> all_days
use package <- result.then(
runners.pkg_interface()
|> snag.context("failed to generate package interface"),
)

package.modules
|> map.keys
|> list.filter_map(fn(k) {
k
|> string.split_once("aoc_" <> int.to_string(year) <> "/day_")
|> result.try(fn(day) {
day.1
|> parse.day
|> result.replace_error(Nil)
})
})
|> list.sort(int.compare)
|> cmd.exec(
timing(input.flags),
do(year, _, runners, allow_crash),
do(year, _, package, allow_crash),
collect_async(year, _),
)
|> Ok
Expand All @@ -295,9 +320,3 @@ fn timing(flags: flag.Map) {
|> result.map(Ending)
|> result.unwrap(Endless)
}

fn all_days(runners) {
runners
|> map.keys()
|> list.sort(by: int.compare)
}
9 changes: 2 additions & 7 deletions src/gladvent/internal/file.gleam
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import simplifile.{type FileError}
import gleam/result
import gladvent/internal/util.{defer}

pub type IODevice

@external(erlang, "gladvent_ffi", "open_file_exclusive")
pub fn open_file_exclusive(s s: String) -> Result(IODevice, FileError)
pub fn open_file_exclusive(s: String) -> Result(IODevice, FileError)

@external(erlang, "gladvent_ffi", "write")
fn do_write(a: IODevice, b: String) -> Result(Nil, FileError)
Expand All @@ -26,9 +27,3 @@ pub fn do_with_file(
})
f(file)
}

fn defer(do later: fn() -> _, after now: fn() -> a) -> a {
let res = now()
later()
res
}

0 comments on commit 726533c

Please sign in to comment.