2019-03-12 08:54:19 +02:00
|
|
|
use crate::executor::RunType;
|
|
|
|
|
use crate::terminal::print_separator;
|
|
|
|
|
use crate::utils::{require, require_option, PathExt};
|
2019-12-11 23:05:38 +02:00
|
|
|
use anyhow::Result;
|
2019-03-12 08:54:19 +02:00
|
|
|
use directories::BaseDirs;
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
use std::env;
|
2019-12-26 22:30:09 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-03-12 08:54:19 +02:00
|
|
|
|
|
|
|
|
const EMACS_UPGRADE: &str = include_str!("emacs.el");
|
2019-12-26 22:30:09 +02:00
|
|
|
#[cfg(windows)]
|
|
|
|
|
const DDOM_PATH: &str = "bin/doom.cmd";
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
const DDOM_PATH: &str = "bin/doom";
|
2019-03-12 08:54:19 +02:00
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 22:30:09 +02:00
|
|
|
fn update_doom(doom: &Path, run_type: RunType) -> Result<()> {
|
|
|
|
|
print_separator("Doom Emacs");
|
|
|
|
|
|
|
|
|
|
run_type.execute(doom).arg("upgrade").check_run()
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn upgrade(&self, run_type: RunType) -> Result<()> {
|
2019-03-12 08:54:19 +02:00
|
|
|
let emacs = require("emacs")?;
|
|
|
|
|
let init_file = require_option(self.directory.as_ref())?.join("init.el").require()?;
|
2019-12-26 22:30:09 +02:00
|
|
|
let doom = require_option(self.directory.as_ref())?.join(DDOM_PATH);
|
|
|
|
|
|
|
|
|
|
if doom.exists() {
|
|
|
|
|
return Emacs::update_doom(&doom, run_type);
|
|
|
|
|
}
|
2019-03-12 08:54:19 +02:00
|
|
|
|
|
|
|
|
print_separator("Emacs");
|
|
|
|
|
|
2019-11-18 21:56:57 +02:00
|
|
|
let mut command = run_type.execute(&emacs);
|
|
|
|
|
|
|
|
|
|
command
|
|
|
|
|
.args(&["--batch", "--debug-init", "-l"])
|
2019-05-15 10:59:05 +03:00
|
|
|
.arg(init_file)
|
2019-11-18 21:56:57 +02:00
|
|
|
.arg("--eval");
|
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
command.arg(
|
|
|
|
|
EMACS_UPGRADE
|
|
|
|
|
.chars()
|
|
|
|
|
.map(|c| if c.is_whitespace() { '\u{00a0}' } else { c })
|
2019-11-18 22:25:29 +02:00
|
|
|
.collect::<String>(),
|
2019-11-18 21:56:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
|
command.arg(EMACS_UPGRADE);
|
|
|
|
|
|
|
|
|
|
command.check_run()
|
2019-03-12 08:54:19 +02:00
|
|
|
}
|
|
|
|
|
}
|