2020-05-18 05:52:56 +02:00
|
|
|
pub mod test_bins;
|
2020-01-04 05:00:39 +01:00
|
|
|
|
2020-04-19 01:05:24 +02:00
|
|
|
use crate::path::canonicalize;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2019-12-09 19:39:51 +01:00
|
|
|
use nu_protocol::{UntaggedValue, Value};
|
2019-09-03 09:43:37 +02:00
|
|
|
use std::path::{Component, Path, PathBuf};
|
2019-07-24 06:10:48 +02:00
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
fn is_value_tagged_dir(value: &Value) -> bool {
|
2020-07-15 02:11:41 +02:00
|
|
|
matches!(&value.value, UntaggedValue::Row(_) | UntaggedValue::Table(_))
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
}
|
2019-09-03 09:43:37 +02:00
|
|
|
|
2019-08-14 22:05:27 +02:00
|
|
|
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
|
2019-09-03 09:43:37 +02:00
|
|
|
pub struct ValueResource {
|
|
|
|
pub at: usize,
|
2019-08-14 22:05:27 +02:00
|
|
|
pub loc: PathBuf,
|
2019-09-03 09:43:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ValueResource {}
|
|
|
|
|
2020-05-18 05:52:56 +02:00
|
|
|
#[derive(Default)]
|
2019-09-03 09:43:37 +02:00
|
|
|
pub struct ValueStructure {
|
|
|
|
pub resources: Vec<ValueResource>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ValueStructure {
|
|
|
|
pub fn new() -> ValueStructure {
|
|
|
|
ValueStructure {
|
|
|
|
resources: Vec::<ValueResource>::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exists(&self, path: &Path) -> bool {
|
|
|
|
if path == Path::new("/") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = if path.starts_with("/") {
|
2020-07-15 09:51:41 +02:00
|
|
|
path.strip_prefix("/").unwrap_or(path)
|
2019-09-03 09:43:37 +02:00
|
|
|
} else {
|
|
|
|
path
|
|
|
|
};
|
|
|
|
|
|
|
|
let comps: Vec<_> = path.components().map(Component::as_os_str).collect();
|
|
|
|
|
|
|
|
let mut is_there = true;
|
|
|
|
|
|
|
|
for (at, fragment) in comps.iter().enumerate() {
|
|
|
|
is_there = is_there
|
|
|
|
&& self
|
|
|
|
.resources
|
|
|
|
.iter()
|
|
|
|
.any(|resource| at == resource.at && *fragment == resource.loc.as_os_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
is_there
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn walk_decorate(&mut self, start: &Value) -> Result<(), ShellError> {
|
2019-09-03 09:43:37 +02:00
|
|
|
self.resources = Vec::<ValueResource>::new();
|
|
|
|
self.build(start, 0)?;
|
|
|
|
self.resources.sort();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn build(&mut self, src: &Value, lvl: usize) -> Result<(), ShellError> {
|
2020-04-26 19:30:52 +02:00
|
|
|
for entry in src.row_entries() {
|
2019-09-03 09:43:37 +02:00
|
|
|
let value = entry.1;
|
|
|
|
let path = entry.0;
|
|
|
|
|
|
|
|
self.resources.push(ValueResource {
|
|
|
|
at: lvl,
|
|
|
|
loc: PathBuf::from(path),
|
|
|
|
});
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
if is_value_tagged_dir(value) {
|
2019-09-03 09:43:37 +02:00
|
|
|
self.build(value, lvl + 1)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
|
|
|
|
pub struct Res {
|
2019-08-14 22:05:27 +02:00
|
|
|
pub at: usize,
|
2019-09-03 09:43:37 +02:00
|
|
|
pub loc: PathBuf,
|
2019-08-14 22:05:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Res {}
|
|
|
|
|
2020-05-18 05:52:56 +02:00
|
|
|
#[derive(Default)]
|
2019-08-14 22:05:27 +02:00
|
|
|
pub struct FileStructure {
|
|
|
|
pub resources: Vec<Res>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileStructure {
|
|
|
|
pub fn new() -> FileStructure {
|
|
|
|
FileStructure {
|
|
|
|
resources: Vec::<Res>::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:16:41 +01:00
|
|
|
#[allow(dead_code)]
|
2019-08-20 13:20:48 +02:00
|
|
|
pub fn contains_more_than_one_file(&self) -> bool {
|
|
|
|
self.resources.len() > 1
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:16:41 +01:00
|
|
|
#[allow(dead_code)]
|
2019-08-20 13:20:48 +02:00
|
|
|
pub fn contains_files(&self) -> bool {
|
2019-12-06 16:28:26 +01:00
|
|
|
!self.resources.is_empty()
|
2019-08-20 13:20:48 +02:00
|
|
|
}
|
|
|
|
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
pub fn paths_applying_with<F>(
|
|
|
|
&mut self,
|
|
|
|
to: F,
|
|
|
|
) -> Result<Vec<(PathBuf, PathBuf)>, Box<dyn std::error::Error>>
|
2019-08-14 22:05:27 +02:00
|
|
|
where
|
2019-08-21 17:48:04 +02:00
|
|
|
F: Fn((PathBuf, usize)) -> Result<(PathBuf, PathBuf), Box<dyn std::error::Error>>,
|
2019-08-14 22:05:27 +02:00
|
|
|
{
|
|
|
|
self.resources
|
|
|
|
.iter()
|
|
|
|
.map(|f| (PathBuf::from(&f.loc), f.at))
|
|
|
|
.map(|f| to(f))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-08-19 02:12:28 +02:00
|
|
|
pub fn walk_decorate(&mut self, start_path: &Path) -> Result<(), ShellError> {
|
2019-08-14 22:05:27 +02:00
|
|
|
self.resources = Vec::<Res>::new();
|
2019-08-19 02:12:28 +02:00
|
|
|
self.build(start_path, 0)?;
|
2019-08-14 22:05:27 +02:00
|
|
|
self.resources.sort();
|
2019-08-19 02:12:28 +02:00
|
|
|
|
|
|
|
Ok(())
|
2019-08-14 22:05:27 +02:00
|
|
|
}
|
|
|
|
|
2019-08-29 14:16:11 +02:00
|
|
|
fn build(&mut self, src: &Path, lvl: usize) -> Result<(), ShellError> {
|
2020-04-19 01:05:24 +02:00
|
|
|
let source = canonicalize(std::env::current_dir()?, src)?;
|
2019-08-14 22:05:27 +02:00
|
|
|
|
|
|
|
if source.is_dir() {
|
2019-09-03 09:43:37 +02:00
|
|
|
for entry in std::fs::read_dir(src)? {
|
2019-08-19 02:12:28 +02:00
|
|
|
let entry = entry?;
|
2019-08-14 22:05:27 +02:00
|
|
|
let path = entry.path();
|
|
|
|
|
|
|
|
if path.is_dir() {
|
2019-08-19 02:12:28 +02:00
|
|
|
self.build(&path, lvl + 1)?;
|
2019-08-14 22:05:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.resources.push(Res {
|
|
|
|
loc: path.to_path_buf(),
|
|
|
|
at: lvl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.resources.push(Res {
|
|
|
|
loc: source,
|
|
|
|
at: lvl,
|
|
|
|
});
|
|
|
|
}
|
2019-08-19 02:12:28 +02:00
|
|
|
|
|
|
|
Ok(())
|
2019-08-14 22:05:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-09-03 09:43:37 +02:00
|
|
|
use super::{FileStructure, Res, ValueResource, ValueStructure};
|
2019-12-04 20:52:31 +01:00
|
|
|
use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::Tag;
|
2020-05-07 13:58:35 +02:00
|
|
|
use nu_test_support::{fs::Stub::EmptyFile, playground::Playground};
|
2019-08-14 22:05:27 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn structured_sample_record(key: &str, value: &str) -> Value {
|
2019-09-03 09:43:37 +02:00
|
|
|
let mut record = TaggedDictBuilder::new(Tag::unknown());
|
2019-12-31 08:36:08 +01:00
|
|
|
record.insert_untagged(key, UntaggedValue::string(value));
|
2019-11-21 15:33:14 +01:00
|
|
|
record.into_value()
|
2019-09-03 09:43:37 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn sample_nushell_source_code() -> Value {
|
2019-09-03 09:43:37 +02:00
|
|
|
/*
|
|
|
|
src
|
|
|
|
commands
|
|
|
|
plugins => "sys.rs"
|
|
|
|
tests
|
|
|
|
helpers => "mod.rs"
|
|
|
|
*/
|
|
|
|
|
|
|
|
let mut src = TaggedDictBuilder::new(Tag::unknown());
|
|
|
|
let mut record = TaggedDictBuilder::new(Tag::unknown());
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
record.insert_value("commands", structured_sample_record("plugins", "sys.rs"));
|
|
|
|
record.insert_value("tests", structured_sample_record("helpers", "mod.rs"));
|
|
|
|
src.insert_value("src", record.into_value());
|
2019-09-03 09:43:37 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
src.into_value()
|
2019-09-03 09:43:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prepares_and_decorates_value_filesystemlike_sources() {
|
|
|
|
let mut res = ValueStructure::new();
|
|
|
|
|
|
|
|
res.walk_decorate(&sample_nushell_source_code())
|
|
|
|
.expect("Can not decorate values traversal.");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
res.resources,
|
|
|
|
vec![
|
|
|
|
ValueResource {
|
|
|
|
loc: PathBuf::from("src"),
|
|
|
|
at: 0,
|
|
|
|
},
|
|
|
|
ValueResource {
|
|
|
|
loc: PathBuf::from("commands"),
|
|
|
|
at: 1,
|
|
|
|
},
|
|
|
|
ValueResource {
|
|
|
|
loc: PathBuf::from("tests"),
|
|
|
|
at: 1,
|
|
|
|
},
|
|
|
|
ValueResource {
|
|
|
|
loc: PathBuf::from("helpers"),
|
|
|
|
at: 2,
|
|
|
|
},
|
|
|
|
ValueResource {
|
|
|
|
loc: PathBuf::from("plugins"),
|
|
|
|
at: 2,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recognizes_if_path_exists_in_value_filesystemlike_sources() {
|
|
|
|
let mut res = ValueStructure::new();
|
|
|
|
|
|
|
|
res.walk_decorate(&sample_nushell_source_code())
|
|
|
|
.expect("Can not decorate values traversal.");
|
|
|
|
|
|
|
|
assert!(res.exists(&PathBuf::from("/")));
|
|
|
|
|
|
|
|
assert!(res.exists(&PathBuf::from("src/commands/plugins")));
|
|
|
|
assert!(res.exists(&PathBuf::from("src/commands")));
|
|
|
|
assert!(res.exists(&PathBuf::from("src/tests")));
|
|
|
|
assert!(res.exists(&PathBuf::from("src/tests/helpers")));
|
|
|
|
assert!(res.exists(&PathBuf::from("src")));
|
|
|
|
|
|
|
|
assert!(res.exists(&PathBuf::from("/src/commands/plugins")));
|
|
|
|
assert!(res.exists(&PathBuf::from("/src/commands")));
|
|
|
|
assert!(res.exists(&PathBuf::from("/src/tests")));
|
|
|
|
assert!(res.exists(&PathBuf::from("/src/tests/helpers")));
|
|
|
|
assert!(res.exists(&PathBuf::from("/src")));
|
|
|
|
|
|
|
|
assert!(!res.exists(&PathBuf::from("/not_valid")));
|
|
|
|
assert!(!res.exists(&PathBuf::from("/src/not_valid")));
|
|
|
|
}
|
|
|
|
|
2019-08-14 22:05:27 +02:00
|
|
|
#[test]
|
2019-09-03 09:43:37 +02:00
|
|
|
fn prepares_and_decorates_filesystem_source_files() {
|
2020-05-07 13:58:35 +02:00
|
|
|
Playground::setup("file_structure_test", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("sample.ini"),
|
|
|
|
EmptyFile("sample.eml"),
|
|
|
|
EmptyFile("cargo_sample.toml"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let mut res = FileStructure::new();
|
|
|
|
|
|
|
|
res.walk_decorate(&dirs.test())
|
|
|
|
.expect("Can not decorate files traversal.");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
res.resources,
|
|
|
|
vec![
|
|
|
|
Res {
|
|
|
|
loc: dirs.test().join("cargo_sample.toml"),
|
|
|
|
at: 0
|
|
|
|
},
|
|
|
|
Res {
|
|
|
|
loc: dirs.test().join("sample.eml"),
|
|
|
|
at: 0
|
|
|
|
},
|
|
|
|
Res {
|
|
|
|
loc: dirs.test().join("sample.ini"),
|
|
|
|
at: 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
})
|
2019-08-14 22:05:27 +02:00
|
|
|
}
|
|
|
|
}
|