mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-14 06:38:24 +00:00
src/build_assets.rs: Refactor into smaller functions (#1822)
To make the code easier to work with.
This commit is contained in:
parent
19c3e82abf
commit
27fa55d274
@ -33,6 +33,24 @@ pub fn build_assets(
|
|||||||
target_dir: &Path,
|
target_dir: &Path,
|
||||||
current_version: &str,
|
current_version: &str,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
let theme_set = build_theme_set(source_dir, include_integrated_assets);
|
||||||
|
|
||||||
|
let syntax_set_builder = build_syntax_set_builder(source_dir, include_integrated_assets)?;
|
||||||
|
|
||||||
|
if std::env::var("BAT_PRINT_SYNTAX_DEPENDENCIES").is_ok() {
|
||||||
|
// To trigger this code, run:
|
||||||
|
// BAT_PRINT_SYNTAX_DEPENDENCIES=1 cargo run -- cache --build --source assets --blank --target /tmp
|
||||||
|
print_syntax_dependencies(&syntax_set_builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
let syntax_set = syntax_set_builder.build();
|
||||||
|
|
||||||
|
print_unlinked_contexts(&syntax_set);
|
||||||
|
|
||||||
|
write_assets(&theme_set, &syntax_set, target_dir, current_version)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_theme_set(source_dir: &Path, include_integrated_assets: bool) -> ThemeSet {
|
||||||
let mut theme_set = if include_integrated_assets {
|
let mut theme_set = if include_integrated_assets {
|
||||||
crate::assets::get_integrated_themeset()
|
crate::assets::get_integrated_themeset()
|
||||||
} else {
|
} else {
|
||||||
@ -56,6 +74,13 @@ pub fn build_assets(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
theme_set
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_syntax_set_builder(
|
||||||
|
source_dir: &Path,
|
||||||
|
include_integrated_assets: bool,
|
||||||
|
) -> Result<SyntaxSetBuilder> {
|
||||||
let mut syntax_set_builder = if !include_integrated_assets {
|
let mut syntax_set_builder = if !include_integrated_assets {
|
||||||
let mut builder = syntect::parsing::SyntaxSetBuilder::new();
|
let mut builder = syntect::parsing::SyntaxSetBuilder::new();
|
||||||
builder.add_plain_text_syntax();
|
builder.add_plain_text_syntax();
|
||||||
@ -75,13 +100,10 @@ pub fn build_assets(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if std::env::var("BAT_PRINT_SYNTAX_DEPENDENCIES").is_ok() {
|
Ok(syntax_set_builder)
|
||||||
// To trigger this code, run:
|
}
|
||||||
// BAT_PRINT_SYNTAX_DEPENDENCIES=1 cargo run -- cache --build --source assets --blank --target /tmp
|
|
||||||
print_syntax_dependencies(&syntax_set_builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
let syntax_set = syntax_set_builder.build();
|
fn print_unlinked_contexts(syntax_set: &SyntaxSet) {
|
||||||
let missing_contexts = syntax_set.find_unlinked_contexts();
|
let missing_contexts = syntax_set.find_unlinked_contexts();
|
||||||
if !missing_contexts.is_empty() {
|
if !missing_contexts.is_empty() {
|
||||||
println!("Some referenced contexts could not be found!");
|
println!("Some referenced contexts could not be found!");
|
||||||
@ -89,10 +111,17 @@ pub fn build_assets(
|
|||||||
println!("- {}", context);
|
println!("- {}", context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_assets(
|
||||||
|
theme_set: &ThemeSet,
|
||||||
|
syntax_set: &SyntaxSet,
|
||||||
|
target_dir: &Path,
|
||||||
|
current_version: &str,
|
||||||
|
) -> Result<()> {
|
||||||
let _ = std::fs::create_dir_all(target_dir);
|
let _ = std::fs::create_dir_all(target_dir);
|
||||||
asset_to_cache(&theme_set, &target_dir.join("themes.bin"), "theme set")?;
|
asset_to_cache(theme_set, &target_dir.join("themes.bin"), "theme set")?;
|
||||||
asset_to_cache(&syntax_set, &target_dir.join("syntaxes.bin"), "syntax set")?;
|
asset_to_cache(syntax_set, &target_dir.join("syntaxes.bin"), "syntax set")?;
|
||||||
|
|
||||||
print!(
|
print!(
|
||||||
"Writing metadata to folder {} ... ",
|
"Writing metadata to folder {} ... ",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user