feat(cli): support eval file

This commit is contained in:
2026-02-16 21:48:46 +08:00
parent f0a0593d4c
commit 16a8480d29
5 changed files with 96 additions and 68 deletions

View File

@@ -4,7 +4,7 @@
[no-exit-message] [no-exit-message]
@eval expr: @eval expr:
cargo run -- eval '{{expr}}' cargo run -- eval --expr '{{expr}}'
[no-exit-message] [no-exit-message]
@replr: @replr:
@@ -12,7 +12,7 @@
[no-exit-message] [no-exit-message]
@evalr expr: @evalr expr:
cargo run --release -- eval '{{expr}}' cargo run --release -- eval --expr '{{expr}}'
[no-exit-message] [no-exit-message]
@repli: @repli:
@@ -20,4 +20,4 @@
[no-exit-message] [no-exit-message]
@evali expr: @evali expr:
cargo run --release --features inspector -- --inspect-brk 127.0.0.1:9229 eval '{{expr}}' cargo run --release --features inspector -- --inspect-brk 127.0.0.1:9229 eval --expr '{{expr}}'

View File

@@ -12,6 +12,8 @@ use crate::ir::{
Arg, ArgId, Bool, Builtin, ExprId, Ir, Null, ReplBinding, ScopedImportBinding, SymId, Thunk, Arg, ArgId, Bool, Builtin, ExprId, Ir, Null, ReplBinding, ScopedImportBinding, SymId, Thunk,
ToIr as _, WithLookup, ToIr as _, WithLookup,
}; };
#[cfg(feature = "inspector")]
use crate::runtime::inspector::InspectorServer;
use crate::runtime::{Runtime, RuntimeContext}; use crate::runtime::{Runtime, RuntimeContext};
use crate::store::{DaemonStore, Store, StoreConfig}; use crate::store::{DaemonStore, Store, StoreConfig};
use crate::value::{Symbol, Value}; use crate::value::{Symbol, Value};
@@ -48,7 +50,7 @@ pub struct Context {
ctx: Ctx, ctx: Ctx,
runtime: Runtime<Ctx>, runtime: Runtime<Ctx>,
#[cfg(feature = "inspector")] #[cfg(feature = "inspector")]
_inspector_server: Option<crate::runtime::inspector::InspectorServer>, _inspector_server: Option<InspectorServer>,
} }
macro_rules! eval { macro_rules! eval {

View File

@@ -1,7 +1,8 @@
use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use anyhow::Result; use anyhow::Result;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand, Args};
use hashbrown::HashSet; use hashbrown::HashSet;
use nix_js::context::Context; use nix_js::context::Context;
use nix_js::error::Source; use nix_js::error::Source;
@@ -25,10 +26,22 @@ struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Command { enum Command {
Eval { expr: String }, Eval {
#[clap(flatten)]
source: ExprSource
},
Repl, Repl,
} }
#[derive(Args)]
#[group(required = true, multiple = false)]
struct ExprSource {
#[clap(short, long)]
expr: Option<String>,
#[clap(short, long)]
file: Option<PathBuf>
}
fn create_context(#[cfg(feature = "inspector")] cli: &Cli) -> Result<Context> { fn create_context(#[cfg(feature = "inspector")] cli: &Cli) -> Result<Context> {
#[cfg(feature = "inspector")] #[cfg(feature = "inspector")]
{ {
@@ -50,9 +63,15 @@ fn create_context(#[cfg(feature = "inspector")] cli: &Cli) -> Result<Context> {
Ok(Context::new()?) Ok(Context::new()?)
} }
fn run_eval(context: &mut Context, expr: String) -> Result<()> { fn run_eval(context: &mut Context, src: ExprSource) -> Result<()> {
let src = Source::new_eval(expr)?; let src = if let Some(expr) = src.expr {
match context.eval(src) { Source::new_eval(expr)?
} else if let Some(file) = src.file {
Source::new_file(file)?
} else {
unreachable!()
};
match context.eval_shallow(src) {
Ok(value) => { Ok(value) => {
println!("{value}"); println!("{value}");
} }
@@ -131,7 +150,9 @@ fn main() -> Result<()> {
)?; )?;
match cli.command { match cli.command {
Command::Eval { expr } => run_eval(&mut context, expr), Command::Eval { source } => {
run_eval(&mut context, source)
}
Command::Repl => run_repl(&mut context), Command::Repl => run_repl(&mut context),
} }
} }

View File

@@ -269,7 +269,8 @@ async fn server(
let mut accept = pin!(listener.accept()); let mut accept = pin!(listener.accept());
let stream = tokio::select! { let stream = tokio::select! {
accept_result = &mut accept => { accept_result =
&mut accept => {
match accept_result { match accept_result {
Ok((s, _)) => s, Ok((s, _)) => s,
Err(err) => { Err(err) => {

View File

@@ -540,6 +540,7 @@ impl NixDaemonClient {
} }
/// Query information about a store path /// Query information about a store path
#[allow(dead_code)]
pub async fn query_path_info(&mut self, path: &str) -> IoResult<Option<UnkeyedValidPathInfo>> { pub async fn query_path_info(&mut self, path: &str) -> IoResult<Option<UnkeyedValidPathInfo>> {
let store_path = StorePath::<String>::from_absolute_path(path.as_bytes()) let store_path = StorePath::<String>::from_absolute_path(path.as_bytes())
.map_err(|e| IoError::new(IoErrorKind::InvalidInput, e.to_string()))?; .map_err(|e| IoError::new(IoErrorKind::InvalidInput, e.to_string()))?;
@@ -613,6 +614,7 @@ impl NixDaemonClient {
} }
/// Query which paths are valid /// Query which paths are valid
#[allow(dead_code)]
pub async fn query_valid_paths(&mut self, paths: Vec<String>) -> IoResult<Vec<String>> { pub async fn query_valid_paths(&mut self, paths: Vec<String>) -> IoResult<Vec<String>> {
let store_paths: IoResult<Vec<StorePath<String>>> = paths let store_paths: IoResult<Vec<StorePath<String>>> = paths
.iter() .iter()
@@ -717,6 +719,7 @@ impl NixDaemonConnection {
} }
/// Query information about a store path /// Query information about a store path
#[allow(dead_code)]
pub async fn query_path_info(&self, path: &str) -> IoResult<Option<UnkeyedValidPathInfo>> { pub async fn query_path_info(&self, path: &str) -> IoResult<Option<UnkeyedValidPathInfo>> {
let mut client = self.client.lock().await; let mut client = self.client.lock().await;
client.query_path_info(path).await client.query_path_info(path).await
@@ -729,6 +732,7 @@ impl NixDaemonConnection {
} }
/// Query which paths are valid /// Query which paths are valid
#[allow(dead_code)]
pub async fn query_valid_paths(&self, paths: Vec<String>) -> IoResult<Vec<String>> { pub async fn query_valid_paths(&self, paths: Vec<String>) -> IoResult<Vec<String>> {
let mut client = self.client.lock().await; let mut client = self.client.lock().await;
client.query_valid_paths(paths).await client.query_valid_paths(paths).await