use std::process::ExitCode;
use clap::Parser;
use tracing::level_filters::LevelFilter;
use crate::commands::CliArguments;
use crate::commands::MagoCommand;
use crate::config::Configuration;
use crate::consts::MAXIMUM_PHP_VERSION;
use crate::consts::MINIMUM_PHP_VERSION;
use crate::error::Error;
use crate::utils::configure_colors;
use crate::utils::logger::initialize_logger;
mod baseline;
mod commands;
mod config;
mod consts;
mod error;
mod macros;
mod service;
mod updater;
mod utils;
#[cfg(all(
not(feature = "dhat-heap"),
any(target_os = "macos", target_os = "windows", target_env = "musl", target_env = "gnu")
))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
const EXIT_CODE_ERROR: u8 = 2;
pub fn main() -> ExitCode {
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();
run().unwrap_or_else(|error| {
tracing::error!("{}", error);
tracing::trace!("Exiting with error code due to: {:#?}", error);
ExitCode::from(EXIT_CODE_ERROR)
})
}
#[inline(always)]
pub fn run() -> Result<ExitCode, Error> {
let arguments = CliArguments::parse();
configure_colors(arguments.colors);
initialize_logger(
if cfg!(debug_assertions) { LevelFilter::DEBUG } else { LevelFilter::INFO },
"MAGO_LOG",
arguments.colors,
);
if let MagoCommand::SelfUpdate(cmd) = arguments.command {
return commands::self_update::execute(cmd);
}
let php_version = arguments.get_php_version()?;
let CliArguments { workspace, config, threads, allow_unsupported_php_version, command, .. } = arguments;
let configuration =
Configuration::load(workspace, config.as_deref(), php_version, threads, allow_unsupported_php_version)?;
if !configuration.allow_unsupported_php_version {
if configuration.php_version < MINIMUM_PHP_VERSION {
return Err(Error::PHPVersionIsTooOld(MINIMUM_PHP_VERSION, configuration.php_version));
}
if configuration.php_version > MAXIMUM_PHP_VERSION {
return Err(Error::PHPVersionIsTooNew(MAXIMUM_PHP_VERSION, configuration.php_version));
}
}
rayon::ThreadPoolBuilder::new()
.num_threads(configuration.threads)
.stack_size(configuration.stack_size)
.build_global()?;
match command {
MagoCommand::Init(cmd) => cmd.execute(configuration, None),
MagoCommand::Config(cmd) => cmd.execute(configuration),
MagoCommand::ListFiles(cmd) => cmd.execute(configuration, arguments.colors),
MagoCommand::Lint(cmd) => cmd.execute(configuration, arguments.colors),
MagoCommand::Format(cmd) => cmd.execute(configuration, arguments.colors),
MagoCommand::Ast(cmd) => cmd.execute(configuration, arguments.colors),
MagoCommand::Analyze(cmd) => cmd.execute(configuration, arguments.colors),
MagoCommand::Guard(cmd) => cmd.execute(configuration, arguments.colors),
MagoCommand::GenerateCompletions(cmd) => cmd.execute(),
MagoCommand::SelfUpdate(_) => {
unreachable!("The self-update command should have been handled before this point.")
}
}
}