From ca9757c0910625ef48fbb34b7c241ff274f33c88 Mon Sep 17 00:00:00 2001 From: Andy Hayden Date: Sun, 21 Oct 2018 21:35:32 -0700 Subject: [PATCH] Ensure DENO_DIR/repl exists and get tests passing again --- src/deno_dir.rs | 6 +++++ src/isolate.rs | 6 ++--- src/repl.rs | 59 +++++++++++++++++++++++++++---------------------- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/deno_dir.rs b/src/deno_dir.rs index e8cc86b1863fc9..bb15f75ef15c5e 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -28,6 +28,8 @@ pub struct DenoDir { // This is where we cache compilation outputs. Example: // /Users/rld/.deno/gen/f39a473452321cacd7c346a870efb0e3e1264b43.js pub deps: PathBuf, + // This is where repl data is stored, e.g. history. + pub repl: PathBuf, // If remote resources should be reloaded. reload: bool, } @@ -49,18 +51,22 @@ impl DenoDir { }; let gen = root.as_path().join("gen"); let deps = root.as_path().join("deps"); + let repl = root.as_path().join("repl"); let deno_dir = DenoDir { root, gen, deps, + repl, reload, }; deno_fs::mkdir(deno_dir.gen.as_ref(), 0o755)?; deno_fs::mkdir(deno_dir.deps.as_ref(), 0o755)?; + deno_fs::mkdir(deno_dir.repl.as_ref(), 0o755)?; debug!("root {}", deno_dir.root.display()); debug!("gen {}", deno_dir.gen.display()); + debug!("repl {}", deno_dir.repl.display()); debug!("deps {}", deno_dir.deps.display()); Ok(deno_dir) diff --git a/src/isolate.rs b/src/isolate.rs index 43f6b9f89cef09..d0f0591ec37a0d 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -96,16 +96,14 @@ impl IsolateState { match repls.entry(name.clone()) { Occupied(entry) => entry.into_mut(), Vacant(entry) => { - let p = self.dir.root.clone(); - entry.insert(DenoRepl::new(&name, &prompt, p)) + entry.insert(DenoRepl::new(&name, &prompt, &self.dir)) } }; } pub fn exit_repl(&self, name: String) { let mut repls = self.repls.lock().unwrap(); - let mut repl = repls.remove(&name).unwrap(); - repl.exit(); + repls.remove(&name).unwrap(); } } diff --git a/src/repl.rs b/src/repl.rs index 401d221d7fa175..4debd255206d16 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -7,6 +7,7 @@ use rustyline::Editor; use msg::ErrorKind; use std::error::Error; +use deno_dir::DenoDir; use errors::new as deno_error; use errors::DenoResult; use std::path::PathBuf; @@ -20,26 +21,20 @@ pub struct DenoRepl { } impl DenoRepl { - pub fn new(name: &String, prompt: &String, path: PathBuf) -> DenoRepl { - // FIXME: hardcoded path to 'history' directory - // TODO: handle situation that 'history' directory doesn't exist - let mut history_path: PathBuf = path.clone(); - history_path.push("history"); - history_path.push(name); - + pub fn new(name: &String, prompt: &String, dir: &DenoDir) -> DenoRepl { let mut repl = DenoRepl { name: name.clone(), prompt: prompt.clone(), editor: Editor::<()>::new(), - history_file: history_path, + history_file: history_path(dir, name), }; repl.load_history(); repl } - fn load_history(&mut self) { - println!("History file: {}", self.history_file.to_str().unwrap()); + fn load_history(&mut self) -> () { + debug!("Loading history file: {:?}", self.history_file); if self .editor .load_history(&self.history_file.to_str().unwrap()) @@ -47,6 +42,23 @@ impl DenoRepl { {} } + fn save_history(&mut self) -> () { + match self + .editor + .save_history(&self.history_file.to_str().unwrap()) + { + Ok(_val) => debug!( + "Saved history file to: {:?}", + self.history_file + ), + Err(e) => eprintln!( + "Unable to save history file: {:?} {}", + self.history_file, + e + ), + }; + } + pub fn readline(&mut self) -> DenoResult { self .editor @@ -56,28 +68,23 @@ impl DenoRepl { line }) .map_err(|err| match err { - Interrupted => exit(1), + Interrupted => { self.save_history(); exit(1) }, err => err, }) .map_err(|err| { deno_error(ErrorKind::Other, err.description().to_string()) }) } +} - pub fn exit(&mut self) { - match self - .editor - .save_history(&self.history_file.to_str().unwrap()) - { - Ok(_val) => println!( - "Saved history file to: {}", - self.history_file.to_str().unwrap() - ), - Err(e) => eprintln!( - "Unable to save history file: {} {}", - self.history_file.to_str().unwrap(), - e - ), - }; +impl Drop for DenoRepl { + fn drop(&mut self) { + self.save_history() } } + +fn history_path(dir: &DenoDir, name: &String) -> PathBuf { + let mut p: PathBuf = dir.repl.clone(); + p.push(format!("{}_history.txt", name)); + p +} \ No newline at end of file