add tracing as global option

This commit is contained in:
Moritz Hölting 2024-06-16 21:59:02 +02:00
parent 9a14d547ce
commit 916901e81a
7 changed files with 93 additions and 21 deletions

View File

@ -29,11 +29,13 @@ colored = "2.1.0"
serde = { version = "1.0.197", features = ["derive"] }
thiserror = "1.0.58"
toml = "0.8.12"
shulkerscript = { git = "https://github.com/moritz-hoelting/shulkerscript-lang", features = ["shulkerbox"], default-features = false, rev = "5336ffb91ed5dfa1b2fce3993d84a2876aa732a3" }
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, rev = "b79c9ecd6d45f9319c9083a8103ef0186839b0c0" }
git2 = { version = "0.18.3", default-features = false }
shulkerscript = { git = "https://github.com/moritz-hoelting/shulkerscript-lang", features = ["shulkerbox"], default-features = false, rev = "dd79541ae914140df4141fe90f71e74fb961f3a3" }
shulkerbox = { git = "https://github.com/moritz-hoelting/shulkerbox", default-features = false, rev = "e31f9f904a5f5905e912907d988c189ffc8cf313" }
git2 = { version = "0.19.0", default-features = false }
path-absolutize = "3.1.1"
color-eyre = "0.6.3"
dotenvy = "0.15.7"
notify-debouncer-mini = { version = "0.4.1", default-features = false, optional = true }
ctrlc = { version = "3.4.4", optional = true }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

View File

