fix(cli): use xdg config directory on macOS

fix: address feedback
This commit is contained in:
WillLillis 2024-12-22 19:10:29 -05:00 committed by Amaan Qureshi
parent b747261929
commit faf97b896a
7 changed files with 59 additions and 60 deletions

View file

@ -37,7 +37,6 @@ clap_complete_nushell.workspace = true
ctor.workspace = true
ctrlc.workspace = true
dialoguer.workspace = true
dirs.workspace = true
filetime.workspace = true
glob.workspace = true
heck.workspace = true

View file

@ -17,6 +17,6 @@ workspace = true
[dependencies]
anyhow.workspace = true
dirs.workspace = true
etcetera.workspace = true
serde.workspace = true
serde_json.workspace = true

View file

@ -2,7 +2,8 @@
use std::{env, fs, path::PathBuf};
use anyhow::{anyhow, Context, Result};
use anyhow::{Context, Result};
use etcetera::BaseStrategy as _;
use serde::{Deserialize, Serialize};
use serde_json::Value;
@ -38,8 +39,24 @@ impl Config {
return Ok(Some(xdg_path));
}
let legacy_path = dirs::home_dir()
.ok_or_else(|| anyhow!("Cannot determine home directory"))?
if cfg!(target_os = "macos") {
let legacy_apple_path = etcetera::base_strategy::Apple::new()?
.data_dir() // `$HOME/Library/Application Support/`
.join("tree-sitter")
.join("config.json");
if legacy_apple_path.is_file() {
fs::create_dir_all(xdg_path.parent().unwrap())?;
fs::rename(&legacy_apple_path, &xdg_path)?;
println!(
"Warning: your config.json file has been automatically migrated from \"{}\" to \"{}\"",
legacy_apple_path.display(),
xdg_path.display()
);
return Ok(Some(xdg_path));
}
}
let legacy_path = etcetera::home_dir()?
.join(".tree-sitter")
.join("config.json");
if legacy_path.is_file() {
@ -50,8 +67,8 @@ impl Config {
}
fn xdg_config_file() -> Result<PathBuf> {
let xdg_path = dirs::config_dir()
.ok_or_else(|| anyhow!("Cannot determine config directory"))?
let xdg_path = etcetera::choose_base_strategy()?
.config_dir()
.join("tree-sitter")
.join("config.json");
Ok(xdg_path)
@ -63,7 +80,7 @@ impl Config {
/// - Location specified by the path parameter if provided
/// - `$TREE_SITTER_DIR/config.json`, if the `TREE_SITTER_DIR` environment variable is set
/// - `tree-sitter/config.json` in your default user configuration directory, as determined by
/// [`dirs::config_dir`](https://docs.rs/dirs/*/dirs/fn.config_dir.html)
/// [`etcetera::choose_base_strategy`](https://docs.rs/etcetera/*/etcetera/#basestrategy)
/// - `$HOME/.tree-sitter/config.json` as a fallback from where tree-sitter _used_ to store
/// its configuration
pub fn load(path: Option<PathBuf>) -> Result<Self> {

View file

@ -26,7 +26,7 @@ default = ["tree-sitter-highlight", "tree-sitter-tags"]
[dependencies]
anyhow.workspace = true
cc.workspace = true
dirs.workspace = true
etcetera.workspace = true
fs4.workspace = true
indoc.workspace = true
lazy_static.workspace = true

View file

@ -20,6 +20,7 @@ use std::{
#[cfg(any(feature = "tree-sitter-highlight", feature = "tree-sitter-tags"))]
use anyhow::Error;
use anyhow::{anyhow, Context, Result};
use etcetera::BaseStrategy as _;
use fs4::fs_std::FileExt;
use indoc::indoc;
use lazy_static::lazy_static;
@ -258,7 +259,7 @@ where
D: Deserializer<'de>,
{
let paths = Vec::<PathBuf>::deserialize(deserializer)?;
let Some(home) = dirs::home_dir() else {
let Ok(home) = etcetera::home_dir() else {
return Ok(paths);
};
let standardized = paths
@ -281,7 +282,7 @@ fn standardize_path(path: PathBuf, home: &Path) -> PathBuf {
impl Config {
#[must_use]
pub fn initial() -> Self {
let home_dir = dirs::home_dir().expect("Cannot determine home directory");
let home_dir = etcetera::home_dir().expect("Cannot determine home directory");
Self {
parser_directories: vec![
home_dir.join("github"),
@ -377,12 +378,22 @@ unsafe impl Sync for Loader {}
impl Loader {
pub fn new() -> Result<Self> {
let parser_lib_path = match env::var("TREE_SITTER_LIBDIR") {
Ok(path) => PathBuf::from(path),
_ => dirs::cache_dir()
.ok_or_else(|| anyhow!("Cannot determine cache directory"))?
let parser_lib_path = if let Ok(path) = env::var("TREE_SITTER_LIBDIR") {
PathBuf::from(path)
} else {
if cfg!(target_os = "macos") {
let legacy_apple_path = etcetera::base_strategy::Apple::new()?
.cache_dir() // `$HOME/Library/Caches/`
.join("tree-sitter");
if legacy_apple_path.exists() && legacy_apple_path.is_dir() {
std::fs::remove_dir_all(legacy_apple_path)?;
}
}
etcetera::choose_base_strategy()?
.cache_dir()
.join("tree-sitter")
.join("lib"),
.join("lib")
};
Ok(Self::with_parser_lib_path(parser_lib_path))
}
@ -733,8 +744,8 @@ impl Loader {
.join("lock")
.join(format!("{}.lock", config.name))
} else {
dirs::cache_dir()
.ok_or_else(|| anyhow!("Cannot determine cache directory"))?
etcetera::choose_base_strategy()?
.cache_dir()
.join("tree-sitter")
.join("lock")
.join(format!("{}.lock", config.name))