From c085ba6afeb50c61bf30e2a38da3f0778d8410ae Mon Sep 17 00:00:00 2001 From: tynanbe Date: Mon, 12 Feb 2024 14:03:51 -0600 Subject: [PATCH] Support gleam_stdlib v1.0 --- .github/workflows/ci.yml | 4 ++-- CHANGELOG.md | 5 +++++ gleam.toml | 6 +++--- manifest.toml | 4 ++-- src/shellout.gleam | 30 ++++++++---------------------- src/shellout_ffi.mjs | 13 ++++--------- test/shellout_test.gleam | 10 +++++----- 7 files changed, 29 insertions(+), 43 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f96d2dd..d915fa0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,9 +15,9 @@ jobs: - uses: erlef/setup-beam@v1 with: - otp-version: "25" + otp-version: "26" rebar3-version: "3" - gleam-version: "0.33" + gleam-version: "1.0.0-rc1" - uses: denoland/setup-deno@v1 with: diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f9a459..c5698c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +- Shellout now supports `gleam_stdlib` v1.0. +- Shellout now requires Gleam v0.34 or later. + ## v1.5.0 - 2023-12-19 - Shellout now requires Gleam v0.33 or later. diff --git a/gleam.toml b/gleam.toml index ab09e26..b90f9c9 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,8 +1,8 @@ name = "shellout" -version = "1.5.0" +version = "1.6.0-dev" description = "A Gleam library for cross-platform shell operations" licences = ["Apache-2.0"] -gleam = ">= 0.33.0" +gleam = ">= 0.34.0" [repository] repo = "shellout" @@ -14,7 +14,7 @@ href = "https://gleam.run/" title = "Website" [dependencies] -gleam_stdlib = "~> 0.34" +gleam_stdlib = "~> 0.34 or ~> 1.0" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index 7445d63..7762492 100644 --- a/manifest.toml +++ b/manifest.toml @@ -3,9 +3,9 @@ packages = [ { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" }, - { name = "gleeunit", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "4E75DCF846D653848094169304743DFFB386E3AECCCF611F99ADB735FF4D4DD9" }, + { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, ] [requirements] -gleam_stdlib = { version = "~> 0.34" } +gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } gleeunit = { version = "~> 1.0" } diff --git a/src/shellout.gleam b/src/shellout.gleam index 15485dd..7e2afbb 100644 --- a/src/shellout.gleam +++ b/src/shellout.gleam @@ -112,7 +112,7 @@ pub const colors: Lookup = [ /// with: display(["bold", "italic", "tubular"]), /// custom: [], /// ) -/// // -> "\u{001b}[1;3mradical\u{001b}[0m\u{001b}[K" +/// // -> "\u{1b}[1;3mradical\u{1b}[0m\u{1b}[K" /// ``` /// pub fn display(values: List(String)) -> StyleFlags { @@ -130,7 +130,7 @@ pub fn display(values: List(String)) -> StyleFlags { /// with: color(["yellow", "brightgreen", "gnarly"]), /// custom: [], /// ) -/// // -> "\u{001b}[33;92muh...\u{001b}[0m\u{001b}[K" +/// // -> "\u{1b}[33;92muh...\u{1b}[0m\u{1b}[K" /// ``` /// pub fn color(values: List(String)) -> StyleFlags { @@ -148,7 +148,7 @@ pub fn color(values: List(String)) -> StyleFlags { /// with: background(["yellow", "brightgreen", "bodacious"]), /// custom: [], /// ) -/// // -> "\u{001b}[43;102mawesome\u{001b}[0m\u{001b}[K" +/// // -> "\u{1b}[43;102mawesome\u{1b}[0m\u{1b}[K" /// ``` /// pub fn background(values: List(String)) -> StyleFlags { @@ -182,7 +182,7 @@ pub fn background(values: List(String)) -> StyleFlags { /// |> dict.merge(background(["brightblack", "excellent"])), /// custom: lookups, /// ) -/// // -> "\u{001b}[1;3;38;2;255;175;243;100mcowabunga\u{001b}[0m\u{001b}[K" +/// // -> "\u{1b}[1;3;38;2;255;175;243;100mcowabunga\u{1b}[0m\u{1b}[K" /// ``` /// pub fn style( @@ -210,15 +210,10 @@ pub fn style( |> escape(string) } -@target(erlang) fn escape(code: String, string: String) -> String { - string.concat(["\u{001b}[", code, "m", string, "\u{001b}[0m\u{001b}[K"]) + "\u{1b}[" <> code <> "m" <> string <> "\u{1b}[0m\u{1b}[K" } -@target(javascript) -@external(javascript, "./shellout_ffi.mjs", "escape") -fn escape(code: String, string: String) -> String - type Style { Name(String) Rgb(List(String)) @@ -261,8 +256,7 @@ fn do_style(lookup: Lookup, strings: List(String), flag: String) -> List(String) let assert [Rgb(values), ..styles] = acc.styles StyleAcc( styles: [Rgb([item, ..values]), ..styles], - rgb_counter: rgb_counter - + 1, + rgb_counter: rgb_counter + 1, ) } _ -> StyleAcc(styles: [Rgb([item]), ..acc.styles], rgb_counter: 1) @@ -451,13 +445,9 @@ fn do_command( /// // Ugh, shell shock ... /// ``` /// -pub fn exit(status: Int) -> Nil { - do_exit(status) -} - @external(erlang, "shellout_ffi", "os_exit") @external(javascript, "./shellout_ffi.mjs", "os_exit") -fn do_exit(status status: Int) -> Nil +pub fn exit(status: Int) -> Nil /// Results in a path to the given `executable` on success, or an `Error` when /// no such path is found. @@ -479,10 +469,6 @@ fn do_exit(status status: Int) -> Nil /// // -> Error("command `dimension_x` not found") /// ``` /// -pub fn which(executable: String) -> Result(String, String) { - do_which(executable) -} - @external(erlang, "shellout_ffi", "os_which") @external(javascript, "./shellout_ffi.mjs", "os_which") -fn do_which(command: String) -> Result(String, String) +pub fn which(executable: String) -> Result(String, String) diff --git a/src/shellout_ffi.mjs b/src/shellout_ffi.mjs index 12a2b70..f0914d4 100644 --- a/src/shellout_ffi.mjs +++ b/src/shellout_ffi.mjs @@ -1,7 +1,7 @@ import { Error, Ok, toList } from "./gleam.mjs"; import { LetBeStderr, LetBeStdout, OverlappedStdio } from "./shellout.mjs"; -import * as child_process from "node:child_process"; -import * as fs from "node:fs"; +import { spawnSync } from "node:child_process"; +import { statSync } from "node:fs"; import * as path from "node:path"; import process from "node:process"; @@ -43,10 +43,6 @@ const Signals = { SIGRTMIN: 34, }; -export function escape(code, string) { - return `\x1b[${code}m${string}\x1b[0m\x1b[K`; -} - export function start_arguments() { return toList(globalThis.Deno?.args ?? process.argv.slice(1)); } @@ -96,7 +92,7 @@ export function os_command(command, args, dir, opts) { result.status = result.code ?? null; } else { spawnOpts.stdio = [stdin, stdout, stderr]; - result = child_process.spawnSync(command, args, spawnOpts); + result = spawnSync(command, args, spawnOpts); } if (result.error) { result = { status: null }; @@ -141,8 +137,7 @@ export function os_which(command) { .concat([command]) .flatMap((item) => pathexts.map((ext) => item + ext)); let result = paths.map( - (item) => - fs.statSync(item, { throwIfNoEntry: false })?.isFile() ? item : Nil, + (item) => statSync(item, { throwIfNoEntry: false })?.isFile() ? item : Nil, ).find((item) => item !== Nil); return result !== Nil ? new Ok(result) : new Error( `command \`${command}\` not found\n`, diff --git a/test/shellout_test.gleam b/test/shellout_test.gleam index 9c8cef6..9c1cfbb 100644 --- a/test/shellout_test.gleam +++ b/test/shellout_test.gleam @@ -41,13 +41,13 @@ pub fn arguments_test() { } pub fn command_test() { - let echo = shellout.command(run: "echo", with: [message], in: ".", opt: _) + let print = shellout.command(run: "echo", with: [message], in: ".", opt: _) - let assert Ok(output) = echo([]) + let assert Ok(output) = print([]) output |> should.not_equal("") - let assert Ok(new_output) = echo([LetBeStderr]) + let assert Ok(new_output) = print([LetBeStderr]) new_output |> should.equal(output) @@ -100,8 +100,8 @@ pub fn style_test() { message |> shellout.style( with: shellout.display(["bold", "italic"]) - |> dict.merge(shellout.color(["pink"])) - |> dict.merge(shellout.background(["brightblack"])), + |> dict.merge(shellout.color(["pink"])) + |> dict.merge(shellout.background(["brightblack"])), custom: lookups, ) styled