Skip to content

Commit

Permalink
Merge branch 'main' into string-subst
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami authored Sep 29, 2024
2 parents 980d195 + 950da22 commit ed413cb
Show file tree
Hide file tree
Showing 22 changed files with 153 additions and 139 deletions.
11 changes: 2 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/deno_task_shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ shell = ["futures", "glob", "os_pipe", "path-dedot", "tokio", "tokio-util"]
serialization = ["serde"]

[dependencies]
anyhow = "1.0.87"
futures = { version = "0.3.30", optional = true }
glob = { version = "0.3.1", optional = true }
path-dedot = { version = "3.1.1", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/deno_task_shell/src/shell/commands/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use anyhow::bail;
use anyhow::Result;
use miette::bail;
use miette::Result;

#[derive(Debug, PartialEq, Eq)]
pub enum ArgKind<'a> {
Expand Down
5 changes: 3 additions & 2 deletions crates/deno_task_shell/src/shell/commands/cat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use anyhow::Result;
use futures::future::LocalBoxFuture;
use miette::IntoDiagnostic;
use miette::Result;
use std::fs::File;
use std::io::IsTerminal;
use std::io::Read;
Expand Down Expand Up @@ -52,7 +53,7 @@ fn execute_cat(mut context: ShellCommandContext) -> Result<ExecuteResult> {
return Ok(ExecuteResult::for_cancellation());
}

let size = file.read(&mut buf)?;
let size = file.read(&mut buf).into_diagnostic()?;
if size == 0 {
break;
} else {
Expand Down
6 changes: 3 additions & 3 deletions crates/deno_task_shell/src/shell/commands/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use std::path::Path;
use std::path::PathBuf;

use anyhow::bail;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use miette::bail;
use miette::Result;
use path_dedot::ParseDot;

use crate::shell::fs_util;
Expand Down Expand Up @@ -47,7 +47,7 @@ fn execute_cd(cwd: &Path, args: Vec<String>) -> Result<PathBuf> {
let path = parse_args(args.clone())?;
let new_dir = if path == "~" {
dirs::home_dir()
.ok_or_else(|| anyhow::anyhow!("Home directory not found"))?
.ok_or_else(|| miette::miette!("Home directory not found"))?
} else {
cwd.join(&path)
};
Expand Down
30 changes: 19 additions & 11 deletions crates/deno_task_shell/src/shell/commands/cp_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
use std::path::Path;
use std::path::PathBuf;

use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use futures::future::BoxFuture;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use miette::bail;
use miette::Context;
use miette::IntoDiagnostic;
use miette::Result;

use crate::shell::types::ExecuteResult;
use crate::shell::types::ShellPipeWriter;
Expand Down Expand Up @@ -87,7 +88,9 @@ async fn do_copy_operation(
bail!("source was a directory; maybe specify -r")
}
} else {
tokio::fs::copy(&from.path, &to.path).await?;
tokio::fs::copy(&from.path, &to.path)
.await
.into_diagnostic()?;
}
Ok(())
}
Expand All @@ -100,13 +103,15 @@ fn copy_dir_recursively(
async move {
tokio::fs::create_dir_all(&to)
.await
.with_context(|| format!("Creating {}", to.display()))?;
.into_diagnostic()
.context(miette::miette!("Creating {}", to.display()))?;
let mut read_dir = tokio::fs::read_dir(&from)
.await
.with_context(|| format!("Reading {}", from.display()))?;
.into_diagnostic()
.context(miette::miette!("Reading {}", from.display()))?;

while let Some(entry) = read_dir.next_entry().await? {
let file_type = entry.file_type().await?;
while let Some(entry) = read_dir.next_entry().await.into_diagnostic()? {
let file_type = entry.file_type().await.into_diagnostic()?;
let new_from = from.join(entry.file_name());
let new_to = to.join(entry.file_name());

Expand All @@ -117,9 +122,12 @@ fn copy_dir_recursively(
format!("Dir {} to {}", new_from.display(), new_to.display())
})?;
} else if file_type.is_file() {
tokio::fs::copy(&new_from, &new_to).await.with_context(|| {
format!("Copying {} to {}", new_from.display(), new_to.display())
})?;
tokio::fs::copy(&new_from, &new_to)
.await
.into_diagnostic()
.with_context(|| {
format!("Copying {} to {}", new_from.display(), new_to.display())
})?;
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/deno_task_shell/src/shell/commands/exit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use anyhow::bail;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use miette::bail;
use miette::Result;

use crate::shell::types::ExecuteResult;

Expand Down
11 changes: 6 additions & 5 deletions crates/deno_task_shell/src/shell/commands/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
use std::fs::File;
use std::io::Read;

use anyhow::bail;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use miette::bail;
use miette::IntoDiagnostic;
use miette::Result;
use tokio_util::sync::CancellationToken;

use crate::ExecuteResult;
Expand Down Expand Up @@ -96,7 +97,7 @@ fn execute_head(mut context: ShellCommandContext) -> Result<ExecuteResult> {
&mut context.stdout,
flags.lines,
context.state.token(),
|buf| file.read(buf).map_err(Into::into),
|buf| file.read(buf).into_diagnostic(),
512,
),
Err(err) => {
Expand Down Expand Up @@ -131,15 +132,15 @@ fn parse_args(args: Vec<String>) -> Result<HeadFlags> {
}
ArgKind::ShortFlag('n') => match iterator.next() {
Some(ArgKind::Arg(arg)) => {
lines = Some(arg.parse::<u64>()?);
lines = Some(arg.parse::<u64>().into_diagnostic()?);
}
_ => bail!("expected a value following -n"),
},
ArgKind::LongFlag(flag) => {
if flag == "lines" || flag == "lines=" {
bail!("expected a value for --lines");
} else if let Some(arg) = flag.strip_prefix("lines=") {
lines = Some(arg.parse::<u64>()?);
lines = Some(arg.parse::<u64>().into_diagnostic()?);
} else {
arg.bail_unsupported()?
}
Expand Down
4 changes: 2 additions & 2 deletions crates/deno_task_shell/src/shell/commands/mkdir.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use anyhow::bail;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use miette::bail;
use miette::Result;
use std::path::Path;

use crate::shell::types::ExecuteResult;
Expand Down
4 changes: 2 additions & 2 deletions crates/deno_task_shell/src/shell/commands/pwd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use anyhow::Context;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use miette::Context;
use miette::Result;
use std::path::Path;

use crate::shell::fs_util;
Expand Down
4 changes: 2 additions & 2 deletions crates/deno_task_shell/src/shell/commands/rm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use anyhow::bail;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use miette::bail;
use miette::Result;
use std::io::ErrorKind;
use std::path::Path;

Expand Down
15 changes: 8 additions & 7 deletions crates/deno_task_shell/src/shell/commands/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

use std::time::Duration;

use anyhow::bail;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use miette::bail;
use miette::IntoDiagnostic;
use miette::Result;

use crate::shell::types::ExecuteResult;
use crate::shell::types::ShellPipeWriter;
Expand Down Expand Up @@ -54,19 +55,19 @@ async fn execute_sleep(args: Vec<String>) -> Result<()> {

fn parse_arg(arg: &str) -> Result<f64> {
if let Some(t) = arg.strip_suffix('s') {
return Ok(t.parse()?);
return t.parse().into_diagnostic();
}
if let Some(t) = arg.strip_suffix('m') {
return Ok(t.parse::<f64>()? * 60.);
return Ok(t.parse::<f64>().into_diagnostic()? * 60.);
}
if let Some(t) = arg.strip_suffix('h') {
return Ok(t.parse::<f64>()? * 60. * 60.);
return Ok(t.parse::<f64>().into_diagnostic()? * 60. * 60.);
}
if let Some(t) = arg.strip_suffix('d') {
return Ok(t.parse::<f64>()? * 60. * 60. * 24.);
return Ok(t.parse::<f64>().into_diagnostic()? * 60. * 60. * 24.);
}

Ok(arg.parse()?)
arg.parse().into_diagnostic()
}

fn parse_args(args: Vec<String>) -> Result<u64> {
Expand Down
4 changes: 2 additions & 2 deletions crates/deno_task_shell/src/shell/commands/unset.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use anyhow::bail;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use miette::bail;
use miette::Result;

use crate::shell::types::ExecuteResult;
use crate::EnvChange;
Expand Down
7 changes: 4 additions & 3 deletions crates/deno_task_shell/src/shell/commands/xargs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use anyhow::bail;
use anyhow::Result;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use miette::bail;
use miette::IntoDiagnostic;
use miette::Result;

use crate::shell::types::ExecuteResult;
use crate::shell::types::ShellPipeReader;
Expand Down Expand Up @@ -51,7 +52,7 @@ fn xargs_collect_args(
let flags = parse_args(cli_args)?;
let mut buf = Vec::new();
stdin.pipe_to(&mut buf)?;
let text = String::from_utf8(buf)?;
let text = String::from_utf8(buf).into_diagnostic()?;
let mut args = flags.initial_args;

if args.is_empty() {
Expand Down
19 changes: 9 additions & 10 deletions crates/deno_task_shell/src/shell/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use std::collections::HashMap;
use std::path::Path;
use std::rc::Rc;

use anyhow::Context;
use anyhow::Error;
use futures::future;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use miette::IntoDiagnostic;
use miette::Error;
use thiserror::Error;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
Expand Down Expand Up @@ -582,9 +581,9 @@ async fn evaluate_arithmetic_part(
_ => {
let var = state
.get_var(name)
.ok_or_else(|| anyhow::anyhow!("Undefined variable: {}", name))?;
.ok_or_else(|| miette::miette!("Undefined variable: {}", name))?;
let parsed_var = var.parse::<ArithmeticResult>().map_err(|e| {
anyhow::anyhow!("Failed to parse variable '{}': {}", name, e)
miette::miette!("Failed to parse variable '{}': {}", name, e)
})?;
match op {
AssignmentOp::MultiplyAssign => val.checked_mul(&parsed_var),
Expand Down Expand Up @@ -653,11 +652,11 @@ async fn evaluate_arithmetic_part(
.get_var(name)
.and_then(|s| s.parse::<ArithmeticResult>().ok())
.ok_or_else(|| {
anyhow::anyhow!("Undefined or non-integer variable: {}", name)
miette::miette!("Undefined or non-integer variable: {}", name)
}),
ArithmeticPart::Number(num_str) => num_str
.parse::<ArithmeticResult>()
.map_err(|e| anyhow::anyhow!(e.to_string())),
.map_err(|e| miette::miette!(e.to_string())),
}
}

Expand Down Expand Up @@ -1104,7 +1103,7 @@ pub enum EvaluateWordTextError {
#[error("glob: no matches found '{}'", pattern)]
NoFilesMatched { pattern: String },
#[error("Failed to get home directory")]
FailedToGetHomeDirectory(anyhow::Error),
FailedToGetHomeDirectory(miette::Error),
}

impl EvaluateWordTextError {
Expand All @@ -1114,8 +1113,8 @@ impl EvaluateWordTextError {
}
}

impl From<anyhow::Error> for EvaluateWordTextError {
fn from(err: anyhow::Error) -> Self {
impl From<miette::Error> for EvaluateWordTextError {
fn from(err: miette::Error) -> Self {
Self::FailedToGetHomeDirectory(err)
}
}
Expand Down Expand Up @@ -1322,7 +1321,7 @@ fn evaluate_word_parts(
WordPart::Tilde(tilde_prefix) => {
if tilde_prefix.only_tilde() {
let home_str = dirs::home_dir()
.context("Failed to get home directory")?
.ok_or_else(|| miette::miette!("Failed to get home directory"))?
.display()
.to_string();
current_text.push(TextPart::Text(home_str));
Expand Down
Loading

0 comments on commit ed413cb

Please sign in to comment.