diff --git a/.travis.yml b/.travis.yml index a86bc3b..4703f86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,4 @@ notifications: script: - cargo build --verbose --all - cargo test --verbose --all - - cargo run -- --verbose && rm -r target/debug/bin + - cargo run -- --verbose diff --git a/Cargo.lock b/Cargo.lock index 87fcc48..2896fcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -202,6 +202,7 @@ dependencies = [ "sys-info 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1412,6 +1413,11 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "xdg" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] "checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c" "checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d" @@ -1574,3 +1580,4 @@ dependencies = [ "checksum winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" "checksum xattr 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "244c3741f4240ef46274860397c7c74e50eb23624996930e484c16679633a54c" +"checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" diff --git a/Cargo.toml b/Cargo.toml index 974c3d1..cd394a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ sys-info = "0.5.8" reqwest = "0.9.22" tar = "0.4.26" flate2 = "1.0.13" +xdg = "^2.1" [dev-dependencies] tempfile = "3.1.0" diff --git a/src/checker.rs b/src/checker.rs index 2517c17..e49828b 100644 --- a/src/checker.rs +++ b/src/checker.rs @@ -1,7 +1,7 @@ use crate::architecture::Architecture; -use crate::error::{Error, Result}; +use crate::error::Result; use crate::ostype::OsType; -use std::{fs, io, path::Path, path::PathBuf}; +use std::{fmt::Display, fs, io, path::Path}; #[cfg(test)] mod tests { @@ -9,7 +9,6 @@ mod tests { use crate::error::Result; use flate2::write::GzEncoder; use flate2::Compression; - use std::env; use std::env::consts; use std::fs::File; use tempfile::NamedTempFile; @@ -49,33 +48,11 @@ mod tests { assert_eq!(false, path_exists(&path)); } - #[test] - fn test_get_base_path() { - let pwd = env::current_dir().unwrap(); - let pwd = pwd.display(); - - // this is a little hacky... it only works as long as debug assertions stay dissabled for - // release builds. but for now `cargo test` and `cargo test --release` work, when executed - // from the project root - #[cfg(debug_assertions)] - let profile = "debug"; - #[cfg(not(debug_assertions))] - let profile = "release"; - - let expected = format!( - "{}{sep}target{sep}{}{sep}deps", - pwd, - profile, - sep = std::path::MAIN_SEPARATOR - ); - let base_path = get_base_path(env::current_exe().unwrap()).unwrap(); - assert_eq!(expected, base_path) - } - #[test] fn test_download() -> Result<()> { let base_url = generate_base_url("2.0.3"); - let base_path = get_base_path(env::current_exe().unwrap()).unwrap(); + let base_path = tempfile::tempdir().unwrap(); + let base_path = base_path.path().display().to_string(); let architecture = consts::ARCH.parse::()?; let os_type = consts::OS.parse::()?; @@ -140,7 +117,7 @@ pub fn generate_base_url(version: &str) -> String { format!("{}/{}", base_url, version) } -pub fn download(base_url: &str, base_path: &str, filename: &str) -> Result<()> { +pub fn download(base_url: &str, base_path: impl Display, filename: impl Display) -> Result<()> { let filepath = format!("{}/{}.tar.gz", base_path, filename); let url = format!("{}/{}.tar.gz", base_url, filename); let mut resp = reqwest::get(&url)?; @@ -158,10 +135,3 @@ pub fn unpack(tar_path: impl AsRef, base_path: impl AsRef) -> Result Ok(()) } - -pub fn get_base_path(path: PathBuf) -> Result { - path.parent() - .map(Path::display) - .map(|path| path.to_string()) - .ok_or(Error::InvalidBasePath) -} diff --git a/src/error.rs b/src/error.rs index 28881c8..20a99fb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,7 +11,7 @@ pub enum Error { Network(reqwest::Error), Encoding(std::str::Utf8Error), Output(fmt::Error), - InvalidBasePath, + Xdg(xdg::BaseDirectoriesError), } impl fmt::Display for Error { @@ -25,11 +25,17 @@ impl fmt::Display for Error { Network(err) => write!(fmt, "Error downloading the file ({})", err), Encoding(err) => write!(fmt, "Encoding error ({})", err), Output(err) => write!(fmt, "Output error ({})", err), - InvalidBasePath => write!(fmt, "Invalid Base Path"), + Xdg(err) => write!(fmt, "XdgError({})", err), } } } +impl From for Error { + fn from(err: xdg::BaseDirectoriesError) -> Self { + Error::Xdg(err) + } +} + impl From for Error { fn from(err: std::io::Error) -> Self { Error::IO(err) diff --git a/src/main.rs b/src/main.rs index 21395e8..1a88505 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ mod ostype; use crate::{architecture::Architecture, error::Result, ostype::OsType}; use std::{ - env, env::consts, io::{self, Write}, process, @@ -14,18 +13,23 @@ use std::{ fn main() -> Result<()> { let version = "2.0.3"; + // create XDG profile + let xdg_dirs = xdg::BaseDirectories::with_profile("editorconfig-checker", version)?; let architecture = consts::ARCH.parse::()?; let os_type = consts::OS.parse::()?; - let base_path = checker::get_base_path(env::current_exe()?)?; + // create XDG cache dir in case it does not exist + xdg_dirs.create_cache_directory("")?; + // use XDG cache home as base path + let base_path = xdg_dirs.get_cache_home(); let filename = checker::generate_filename(os_type, architecture); - let tar_path = format!("{}/{}.tar.gz", base_path, filename); - let binary_path = format!("{}/bin/{}", base_path, filename); + let tar_path = format!("{}/{}.tar.gz", base_path.display(), filename); + let binary_path = format!("{}/bin/{}", base_path.display(), filename); if !checker::path_exists(&binary_path) { let base_url: String = checker::generate_base_url(version); - checker::download(&base_url, &base_path, &filename)?; + checker::download(&base_url, base_path.display(), &filename)?; checker::unpack(&tar_path, &base_path)?; }