1
0
mirror of https://github.com/nushell/nushell.git synced 2025-05-05 18:44:24 +02:00
nushell/crates/nu-command/src/commands
Andrés N. Robalino 9d8845d7ad
Allow custom lib dir path for sourcing nu script libraries. ()
Given we can write nu scripts. As the codebase grows, splitting into many smaller nu scripts is necessary.

In general, when we work with paths and files we seem to face quite a few difficulties. Here we just tackle one of them and it involves sourcing
files that also source other nu files and so forth. The current working directory becomes important here and being on a different directory
when sourcing scripts will not work. Mostly because we expand the path on the current working directory and parse the files when a source command
call is done.

For the moment, we introduce a `lib_dirs` configuration value and, unfortunately, introduce a new dependency in `nu-parser` (`nu-data`) to get
a handle of the configuration file to retrieve it. This should give clues and ideas as the new parser engine continues (introduce a way to also know paths)

With this PR we can do the following:

Let's assume we want to write a nu library called `my_library`. We will have the code in a directory called `project`: The file structure will looks like this:

```
project/my_library.nu
project/my_library/hello.nu
project/my_library/name.nu
```

This "pattern" works well, that is, when creating a library have a directory named `my_library` and next to it a `my_library.nu` file. Filling them like this:

```

source my_library/hello.nu
source my_library/name.nu
```

```

def hello [] {
  "hello world"
}
```

```

def name [] {
  "Nu"
end
```

Assuming this `project` directory is stored at `/path/to/lib/project`, we can do:

```
config set lib_dirs ['path/to/lib/project']
```

Given we have this `lib_dirs` configuration value, we can be anywhere while using Nu and do the following:

```
source my_library.nu

echo (hello) (name)

```
2021-08-26 02:04:04 -05:00
..
charting Begin directory contrib docs and split commands () 2021-06-19 12:06:44 +12:00
config Begin directory contrib docs and split commands () 2021-06-19 12:06:44 +12:00
conversions Implement into path conversion () 2021-07-23 19:14:02 +12:00
core_commands Allow custom lib dir path for sourcing nu script libraries. () 2021-08-26 02:04:04 -05:00
dataframe Rolling and cumulative commands () 2021-08-24 09:10:29 -05:00
env Fix clippy lint and disable broken lint () 2021-07-30 08:11:47 +12:00
filesystem Read from standard input in rm () 2021-07-25 15:01:53 +12:00
filters The zip command. () 2021-08-14 23:36:08 -05:00
formats Reimplement parsers with nu-serde () 2021-08-06 11:46:19 -05:00
generators Improve md5 and sha256 code () 2021-07-29 10:22:16 -05:00
math Begin directory contrib docs and split commands () 2021-06-19 12:06:44 +12:00
network port network url to engine-p () 2021-06-26 13:19:10 +12:00
path Move path handling to nu-path () 2021-06-20 11:07:26 +12:00
pathvar Support other variables than PATH in pathvar (2nd attempt) () 2021-07-24 11:44:36 -05:00
platform Fix clippy lint and disable broken lint () 2021-07-30 08:11:47 +12:00
random Bump rand version used by nu-command to 0.8 () 2021-07-05 16:12:44 +12:00
shells Begin directory contrib docs and split commands () 2021-06-19 12:06:44 +12:00
strings port capitalize to engine-p () 2021-07-23 19:13:11 +12:00
viewers All is a DataFrame () 2021-07-25 22:01:54 +12:00
mod.rs Rolling and cumulative commands () 2021-08-24 09:10:29 -05:00