2020-03-21 16:48:27 +01:00
|
|
|
use std::fs;
|
2021-10-11 09:47:48 +02:00
|
|
|
use std::io;
|
2022-11-03 03:30:27 +01:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2020-03-21 16:48:27 +01:00
|
|
|
|
2020-04-21 15:50:46 +02:00
|
|
|
use clap::crate_version;
|
2020-03-21 16:48:27 +01:00
|
|
|
|
2020-04-22 18:10:26 +02:00
|
|
|
use bat::assets::HighlightingAssets;
|
|
|
|
use bat::assets_metadata::AssetsMetadata;
|
2020-04-22 21:45:47 +02:00
|
|
|
use bat::error::*;
|
2020-03-21 16:48:27 +01:00
|
|
|
|
2022-11-03 03:30:27 +01:00
|
|
|
pub fn clear_assets(cache_dir: &Path) {
|
|
|
|
clear_asset(cache_dir.join("themes.bin"), "theme set cache");
|
|
|
|
clear_asset(cache_dir.join("syntaxes.bin"), "syntax set cache");
|
|
|
|
clear_asset(cache_dir.join("metadata.yaml"), "metadata file");
|
2020-03-21 16:48:27 +01:00
|
|
|
}
|
|
|
|
|
2022-11-03 03:30:27 +01:00
|
|
|
pub fn assets_from_cache_or_binary(
|
|
|
|
use_custom_assets: bool,
|
|
|
|
cache_dir: &Path,
|
|
|
|
) -> Result<HighlightingAssets> {
|
2021-08-18 20:37:38 +02:00
|
|
|
if let Some(metadata) = AssetsMetadata::load_from_folder(cache_dir)? {
|
2020-04-21 17:19:07 +02:00
|
|
|
if !metadata.is_compatible_with(crate_version!()) {
|
2020-04-21 15:50:46 +02:00
|
|
|
return Err(format!(
|
|
|
|
"The binary caches for the user-customized syntaxes and themes \
|
|
|
|
in '{}' are not compatible with this version of bat ({}). To solve this, \
|
|
|
|
either rebuild the cache (bat cache --build) or remove \
|
|
|
|
the custom syntaxes/themes (bat cache --clear).\n\
|
|
|
|
For more information, see:\n\n \
|
|
|
|
https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions",
|
|
|
|
cache_dir.to_string_lossy(),
|
|
|
|
crate_version!()
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 20:11:58 +02:00
|
|
|
let custom_assets = if use_custom_assets {
|
2021-08-18 20:37:38 +02:00
|
|
|
HighlightingAssets::from_cache(cache_dir).ok()
|
2021-07-27 20:11:58 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
Ok(custom_assets.unwrap_or_else(HighlightingAssets::from_binary))
|
2020-03-21 16:48:27 +01:00
|
|
|
}
|
2021-07-13 21:53:29 +02:00
|
|
|
|
2022-11-03 03:30:27 +01:00
|
|
|
fn clear_asset(path: PathBuf, description: &str) {
|
2024-02-24 22:36:14 +01:00
|
|
|
print!("Clearing {description} ... ");
|
2021-10-11 09:47:48 +02:00
|
|
|
match fs::remove_file(&path) {
|
|
|
|
Err(err) if err.kind() == io::ErrorKind::NotFound => {
|
|
|
|
println!("skipped (not present)");
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
println!("could not remove the cache file {:?}: {}", &path, err);
|
|
|
|
}
|
|
|
|
Ok(_) => println!("okay"),
|
|
|
|
}
|
2021-07-13 21:53:29 +02:00
|
|
|
}
|