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
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
with:
use-cross: ${{ matrix.job.use-cross }}
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
id: package

View File

@ -12,25 +12,18 @@ build = "build.rs"
edition = '2018'
[features]
default = ["full-application"]
default = ["application"]
# Feature required for bat the application. Should be disabled when depending on
# bat as a library.
full-application = [
"application",
"atty",
application = [
"bugreport",
"clap",
"dirs-next",
"build-assets",
"git",
"lazy_static",
"paging",
"regex-onig",
"wild",
"minimal-application",
]
# Mainly for developers that want to iterate quickly
# Be aware that the included features might change in the future
quick-build-application = [
"application",
minimal-application = [
"atty",
"clap",
"dirs-next",
@ -39,9 +32,10 @@ quick-build-application = [
"regex-onig",
"wild",
]
application = []
git = ["git2"] # Support indicating git modifications
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:
regex-onig = ["syntect/regex-onig"] # Use the "oniguruma" regex engine
@ -77,7 +71,7 @@ default-features = false
[dependencies.syntect]
version = "4.6.0"
default-features = false
features = ["parsing", "yaml-load", "dump-load", "dump-create"]
features = ["parsing", "dump-load"]
[dependencies.clap]
version = "2.33"

View File

@ -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,

View File

@ -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)?;

View File

@ -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,8 +37,8 @@ use bat::{
const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.rs");
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
if matches.is_present("build") {
#[cfg(feature = "build-assets")]
fn build_assets(matches: &clap::ArgMatches) -> Result<()> {
let source_dir = matches
.value_of("source")
.map(Path::new)
@ -52,8 +50,16 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
let blank = matches.is_present("blank");
let assets = HighlightingAssets::from_files(source_dir, !blank)?;
assets.save_to_cache(target_dir, crate_version!())?;
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") {
#[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();
}

View File

@ -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)

View File

@ -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);

View File

@ -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;