Skip to content

Commit

Permalink
eval-cache: parse builtins.getEnv ops
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed Dec 10, 2024
1 parent db06c09 commit 747ce5e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
37 changes: 18 additions & 19 deletions devenv-eval-cache/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a> CachedCommand<'a> {
let status = child.wait().map_err(CommandError::Io)?;

let stdout = stdout_thread.await.unwrap().map_err(CommandError::Io)?;
let (mut ops, stderr) = stderr_thread.await.unwrap();
let (ops, stderr) = stderr_thread.await.unwrap();

if !status.success() {
return Ok(Output {
Expand All @@ -144,28 +144,26 @@ impl<'a> CachedCommand<'a> {
});
}

// Remove excluded paths if any are a parent directory
ops.retain_mut(|op| {
!self
.excluded_paths
.iter()
.any(|path| op.source().starts_with(path))
});

// Convert Ops to FilePaths
let mut file_path_futures = ops
let mut sources = ops
.into_iter()
.map(|op| {
tokio::task::spawn_blocking(move || {
FilePath::new(op.source().to_path_buf()).map_err(CommandError::Io)
})
.filter_map(|op| op.owned_source())
.filter(|source| {
!self
.excluded_paths
.iter()
.any(|path| source.starts_with(path))
})
.collect::<Vec<_>>();
.collect::<Vec<PathBuf>>();

// Watch additional paths
file_path_futures.extend(self.extra_paths.into_iter().map(|path| {
tokio::task::spawn_blocking(move || FilePath::new(path).map_err(CommandError::Io))
}));
sources.extend_from_slice(&self.extra_paths);

let file_path_futures = sources
.into_iter()
.map(|source| {
tokio::task::spawn_blocking(move || FilePath::new(source).map_err(CommandError::Io))
})
.collect::<Vec<_>>();

let mut file_paths = join_all(file_path_futures)
.await
Expand Down Expand Up @@ -369,6 +367,7 @@ fn extract_op_from_log_line(log: &InternalLog) -> Option<Op> {
{
Some(op)
}
Op::GetEnv { .. } => Some(op),
_ => None,
}),
_ => None,
Expand Down
45 changes: 38 additions & 7 deletions devenv-eval-cache/src/op.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::internal_log::InternalLog;

use regex::Regex;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

/// A sum-type of filesystem operations that we can extract from the Nix logs.
#[derive(Clone, Debug, PartialEq)]
Expand All @@ -14,6 +14,8 @@ pub enum Op {
ReadFile { source: PathBuf },
/// List a directory's contents with `builtins.readDir`.
ReadDir { source: PathBuf },
/// Read an environment variable with `builtins.getEnv`.
GetEnv { name: String },
/// Used a tracked devenv string path.
TrackedPath { source: PathBuf },
}
Expand All @@ -30,6 +32,8 @@ impl Op {
Regex::new("^devenv readFile: '(?P<source>.*)'$").expect("invalid regex");
static ref READ_DIR: Regex =
Regex::new("^devenv readDir: '(?P<source>.*)'$").expect("invalid regex");
static ref GET_ENV: Regex =
Regex::new("^devenv getEnv: '(?P<name>.*)'$").expect("invalid regex");
static ref TRACKED_PATH: Regex =
Regex::new("^trace: devenv path: '(?P<source>.*)'$").expect("invalid regex");
}
Expand All @@ -53,6 +57,9 @@ impl Op {
} else if let Some(matches) = READ_DIR.captures(msg) {
let source = PathBuf::from(&matches["source"]);
Some(Op::ReadDir { source })
} else if let Some(matches) = GET_ENV.captures(msg) {
let name = matches["name"].to_string();
Some(Op::GetEnv { name })
} else if let Some(matches) = TRACKED_PATH.captures(msg) {
let source = PathBuf::from(&matches["source"]);
Some(Op::TrackedPath { source })
Expand All @@ -64,13 +71,25 @@ impl Op {
}
}

pub fn source(&self) -> &PathBuf {
pub fn source(&self) -> Option<&Path> {
match self {
Op::CopiedSource { source, .. } => source,
Op::EvaluatedFile { source } => source,
Op::ReadFile { source } => source,
Op::ReadDir { source } => source,
Op::TrackedPath { source } => source,
Op::CopiedSource { source, .. } => Some(source.as_path()),
Op::EvaluatedFile { source } => Some(source.as_path()),
Op::ReadFile { source } => Some(source.as_path()),
Op::ReadDir { source } => Some(source.as_path()),
Op::TrackedPath { source } => Some(source.as_path()),
_ => None,
}
}

pub fn owned_source(self) -> Option<PathBuf> {
match self {
Op::CopiedSource { source, .. } => Some(source),
Op::EvaluatedFile { source } => Some(source),
Op::ReadFile { source } => Some(source),
Op::ReadDir { source } => Some(source),
Op::TrackedPath { source } => Some(source),
_ => None,
}
}
}
Expand Down Expand Up @@ -137,6 +156,18 @@ mod tests {
);
}

#[test]
fn test_get_env() {
let log = create_log("devenv getEnv: 'SOME_ENV'");
let op = Op::from_internal_log(&log);
assert_eq!(
op,
Some(Op::GetEnv {
name: "SOME_ENV".to_string(),
})
);
}

#[test]
fn test_tracked_path() {
let log = create_log("trace: devenv path: '/path/to/file'");
Expand Down

0 comments on commit 747ce5e

Please sign in to comment.