Skip to content

Commit

Permalink
spinners
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed May 25, 2024
1 parent 92aa7e0 commit 032475c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/gladvent/internal/cmd/run.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import filepath
import gladvent/internal/cmd.{Ending, Endless}
import gladvent/internal/parse.{type Day}
import gladvent/internal/runners
import gladvent/internal/util
import gleam
import gleam/dict as map
import gleam/dynamic.{type Dynamic}
Expand All @@ -17,6 +18,7 @@ import gleam/string
import glint
import simplifile
import snag.{type Result, type Snag}
import spinner

type AsyncResult =
gleam.Result(RunResult, String)
Expand Down Expand Up @@ -258,6 +260,18 @@ pub fn run_command() -> glint.Command(Result(List(String))) {
use days <- result.then(parse.days(args))
let assert Ok(year) = glint.get_flag(flags, cmd.year_flag())
let assert Ok(allow_crash) = glint.get_flag(flags, allow_crash_flag())

let spinner =
spinner.new(
"running days "
<> string.join(args, ", ")
<> " in "
<> int.to_string(year),
)
|> spinner.start()

use <- util.defer(do: fn() { spinner.stop(spinner) })

let timing =
glint.get_flag(flags, timeout_flag())
|> result.map(Ending)
Expand All @@ -276,8 +290,16 @@ pub fn run_all_command() -> glint.Command(Result(List(String))) {
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_flag(flags, cmd.year_flag())
let assert Ok(allow_crash) = glint.get_flag(flags, allow_crash_flag())

let spinner =
spinner.new("running all days in " <> int.to_string(year))
|> spinner.start()

use <- util.defer(do: fn() { spinner.stop(spinner) })

let timing =
glint.get_flag(flags, timeout_flag())
|> result.map(Ending)
Expand Down
14 changes: 10 additions & 4 deletions src/gladvent/internal/runners.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,39 @@ fn package_interface_error_to_snag(e: PkgInterfaceErr) -> snag.Snag {
pub fn pkg_interface() -> Result(package_interface.Package) {
use <- snagify_error(with: package_interface_error_to_snag)
let spinner =
spinner.new("generating " <> package_interface_path)
spinner.new("initializing package interface")
|> spinner.start()

use <- defer(do: fn() { spinner.stop(spinner) })

let root = cmd.root()

spinner.set_text(spinner, "reading gleam.toml")
use gleam_toml <- result.try(
simplifile.read(filepath.join(root, "gleam.toml"))
|> result.map_error(FailedToReadGleamToml),
)

spinner.set_text(spinner, "decoding gleam.toml")
use toml <- result.try(
tom.parse(gleam_toml)
|> result.map_error(FailedToDecodeGleamToml),
)

spinner.set_text(spinner, "fetching project name")
use name <- result.try(
tom.get_string(toml, ["name"])
|> result.map_error(FailedToGetPackageName),
)

spinner.set_text(spinner, "clearing build cache")
use _ <- result.try(
{
use cache <- list.try_map(["build/prod/erlang", "build/dev/erlang"])
["build/prod/erlang", "build/dev/erlang"]
|> list.try_map(fn(cache) {
filepath.join(root, cache)
|> filepath.join(name)
|> simplifile.delete
}
})
|> result.try_recover(fn(e) {
case e {
simplifile.Enoent -> Ok([])
Expand All @@ -108,11 +112,13 @@ pub fn pkg_interface() -> Result(package_interface.Package) {
}),
)

spinner.set_text(spinner, "rebuilding project")
use _ <- result.try(
shellout.command("gleam", ["build"], root, [])
|> result.map_error(fn(e) { FailedToGeneratePackageInterface(e.1) }),
)

spinner.set_text(spinner, "generating package interface file")
use _ <- result.try(
shellout.command(
"gleam",
Expand Down

0 comments on commit 032475c

Please sign in to comment.