2022-10-08 20:22:26 +02:00
|
|
|
use std::{fs, path::PathBuf};
|
2022-08-11 23:27:02 +02:00
|
|
|
|
2022-09-18 21:59:59 +02:00
|
|
|
use clap::Parser;
|
2022-08-11 23:27:02 +02:00
|
|
|
use rustyline::{error::ReadlineError, Editor};
|
2022-12-08 22:19:22 +01:00
|
|
|
use tvix_eval::Value; //{Error, EvalWarning, Evaluation, Value};
|
2022-08-04 15:43:51 +02:00
|
|
|
|
2022-09-18 21:59:59 +02:00
|
|
|
#[derive(Parser)]
|
|
|
|
struct Args {
|
|
|
|
/// Path to a script to evaluate
|
|
|
|
script: Option<PathBuf>,
|
|
|
|
|
2022-10-11 01:41:42 +02:00
|
|
|
#[clap(long, short = 'E')]
|
|
|
|
expr: Option<String>,
|
2022-12-09 11:29:08 +01:00
|
|
|
|
2022-12-09 11:33:06 +01:00
|
|
|
/// A colon-separated list of directories to use to resolve `<...>`-style paths
|
|
|
|
#[clap(long, short = 'I', env = "NIX_PATH")]
|
|
|
|
nix_search_path: Option<String>,
|
|
|
|
|
2022-12-09 11:29:08 +01:00
|
|
|
/// Print "raw" (unquoted) output.
|
|
|
|
#[clap(long)]
|
|
|
|
raw: bool,
|
2022-12-08 22:19:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Interprets the given code snippet, printing out warnings, errors
|
|
|
|
/// and the result itself. The return value indicates whether
|
|
|
|
/// evaluation succeeded.
|
2022-12-09 11:29:08 +01:00
|
|
|
fn interpret(code: &str, path: Option<PathBuf>, args: &Args) -> bool {
|
2022-12-09 11:33:06 +01:00
|
|
|
let mut eval = tvix_eval::Evaluation::new(code, path);
|
|
|
|
eval.nix_path = args.nix_search_path.clone();
|
|
|
|
|
2022-12-09 11:16:01 +01:00
|
|
|
let source_map = eval.source_map();
|
2022-12-08 22:19:22 +01:00
|
|
|
let result = eval.evaluate();
|
|
|
|
|
|
|
|
for error in &result.errors {
|
|
|
|
error.fancy_format_stderr(&source_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
for warning in &result.warnings {
|
|
|
|
warning.fancy_format_stderr(&source_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(value) = result.value.as_ref() {
|
2022-12-09 11:29:08 +01:00
|
|
|
println_result(value, args.raw);
|
2022-12-08 22:19:22 +01:00
|
|
|
}
|
2022-10-11 01:41:42 +02:00
|
|
|
|
2022-12-08 22:19:22 +01:00
|
|
|
// inform the caller about any errors
|
|
|
|
result.errors.is_empty()
|
2022-09-18 21:59:59 +02:00
|
|
|
}
|
|
|
|
|
2022-08-04 15:29:38 +02:00
|
|
|
fn main() {
|
2022-09-18 21:59:59 +02:00
|
|
|
let args = Args::parse();
|
2022-08-04 15:43:51 +02:00
|
|
|
|
2022-12-09 11:29:08 +01:00
|
|
|
if let Some(file) = &args.script {
|
|
|
|
run_file(file.clone(), &args)
|
|
|
|
} else if let Some(expr) = &args.expr {
|
|
|
|
if !interpret(expr, None, &args) {
|
2022-12-08 22:19:22 +01:00
|
|
|
std::process::exit(1);
|
2022-10-11 01:41:42 +02:00
|
|
|
}
|
2022-08-04 15:43:51 +02:00
|
|
|
} else {
|
2022-12-09 11:29:08 +01:00
|
|
|
run_prompt(&args)
|
2022-08-04 15:43:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:29:08 +01:00
|
|
|
fn run_file(mut path: PathBuf, args: &Args) {
|
2022-10-08 20:22:26 +02:00
|
|
|
if path.is_dir() {
|
|
|
|
path.push("default.nix");
|
|
|
|
}
|
|
|
|
let contents = fs::read_to_string(&path).expect("failed to read the input file");
|
2022-12-08 22:19:22 +01:00
|
|
|
|
2022-12-09 11:29:08 +01:00
|
|
|
if !interpret(&contents, Some(path), args) {
|
2022-12-08 22:19:22 +01:00
|
|
|
std::process::exit(1);
|
2022-08-11 23:27:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 23:47:39 +01:00
|
|
|
fn println_result(result: &Value, raw: bool) {
|
|
|
|
if raw {
|
|
|
|
println!("{}", result.to_str().unwrap().as_str())
|
|
|
|
} else {
|
|
|
|
println!("=> {} :: {}", result, result.type_of())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 23:27:02 +02:00
|
|
|
fn state_dir() -> Option<PathBuf> {
|
|
|
|
let mut path = dirs::data_dir();
|
2022-08-14 01:51:09 +02:00
|
|
|
if let Some(p) = path.as_mut() {
|
|
|
|
p.push("tvix")
|
|
|
|
}
|
2022-08-11 23:27:02 +02:00
|
|
|
path
|
2022-08-04 15:43:51 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 11:29:08 +01:00
|
|
|
fn run_prompt(args: &Args) {
|
2022-08-11 23:27:02 +02:00
|
|
|
let mut rl = Editor::<()>::new().expect("should be able to launch rustyline");
|
|
|
|
|
|
|
|
let history_path = match state_dir() {
|
2022-08-14 19:08:15 +02:00
|
|
|
// Attempt to set up these paths, but do not hard fail if it
|
|
|
|
// doesn't work.
|
2022-08-11 23:27:02 +02:00
|
|
|
Some(mut path) => {
|
2022-08-26 18:41:56 +02:00
|
|
|
let _ = std::fs::create_dir_all(&path);
|
2022-08-11 23:27:02 +02:00
|
|
|
path.push("history.txt");
|
2022-08-26 18:41:56 +02:00
|
|
|
let _ = rl.load_history(&path);
|
2022-08-11 23:27:02 +02:00
|
|
|
Some(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
None => None,
|
|
|
|
};
|
2022-08-04 15:43:51 +02:00
|
|
|
|
|
|
|
loop {
|
2022-08-11 23:27:02 +02:00
|
|
|
let readline = rl.readline("tvix-repl> ");
|
|
|
|
match readline {
|
|
|
|
Ok(line) => {
|
|
|
|
if line.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-05 04:12:36 +02:00
|
|
|
rl.add_history_entry(&line);
|
2022-12-09 11:29:08 +01:00
|
|
|
interpret(&line, None, args);
|
2022-08-11 23:27:02 +02:00
|
|
|
}
|
|
|
|
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,
|
|
|
|
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("error: {}", err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 15:43:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 23:27:02 +02:00
|
|
|
if let Some(path) = history_path {
|
|
|
|
rl.save_history(&path).unwrap();
|
2022-08-04 15:43:51 +02:00
|
|
|
}
|
2022-08-04 15:29:38 +02:00
|
|
|
}
|