1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-01-31 10:11:07 +00:00

Strip dependencies of bat-as-a-library

This commit is contained in:
David Tolnay 2020-03-30 09:43:13 -07:00 committed by David Peter
parent e7e1967bb0
commit 570805bc98
13 changed files with 100 additions and 77 deletions

View File

@ -15,14 +15,27 @@ exclude = [
build = "build.rs" build = "build.rs"
edition = '2018' edition = '2018'
[features]
default = ["application"]
# Feature required for bat the application. Should be disabled when depending on
# bat as a library.
application = [
"atty",
"clap",
"dirs",
"lazy_static",
"liquid",
"wild",
]
[dependencies] [dependencies]
atty = "0.2.14" atty = { version = "0.2.14", optional = true }
ansi_term = "^0.12.1" ansi_term = "^0.12.1"
ansi_colours = "^1.0" ansi_colours = "^1.0"
console = "0.10" console = "0.10"
dirs = "2.0" dirs = { version = "2.0", optional = true }
lazy_static = "1.4" lazy_static = { version = "1.4", optional = true }
wild = "2.0" wild = { version = "2.0", optional = true }
content_inspector = "0.2.4" content_inspector = "0.2.4"
encoding = "0.2" encoding = "0.2"
shell-words = "0.1.0" shell-words = "0.1.0"
@ -32,7 +45,6 @@ globset = "0.4"
[dependencies.git2] [dependencies.git2]
version = "0.13" version = "0.13"
default-features = false default-features = false
features = []
[dependencies.syntect] [dependencies.syntect]
version = "3.2.0" version = "3.2.0"
@ -41,22 +53,22 @@ features = ["parsing", "yaml-load", "dump-load", "dump-create"]
[dependencies.clap] [dependencies.clap]
version = "2.33" version = "2.33"
optional = true
default-features = false default-features = false
features = ["suggestions", "color", "wrap_help"] features = ["suggestions", "color", "wrap_help"]
[dependencies.error-chain] [dependencies.error-chain]
version = "0.12" version = "0.12"
default-features = false default-features = false
features = []
[dev-dependencies] [dev-dependencies]
tempdir = "0.3" tempdir = "0.3"
assert_cmd = "0.12.0" assert_cmd = "0.12.0"
[build-dependencies] [build-dependencies]
clap = "2.33" clap = { version = "2.33", optional = true }
liquid = "0.20" liquid = { version = "0.20", optional = true }
lazy_static = "1.4" lazy_static = { version = "1.4", optional = true }
[profile.release] [profile.release]
lto = true lto = true

View File

@ -1,38 +1,42 @@
// TODO: Re-enable generation of shell completion files (below) when clap 3 is out. // TODO: Re-enable generation of shell completion files (below) when clap 3 is out.
// For more details, see https://github.com/sharkdp/bat/issues/372 // For more details, see https://github.com/sharkdp/bat/issues/372
#[macro_use] // For bat-as-a-library, no build script is required. The build script is for
extern crate lazy_static; // the manpage and completions, which are only relevant to the bat application.
extern crate liquid; #[cfg(not(feature = "application"))]
fn main() {}
use std::error::Error; #[cfg(feature = "application")]
use std::fs; fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::path::Path; use std::error::Error;
use std::fs;
use std::path::Path;
// Read environment variables. use lazy_static::lazy_static;
lazy_static! {
pub static ref PROJECT_NAME: &'static str = option_env!("PROJECT_NAME").unwrap_or("bat");
pub static ref PROJECT_VERSION: &'static str = option_env!("CARGO_PKG_VERSION").unwrap();
pub static ref EXECUTABLE_NAME: &'static str = option_env!("PROJECT_EXECUTABLE")
.or(option_env!("PROJECT_NAME"))
.unwrap_or("bat");
}
/// Generates a file from a liquid template. // Read environment variables.
fn template( lazy_static! {
variables: &liquid::Object, static ref PROJECT_NAME: &'static str = option_env!("PROJECT_NAME").unwrap_or("bat");
in_file: &str, static ref PROJECT_VERSION: &'static str = option_env!("CARGO_PKG_VERSION").unwrap();
out_file: impl AsRef<Path>, static ref EXECUTABLE_NAME: &'static str = option_env!("PROJECT_EXECUTABLE")
) -> Result<(), Box<dyn Error>> { .or(option_env!("PROJECT_NAME"))
let template = liquid::ParserBuilder::with_stdlib() .unwrap_or("bat");
.build()? }
.parse(&fs::read_to_string(in_file)?)?;
fs::write(out_file, template.render(variables)?)?; /// Generates a file from a liquid template.
Ok(()) fn template(
} variables: &liquid::Object,
in_file: &str,
out_file: impl AsRef<Path>,
) -> Result<(), Box<dyn Error>> {
let template = liquid::ParserBuilder::with_stdlib()
.build()?
.parse(&fs::read_to_string(in_file)?)?;
fs::write(out_file, template.render(variables)?)?;
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let variables = liquid::object!({ let variables = liquid::object!({
"PROJECT_NAME": PROJECT_NAME.to_owned(), "PROJECT_NAME": PROJECT_NAME.to_owned(),
"PROJECT_EXECUTABLE": EXECUTABLE_NAME.to_owned(), "PROJECT_EXECUTABLE": EXECUTABLE_NAME.to_owned(),

3
ci/script.bash vendored
View File

@ -12,3 +12,6 @@ if [[ $TARGET != arm-unknown-linux-gnueabihf ]] && [[ $TARGET != aarch64-unknown
# Run 'bat' on its own source code and the README # Run 'bat' on its own source code and the README
cargo run --target "$TARGET" -- src/bin/bat/main.rs README.md --paging=never cargo run --target "$TARGET" -- src/bin/bat/main.rs README.md --paging=never
fi fi
# Check bat-as-a-library, which has a smaller set of dependencies
cargo check --target "$TARGET" --verbose --lib --no-default-features

View File

@ -9,13 +9,9 @@ use crate::{
config::{get_args_from_config_file, get_args_from_env_var}, config::{get_args_from_config_file, get_args_from_env_var},
}; };
use clap::ArgMatches; use clap::ArgMatches;
use wild;
use console::Term; use console::Term;
#[cfg(windows)]
use ansi_term;
use bat::{ use bat::{
config::{ config::{
Config, HighlightedLineRanges, InputFile, LineRange, LineRanges, MappingTarget, OutputWrap, Config, HighlightedLineRanges, InputFile, LineRange, LineRanges, MappingTarget, OutputWrap,

View File

@ -1,4 +1,4 @@
use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, SubCommand}; use clap::{crate_name, crate_version, App as ClapApp, AppSettings, Arg, ArgGroup, SubCommand};
use std::path::Path; use std::path::Path;
pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {

View File

@ -4,8 +4,6 @@ use std::fs;
use std::io::{self, Write}; use std::io::{self, Write};
use std::path::PathBuf; use std::path::PathBuf;
use shell_words;
use crate::directories::PROJECT_DIRS; use crate::directories::PROJECT_DIRS;
pub fn config_file() -> PathBuf { pub fn config_file() -> PathBuf {

View File

@ -1,7 +1,6 @@
use std::env; use std::env;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use dirs;
use lazy_static::lazy_static; use lazy_static::lazy_static;
/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification. /// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.

View File

@ -1,11 +1,6 @@
// `error_chain!` can recurse deeply // `error_chain!` can recurse deeply
#![recursion_limit = "1024"] #![recursion_limit = "1024"]
#[macro_use]
extern crate clap;
extern crate dirs as dirs_rs;
mod app; mod app;
mod assets; mod assets;
mod clap_app; mod clap_app;

View File

@ -2,20 +2,20 @@ use error_chain::error_chain;
error_chain! { error_chain! {
foreign_links { foreign_links {
Clap(::clap::Error); Clap(clap::Error);
Io(::std::io::Error); Io(std::io::Error);
SyntectError(::syntect::LoadingError); SyntectError(syntect::LoadingError);
ParseIntError(::std::num::ParseIntError); ParseIntError(std::num::ParseIntError);
GlobParsingError(::globset::Error); GlobParsingError(globset::Error);
} }
} }
pub fn default_error_handler(error: &Error) { pub fn default_error_handler(error: &Error) {
match error { match error {
Error(ErrorKind::Io(ref io_error), _) Error(ErrorKind::Io(ref io_error), _)
if io_error.kind() == ::std::io::ErrorKind::BrokenPipe => if io_error.kind() == std::io::ErrorKind::BrokenPipe =>
{ {
::std::process::exit(0); std::process::exit(0);
} }
_ => { _ => {
use ansi_term::Colour::Red; use ansi_term::Colour::Red;
@ -23,3 +23,36 @@ pub fn default_error_handler(error: &Error) {
} }
}; };
} }
// Mock out a type for clap::Error if we aren't pulling in a dependency on clap.
//
// This can be removed after migrating away from error_chain to some modern
// derive-based error library such as thiserror, in favor of:
//
// #[derive(Error)]
// pub enum Error {
// #[cfg(feature = "application")]
// Clap(clap::Error),
// ...
// }
//
#[cfg(not(feature = "application"))]
mod clap {
use std::fmt::{self, Debug, Display};
pub struct Error(());
impl Display for Error {
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
unreachable!()
}
}
impl Debug for Error {
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
unreachable!()
}
}
impl std::error::Error for Error {}
}

View File

@ -1,17 +1,6 @@
// `error_chain!` can recurse deeply // `error_chain!` can recurse deeply
#![recursion_limit = "1024"] #![recursion_limit = "1024"]
extern crate ansi_term;
extern crate atty;
extern crate console;
extern crate content_inspector;
extern crate dirs as dirs_rs;
extern crate encoding;
extern crate git2;
extern crate shell_words;
extern crate syntect;
extern crate wild;
pub(crate) mod assets; pub(crate) mod assets;
pub mod config; pub mod config;
pub(crate) mod controller; pub(crate) mod controller;

View File

@ -4,8 +4,6 @@ use std::io::{self, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Child, Command, Stdio}; use std::process::{Child, Command, Stdio};
use shell_words;
use crate::config::PagingMode; use crate::config::PagingMode;
use crate::errors::*; use crate::errors::*;
use crate::less::retrieve_less_version; use crate::less::retrieve_less_version;

View File

@ -1,5 +1,3 @@
extern crate ansi_colours;
use ansi_term::Colour::{Fixed, RGB}; use ansi_term::Colour::{Fixed, RGB};
use ansi_term::{self, Style}; use ansi_term::{self, Style};

View File

@ -4,13 +4,11 @@ use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
extern crate tempdir; use tempdir::TempDir;
use self::tempdir::TempDir;
extern crate git2; use git2::build::CheckoutBuilder;
use self::git2::build::CheckoutBuilder; use git2::Repository;
use self::git2::Repository; use git2::Signature;
use self::git2::Signature;
pub struct BatTester { pub struct BatTester {
/// Temporary working directory /// Temporary working directory