Skip to content

Commit

Permalink
testing new glint v1 release candidate changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Mar 25, 2024
1 parent a372464 commit c5fb981
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 112 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ build
src/days
src/aoc_*
input
package-interface.json
4 changes: 2 additions & 2 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ internal_modules = ["gladvent/internal/*"]
gleam = "~> 1.0"

[dependencies]
glint = "~> 0.18"
simplifile = "~> 0.3"
glint = "1.0.0-rc1"
simplifile = "~> 1.0"
argv = "~> 1.0"
shellout = "~> 1.6"
gleam_package_interface = "~> 1.0"
Expand Down
16 changes: 8 additions & 8 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
# You typically do not need to edit this file

packages = [
{ name = "argv", version = "1.0.1", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "A6E9009E50BBE863EB37D963E4315398D41A3D87D0075480FC244125808F964A" },
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ 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_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ 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_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
{ 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.18.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "BB0F14643CC51C069A5DC6E9082EAFCD9967AFD1C9CC408803D1A40A3FD43B54" },
{ name = "glint", version = "1.0.0-rc1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "91EC8EFA3E8FBCB842C219AA02E8072204CEE02597B4C36C2ED78AD86DD1B2EC" },
{ 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 = "simplifile", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C44DB387524F90DC42142699C78C850003289D32C7C99C7D32873792A299CDF7" },
{ 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" },
Expand All @@ -29,8 +29,8 @@ gleam_otp = { version = "~> 0.4" }
gleam_package_interface = { version = "~> 1.0" }
gleam_stdlib = { version = "~> 0.36 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
glint = { version = "~> 0.18" }
glint = { version = "1.0.0-rc1" }
shellout = { version = "~> 1.6" }
simplifile = { version = "~> 0.3" }
simplifile = { version = "~> 1.0" }
snag = { version = "~> 0.2" }
spinner = { version = "~> 1.1" }
6 changes: 3 additions & 3 deletions src/gladvent.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import argv
pub fn main() {
let commands =
glint.new()
|> glint.with_name("gladvent")
|> glint.as_gleam_module
|> glint.name("gladvent")
|> glint.as_module
|> glint.group_flag(at: [], for: cmd.year, of: cmd.year_flag())
|> glint.with_pretty_help(glint.default_pretty_help())
|> glint.pretty_help(glint.default_pretty_help())
|> glint.add(at: ["new"], do: new.new_command())
|> glint.group_flag(at: ["run"], for: run.timeout, of: run.timeout_flag())
|> glint.group_flag(
Expand Down
22 changes: 10 additions & 12 deletions src/gladvent/internal/cmd.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import gleam/pair
import gleam/list
import gleam/int
import gleam/string
import glint/flag
import snag
import glint

pub fn input_dir(year) {
input_root <> int.to_string(year) <> "/"
Expand Down Expand Up @@ -91,15 +91,13 @@ fn current_year() -> Int {
pub const year = "year"

pub fn year_flag() {
flag.int()
|> flag.default(current_year())
|> flag.constraint(fn(year) {
case year < 2015 {
True ->
snag.error(
"advent of code did not exist prior to 2015, did you mistype?",
)
False -> Ok(Nil)
}
})
use year <- glint.constraint(
glint.int()
|> glint.default(current_year()),
)
case year < 2015 {
True ->
snag.error("advent of code did not exist prior to 2015, did you mistype?")
False -> Ok(year)
}
}
46 changes: 30 additions & 16 deletions src/gladvent/internal/cmd/new.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import gladvent/internal/file
import simplifile as efile
import gladvent/internal/cmd
import glint
import glint/flag
import gladvent/internal/parse.{type Day}
import gleam/pair

type Context {
Context(year: Int, day: Day)
Context(year: Int, day: Day, add_parse: Bool)
}

fn create_src_dir(ctx: Context) {
Expand All @@ -23,8 +22,13 @@ fn create_src_dir(ctx: Context) {
fn create_src_file(ctx: Context) {
let gleam_src_path = gleam_src_path(ctx.year, ctx.day)

let file_data = case ctx.add_parse {
True -> parse_starter <> "\n" <> gleam_starter
False -> gleam_starter
}

gleam_src_path
|> file.do_with_file(file.write(_, gleam_starter))
|> file.do_with_file(file.write(_, file_data))
|> result.flatten
|> result.map_error(handle_file_open_failure(_, gleam_src_path))
|> result.replace(gleam_src_path)
Expand Down Expand Up @@ -148,6 +152,11 @@ pub fn pt_2(input: String) {
}
"

const parse_starter = "pub fn parse(input: String) {
todo as \"parse not implemented\"
}
"

fn collect_async(year: Int, x: #(Day, Result(String, String))) -> String {
fn(res) {
case res {
Expand All @@ -167,17 +176,22 @@ fn collect(year: Int, x: #(Day, String)) -> String {
}

pub fn new_command() {
{
use input <- glint.command()
use days <- result.map(parse.days(input.args))
let assert Ok(year) = flag.get_int(input.flags, cmd.year)
cmd.exec(
days,
cmd.Endless,
fn(day) { do(Context(year, day)) },
collect_async(year, _),
)
}
|> glint.description("Create .gleam and input files")
|> glint.unnamed_args(glint.MinArgs(1))
use <- glint.command_help("Create .gleam and input files")
use <- glint.unnamed_args(glint.MinArgs(1))
use parse <- glint.flag(
"parse",
glint.bool()
|> glint.default(False)
|> glint.flag_help("Generate day runners with a parse function"),
)
use _, args, flags <- glint.command()
use days <- result.map(parse.days(args))
let assert Ok(year) = glint.get_int(flags, cmd.year)
let assert Ok(parse) = parse(flags)
cmd.exec(
days,
cmd.Endless,
fn(day) { do(Context(year, day, parse)) },
collect_async(year, _),
)
}
130 changes: 59 additions & 71 deletions src/gladvent/internal/cmd/run.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import gladvent/internal/parse.{type Day}
import gleam/dict as map
import gladvent/internal/cmd.{Ending, Endless}
import glint
import glint/flag
import gleam
import gladvent/internal/runners
import gleam/dynamic.{type Dynamic}
Expand Down Expand Up @@ -162,8 +161,8 @@ fn gleam_err_to_string(g: GleamErr) -> String {
"at line",
int.to_string(g.line),
g.value
|> option.map(fn(val) { "with value " <> string.inspect(val) })
|> option.unwrap(""),
|> option.map(fn(val) { "with value " <> string.inspect(val) })
|> option.unwrap(""),
],
" ",
)
Expand Down Expand Up @@ -238,82 +237,71 @@ pub const timeout = "timeout"
pub const allow_crash = "allow-crash"

pub fn timeout_flag() {
flag.int()
|> flag.constraint(fn(i) {
case i > 0 {
True -> Ok(Nil)
False -> snag.error("timeout value must greater than zero")
}
})
|> flag.description("Run with specified timeout")
use i <- glint.constraint(
glint.int()
|> glint.flag_help("Run with specified timeout"),
)

case i > 0 {
True -> Ok(i)
False -> snag.error("timeout value must greater than zero")
}
}

pub fn allow_crash_flag() {
flag.bool()
|> flag.default(False)
|> flag.description("Don't catch exceptions thrown by runners")
glint.bool()
|> glint.default(False)
|> glint.flag_help("Don't catch exceptions thrown by runners")
}

pub fn run_command() -> glint.Command(Result(List(String))) {
{
use input <- glint.command()
let assert Ok(year) = flag.get_int(input.flags, cmd.year)
let assert Ok(allow_crash) = flag.get_bool(input.flags, allow_crash)
let timing =
flag.get_int(input.flags, timeout)
|> result.map(Ending)
|> result.unwrap(Endless)

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, do(year, _, package, allow_crash), collect_async(
year,
_,
))
|> Ok
}
|> glint.description("Run the specified days")
|> glint.unnamed_args(glint.MinArgs(1))
use <- glint.command_help("Run the specified days")
use <- glint.unnamed_args(glint.MinArgs(1))
use _, args, flags <- glint.command()
use days <- result.then(parse.days(args))
let assert Ok(year) = glint.get_int(flags, cmd.year)
let assert Ok(allow_crash) = glint.get_bool(flags, allow_crash)
let timing =
glint.get_int(flags, timeout)
|> result.map(Ending)
|> result.unwrap(Endless)

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

days
|> cmd.exec(timing, do(year, _, package, allow_crash), collect_async(year, _))
}

pub fn run_all_command() -> glint.Command(Result(List(String))) {
{
use input <- glint.command()
let assert Ok(year) = flag.get_int(input.flags, cmd.year)
let assert Ok(allow_crash) = flag.get_bool(input.flags, allow_crash)
let timing =
flag.get_int(input.flags, timeout)
|> result.map(Ending)
|> result.unwrap(Endless)

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

package.modules
|> map.keys
|> list.filter_map(fn(k) {
use day <- result.try(string.split_once(
k,
"aoc_" <> int.to_string(year) <> "/day_",
))
day.1
|> parse.day
|> result.replace_error(Nil)
})
|> list.sort(int.compare)
|> cmd.exec(timing, do(year, _, package, allow_crash), collect_async(
year,
_,
use <- glint.command_help("Run all registered days")
use <- glint.unnamed_args(glint.EqArgs(0))
use _, _, flags <- glint.command()
let assert Ok(year) = glint.get_int(flags, cmd.year)
let assert Ok(allow_crash) = glint.get_bool(flags, allow_crash)
let timing =
glint.get_int(flags, timeout)
|> result.map(Ending)
|> result.unwrap(Endless)

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

package.modules
|> map.keys
|> list.filter_map(fn(k) {
use day <- result.try(string.split_once(
k,
"aoc_" <> int.to_string(year) <> "/day_",
))
|> Ok
}
|> glint.description("Run all registered days")
|> glint.unnamed_args(glint.EqArgs(0))
day.1
|> parse.day
|> result.replace_error(Nil)
})
|> list.sort(int.compare)
|> cmd.exec(timing, do(year, _, package, allow_crash), collect_async(year, _))
}

0 comments on commit c5fb981

Please sign in to comment.