Better Emacs handling in Windows (fix #112)

This commit is contained in:
Roey Darwish Dror
2019-03-12 08:54:19 +02:00
parent 12123ce6cc
commit bb1312e6d9
4 changed files with 54 additions and 31 deletions

View File

@@ -20,8 +20,6 @@ use std::borrow::Cow;
use std::env; use std::env;
use std::fmt::Debug; use std::fmt::Debug;
use std::io; use std::io;
#[cfg(windows)]
use std::path::PathBuf;
use std::process::exit; use std::process::exit;
fn execute<'a, F, M>(report: &mut Report<'a>, key: M, func: F, no_retry: bool) -> Result<(), Error> fn execute<'a, F, M>(report: &mut Report<'a>, key: M, func: F, no_retry: bool) -> Result<(), Error>
@@ -164,16 +162,10 @@ fn run() -> Result<(), Error> {
#[cfg(unix)] #[cfg(unix)]
execute(&mut report, "nix", || unix::run_nix(run_type), config.no_retry())?; execute(&mut report, "nix", || unix::run_nix(run_type), config.no_retry())?;
let emacs = emacs::Emacs::new(&base_dirs);
if config.should_run(Step::Emacs) { if config.should_run(Step::Emacs) {
#[cfg(unix)] if let Some(directory) = emacs.directory() {
git_repos.insert(base_dirs.home_dir().join(".emacs.d")); git_repos.insert(directory);
#[cfg(windows)]
{
git_repos.insert(base_dirs.data_dir().join(".emacs.d"));
if let Ok(home) = env::var("HOME") {
git_repos.insert(PathBuf::from(home).join(".emacs.d"));
}
} }
} }
@@ -264,12 +256,7 @@ fn run() -> Result<(), Error> {
)?; )?;
if config.should_run(Step::Emacs) { if config.should_run(Step::Emacs) {
execute( execute(&mut report, "Emacs", || emacs.upgrade(run_type), config.no_retry())?;
&mut report,
"Emacs",
|| generic::run_emacs(&base_dirs, run_type),
config.no_retry(),
)?;
} }
execute( execute(

49
src/steps/emacs.rs Normal file
View File

@@ -0,0 +1,49 @@
use crate::error::Error;
use crate::executor::RunType;
use crate::terminal::print_separator;
use crate::utils::{require, require_option, PathExt};
use directories::BaseDirs;
#[cfg(windows)]
use std::env;
use std::path::PathBuf;
const EMACS_UPGRADE: &str = include_str!("emacs.el");
pub struct Emacs {
directory: Option<PathBuf>,
}
impl Emacs {
fn directory_path(base_dirs: &BaseDirs) -> Option<PathBuf> {
#[cfg(unix)]
return base_dirs.home_dir().join(".emacs.d").if_exists();
#[cfg(windows)]
return env::var("HOME")
.ok()
.and_then(|home| PathBuf::from(home).join(".emacs.d").if_exists())
.or_else(|| base_dirs.data_dir().join(".emacs.d").if_exists());
}
pub fn new(base_dirs: &BaseDirs) -> Self {
Self {
directory: Emacs::directory_path(base_dirs),
}
}
pub fn directory(&self) -> Option<&PathBuf> {
self.directory.as_ref()
}
pub fn upgrade(&self, run_type: RunType) -> Result<(), Error> {
let emacs = require("emacs")?;
let init_file = require_option(self.directory.as_ref())?.join("init.el").require()?;
print_separator("Emacs");
run_type
.execute(&emacs)
.args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE])
.check_run()
}
}

View File

@@ -7,8 +7,6 @@ use failure::ResultExt;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
const EMACS_UPGRADE: &str = include_str!("emacs.el");
pub fn run_cargo_update(run_type: RunType) -> Result<(), Error> { pub fn run_cargo_update(run_type: RunType) -> Result<(), Error> {
let cargo_update = utils::require("cargo-install-update")?; let cargo_update = utils::require("cargo-install-update")?;
@@ -29,18 +27,6 @@ pub fn run_gem(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
run_type.execute(&gem).args(&["update", "--user-install"]).check_run() run_type.execute(&gem).args(&["update", "--user-install"]).check_run()
} }
pub fn run_emacs(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
let emacs = utils::require("emacs")?;
let init_file = base_dirs.home_dir().join(".emacs.d/init.el").require()?;
print_separator("Emacs");
run_type
.execute(&emacs)
.args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE])
.check_run()
}
#[cfg(not(any( #[cfg(not(any(
target_os = "freebsd", target_os = "freebsd",
target_os = "openbsd", target_os = "openbsd",

View File

@@ -1,3 +1,4 @@
pub mod emacs;
pub mod generic; pub mod generic;
pub mod git; pub mod git;
pub mod node; pub mod node;