Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to builtin Task #5

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
on:
on:
pull_request:
workflow_dispatch:

Expand All @@ -7,14 +7,16 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
env:
ROC: ./roc_nightly/roc

jobs:
test-examples:
runs-on: [ubuntu-20.04]
steps:
- uses: actions/checkout@v3

# get roc cli
# get roc cli
- name: get latest roc nightly
run: |
curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-latest.tar.gz
Expand All @@ -32,5 +34,9 @@ jobs:

- run: ./roc_nightly/roc version

# run all tests
- run: ./roc_nightly/roc test package/main.roc
# expect for testing
# - run: sudo apt install -y expect
# - run: expect -v

# run all tests
- run: ./ci/all_tests.sh
54 changes: 54 additions & 0 deletions ci/all_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euxo pipefail

if [ -z "${ROC}" ]; then
echo "ERROR: The ROC environment variable is not set.
Set it to something like:
/home/username/Downloads/roc_nightly-linux_x86_64-2023-10-30-cb00cfb/roc
or
/home/username/gitrepos/roc/target/build/release/roc" >&2

exit 1
fi

EXAMPLES_DIR='./examples'
PACKAGE_DIR='./package'

# roc check
for ROC_FILE in $EXAMPLES_DIR/*.roc; do
$ROC check $ROC_FILE
done

# roc build
for ROC_FILE in $EXAMPLES_DIR/*.roc; do
$ROC build $ROC_FILE --linker=legacy
done

# check output
# for ROC_FILE in $EXAMPLES_DIR/*.roc; do
# ROC_FILE_ONLY="$(basename "$ROC_FILE")"
# NO_EXT_NAME=${ROC_FILE_ONLY%.*}
# expect ci/expect_scripts/$NO_EXT_NAME.exp
# done

# `roc test` every roc file if it contains a test, skip roc_nightly folder
find . -type d -name "roc_nightly" -prune -o -type f -name "*.roc" -print | while read file; do
if grep -qE '^\s*expect(\s+|$)' "$file"; then

# don't exit script if test_command fails
set +e
test_command=$($ROC test "$file")
test_exit_code=$?
set -e

if [[ $test_exit_code -ne 0 && $test_exit_code -ne 2 ]]; then
exit $test_exit_code
fi
fi

done

# test building docs website
$ROC docs $PACKAGE_DIR/main.roc
5 changes: 2 additions & 3 deletions examples/basic.roc
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
pf: platform "../../basic-cli/platform/main.roc",
weaver: "../package/main.roc",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import weaver.Opt
import weaver.Cli
import weaver.Param

main =
args = Arg.list!
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand Down
5 changes: 2 additions & 3 deletions examples/default-values.roc
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
pf: platform "../../basic-cli/platform/main.roc",
weaver: "../package/main.roc",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import weaver.Opt
import weaver.Cli
import weaver.Param

main =
args = Arg.list!
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand Down
5 changes: 2 additions & 3 deletions examples/single-arg.roc
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
pf: platform "../../basic-cli/platform/main.roc",
weaver: "../package/main.roc",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import weaver.Opt
import weaver.Cli

main =
args = Arg.list!
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand Down
5 changes: 2 additions & 3 deletions examples/subcommands.roc
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
pf: platform "../../basic-cli/platform/main.roc",
weaver: "../package/main.roc",
}

import pf.Stdout
import pf.Arg
import pf.Task exposing [Task]
import weaver.Opt
import weaver.Cli
import weaver.Param
import weaver.SubCmd

main =
args = Arg.list!
args = Arg.list! {}

when Cli.parseOrDisplayMessage cliParser args is
Ok data ->
Expand Down
22 changes: 6 additions & 16 deletions package/Opt.roc
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ import Parser exposing [ArgValue]
builderWithOptionParser : OptionConfig, (List ArgValue -> Result data ArgExtractErr) -> CliBuilder data fromAction toAction
builderWithOptionParser = \option, valueParser ->
argParser = \args ->
{ values, remainingArgs } <- extractOptionValues { args, option }
|> Result.try
data <- valueParser values
|> Result.try
{ values, remainingArgs } = extractOptionValues? { args, option }

data = valueParser? values

Ok { data, remainingArgs }

Expand Down Expand Up @@ -124,10 +123,7 @@ single = \{ parser, type, short ? "", long ? "", help ? "", default ? NoDefault
NoDefault -> Err (MissingOption option)

valueParser = \values ->
value <- getMaybeValue values option
|> Result.try

when value is
when getMaybeValue? values option is
Err NoValue -> defaultGenerator {}
Ok (Err NoValue) -> Err (NoValueProvidedForOption option)
Ok (Ok val) ->
Expand Down Expand Up @@ -172,10 +168,7 @@ maybe = \{ parser, type, short ? "", long ? "", help ? "" } ->
option = { expectedValue: ExpectsValue type, plurality: Optional, short, long, help }

valueParser = \values ->
value <- getMaybeValue values option
|> Result.try

when value is
when getMaybeValue? values option is
Err NoValue -> Ok (Err NoValue)
Ok (Err NoValue) -> Err (NoValueProvidedForOption option)
Ok (Ok val) ->
Expand Down Expand Up @@ -250,10 +243,7 @@ flag = \{ short ? "", long ? "", help ? "" } ->
option = { expectedValue: NothingExpected, plurality: Optional, short, long, help }

valueParser = \values ->
value <- getMaybeValue values option
|> Result.try

when value is
when getMaybeValue? values option is
Err NoValue -> Ok Bool.false
Ok (Err NoValue) -> Ok Bool.true
Ok (Ok _val) -> Err (OptionDoesNotExpectValue option)
Expand Down
7 changes: 3 additions & 4 deletions package/Param.roc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ import Extract exposing [extractParamValues]
builderWithParameterParser : ParameterConfig, (List Str -> Result data ArgExtractErr) -> CliBuilder data fromAction toAction
builderWithParameterParser = \param, valueParser ->
argParser = \args ->
{ values, remainingArgs } <- extractParamValues { args, param }
|> Result.try
data <- valueParser values
|> Result.try
{ values, remainingArgs } = extractParamValues? { args, param }

data = valueParser? values

Ok { data, remainingArgs }

Expand Down
36 changes: 16 additions & 20 deletions package/Validate.roc
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,26 @@ validateCommand :
}
-> Result {} CliValidationErr
validateCommand = \{ name, options, parentOptions, parameters, subcommands, subcommandPath } ->
{} <- ensureCommandIsWellNamed { name, subcommandPath }
|> Result.try
_ <- options
|> List.mapTry \option ->
{} <- ensureOptionIsWellNamed { option, subcommandPath }
|> Result.try
{} <- ensureOptionValueTypeIsWellNamed { option, subcommandPath }
|> Result.try

Ok {}
|> Result.try
ensureCommandIsWellNamed? { name, subcommandPath }

_ <- parameters
|> List.mapTry \param ->
{} <- ensureParamIsWellNamed { name: param.name, subcommandPath }
|> Result.try
{} <- ensureParamValueTypeIsWellNamed { param, subcommandPath }
|> Result.try
_ =
options
|> List.mapTry? \option ->
ensureOptionIsWellNamed? { option, subcommandPath }
ensureOptionValueTypeIsWellNamed? { option, subcommandPath }

Ok {}
|> Result.try
Ok {}

_ =
parameters
|> List.mapTry? \param ->
ensureParamIsWellNamed? { name: param.name, subcommandPath }
ensureParamValueTypeIsWellNamed? { param, subcommandPath }

Ok {}

{} <- checkIfThereAreOverlappingParameters parameters subcommandPath
|> Result.try
checkIfThereAreOverlappingParameters? parameters subcommandPath

when subcommands is
HasSubcommands subcommandConfigs if !(Dict.isEmpty subcommandConfigs) ->
Expand Down
Loading