From c13258b1d6e2f110dec9e857b7b11912813c3989 Mon Sep 17 00:00:00 2001 From: Texas Toland Date: Wed, 13 Mar 2024 21:38:04 -0500 Subject: [PATCH 1/5] Refactor toolkit.nu - Runs without first generating a script - Returns error (file) count - Requires `--and-exit` to exit with error code - Enables alternative report with env `STUB_IDE_CHECK=true` - Expands documentation - All subcommands share same file querying - Prepares for nupm test integration --- .github/workflows/ci.yml | 18 +++--- toolkit.nu | 127 +++++++++++++++++++++++++-------------- 2 files changed, 89 insertions(+), 56 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d60a230bb..290ca9187 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,5 @@ on: - pull_request: + pull_request: env: NUSHELL_CARGO_PROFILE: ci @@ -11,19 +11,15 @@ jobs: steps: - uses: actions/checkout@v4 - - name: 'Fetch main branch' + - name: "Fetch main branch" run: | - git fetch origin main --depth 1 + git fetch origin main --depth 1 - uses: hustcer/setup-nu@v3.9 with: - version: '*' - check-latest: true - features: full # dataframe and extra included + version: "*" + check-latest: true + features: full # dataframe and extra included - name: toolkit check pr shell: nu {0} run: | - nu -c "use toolkit.nu *; check pr" - - name: run nu-check on modified files - shell: nu {0} - run: | - nu ./check-files.nu + nu -c 'use toolkit.nu *; check pr --and-exit' diff --git a/toolkit.nu b/toolkit.nu index e038b651d..d83adc43a 100644 --- a/toolkit.nu +++ b/toolkit.nu @@ -4,61 +4,98 @@ # the main purpose of `toolkit` is to offer an easy to use interface for the # developer during a PR cycle. - -# check that all the tests pass +# Check that all the tests pass. +# +# Input: +# Optional file paths to check or infer them from Git export def test [ -] { - print "toolkit test: not implemented!" + --full # Check all files instead of input + --and-exit # Exit with error count +]: [list -> int, nothing -> int] { + with files --full=$full --and-exit=$and_exit { |files| + print "test: not implemented!" + [0] # success code + } } -# run all the necessary checks and tests to submit a perfect PR +# Run all the necessary checks and tests to submit a perfect PR. +# +# Input: +# Optional file paths to check or infer them from Git export def "check pr" [ -] { - generate-file-list - test + --full # Check all files instead of input + --and-exit # Exit with error count +]: [list -> int, nothing -> int] { + with files --full=$full --and-exit=$and_exit { |files| + [ + { lint } + { test } + ] | par-each { |task| $files | do $task } # TODO: buffer output + } } -export def main [] { help toolkit } - -export def generate-file-list [ --full ] { - let start = "let files = [" - - mut files = [""] +# View subcommands. +export def main []: nothing -> nothing { + help toolkit +} - if $full { - # all the *.nu files in the repo - # exept for `before_v0.60` - print "checking all files..." - mut $files = glob **/*.nu --exclude [before_v0.60/**] +# Wrap file lookup and exit codes. +def "with files" [ + task: closure + --full + --and-exit +]: [list -> int, nothing -> int] { + let files = match [$in, $full] { + [_ true] => (glob **/*.nu --exclude [before_v0.60/**]) + [null _] => (git diff --name-only origin | lines) + [$files _] => $files + } + let error_count = $files + | where $it ends-with .nu and ($it | path exists) + | each { path expand } + | do $task $files # run the closure with both input and param + | math sum # it MUST return a non-empty list of ints + if $and_exit { + exit $error_count } else { - # only the *.nu files changed in comparison with origin/main - $files = (git diff --name-only origin/main | lines | filter { str ends-with '.nu'} | each { path expand }) + $error_count } +} - - let new_list = $files | str join ",\n" | append "]" - - let final = " - - mut exit_code = 0 - for file in $files { - let diagnostics_table = nu --ide-check 10 $file | to text | ['[', $in, ']'] | str join | from json - let result = $diagnostics_table | where type == \"diagnostic\" | is-empty - if $result { - print $\"✔ ($file) is ok\" - } else { - print $\"❌ ($file) has errors:\" - print ($diagnostics_table | where type == \"diagnostic\" | reject span) - $exit_code = 1 - } +# Check the input file with nu --ide-check. +export def "lint ide-check" []: path -> int { + let file = $in + let stub = $env.STUB_IDE_CHECK? | default false | into bool + let diagnostics = if $stub { + do { nu -n -c $"use '($file)'" } + | complete + | [[severity message]; [$in.exit_code $in.stderr]] + | where severity != 0 + } else { + nu --ide-check 10 $file + | $"[($in)]" + | from nuon + | where type == diagnostic + | select severity message } - print $\"💚 All files checked!\" - -exit $exit_code -" + let error_count = $diagnostics | length + if $error_count == 0 { + print $"lint: ✔ ($file) is ok" + } else { + print $"lint: ❌ ($file) has errors:\n($diagnostics | table)" + } + $error_count +} - $start - | append $new_list - | append $final - | save "check-files.nu" --force +# Check that all the files parse. +# +# Input: +# Optional file paths to check or infer them from Git +export def lint [ + --full # Check all files instead of input + --and-exit # Exit with error count +]: [list -> int, nothing -> int] { + with files --full=$full --and-exit=$and_exit { + par-each { lint ide-check } + } } From 00815dc40e953227d85344807c94289401e10635 Mon Sep 17 00:00:00 2001 From: Texas Toland Date: Wed, 13 Mar 2024 22:15:28 -0500 Subject: [PATCH 2/5] Fix CI --- toolkit.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolkit.nu b/toolkit.nu index d83adc43a..09afc1aac 100644 --- a/toolkit.nu +++ b/toolkit.nu @@ -47,7 +47,7 @@ def "with files" [ ]: [list -> int, nothing -> int] { let files = match [$in, $full] { [_ true] => (glob **/*.nu --exclude [before_v0.60/**]) - [null _] => (git diff --name-only origin | lines) + [null _] => (git diff --name-only origin/main | lines) [$files _] => $files } let error_count = $files From b0ac04ea52bcd8dc0884e95457c2ce0fb82af8c9 Mon Sep 17 00:00:00 2001 From: Texas Toland Date: Wed, 13 Mar 2024 22:25:48 -0500 Subject: [PATCH 3/5] Fail gracefulliy if no .nu files changed --- toolkit.nu | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/toolkit.nu b/toolkit.nu index 09afc1aac..0edbd497f 100644 --- a/toolkit.nu +++ b/toolkit.nu @@ -49,12 +49,16 @@ def "with files" [ [_ true] => (glob **/*.nu --exclude [before_v0.60/**]) [null _] => (git diff --name-only origin/main | lines) [$files _] => $files + } | where $it ends-with .nu and ($it | path exists) + let error_count = if ($files | length) == 0 { + print 'warning: no .nu files found!' + 0 + } else { + $files + | each { path expand } + | do $task $files # run the closure with both input and param + | math sum # it MUST return a non-empty list of ints } - let error_count = $files - | where $it ends-with .nu and ($it | path exists) - | each { path expand } - | do $task $files # run the closure with both input and param - | math sum # it MUST return a non-empty list of ints if $and_exit { exit $error_count } else { From 76a77529bc5a73548a9a3bce4baffa5753a31ede Mon Sep 17 00:00:00 2001 From: Texas Toland Date: Thu, 14 Mar 2024 09:35:26 -0500 Subject: [PATCH 4/5] Pass long flags in toolkit for clarity --- .github/workflows/ci.yml | 2 +- .github/workflows/daily.yml | 22 +++++++++------------- toolkit.nu | 2 +- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 290ca9187..99bb08209 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,4 +22,4 @@ jobs: - name: toolkit check pr shell: nu {0} run: | - nu -c 'use toolkit.nu *; check pr --and-exit' + nu --commands 'use toolkit.nu *; check pr --and-exit' diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 366cc6b6c..9c1911e60 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -1,9 +1,9 @@ on: push: - branches: + branches: - main schedule: - - cron: '30 0 * * *' # every day at 00:30 AM UTC + - cron: "30 0 * * *" # every day at 00:30 AM UTC env: NUSHELL_CARGO_PROFILE: ci @@ -15,19 +15,15 @@ jobs: steps: - uses: actions/checkout@v4 - - name: 'Fetch main branch' + - name: "Fetch main branch" run: | - git fetch origin main --depth 1 + git fetch origin main --depth 1 - uses: hustcer/setup-nu@v3.9 with: - version: '*' - check-latest: true - features: full # dataframe and extra included - - name: toolkit generate-file-list --full + version: "*" + check-latest: true + features: full # dataframe and extra included + - name: toolkit check pr --full shell: nu {0} run: | - nu -c "use toolkit.nu *; generate-file-list --full" - - name: run nu-check on all files - shell: nu {0} - run: | - nu ./check-files.nu + nu --commands 'use toolkit.nu *; check pr --full --and-exit' diff --git a/toolkit.nu b/toolkit.nu index 0edbd497f..a24445ae7 100644 --- a/toolkit.nu +++ b/toolkit.nu @@ -71,7 +71,7 @@ export def "lint ide-check" []: path -> int { let file = $in let stub = $env.STUB_IDE_CHECK? | default false | into bool let diagnostics = if $stub { - do { nu -n -c $"use '($file)'" } + do { nu --no-config-file --commands $"use '($file)'" } | complete | [[severity message]; [$in.exit_code $in.stderr]] | where severity != 0 From 6e4a89aadd559d7c3f7e828551b59ac438c0e98b Mon Sep 17 00:00:00 2001 From: Texas Toland Date: Thu, 14 Mar 2024 11:15:08 -0500 Subject: [PATCH 5/5] Enable stub and simplify CI command --- .github/workflows/ci.yml | 4 +++- .github/workflows/daily.yml | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 99bb08209..fd76791cd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,5 +21,7 @@ jobs: features: full # dataframe and extra included - name: toolkit check pr shell: nu {0} + # nix STUB_IDE_CHECK when nushell/nushell#12208 fixed run: | - nu --commands 'use toolkit.nu *; check pr --and-exit' + use ${{ github.workspace }}/toolkit.nu * + STUB_IDE_CHECK=true check pr --and-exit diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 9c1911e60..0a874e153 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -25,5 +25,7 @@ jobs: features: full # dataframe and extra included - name: toolkit check pr --full shell: nu {0} + # nix STUB_IDE_CHECK when nushell/nushell#12208 fixed run: | - nu --commands 'use toolkit.nu *; check pr --full --and-exit' + use ${{ github.workspace }}/toolkit.nu * + STUB_IDE_CHECK=true check pr --full --and-exit