Skip to content

Commit

Permalink
Make 'build-assets' an optional capability for application
Browse files Browse the repository at this point in the history
Also structure features a bit more clever to avoid duplication of
feature dependency declarations.
  • Loading branch information
Enselic committed Aug 17, 2021
1 parent deddc81 commit 25fa577
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 8 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
20 changes: 9 additions & 11 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
Expand All @@ -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");
Expand Down Expand Up @@ -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(
Expand All @@ -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(())
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/assets_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ 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()),
creation_time: Some(SystemTime::now()),
}
}

#[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)?;
Expand Down
36 changes: 21 additions & 15 deletions src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 25fa577

Please sign in to comment.