mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
Remove pwd
This commit is contained in:
parent
166c07b28d
commit
ca62f568be
2 changed files with 12 additions and 12 deletions
|
@ -3,7 +3,7 @@ use std::path::PathBuf;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
not_bash::{fs2, pushd, pwd, rm_rf, run},
|
not_bash::{fs2, pushd, rm_rf, run},
|
||||||
project_root,
|
project_root,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ pub fn run_dist(nightly: bool) -> Result<()> {
|
||||||
fn dist_client(nightly: bool) -> Result<()> {
|
fn dist_client(nightly: bool) -> Result<()> {
|
||||||
let _d = pushd("./editors/code");
|
let _d = pushd("./editors/code");
|
||||||
|
|
||||||
let package_json_path = pwd().join("package.json");
|
let package_json_path = PathBuf::from("./package.json");
|
||||||
let original_package_json = fs2::read_to_string(&package_json_path)?;
|
let original_package_json = fs2::read_to_string(&package_json_path)?;
|
||||||
let _restore =
|
let _restore =
|
||||||
Restore { path: package_json_path.clone(), contents: original_package_json.clone() };
|
Restore { path: package_json_path.clone(), contents: original_package_json.clone() };
|
||||||
|
|
|
@ -71,10 +71,6 @@ pub fn pushd(path: impl Into<PathBuf>) -> Pushd {
|
||||||
Pushd { _p: () }
|
Pushd { _p: () }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pwd() -> PathBuf {
|
|
||||||
Env::with(|env| env.cwd())
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for Pushd {
|
impl Drop for Pushd {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
Env::with(|env| env.popd())
|
Env::with(|env| env.popd())
|
||||||
|
@ -101,6 +97,7 @@ pub fn run_process(cmd: String, echo: bool) -> Result<String> {
|
||||||
fn run_process_inner(cmd: &str, echo: bool) -> Result<String> {
|
fn run_process_inner(cmd: &str, echo: bool) -> Result<String> {
|
||||||
let mut args = shelx(cmd);
|
let mut args = shelx(cmd);
|
||||||
let binary = args.remove(0);
|
let binary = args.remove(0);
|
||||||
|
let current_dir = Env::with(|it| it.cwd().to_path_buf());
|
||||||
|
|
||||||
if echo {
|
if echo {
|
||||||
println!("> {}", cmd)
|
println!("> {}", cmd)
|
||||||
|
@ -108,7 +105,7 @@ fn run_process_inner(cmd: &str, echo: bool) -> Result<String> {
|
||||||
|
|
||||||
let output = Command::new(binary)
|
let output = Command::new(binary)
|
||||||
.args(args)
|
.args(args)
|
||||||
.current_dir(pwd())
|
.current_dir(current_dir)
|
||||||
.stdin(Stdio::null())
|
.stdin(Stdio::null())
|
||||||
.stderr(Stdio::inherit())
|
.stderr(Stdio::inherit())
|
||||||
.output()?;
|
.output()?;
|
||||||
|
@ -130,7 +127,6 @@ fn shelx(cmd: &str) -> Vec<String> {
|
||||||
cmd.split_whitespace().map(|it| it.to_string()).collect()
|
cmd.split_whitespace().map(|it| it.to_string()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct Env {
|
struct Env {
|
||||||
pushd_stack: Vec<PathBuf>,
|
pushd_stack: Vec<PathBuf>,
|
||||||
}
|
}
|
||||||
|
@ -138,19 +134,23 @@ struct Env {
|
||||||
impl Env {
|
impl Env {
|
||||||
fn with<F: FnOnce(&mut Env) -> T, T>(f: F) -> T {
|
fn with<F: FnOnce(&mut Env) -> T, T>(f: F) -> T {
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static ENV: RefCell<Env> = Default::default();
|
static ENV: RefCell<Env> = RefCell::new(Env {
|
||||||
|
pushd_stack: vec![env::current_dir().unwrap()]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
ENV.with(|it| f(&mut *it.borrow_mut()))
|
ENV.with(|it| f(&mut *it.borrow_mut()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pushd(&mut self, dir: PathBuf) {
|
fn pushd(&mut self, dir: PathBuf) {
|
||||||
let dir = self.cwd().join(dir);
|
let dir = self.cwd().join(dir);
|
||||||
self.pushd_stack.push(dir)
|
self.pushd_stack.push(dir);
|
||||||
|
env::set_current_dir(self.cwd()).unwrap();
|
||||||
}
|
}
|
||||||
fn popd(&mut self) {
|
fn popd(&mut self) {
|
||||||
self.pushd_stack.pop().unwrap();
|
self.pushd_stack.pop().unwrap();
|
||||||
|
env::set_current_dir(self.cwd()).unwrap();
|
||||||
}
|
}
|
||||||
fn cwd(&self) -> PathBuf {
|
fn cwd(&self) -> &Path {
|
||||||
self.pushd_stack.last().cloned().unwrap_or_else(|| env::current_dir().unwrap())
|
self.pushd_stack.last().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue