From 5fbe5cf7858cc561e47aecb323fecb7ab6a7e122 Mon Sep 17 00:00:00 2001 From: Xavier L'Heureux Date: Thu, 14 May 2020 04:17:23 -0400 Subject: [PATCH] Use the directories crate instead of app_dirs (#1782) The app_dirs crate is abandonned since quite a bit of time. Use the directories crate instead, which is maintained and have more OS support. --- Cargo.lock | 54 ++++++------------------ crates/nu-cli/Cargo.toml | 2 +- crates/nu-cli/src/data/config.rs | 39 +++++++---------- crates/nu-test-support/Cargo.toml | 2 +- docs/commands/to-toml.md | 2 +- tests/fixtures/formats/cargo_sample.toml | 2 +- 6 files changed, 33 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5619c7fd..12f7c263 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,18 +33,6 @@ dependencies = [ "winapi 0.3.8", ] -[[package]] -name = "app_dirs" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e73a24bad9bd6a94d6395382a6c69fe071708ae4409f763c5475e14ee896313d" -dependencies = [ - "ole32-sys", - "shell32-sys", - "winapi 0.2.8", - "xdg", -] - [[package]] name = "arc-swap" version = "0.4.5" @@ -752,6 +740,16 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "directories" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "551a778172a450d7fc12e629ca3b0428d00f6afa9a43da1b630d54604e97371c" +dependencies = [ + "cfg-if", + "dirs-sys", +] + [[package]] name = "dirs" version = "1.0.5" @@ -2121,7 +2119,6 @@ name = "nu-cli" version = "0.14.1" dependencies = [ "ansi_term 0.12.1", - "app_dirs", "async-stream", "base64 0.12.0", "bigdecimal", @@ -2136,6 +2133,7 @@ dependencies = [ "csv", "ctrlc", "derive-new", + "directories 2.0.2", "dirs 2.0.2", "dunce", "eml-parser", @@ -2303,7 +2301,7 @@ dependencies = [ name = "nu-test-support" version = "0.14.1" dependencies = [ - "app_dirs", + "directories 2.0.2", "dunce", "getset", "glob", @@ -2600,16 +2598,6 @@ dependencies = [ "objc", ] -[[package]] -name = "ole32-sys" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d2c49021782e5233cd243168edfa8037574afed4eba4bbaf538b3d8d1789d8c" -dependencies = [ - "winapi 0.2.8", - "winapi-build", -] - [[package]] name = "once_cell" version = "1.3.1" @@ -2906,7 +2894,7 @@ checksum = "6b0a3be00b19ee7bd33238c1c523a7ab4df697345f6b36f90827a7860ea938d4" dependencies = [ "ansi_term 0.11.0", "config", - "directories", + "directories 1.0.2", "isatty", "petgraph", "serde 1.0.106", @@ -3421,16 +3409,6 @@ dependencies = [ "yaml-rust", ] -[[package]] -name = "shell32-sys" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ee04b46101f57121c9da2b151988283b6beb79b34f5bb29a58ee48cb695122c" -dependencies = [ - "winapi 0.2.8", - "winapi-build", -] - [[package]] name = "shellexpand" version = "2.0.0" @@ -4199,12 +4177,6 @@ dependencies = [ "log", ] -[[package]] -name = "xdg" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" - [[package]] name = "xml-rs" version = "0.8.2" diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index 692f0591..51797c9f 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -20,7 +20,7 @@ nu-test-support = { version = "0.14.1", path = "../nu-test-support" } ansi_term = "0.12.1" -app_dirs = "1.2.1" +directories = "2.0.2" async-stream = "0.2" base64 = "0.12.0" bigdecimal = { version = "0.1.0", features = ["serde"] } diff --git a/crates/nu-cli/src/data/config.rs b/crates/nu-cli/src/data/config.rs index c6428978..5bda300d 100644 --- a/crates/nu-cli/src/data/config.rs +++ b/crates/nu-cli/src/data/config.rs @@ -10,7 +10,7 @@ pub(crate) use nuconfig::NuConfig; use crate::commands::from_toml::convert_toml_value_to_nu_value; use crate::commands::to_toml::value_to_toml_value; use crate::prelude::*; -use app_dirs::*; +use directories::ProjectDirs; use indexmap::IndexMap; use log::trace; use nu_errors::ShellError; @@ -20,13 +20,8 @@ use std::fs::{self, OpenOptions}; use std::io; use std::path::{Path, PathBuf}; -pub const APP_INFO: AppInfo = AppInfo { - name: "nu", - author: "nu shell developers", -}; - pub fn config_path() -> Result { - app_path(AppDataType::UserConfig, "config") + app_path("config", ProjectDirs::config_dir) } pub fn default_path() -> Result { @@ -34,28 +29,26 @@ pub fn default_path() -> Result { } pub fn default_path_for(file: &Option) -> Result { - let filename = &mut config_path()?; - let filename = match file { - None => { - filename.push("config.toml"); - filename - } - Some(file) => { - filename.push(file); - filename - } - }; + let mut filename = config_path()?; + let file: &Path = file + .as_ref() + .map(AsRef::as_ref) + .unwrap_or_else(|| "config.toml".as_ref()); + filename.push(file); - Ok(filename.clone()) + Ok(filename) } pub fn user_data() -> Result { - app_path(AppDataType::UserData, "user data") + app_path("user data", ProjectDirs::data_local_dir) } -pub fn app_path(app_data_type: AppDataType, display: &str) -> Result { - let path = app_root(app_data_type, &APP_INFO).map_err(|err| { - ShellError::untagged_runtime_error(&format!("Couldn't open {} path:\n{}", display, err)) +fn app_path &Path>(display: &str, f: F) -> Result { + let dir = ProjectDirs::from("org", "nushell", "nu") + .ok_or_else(|| ShellError::untagged_runtime_error("Couldn't find project directory"))?; + let path = f(&dir).to_owned(); + std::fs::create_dir_all(&path).map_err(|err| { + ShellError::untagged_runtime_error(&format!("Couldn't create {} path:\n{}", display, err)) })?; Ok(path) diff --git a/crates/nu-test-support/Cargo.toml b/crates/nu-test-support/Cargo.toml index 8740d64b..aeb9431c 100644 --- a/crates/nu-test-support/Cargo.toml +++ b/crates/nu-test-support/Cargo.toml @@ -14,7 +14,7 @@ nu-parser = { path = "../nu-parser", version = "0.14.1" } nu-source = { path = "../nu-source", version = "0.14.1" } nu-protocol = { path = "../nu-protocol", version = "0.14.1" } -app_dirs = "1.2.1" +directories = "2.0.2" dunce = "1.0.0" getset = "0.1.0" glob = "0.3.0" diff --git a/docs/commands/to-toml.md b/docs/commands/to-toml.md index 3efa04d4..85c4873e 100644 --- a/docs/commands/to-toml.md +++ b/docs/commands/to-toml.md @@ -41,7 +41,7 @@ path = "/home/shaurya/Desktop" > open cargo_sample.toml | to toml [dependencies] ansi_term = "0.11.0" -app_dirs = "1.2.1" +directories = "2.0.2" byte-unit = "2.1.0" bytes = "0.4.12" chrono-humanize = "0.0.11" diff --git a/tests/fixtures/formats/cargo_sample.toml b/tests/fixtures/formats/cargo_sample.toml index 02bab7e3..751f3ab9 100644 --- a/tests/fixtures/formats/cargo_sample.toml +++ b/tests/fixtures/formats/cargo_sample.toml @@ -43,7 +43,7 @@ getset = "0.0.7" logos = "0.10.0-rc2" logos-derive = "0.10.0-rc2" language-reporting = "0.3.0" -app_dirs = "1.2.1" +directories = "2.0.2" toml = "0.5.1" toml-query = "0.9.0" clap = "2.33.0"