use displaydoc::Display;
use super::{remove_dirs, status_from_dirs, BuildStatus, BuildTool, BuildToolKind, BuildToolProbe};
use crate::build_tool_manager::BuildToolManager;
use std::path::{Path, PathBuf};
pub fn register(manager: &mut BuildToolManager) {
let probe = Box::new(MixProbe {});
manager.register(probe);
}
#[derive(Debug)]
pub struct MixProbe;
impl BuildToolProbe for MixProbe {
fn probe(&self, dir: &Path) -> Option<Box<dyn BuildTool>> {
if dir.join("mix.exs").is_file() {
Some(Box::new(Mix::new(dir)))
} else {
None
}
}
fn applies_to(&self, kind: BuildToolKind) -> bool {
use BuildToolKind::*;
matches!(kind, Mix | Elixir | Ex | Exs)
}
}
#[derive(Debug, Display)]
pub struct Mix {
dir: PathBuf,
}
impl Mix {
fn new(path: &Path) -> Self {
Self {
dir: path.to_owned(),
}
}
}
static EPHEMERAL_DIRS: &[&str] = &["_build", "deps", ".elixir_ls"];
impl BuildTool for Mix {
fn clean_project(&mut self, dry_run: bool) -> anyhow::Result<()> {
remove_dirs(&self.dir, EPHEMERAL_DIRS, dry_run)
}
fn status(&self) -> anyhow::Result<BuildStatus> {
status_from_dirs(&self.dir, EPHEMERAL_DIRS)
}
fn project_name(&self) -> Option<anyhow::Result<String>> {
None
}
}
#[cfg(test)]
mod test {
use assert_fs::{
fixture::{FileWriteStr, PathChild},
TempDir,
};
use claim::assert_matches;
use super::*;
#[test]
fn elixir_ls_cache_is_removed_even_if_not_gitignored() {
let root = TempDir::new().unwrap();
root.child("normal")
.child(".elixir_ls")
.child("dummy")
.write_str("dummy")
.unwrap();
root.child("normal")
.child(".gitignore")
.write_str(".elixir_ls/")
.unwrap();
root.child("not-ignored")
.child(".elixir_ls")
.child("dummy")
.write_str("dummy")
.unwrap();
let normal_status = Mix::new(&root.child("normal")).status().unwrap();
assert_matches!(normal_status, BuildStatus::Built{freeable_bytes} if freeable_bytes > 0);
let not_ignored_status = Mix::new(&root.child("not-ignored")).status().unwrap();
assert_matches!(not_ignored_status, BuildStatus::Built{freeable_bytes} if freeable_bytes > 0);
}
}