1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-01-31 02:01:05 +00: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:
Martin Nordholts 2021-08-10 22:18:47 +02:00
parent deddc81426
commit 25fa577cd0
8 changed files with 45 additions and 44 deletions

View File

@ -246,12 +246,12 @@ jobs:
command: check command: check
args: --locked --target=${{ matrix.job.target }} --verbose --lib --no-default-features --features regex-onig,git,paging args: --locked --target=${{ matrix.job.target }} --verbose --lib --no-default-features --features regex-onig,git,paging
- name: "Feature check: quick-build-application" - name: "Feature check: minimal-application"
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
use-cross: ${{ matrix.job.use-cross }} use-cross: ${{ matrix.job.use-cross }}
command: check command: check
args: --locked --target=${{ matrix.job.target }} --verbose --no-default-features --features quick-build-application args: --locked --target=${{ matrix.job.target }} --verbose --no-default-features --features minimal-application
- name: Create tarball - name: Create tarball
id: package id: package

View File

@ -12,25 +12,18 @@ build = "build.rs"
edition = '2018' edition = '2018'
[features] [features]
default = ["full-application"] default = ["application"]
# Feature required for bat the application. Should be disabled when depending on # Feature required for bat the application. Should be disabled when depending on
# bat as a library. # bat as a library.
full-application = [ application = [
"application",
"atty",
"bugreport", "bugreport",
"clap", "build-assets",
"dirs-next",
"git", "git",
"lazy_static", "minimal-application",
"paging",
"regex-onig",
"wild",
] ]
# Mainly for developers that want to iterate quickly # Mainly for developers that want to iterate quickly
# Be aware that the included features might change in the future # Be aware that the included features might change in the future
quick-build-application = [ minimal-application = [
"application",
"atty", "atty",
"clap", "clap",
"dirs-next", "dirs-next",
@ -39,9 +32,10 @@ quick-build-application = [
"regex-onig", "regex-onig",
"wild", "wild",
] ]
application = []
git = ["git2"] # Support indicating git modifications git = ["git2"] # Support indicating git modifications
paging = ["shell-words"] # Support applying a pager on the output paging = ["shell-words"] # Support applying a pager on the output
# Add "syntect/plist-load" when https://github.com/trishume/syntect/pull/345 reaches us
build-assets = ["syntect/yaml-load", "syntect/dump-create"]
# You need to use one of these if you depend on bat as a library: # You need to use one of these if you depend on bat as a library:
regex-onig = ["syntect/regex-onig"] # Use the "oniguruma" regex engine regex-onig = ["syntect/regex-onig"] # Use the "oniguruma" regex engine
@ -77,7 +71,7 @@ default-features = false
[dependencies.syntect] [dependencies.syntect]
version = "4.6.0" version = "4.6.0"
default-features = false default-features = false
features = ["parsing", "yaml-load", "dump-load", "dump-create"] features = ["parsing", "dump-load"]
[dependencies.clap] [dependencies.clap]
version = "2.33" version = "2.33"

View File

@ -4,13 +4,12 @@ use std::path::{Path, PathBuf};
use lazycell::LazyCell; 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::highlighting::{Theme, ThemeSet};
use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder}; use syntect::parsing::{SyntaxReference, SyntaxSet};
use path_abs::PathAbs; use path_abs::PathAbs;
use crate::assets_metadata::AssetsMetadata;
use crate::bat_warning; use crate::bat_warning;
use crate::error::*; use crate::error::*;
use crate::input::{InputReader, OpenedInput, OpenedInputKind}; use crate::input::{InputReader, OpenedInput, OpenedInputKind};
@ -72,6 +71,7 @@ impl HighlightingAssets {
"Monokai Extended" "Monokai Extended"
} }
#[cfg(feature = "build-assets")]
pub fn from_files(source_dir: &Path, include_integrated_assets: bool) -> Result<Self> { pub fn from_files(source_dir: &Path, include_integrated_assets: bool) -> Result<Self> {
let mut theme_set = if include_integrated_assets { let mut theme_set = if include_integrated_assets {
get_integrated_themeset() get_integrated_themeset()
@ -97,11 +97,11 @@ impl HighlightingAssets {
} }
let mut syntax_set_builder = if !include_integrated_assets { 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.add_plain_text_syntax();
builder builder
} else { } else {
get_integrated_syntaxset().into_builder() from_binary::<SyntaxSet>(get_serialized_integrated_syntaxset()).into_builder()
}; };
let syntax_dir = source_dir.join("syntaxes"); 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<()> { pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
let _ = fs::create_dir_all(target_dir); let _ = fs::create_dir_all(target_dir);
asset_to_cache( asset_to_cache(
@ -169,7 +170,7 @@ impl HighlightingAssets {
"Writing metadata to folder {} ... ", "Writing metadata to folder {} ... ",
target_dir.to_string_lossy() 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"); println!("okay");
Ok(()) Ok(())
@ -416,17 +417,14 @@ fn get_serialized_integrated_syntaxset() -> &'static [u8] {
include_bytes!("../assets/syntaxes.bin") include_bytes!("../assets/syntaxes.bin")
} }
fn get_integrated_syntaxset() -> SyntaxSet {
from_binary(get_serialized_integrated_syntaxset())
}
fn get_integrated_themeset() -> ThemeSet { fn get_integrated_themeset() -> ThemeSet {
from_binary(include_bytes!("../assets/themes.bin")) 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<()> { fn asset_to_cache<T: serde::Serialize>(asset: &T, path: &Path, description: &str) -> Result<()> {
print!("Writing {} to {} ... ", description, path.to_string_lossy()); 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!( format!(
"Could not save {} to {}", "Could not save {} to {}",
description, description,

View File

@ -16,6 +16,7 @@ pub struct AssetsMetadata {
const FILENAME: &str = "metadata.yaml"; const FILENAME: &str = "metadata.yaml";
impl AssetsMetadata { impl AssetsMetadata {
#[cfg(feature = "build-assets")]
pub(crate) fn new(current_version: &str) -> AssetsMetadata { pub(crate) fn new(current_version: &str) -> AssetsMetadata {
AssetsMetadata { AssetsMetadata {
bat_version: Some(current_version.to_owned()), 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<()> { pub(crate) fn save_to_folder(&self, path: &Path) -> Result<()> {
let file = File::create(path.join(FILENAME))?; let file = File::create(path.join(FILENAME))?;
serde_yaml::to_writer(file, self)?; serde_yaml::to_writer(file, self)?;

View File

@ -23,12 +23,10 @@ use crate::{
}; };
use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir}; use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
use clap::crate_version;
use directories::PROJECT_DIRS; use directories::PROJECT_DIRS;
use globset::GlobMatcher; use globset::GlobMatcher;
use bat::{ use bat::{
assets::HighlightingAssets,
config::Config, config::Config,
controller::Controller, controller::Controller,
error::*, error::*,
@ -39,21 +37,29 @@ use bat::{
const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.rs"); 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<()> { fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
if matches.is_present("build") { if matches.is_present("build") {
let source_dir = matches #[cfg(feature = "build-assets")]
.value_of("source") build_assets(matches)?;
.map(Path::new) #[cfg(not(feature = "build-assets"))]
.unwrap_or_else(|| PROJECT_DIRS.config_dir()); println!("bat has been built without the 'build-assets' feature. The 'cache --build' option is not available.");
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!())?;
} else if matches.is_present("clear") { } else if matches.is_present("clear") {
clear_assets(); clear_assets();
} }

View File

@ -88,7 +88,7 @@ pub struct Config<'a> {
pub use_custom_assets: bool, 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> { pub fn get_pager_executable(config_pager: Option<&str>) -> Option<String> {
if let Ok(Some(pager)) = crate::pager::get_pager(config_pager) { if let Ok(Some(pager)) = crate::pager::get_pager(config_pager) {
Some(pager.bin) Some(pager.bin)

View File

@ -3,7 +3,7 @@ use std::io::Write;
error_chain! { error_chain! {
foreign_links { foreign_links {
Clap(::clap::Error) #[cfg(feature = "application")]; Clap(::clap::Error) #[cfg(feature = "minimal-application")];
Io(::std::io::Error); Io(::std::io::Error);
SyntectError(::syntect::LoadingError); SyntectError(::syntect::LoadingError);
ParseIntError(::std::num::ParseIntError); ParseIntError(::std::num::ParseIntError);

View File

@ -40,6 +40,7 @@ mod preprocessor;
mod pretty_printer; mod pretty_printer;
pub(crate) mod printer; pub(crate) mod printer;
pub mod style; pub mod style;
#[cfg(feature = "build-assets")]
mod syntax_dependencies; mod syntax_dependencies;
pub(crate) mod syntax_mapping; pub(crate) mod syntax_mapping;
mod terminal; mod terminal;