-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
einmal gott spielen und die richtigen ideen haben
- Loading branch information
Showing
10 changed files
with
100 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
use std::{env, path::PathBuf}; | ||
use std::path::PathBuf; | ||
|
||
use color_eyre::Result; | ||
use color_eyre::{ | ||
eyre::{bail, Context}, | ||
Result, | ||
}; | ||
use tokio::process::Command; | ||
|
||
/// Get the data home directory. | ||
/// | ||
/// This function returns the directory where hulk stores its data. The directory is determined by | ||
/// the `HULK_DATA_HOME` environment variable. If the environment variable is not set, the | ||
/// user-specific data directory (set by `XDG_DATA_HOME`) is used. | ||
pub fn get_data_home() -> Result<PathBuf> { | ||
if let Ok(home) = env::var("HULK_DATA_HOME") { | ||
return Ok(PathBuf::from(home)); | ||
use crate::Repository; | ||
|
||
impl Repository { | ||
/// Get the data home directory. | ||
/// | ||
/// This function returns the directory where hulk stores its data. The directory is determined by | ||
/// the `HULK_DATA_HOME` environment variable. If the environment variable is not set, the | ||
/// user-specific data directory (set by `XDG_DATA_HOME`) is used. | ||
pub async fn resolve_data_home(&self) -> Result<PathBuf> { | ||
let output = Command::new(self.root.join("scripts/resolve_data_home")) | ||
.output() | ||
.await | ||
.wrap_err("failed to spawn resolve_data_home script")?; | ||
|
||
if !output.status.success() { | ||
bail!("failed to resolve data home"); | ||
} | ||
|
||
let data_home = String::from_utf8(output.stdout).wrap_err("failed to parse data home")?; | ||
Ok(PathBuf::from(data_home)) | ||
} | ||
|
||
let base_directories = xdg::BaseDirectories::with_prefix("hulk")?; | ||
Ok(base_directories.get_data_home()) | ||
pub fn data_home_script(&self) -> Result<String> { | ||
let root = self.current_dir_to_root()?; | ||
Ok(format!( | ||
"{root}/scripts/resolve_data_home", | ||
root = root.display(), | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use std::{ | ||
env::current_dir, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use color_eyre::eyre::{Context, ContextCompat, Result}; | ||
use pathdiff::diff_paths; | ||
|
||
use crate::Repository; | ||
|
||
impl Repository { | ||
pub fn root_to_current_dir(&self) -> Result<PathBuf> { | ||
let current_dir = current_dir().wrap_err("failed to get current directory")?; | ||
let path = diff_paths(¤t_dir, &self.root) | ||
.wrap_err("failed to express current directory relative to repository root")?; | ||
Ok(Path::new("./").join(path)) | ||
} | ||
|
||
pub fn current_dir_to_root(&self) -> Result<PathBuf> { | ||
let current_dir = current_dir().wrap_err("failed to get current directory")?; | ||
let path = diff_paths(&self.root, ¤t_dir) | ||
.wrap_err("failed to express repository root relative to current directory")?; | ||
Ok(Path::new("./").join(path)) | ||
} | ||
} |
Oops, something went wrong.