Skip to content

Commit

Permalink
Refactor toolkit.nu (nushell#791)
Browse files Browse the repository at this point in the history
Closes nushell#789 🍻

- 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
  • Loading branch information
texastoland committed Mar 15, 2024
1 parent 191636a commit 878bfc6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 69 deletions.
20 changes: 9 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
pull_request:
pull_request:

env:
NUSHELL_CARGO_PROFILE: ci
Expand All @@ -11,19 +11,17 @@ 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/[email protected]
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}
# nix STUB_IDE_CHECK when nushell/nushell#12208 fixed
run: |
nu -c "use toolkit.nu *; check pr"
- name: run nu-check on modified files
shell: nu {0}
run: |
nu ./check-files.nu
use ${{ github.workspace }}/toolkit.nu *
STUB_IDE_CHECK=true check pr --and-exit
24 changes: 11 additions & 13 deletions .github/workflows/daily.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,19 +15,17 @@ 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/[email protected]
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}
# nix STUB_IDE_CHECK when nushell/nushell#12208 fixed
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
use ${{ github.workspace }}/toolkit.nu *
STUB_IDE_CHECK=true check pr --full --and-exit
131 changes: 86 additions & 45 deletions toolkit.nu
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,102 @@
# 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<path> -> 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<path> -> 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<path> -> int, nothing -> int] {
let files = match [$in, $full] {
[_ 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 {
# 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 })
$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 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
}
if $and_exit {
exit $error_count
} else {
$error_count
}
print $\"πŸ’š All files checked!\"
}

exit $exit_code
"
# 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 --no-config-file --commands $"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
}
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<path> -> int, nothing -> int] {
with files --full=$full --and-exit=$and_exit {
par-each { lint ide-check }
}
}

0 comments on commit 878bfc6

Please sign in to comment.