@ -1,15 +1,27 @@
use crate::subcommands::{self, BuildArgs, CleanArgs, InitArgs};
use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, ValueEnum};
use color_eyre::eyre::Result;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
#[derive(Debug, Clone, Parser)]
#[command(version, about, long_about = None)]
pub struct Args {
#[command(subcommand)]
cmd: Command,
/// Enable verbose output.
#[clap(short, long)]
verbose: bool,
/// Enable tracing output
///
/// When specified without a value, defaults to `info`.
#[clap(
long,
global = true,
default_missing_value = "info",
require_equals = true,
num_args = 0..=1,
value_name = "LEVEL"
)]
trace: Option<TracingLevel>,
}
#[derive(Debug, Clone, Subcommand)]
@ -29,20 +41,34 @@ pub enum Command {
LangDebug(subcommands::LangDebugArgs),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, ValueEnum)]
pub enum TracingLevel {
Trace,
Debug,
#[default]
Info,
Warn,
Error,
}
impl Args {
pub fn run(&self) -> Result<()> {
self.cmd.run(self.verbose)
if let Some(level) = self.trace {
setup_tracing(level);
}
self.cmd.run()
}
}
impl Command {
pub fn run(&self, verbose: bool) -> Result<()> {
pub fn run(&self) -> Result<()> {
match self {
Command::Init(args) => subcommands::init(verbose, args)?,
Command::Build(args) => subcommands::build(verbose, args)?,
Command::Clean(args) => subcommands::clean(verbose, args)?,
Command::Init(args) => subcommands::init(args)?,
Command::Build(args) => subcommands::build(args)?,
Command::Clean(args) => subcommands::clean(args)?,
#[cfg(feature = "watch")]
Command::Watch(args) => subcommands::watch(verbose, args)?,
Command::Watch(args) => subcommands::watch(args)?,
#[cfg(feature = "lang-debug")]
Command::LangDebug(args) => subcommands::lang_debug(args)?,
}
@ -51,6 +77,30 @@ impl Command {
}
}
impl From<TracingLevel> for Level {
fn from(value: TracingLevel) -> Self {
match value {
TracingLevel::Trace => Level::TRACE,
TracingLevel::Debug => Level::DEBUG,
TracingLevel::Info => Level::INFO,
TracingLevel::Warn => Level::WARN,
TracingLevel::Error => Level::ERROR,
}
}
}
fn setup_tracing(level: TracingLevel) {
// a builder for `FmtSubscriber`.
let subscriber = FmtSubscriber::builder()
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
// will be written to stdout.
.with_max_level(Level::from(level))
// completes the builder.
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
}
#[cfg(test)]
mod tests {
use clap::CommandFactory;

View File

@ -18,14 +18,18 @@ pub struct BuildArgs {
/// The path of the project to build.
#[clap(default_value = ".")]
pub path: PathBuf,
/// Path of output directory
///
/// The path of the directory to place the compiled datapack.
#[clap(short, long, env = "DATAPACK_DIR")]
pub output: Option<PathBuf>,
/// Path of the assets folder
///
/// The path of a folder which files and subfolders will be copied to the root of the datapack.
/// Overrides the `assets` field in the pack.toml file.
#[clap(short, long)]
pub assets: Option<PathBuf>,
/// Whether to package the project to a zip file.
/// Package the project to a zip file.
#[clap(short, long)]
pub zip: bool,
}
@ -41,7 +45,7 @@ impl Default for BuildArgs {
}
}
pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
pub fn build(args: &BuildArgs) -> Result<()> {
if args.zip && !cfg!(feature = "zip") {
print_error("The zip feature is not enabled. Please install with the `zip` feature enabled to use the `--zip` option.");
return Err(Report::from(Error::FeatureNotEnabledError(

View File

@ -19,9 +19,13 @@ pub struct CleanArgs {
/// Force clean
#[clap(short, long)]
pub force: bool,
/// Enable verbose output.
#[clap(short, long)]
verbose: bool,
}
pub fn clean(verbose: bool, args: &CleanArgs) -> Result<()> {
pub fn clean(args: &CleanArgs) -> Result<()> {
let verbose = args.verbose;
let path = args.path.as_path();
let dist_path = args
.output

View File

@ -36,6 +36,9 @@ pub struct InitArgs {
/// The version control system to initialize.
#[clap(long, default_value = "git")]
pub vcs: VersionControlSystem,
/// Enable verbose output.
#[clap(short, long)]
verbose: bool,
}
#[derive(Debug, Clone, Copy, Default, ValueEnum)]
@ -45,7 +48,8 @@ pub enum VersionControlSystem {
None,
}
pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
pub fn init(args: &InitArgs) -> Result<()> {
let verbose = args.verbose;
let path = args.path.as_path();
let description = args.description.as_deref();
let pack_format = args.pack_format;

View File

@ -9,6 +9,8 @@ pub struct LangDebugArgs {
#[clap(default_value = ".")]
pub path: PathBuf,
/// The state to dump.
///
/// Output can be the raw tokens, the abstract syntax tree, or the transpiled datapack.
#[clap(short, long, value_name = "STATE", default_value = "ast")]
pub dump: DumpState,
/// Pretty-print the output.

View File

@ -20,17 +20,23 @@ pub struct WatchArgs {
/// The path of the project to watch.
#[clap(default_value = ".")]
pub path: PathBuf,
/// Do not run the command when starting, only after changes are detected.
/// Only run after changes are detected.
///
/// Skips the initial run of the commands.
#[clap(short, long)]
pub no_inital: bool,
/// The time to wait in ms before running the command after changes are detected.
#[clap(short, long, value_name = "TIME_IN_MS", default_value = "2000")]
pub debounce_time: u64,
/// Additional paths to watch for changes.
/// By default, the `src` directory, `pack.png`, and `pack.toml` as well as the defined assets directory in the config are watched.
///
/// By default, the `src` directory, `pack.png`, and `pack.toml` as well as the defined
/// assets directory in the config are watched.
#[clap(short, long, value_name = "PATH")]
pub watch: Vec<PathBuf>,
/// The commands to run in the project directory when changes are detected. Use multiple times to run multiple commands.
/// The commands to run in the project directory when changes are detected.
///
/// Use multiple times to run multiple commands.
#[clap(short = 'x', long, value_name = "COMMAND", default_value = "build .")]
pub execute: Vec<String>,
}
@ -41,7 +47,7 @@ enum WatchCommand {
External(String),
}
pub fn watch(_verbose: bool, args: &WatchArgs) -> Result<()> {
pub fn watch(args: &WatchArgs) -> Result<()> {
print_info(format!("Watching project at {}", args.path.display()));
let commands = args