nushell/src/commands/classified.rs

386 lines
13 KiB
Rust
Raw Normal View History

2019-08-02 21:15:07 +02:00
use crate::commands::Command;
2019-08-09 06:51:21 +02:00
use crate::parser::{hir, TokenNode};
2019-05-22 09:12:03 +02:00
use crate::prelude::*;
2019-05-26 08:54:41 +02:00
use bytes::{BufMut, BytesMut};
2019-06-15 19:52:55 +02:00
use futures::stream::StreamExt;
2019-05-26 08:54:41 +02:00
use futures_codec::{Decoder, Encoder, Framed};
2019-06-22 05:43:37 +02:00
use log::{log_enabled, trace};
2019-05-26 08:54:41 +02:00
use std::io::{Error, ErrorKind};
2019-05-22 09:12:03 +02:00
use std::sync::Arc;
use subprocess::Exec;
2019-06-22 05:43:37 +02:00
2019-05-25 21:07:52 +02:00
/// A simple `Codec` implementation that splits up data into lines.
pub struct LinesCodec {}
impl Encoder for LinesCodec {
type Item = String;
type Error = Error;
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.put(item);
Ok(())
}
}
impl Decoder for LinesCodec {
type Item = String;
type Error = Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
match src.iter().position(|b| b == &b'\n') {
Some(pos) if !src.is_empty() => {
let buf = src.split_to(pos + 1);
String::from_utf8(buf.to_vec())
.map(Some)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))
}
_ if !src.is_empty() => {
let drained = src.take();
String::from_utf8(drained.to_vec())
.map(Some)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))
}
2019-05-26 08:54:41 +02:00
_ => Ok(None),
2019-05-25 21:07:52 +02:00
}
}
}
2019-05-22 09:12:03 +02:00
pub(crate) struct ClassifiedInputStream {
pub(crate) objects: InputStream,
pub(crate) stdin: Option<std::fs::File>,
2019-05-24 09:29:16 +02:00
}
impl ClassifiedInputStream {
pub(crate) fn new() -> ClassifiedInputStream {
2019-05-24 09:29:16 +02:00
ClassifiedInputStream {
objects: VecDeque::new().into(),
2019-05-24 09:29:16 +02:00
stdin: None,
}
}
pub(crate) fn from_input_stream(stream: impl Into<InputStream>) -> ClassifiedInputStream {
ClassifiedInputStream {
objects: stream.into(),
2019-05-24 09:29:16 +02:00
stdin: None,
}
}
pub(crate) fn from_stdout(stdout: std::fs::File) -> ClassifiedInputStream {
2019-05-24 09:29:16 +02:00
ClassifiedInputStream {
objects: VecDeque::new().into(),
2019-05-24 09:29:16 +02:00
stdin: Some(stdout),
}
}
}
pub(crate) struct ClassifiedPipeline {
pub(crate) commands: Vec<ClassifiedCommand>,
2019-05-26 08:54:41 +02:00
}
pub(crate) enum ClassifiedCommand {
#[allow(unused)]
2019-06-22 05:43:37 +02:00
Expr(TokenNode),
2019-05-22 09:12:03 +02:00
Internal(InternalCommand),
External(ExternalCommand),
}
2019-07-09 06:31:26 +02:00
impl ClassifiedCommand {
2019-07-12 21:22:08 +02:00
#[allow(unused)]
2019-07-09 06:31:26 +02:00
pub fn span(&self) -> Span {
match self {
ClassifiedCommand::Expr(token) => token.span(),
ClassifiedCommand::Internal(internal) => internal.name_span.into(),
ClassifiedCommand::External(external) => external.name_span.into(),
}
}
}
pub(crate) struct InternalCommand {
pub(crate) command: Arc<Command>,
pub(crate) name_span: Span,
pub(crate) args: hir::Call,
2019-05-22 09:12:03 +02:00
}
2019-05-24 09:29:16 +02:00
impl InternalCommand {
pub(crate) async fn run(
2019-05-24 09:29:16 +02:00
self,
context: &mut Context,
input: ClassifiedInputStream,
2019-07-24 00:22:11 +02:00
source: Text,
2019-05-24 09:29:16 +02:00
) -> Result<InputStream, ShellError> {
if log_enabled!(log::Level::Trace) {
2019-07-12 21:22:08 +02:00
trace!(target: "nu::run::internal", "->");
trace!(target: "nu::run::internal", "{}", self.command.name());
2019-07-24 00:22:11 +02:00
trace!(target: "nu::run::internal", "{}", self.args.debug(&source));
}
2019-07-12 21:22:08 +02:00
let objects: InputStream =
trace_stream!(target: "nu::trace_stream::internal", "input" = input.objects);
2019-06-22 05:43:37 +02:00
let result = context.run_command(
self.command,
self.name_span.clone(),
context.source_map.clone(),
self.args,
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
&source,
objects,
);
2019-06-22 05:43:37 +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
let result = trace_out_stream!(target: "nu::trace_stream::internal", source: &source, "output" = result);
let mut result = result.values;
2019-06-15 19:52:55 +02:00
let mut stream = VecDeque::new();
while let Some(item) = result.next().await {
match item? {
ReturnSuccess::Action(action) => match action {
2019-06-15 19:52:55 +02:00
CommandAction::ChangePath(path) => {
2019-08-07 19:49:11 +02:00
context.shell_manager.set_path(path);
2019-06-14 00:49:16 +02:00
}
CommandAction::AddSpanSource(uuid, span_source) => {
context.add_span_source(uuid, span_source);
}
2019-07-16 21:10:25 +02:00
CommandAction::Exit => std::process::exit(0),
2019-08-14 19:02:39 +02:00
CommandAction::EnterValueShell(value) => {
2019-08-19 10:07:55 +02:00
context
.shell_manager
.insert_at_current(Box::new(ValueShell::new(value)));
2019-08-14 19:02:39 +02:00
}
2019-08-07 19:49:11 +02:00
CommandAction::EnterShell(location) => {
let path = std::path::Path::new(&location);
if path.is_dir() {
// If it's a directory, add a new filesystem shell
2019-08-19 10:07:55 +02:00
context.shell_manager.insert_at_current(Box::new(
FilesystemShell::with_location(
2019-08-10 07:02:15 +02:00
location,
context.registry().clone(),
2019-08-19 10:07:55 +02:00
)?,
));
2019-08-07 19:49:11 +02:00
} else {
// If it's a file, attempt to open the file as a value and enter it
let cwd = context.shell_manager.path();
let full_path = std::path::PathBuf::from(cwd);
2019-08-29 05:53:45 +02:00
let (_file_extension, contents, contents_tag, span_source) =
2019-08-07 19:49:11 +02:00
crate::commands::open::fetch(
&full_path,
&location,
Span::unknown(),
2019-08-24 21:36:19 +02:00
)
.await?;
2019-08-07 19:49:11 +02:00
2019-08-10 22:18:14 +02:00
if let Some(uuid) = contents_tag.origin {
// If we have loaded something, track its source
context.add_span_source(uuid, span_source);
}
2019-08-29 05:53:45 +02:00
context
.shell_manager
.insert_at_current(Box::new(ValueShell::new(
contents.tagged(contents_tag),
)))
2019-08-07 19:49:11 +02:00
}
}
CommandAction::PreviousShell => {
context.shell_manager.prev();
}
CommandAction::NextShell => {
context.shell_manager.next();
}
CommandAction::LeaveShell => {
2019-08-19 10:07:55 +02:00
context.shell_manager.remove_at_current();
2019-08-07 19:49:11 +02:00
if context.shell_manager.is_empty() {
std::process::exit(0);
}
}
2019-06-15 19:52:55 +02:00
},
2019-05-24 09:29:16 +02:00
ReturnSuccess::Value(v) => {
2019-06-15 19:52:55 +02:00
stream.push_back(v);
}
}
}
Ok(stream.into())
2019-05-24 09:29:16 +02:00
}
}
pub(crate) struct ExternalCommand {
pub(crate) name: String,
2019-06-15 06:56:18 +02:00
#[allow(unused)]
pub(crate) name_span: Span,
pub(crate) args: Vec<Tagged<String>>,
2019-05-22 09:12:03 +02:00
}
2019-05-24 09:29:16 +02:00
pub(crate) enum StreamNext {
Last,
External,
Internal,
}
2019-05-24 09:29:16 +02:00
impl ExternalCommand {
pub(crate) async fn run(
2019-05-24 09:29:16 +02:00
self,
context: &mut Context,
2019-05-24 21:35:22 +02:00
input: ClassifiedInputStream,
stream_next: StreamNext,
2019-05-24 09:29:16 +02:00
) -> Result<ClassifiedInputStream, ShellError> {
let stdin = input.stdin;
2019-08-01 03:58:42 +02:00
let inputs: Vec<Tagged<Value>> = input.objects.into_vec().await;
2019-07-09 06:31:26 +02:00
let name_span = self.name_span.clone();
2019-07-12 21:22:08 +02:00
trace!(target: "nu::run::external", "-> {}", self.name);
trace!(target: "nu::run::external", "inputs = {:?}", inputs);
2019-06-22 05:43:37 +02:00
let mut arg_string = format!("{}", self.name);
for arg in &self.args {
2019-06-22 05:43:37 +02:00
arg_string.push_str(&arg);
}
2019-06-02 09:51:54 +02:00
let mut process;
2019-06-18 04:04:34 +02:00
2019-06-02 09:51:54 +02:00
#[cfg(windows)]
{
process = Exec::shell(&self.name);
if arg_string.contains("$it") {
let mut first = true;
2019-06-22 05:43:37 +02:00
2019-06-02 09:51:54 +02:00
for i in &inputs {
2019-06-15 06:20:58 +02:00
if i.as_string().is_err() {
let mut span = None;
for arg in &self.args {
if arg.item.contains("$it") {
2019-08-01 03:58:42 +02:00
span = Some(arg.span());
2019-06-15 06:20:58 +02:00
}
}
if let Some(span) = span {
return Err(ShellError::labeled_error(
"External $it needs string data",
"given object instead of string data",
span,
));
} else {
return Err(ShellError::string("Error: $it needs string data"));
}
}
2019-06-02 09:51:54 +02:00
if !first {
process = process.arg("&&");
process = process.arg(&self.name);
} else {
first = false;
}
for arg in &self.args {
2019-06-22 05:43:37 +02:00
if arg.chars().all(|c| c.is_whitespace()) {
continue;
}
process = process.arg(&arg.replace("$it", &i.as_string()?));
2019-06-02 09:51:54 +02:00
}
}
} else {
for arg in &self.args {
let arg_chars: Vec<_> = arg.chars().collect();
if arg_chars.len() > 1
&& arg_chars[0] == '"'
&& arg_chars[arg_chars.len() - 1] == '"'
{
// quoted string
let new_arg: String = arg_chars[1..arg_chars.len() - 1].iter().collect();
process = process.arg(new_arg);
} else {
process = process.arg(arg.item.clone());
}
2019-06-02 09:51:54 +02:00
}
}
2019-06-02 09:51:54 +02:00
}
#[cfg(not(windows))]
{
let mut new_arg_string = self.name.to_string();
if arg_string.contains("$it") {
let mut first = true;
for i in &inputs {
let i = match i.as_string() {
2019-08-29 02:45:34 +02:00
Err(_err) => {
let mut span = name_span;
for arg in &self.args {
if arg.item.contains("$it") {
span = arg.span();
}
2019-06-15 06:20:58 +02:00
}
return Err(ShellError::labeled_error(
"External $it needs string data",
"given object instead of string data",
span,
));
2019-06-15 06:20:58 +02:00
}
Ok(val) => val,
};
2019-06-02 09:51:54 +02:00
if !first {
2019-06-03 02:37:16 +02:00
new_arg_string.push_str("&&");
2019-06-02 09:51:54 +02:00
new_arg_string.push_str(&self.name);
} else {
first = false;
}
for arg in &self.args {
2019-06-22 05:43:37 +02:00
if arg.chars().all(|c| c.is_whitespace()) {
continue;
}
2019-06-02 09:51:54 +02:00
new_arg_string.push_str(" ");
new_arg_string.push_str(&arg.replace("$it", &i));
2019-06-02 09:51:54 +02:00
}
}
} else {
for arg in &self.args {
new_arg_string.push_str(" ");
new_arg_string.push_str(&arg);
}
}
2019-06-18 04:04:34 +02:00
2019-06-02 09:51:54 +02:00
process = Exec::shell(new_arg_string);
2019-05-26 00:23:35 +02:00
}
2019-08-07 19:49:11 +02:00
process = process.cwd(context.shell_manager.path());
2019-05-24 09:29:16 +02:00
let mut process = match stream_next {
StreamNext::Last => process,
StreamNext::External | StreamNext::Internal => {
process.stdout(subprocess::Redirection::Pipe)
}
};
2019-05-24 09:29:16 +02:00
if let Some(stdin) = stdin {
2019-05-24 09:29:16 +02:00
process = process.stdin(stdin);
}
let mut popen = process.popen()?;
2019-05-24 09:29:16 +02:00
match stream_next {
StreamNext::Last => {
popen.wait()?;
Ok(ClassifiedInputStream::new())
}
StreamNext::External => {
let stdout = popen.stdout.take().unwrap();
Ok(ClassifiedInputStream::from_stdout(stdout))
}
StreamNext::Internal => {
let stdout = popen.stdout.take().unwrap();
let file = futures::io::AllowStdIo::new(stdout);
let stream = Framed::new(file, LinesCodec {});
let stream = stream.map(move |line| {
Tagged::from_simple_spanned_item(Value::string(line.unwrap()), name_span)
});
Ok(ClassifiedInputStream::from_input_stream(
2019-08-01 03:58:42 +02:00
stream.boxed() as BoxStream<'static, Tagged<Value>>
))
2019-05-24 09:29:16 +02:00
}
}
}
}