Skip to main content

cli_xtask/command/
main.rs

1use crate::{
2    Result, Run, Xtask,
3    config::{Config, ConfigBuilder, DistConfigBuilder, DistTargetConfigBuilder},
4    workspace,
5};
6
7impl<Subcommand> Xtask<Subcommand>
8where
9    Subcommand: clap::Subcommand + Run,
10{
11    /// Entry point for xtask crate.
12    ///
13    /// This function initializes error handler and logger, then runs the
14    /// subcommand. Default configuration will be passed to subcommand.
15    ///
16    /// # Examples
17    ///
18    /// ```rust
19    /// use cli_xtask::{Result, Xtask};
20    ///
21    /// fn main() -> Result<()> {
22    ///     <Xtask>::main()
23    /// }
24    /// ```
25    pub fn main() -> Result<()> {
26        Self::main_with_config(|| {
27            let workspace = workspace::current();
28            let (dist, package) = DistConfigBuilder::from_root_package(workspace)?;
29            let targets = package
30                .all_binaries()
31                .into_iter()
32                .map(DistTargetConfigBuilder::build)
33                .collect::<Result<Vec<_>>>()?;
34            let package = package.targets(targets).build()?;
35            let dist = dist.package(package).build()?;
36            let config = ConfigBuilder::new().dist(dist).build()?;
37            Ok(config)
38        })
39    }
40
41    /// Entry point for xtask crate.
42    ///
43    /// This function initializes error handler and logger, then runs the
44    /// subcommand. Generated configuration by `config` argument will be
45    /// passed to subcommand.
46    ///
47    /// # Examples
48    ///
49    /// ```rust
50    /// use cli_xtask::{config::Config, Result, Xtask};
51    ///
52    /// fn main() -> Result<()> {
53    ///     <Xtask>::main_with_config(|| Ok(Config::new()))
54    /// }
55    /// ```
56    pub fn main_with_config<'a>(config: impl FnOnce() -> Result<Config<'a>>) -> Result<()> {
57        let args = <Self as clap::Parser>::parse();
58
59        crate::error_handler::install()?;
60        crate::logger::install(args.verbosity.get())?;
61
62        args.run(&config()?)?;
63
64        Ok(())
65    }
66}