mirror of
https://github.com/sharkdp/bat.git
synced 2025-08-09 15:55:10 +02:00
Make 'build-assets' an optional capability for application
Also structure features a bit more clever to avoid duplication of feature dependency declarations.
This commit is contained in:
@ -4,13 +4,12 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use lazycell::LazyCell;
|
||||
|
||||
use syntect::dumps::{dump_to_file, from_binary, from_reader};
|
||||
use syntect::dumps::{from_binary, from_reader};
|
||||
use syntect::highlighting::{Theme, ThemeSet};
|
||||
use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
|
||||
use syntect::parsing::{SyntaxReference, SyntaxSet};
|
||||
|
||||
use path_abs::PathAbs;
|
||||
|
||||
use crate::assets_metadata::AssetsMetadata;
|
||||
use crate::bat_warning;
|
||||
use crate::error::*;
|
||||
use crate::input::{InputReader, OpenedInput, OpenedInputKind};
|
||||
@ -72,6 +71,7 @@ impl HighlightingAssets {
|
||||
"Monokai Extended"
|
||||
}
|
||||
|
||||
#[cfg(feature = "build-assets")]
|
||||
pub fn from_files(source_dir: &Path, include_integrated_assets: bool) -> Result<Self> {
|
||||
let mut theme_set = if include_integrated_assets {
|
||||
get_integrated_themeset()
|
||||
@ -97,11 +97,11 @@ impl HighlightingAssets {
|
||||
}
|
||||
|
||||
let mut syntax_set_builder = if !include_integrated_assets {
|
||||
let mut builder = SyntaxSetBuilder::new();
|
||||
let mut builder = syntect::parsing::SyntaxSetBuilder::new();
|
||||
builder.add_plain_text_syntax();
|
||||
builder
|
||||
} else {
|
||||
get_integrated_syntaxset().into_builder()
|
||||
from_binary::<SyntaxSet>(get_serialized_integrated_syntaxset()).into_builder()
|
||||
};
|
||||
|
||||
let syntax_dir = source_dir.join("syntaxes");
|
||||
@ -152,6 +152,7 @@ impl HighlightingAssets {
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(feature = "build-assets")]
|
||||
pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
|
||||
let _ = fs::create_dir_all(target_dir);
|
||||
asset_to_cache(
|
||||
@ -169,7 +170,7 @@ impl HighlightingAssets {
|
||||
"Writing metadata to folder {} ... ",
|
||||
target_dir.to_string_lossy()
|
||||
);
|
||||
AssetsMetadata::new(current_version).save_to_folder(target_dir)?;
|
||||
crate::assets_metadata::AssetsMetadata::new(current_version).save_to_folder(target_dir)?;
|
||||
println!("okay");
|
||||
|
||||
Ok(())
|
||||
@ -416,17 +417,14 @@ fn get_serialized_integrated_syntaxset() -> &'static [u8] {
|
||||
include_bytes!("../assets/syntaxes.bin")
|
||||
}
|
||||
|
||||
fn get_integrated_syntaxset() -> SyntaxSet {
|
||||
from_binary(get_serialized_integrated_syntaxset())
|
||||
}
|
||||
|
||||
fn get_integrated_themeset() -> ThemeSet {
|
||||
from_binary(include_bytes!("../assets/themes.bin"))
|
||||
}
|
||||
|
||||
#[cfg(feature = "build-assets")]
|
||||
fn asset_to_cache<T: serde::Serialize>(asset: &T, path: &Path, description: &str) -> Result<()> {
|
||||
print!("Writing {} to {} ... ", description, path.to_string_lossy());
|
||||
dump_to_file(asset, &path).chain_err(|| {
|
||||
syntect::dumps::dump_to_file(asset, &path).chain_err(|| {
|
||||
format!(
|
||||
"Could not save {} to {}",
|
||||
description,
|
||||
|
@ -16,6 +16,7 @@ pub struct AssetsMetadata {
|
||||
const FILENAME: &str = "metadata.yaml";
|
||||
|
||||
impl AssetsMetadata {
|
||||
#[cfg(feature = "build-assets")]
|
||||
pub(crate) fn new(current_version: &str) -> AssetsMetadata {
|
||||
AssetsMetadata {
|
||||
bat_version: Some(current_version.to_owned()),
|
||||
@ -23,6 +24,7 @@ impl AssetsMetadata {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "build-assets")]
|
||||
pub(crate) fn save_to_folder(&self, path: &Path) -> Result<()> {
|
||||
let file = File::create(path.join(FILENAME))?;
|
||||
serde_yaml::to_writer(file, self)?;
|
||||
|
@ -23,12 +23,10 @@ use crate::{
|
||||
};
|
||||
|
||||
use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
|
||||
use clap::crate_version;
|
||||
use directories::PROJECT_DIRS;
|
||||
use globset::GlobMatcher;
|
||||
|
||||
use bat::{
|
||||
assets::HighlightingAssets,
|
||||
config::Config,
|
||||
controller::Controller,
|
||||
error::*,
|
||||
@ -39,21 +37,29 @@ use bat::{
|
||||
|
||||
const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.rs");
|
||||
|
||||
#[cfg(feature = "build-assets")]
|
||||
fn build_assets(matches: &clap::ArgMatches) -> Result<()> {
|
||||
let source_dir = matches
|
||||
.value_of("source")
|
||||
.map(Path::new)
|
||||
.unwrap_or_else(|| PROJECT_DIRS.config_dir());
|
||||
let target_dir = matches
|
||||
.value_of("target")
|
||||
.map(Path::new)
|
||||
.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
|
||||
|
||||
let blank = matches.is_present("blank");
|
||||
|
||||
let assets = bat::assets::HighlightingAssets::from_files(source_dir, !blank)?;
|
||||
assets.save_to_cache(target_dir, clap::crate_version!())
|
||||
}
|
||||
|
||||
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
|
||||
if matches.is_present("build") {
|
||||
let source_dir = matches
|
||||
.value_of("source")
|
||||
.map(Path::new)
|
||||
.unwrap_or_else(|| PROJECT_DIRS.config_dir());
|
||||
let target_dir = matches
|
||||
.value_of("target")
|
||||
.map(Path::new)
|
||||
.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
|
||||
|
||||
let blank = matches.is_present("blank");
|
||||
|
||||
let assets = HighlightingAssets::from_files(source_dir, !blank)?;
|
||||
assets.save_to_cache(target_dir, crate_version!())?;
|
||||
#[cfg(feature = "build-assets")]
|
||||
build_assets(matches)?;
|
||||
#[cfg(not(feature = "build-assets"))]
|
||||
println!("bat has been built without the 'build-assets' feature. The 'cache --build' option is not available.");
|
||||
} else if matches.is_present("clear") {
|
||||
clear_assets();
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ pub struct Config<'a> {
|
||||
pub use_custom_assets: bool,
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "application", feature = "paging"))]
|
||||
#[cfg(all(feature = "minimal-application", feature = "paging"))]
|
||||
pub fn get_pager_executable(config_pager: Option<&str>) -> Option<String> {
|
||||
if let Ok(Some(pager)) = crate::pager::get_pager(config_pager) {
|
||||
Some(pager.bin)
|
||||
|
@ -3,7 +3,7 @@ use std::io::Write;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Clap(::clap::Error) #[cfg(feature = "application")];
|
||||
Clap(::clap::Error) #[cfg(feature = "minimal-application")];
|
||||
Io(::std::io::Error);
|
||||
SyntectError(::syntect::LoadingError);
|
||||
ParseIntError(::std::num::ParseIntError);
|
||||
|
@ -40,6 +40,7 @@ mod preprocessor;
|
||||
mod pretty_printer;
|
||||
pub(crate) mod printer;
|
||||
pub mod style;
|
||||
#[cfg(feature = "build-assets")]
|
||||
mod syntax_dependencies;
|
||||
pub(crate) mod syntax_mapping;
|
||||
mod terminal;
|
||||
|
Reference in New Issue
Block a user