Skip to content

Commit

Permalink
some simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
bwireman committed Jul 7, 2024
1 parent c472ed4 commit 37ce701
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 53 deletions.
2 changes: 1 addition & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ packages = [
{ name = "gleamsver", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleamsver", source = "hex", outer_checksum = "66FAD161BE3E28FF26B573C7D3663335E6B4CE5C0B12AC4AF33116572EE6C19A" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "gleither", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleither", source = "hex", outer_checksum = "A215388078C9B36403A8B9ECBE4B1B1A1F2DC2A1606ACD9DB0BFB1AEEBA94F7B" },
{ name = "go_over", version = "2.1.0", build_tools = ["gleam"], requirements = ["birl", "filepath", "gleam_hexpm", "gleam_json", "gleam_stdlib", "gleamsver", "shellout", "simplifile", "tom", "yamerl"], otp_app = "go_over", source = "hex", outer_checksum = "2C82D50418D75DDE8347E27BA547EB2B2017BE8E7B1F576A3343EB5B90C21932" },
{ name = "go_over", version = "2.2.0", build_tools = ["gleam"], requirements = ["birl", "filepath", "gleam_hexpm", "gleam_json", "gleam_stdlib", "gleamsver", "shellout", "simplifile", "tom", "yamerl"], otp_app = "go_over", source = "hex", outer_checksum = "3709A32C3AF6191F82078EC16C1FEA189C30B8FB7861A85558DE0D1753F5579E" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
{ name = "shellout", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "E2FCD18957F0E9F67E1F497FC9FF57393392F8A9BAEAEA4779541DE7A68DD7E0" },
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" },
Expand Down
102 changes: 55 additions & 47 deletions src/cactus/run.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cactus/util.{
type CactusErr, ActionFailedErr, InvalidFieldErr, as_invalid_field_err, cactus,
}
import gleam/dict.{type Dict}
import gleam/io
import gleam/list
import gleam/option.{Some}
Expand All @@ -24,38 +25,45 @@ pub type Action {
Action(command: String, kind: ActionKind, args: List(String))
}

fn do_parse_kind(kind: String) -> Result(ActionKind, CactusErr) {
case kind {
"module" -> Ok(Module)
"sub_command" -> Ok(SubCommand)
"binary" -> Ok(Binary)
_ ->
Error(InvalidFieldErr(
Some("kind"),
Right(
"got: "
<> util.quote(kind)
<> " expected: one of ['sub_command', 'binary', or 'module']",
),
))
}
}

fn do_parse_action(t: Dict(String, Toml)) -> Result(Action, CactusErr) {
let kind =
tom.get_string(t, ["kind"])
|> result.map(string.lowercase)
|> result.unwrap("module")

use command <- try(as_invalid_field_err(tom.get_string(t, ["command"])))
use args <- try(
tom.get_array(t, ["args"])
|> result.unwrap([])
|> list.map(as_string)
|> result.all(),
)
use action_kind <- result.map(do_parse_kind(kind))

Action(command: command, kind: action_kind, args: args)
}

pub fn parse_action(raw: Toml) -> Result(Action, CactusErr) {
case raw {
tom.InlineTable(t) -> {
use command <- try(as_invalid_field_err(tom.get_string(t, ["command"])))
use args <- try(
tom.get_array(t, ["args"])
|> result.unwrap([])
|> list.map(as_string)
|> result.all(),
)
let kind =
tom.get_string(t, ["kind"])
|> result.map(string.lowercase)
|> result.unwrap("module")

use action_kind <- try(case kind {
"module" -> Ok(Module)
"sub_command" -> Ok(SubCommand)
"binary" -> Ok(Binary)
_ ->
Error(InvalidFieldErr(
Some("kind"),
Right(
"got: "
<> util.quote(kind)
<> " expected: one of ['sub_command', 'binary', or 'module']",
),
))
})
tom.InlineTable(t) -> do_parse_action(t)

Ok(Action(command: command, kind: action_kind, args: args))
}
_ ->
Error(InvalidFieldErr(
Some(actions),
Expand All @@ -75,28 +83,28 @@ pub fn get_actions(
as_invalid_field_err(tom.get_array(action_body, [actions]))
}

fn do_run(action: Action) {
let #(bin, args) = case action.kind {
Module -> #(gleam, ["run", "-m", action.command, "--", ..action.args])
SubCommand -> #(gleam, [action.command, ..action.args])
Binary -> #(action.command, action.args)
}

io.println(string.join(["Running", bin, ..args], " "))
case shellout.command(run: bin, with: args, in: ".", opt: []) {
Ok(res) -> {
io.print(res)
Ok(res)
}
Error(#(_, err)) -> Error(ActionFailedErr(err))
}
}

pub fn run(path: String, action: String) -> Result(List(String), CactusErr) {
use actions <- try(get_actions(path, action))
actions
|> list.map(parse_action)
|> list.map(fn(parse_result) {
result.try(parse_result, fn(action) {
let #(bin, args) = case action.kind {
Module -> #(gleam, ["run", "-m", action.command, "--", ..action.args])
SubCommand -> #(gleam, [action.command, ..action.args])
Binary -> #(action.command, action.args)
}

io.println(string.join(["Running", bin, ..args], " "))
case shellout.command(run: bin, with: args, in: ".", opt: []) {
Ok(res) -> {
io.print(res)
Ok(res)
}
Error(#(_, err)) -> Error(ActionFailedErr(err))
}
})
})
|> list.map(result.try(_, do_run))
|> result.all
}

Expand Down
6 changes: 1 addition & 5 deletions src/cactus/write.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import gleam/set
import simplifile
import tom

@target(erlang)
pub const tmpl = "gleam run -m cactus --target erlang -- "

@target(javascript)
pub const tmpl = "gleam run -m cactus --target javascript -- "
pub const tmpl = "gleam run -m cactus -- "

const valid_hooks = [
"applypatch-msg", "commit-msg", "fsmonitor-watchman", "post-update",
Expand Down

0 comments on commit 37ce701

Please sign in to comment.