Files
topgrade/src/steps/emacs.rs

112 lines
3.2 KiB
Rust
Raw Normal View History

#[cfg(any(windows, target_os = "macos"))]
use std::env;
2019-12-26 22:30:09 +02:00
use std::path::{Path, PathBuf};
use anyhow::Result;
use directories::BaseDirs;
use crate::execution_context::ExecutionContext;
use crate::terminal::print_separator;
use crate::utils::{require, require_option, PathExt};
use crate::Step;
const EMACS_UPGRADE: &str = include_str!("emacs.el");
2019-12-26 22:30:09 +02:00
#[cfg(windows)]
2020-03-23 06:09:16 +02:00
const DOOM_PATH: &str = "bin/doom.cmd";
2019-12-26 22:30:09 +02:00
#[cfg(unix)]
2020-03-23 06:09:16 +02:00
const DOOM_PATH: &str = "bin/doom";
pub struct Emacs {
directory: Option<PathBuf>,
2020-01-29 21:31:14 +02:00
doom: Option<PathBuf>,
}
impl Emacs {
fn directory_path(base_dirs: &BaseDirs) -> Option<PathBuf> {
#[cfg(unix)]
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
let emacs_xdg_dir = env::var("XDG_CONFIG_HOME")
.ok()
.and_then(|config| PathBuf::from(config).join("emacs").if_exists())
.or_else(|| base_dirs.home_dir().join(".config/emacs").if_exists());
} else {
let emacs_xdg_dir = base_dirs.config_dir().join("emacs").if_exists();
}
}
#[cfg(unix)]
2020-12-01 08:49:09 +02:00
return base_dirs.home_dir().join(".emacs.d").if_exists().or(emacs_xdg_dir);
#[cfg(windows)]
return env::var("HOME")
.ok()
.and_then(|home| {
PathBuf::from(&home)
.join(".emacs.d")
.if_exists()
.or_else(|| PathBuf::from(&home).join(".config\\emacs").if_exists())
})
.or_else(|| base_dirs.data_dir().join(".emacs.d").if_exists());
}
pub fn new(base_dirs: &BaseDirs) -> Self {
2020-01-29 21:31:14 +02:00
let directory = Emacs::directory_path(base_dirs);
2020-03-23 06:09:16 +02:00
let doom = directory.as_ref().and_then(|d| d.join(DOOM_PATH).if_exists());
2020-01-29 21:31:14 +02:00
Self { directory, doom }
}
pub fn is_doom(&self) -> bool {
self.doom.is_some()
}
pub fn directory(&self) -> Option<&PathBuf> {
self.directory.as_ref()
}
fn update_doom(doom: &Path, ctx: &ExecutionContext) -> Result<()> {
2019-12-26 22:30:09 +02:00
print_separator("Doom Emacs");
let mut command = ctx.run_type().execute(doom);
command.args(&["-y", "upgrade"]);
if ctx.config().yes(Step::Emacs) {
command.arg("--force");
}
command.check_run()
2019-12-26 22:30:09 +02:00
}
pub fn upgrade(&self, ctx: &ExecutionContext) -> Result<()> {
let emacs = require("emacs")?;
2020-08-21 23:04:36 +03:00
let init_file = require_option(self.directory.as_ref(), String::from("Emacs directory does not exist"))?
.join("init.el")
.require()?;
2019-12-26 22:30:09 +02:00
2020-01-29 21:31:14 +02:00
if let Some(doom) = &self.doom {
return Emacs::update_doom(doom, ctx);
2019-12-26 22:30:09 +02:00
}
print_separator("Emacs");
let mut command = ctx.run_type().execute(&emacs);
2019-11-18 21:56:57 +02:00
command
2021-10-28 22:05:35 +03:00
.args(&["--batch", "--debug-init", "-l"])
.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()
}
}