Skip to content

dotnu - tools for Nushell module developers ๐Ÿ› ๏ธ

License

Notifications You must be signed in to change notification settings

nushell-prophet/dotnu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

dotnu

dotnu - tools for Nushell module developers ๐Ÿ› ๏ธ

(A good companion for numd)

dotnu video demo

dotnu demo

Quickstart

> git clone https://github.com/nushell-prophet/dotnu; cd dotnu
> use dotnu

Commands

dotnu dependencies

> dotnu dependencies --help | numd parse-help
Description:
  Check .nu module files to determine which commands depend on other commands.

Usage:
  > dependencies {flags} ...(paths)

Flags:
  --keep_builtins - keep builtin commands in the result page
  --definitions_only - output only commands' names definitions

Parameters:
  ...paths <path>: paths to nushell module files

Examples:
  > dependencies ...(glob tests/assets/module-say/say/*.nu)
  โ•ญโ”€#โ”€โ”ฌโ”€โ”€callerโ”€โ”€โ”ฌโ”€filename_of_callerโ”€โ”ฌโ”€โ”€calleeโ”€โ”€โ”ฌโ”€stepโ”€โ•ฎ
  โ”‚ 0 โ”‚ hello    โ”‚ hello.nu           โ”‚          โ”‚    0 โ”‚
  โ”‚ 1 โ”‚ question โ”‚ ask.nu             โ”‚          โ”‚    0 โ”‚
  โ”‚ 2 โ”‚ say      โ”‚ mod.nu             โ”‚ hello    โ”‚    0 โ”‚
  โ”‚ 3 โ”‚ say      โ”‚ mod.nu             โ”‚ hi       โ”‚    0 โ”‚
  โ”‚ 4 โ”‚ say      โ”‚ mod.nu             โ”‚ question โ”‚    0 โ”‚
  โ”‚ 5 โ”‚ hi       โ”‚ mod.nu             โ”‚          โ”‚    0 โ”‚
  โ”‚ 6 โ”‚ test-hi  โ”‚ test-hi.nu         โ”‚ hi       โ”‚    0 โ”‚
  โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

dotnu filter-commands-with-no-tests

> dotnu filter-commands-with-no-tests --help | numd parse-help
Description:
  Filter commands after `dotnu dependencies` that aren't used by any other command containing `test` in its name.

Usage:
  > filter-commands-with-no-tests

Examples:
  > dependencies ...(glob tests/assets/module-say/say/*.nu) | filter-commands-with-no-tests
  โ•ญโ”€#โ”€โ”ฌโ”€โ”€callerโ”€โ”€โ”ฌโ”€filename_of_callerโ”€โ•ฎ
  โ”‚ 0 โ”‚ hello    โ”‚ hello.nu           โ”‚
  โ”‚ 1 โ”‚ question โ”‚ ask.nu             โ”‚
  โ”‚ 2 โ”‚ say      โ”‚ mod.nu             โ”‚
  โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

dotnu parse-docstrings

dotnu parse-docstrings parses command definitions along with their docstrings from a module file and outputs a table. It uses regex instead of the internal parsing engine of nushell to enable str replace operations inside the dotnu update-docstring-examples that will be showecased further.

To see it in action let's first examine an example module:

> let hello_module_path = [tests assets module-say say hello.nu] | path join
> open $hello_module_path | lines
โ•ญโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚  0 โ”‚ # Output greeting!               โ”‚
โ”‚  1 โ”‚ #                                โ”‚
โ”‚  2 โ”‚ # Say hello to Maxim             โ”‚
โ”‚  3 โ”‚ # > hello Maxim                  โ”‚
โ”‚  4 โ”‚ # hello Maxim!                   โ”‚
โ”‚  5 โ”‚ #                                โ”‚
โ”‚  6 โ”‚ # Say hello to Darren            โ”‚
โ”‚  7 โ”‚ # and capitlize letters          โ”‚
โ”‚  8 โ”‚ # > hello Darren                 โ”‚
โ”‚  9 โ”‚ # | str capitalize               โ”‚
โ”‚ 10 โ”‚ # Hello Darren!                  โ”‚
โ”‚ 11 โ”‚ export def main [name: string] { โ”‚
โ”‚ 12 โ”‚     $"hello ($name)!"            โ”‚
โ”‚ 13 โ”‚ }                                โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

And now let's use dotnu parse-docstrings and see its structured output (I get 0 row here for better output formatting).

> dotnu parse-docstrings $hello_module_path | reject input | get 0 | table -e
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ command_name        โ”‚ hello                                                            โ”‚
โ”‚ command_description โ”‚ Output greeting!                                                 โ”‚
โ”‚                     โ”‚ โ•ญโ”€#โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€annotationโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€commandโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€resultโ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚
โ”‚ examples            โ”‚ โ”‚ 0 โ”‚ Say hello to Maxim    โ”‚ > hello Maxim    โ”‚ hello Maxim!  โ”‚ โ”‚
โ”‚                     โ”‚ โ”‚ 1 โ”‚ Say hello to Darren   โ”‚ > hello Darren   โ”‚ Hello Darren! โ”‚ โ”‚
โ”‚                     โ”‚ โ”‚   โ”‚ and capitlize letters โ”‚ | str capitalize โ”‚               โ”‚ โ”‚
โ”‚                     โ”‚ โ•ฐโ”€#โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€annotationโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€commandโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€resultโ”€โ”€โ”€โ”€โ”€โ•ฏ โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

dotnu parse-docstrings uses the following assumptions:

  1. The command description and example blocks are divided by a line with only the # symbol. The command description is optional.
  2. Examples of command usage may contain their own annotations (rows before the line starting with >). Example annotations are optional.
  3. Examples of command usage consist of consecutive lines starting with > or | symbols.

dotnu update-docstring-examples

dotnu update-docstring-examples executes and updates examples in the specified nushell module file.

To prevent data loss it checks the current repository for uncommitted changes (this check can be disabled using --no_git_check).

If an example produces an error, this error is printed to the terminal output, and the file is updated with the text example update failed on the failed example result place.

> dotnu update-docstring-examples --help | numd parse-help
Description:
  Execute examples in the docstrings of the module commands and update the results accordingly.

Usage:
  > update-docstring-examples {flags} <module_file>

Flags:
  --command_filter <String> - filter commands by their name to update examples at (default: '')
  --use_statement <String> - use statement to execute examples with (like 'use module.nu'). Can be omitted to try to deduce automatically (default: '')
  --echo - output script to stdout instead of updating the module_file provided
  --no_git_check - don't check for the emptiness of the working tree

Parameters:
  module_file <path>: path to a nushell module file

dotnu set-x

dotnu set-x opens a regular .nu script. It divides it into blocks using the specified regex (by default, it is "\n\n") and generates a new script that will print the code of each block before executing it, along with the timings of each block's execution.

Let's check the code of the simple set-x-demo.nu script

> let $filename = [tests assets set-x-demo.nu] | path join
> open $filename | lines | table -i false
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ sleep 0.5sec โ”‚
โ”‚              โ”‚
โ”‚ sleep 0.7sec โ”‚
โ”‚              โ”‚
โ”‚ sleep 0.8sec โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Let's see how dotnu set-x will modify this script

> dotnu set-x $filename --echo | lines | table -i false
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ mut $prev_ts = date now                                                         โ”‚
โ”‚ print ("> sleep 0.5sec" | nu-highlight)                                         โ”‚
โ”‚ sleep 0.5sec                                                                    โ”‚
โ”‚ print $'(ansi grey)((date now) - $prev_ts)(ansi reset)'; $prev_ts = (date now); โ”‚
โ”‚                                                                                 โ”‚
โ”‚                                                                                 โ”‚
โ”‚ print ("> sleep 0.7sec" | nu-highlight)                                         โ”‚
โ”‚ sleep 0.7sec                                                                    โ”‚
โ”‚ print $'(ansi grey)((date now) - $prev_ts)(ansi reset)'; $prev_ts = (date now); โ”‚
โ”‚                                                                                 โ”‚
โ”‚                                                                                 โ”‚
โ”‚ print ("> sleep 0.8sec" | nu-highlight)                                         โ”‚
โ”‚ sleep 0.8sec                                                                    โ”‚
โ”‚ print $'(ansi grey)((date now) - $prev_ts)(ansi reset)'; $prev_ts = (date now); โ”‚
โ”‚                                                                                 โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

dotnu generate-nupm-tests

> dotnu generate-nupm-tests --help | numd parse-help
Description:
  Generate nupm tests from examples in docstrings

Usage:
  > generate-nupm-tests {flags} <$module_file>

Flags:
  --echo - output script to stdout instead of updating the module_file provided

Parameters:
  $module_file <path>: path to a nushell module file

About

dotnu - tools for Nushell module developers ๐Ÿ› ๏ธ

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published