1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::{
    config::{Config, ConfigBuilder, DistConfigBuilder, DistTargetConfigBuilder},
    workspace, Result, Run, Xtask,
};

impl<Subcommand> Xtask<Subcommand>
where
    Subcommand: clap::Subcommand + Run,
{
    /// Entry point for xtask crate.
    ///
    /// This function initializes error handler and logger, then runs the
    /// subcommand. Default configuration will be passed to subcommand.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use cli_xtask::{Result, Xtask};
    ///
    /// fn main() -> Result<()> {
    ///     <Xtask>::main()
    /// }
    /// ```
    pub fn main() -> Result<()> {
        Self::main_with_config(|| {
            let workspace = workspace::current();
            let (dist, package) = DistConfigBuilder::from_root_package(workspace)?;
            let targets = package
                .all_binaries()
                .into_iter()
                .map(DistTargetConfigBuilder::build)
                .collect::<Result<Vec<_>>>()?;
            let package = package.targets(targets).build()?;
            let dist = dist.package(package).build()?;
            let config = ConfigBuilder::new().dist(dist).build()?;
            Ok(config)
        })
    }

    /// Entry point for xtask crate.
    ///
    /// This function initializes error handler and logger, then runs the
    /// subcommand. Generated configuration by `config` argument will be
    /// passed to subcommand.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use cli_xtask::{config::Config, Result, Xtask};
    ///
    /// fn main() -> Result<()> {
    ///     <Xtask>::main_with_config(|| Ok(Config::new()))
    /// }
    /// ```
    pub fn main_with_config<'a>(config: impl FnOnce() -> Result<Config<'a>>) -> Result<()> {
        let args = <Self as clap::Parser>::parse();

        crate::error_handler::install()?;
        crate::logger::install(args.verbosity.get())?;

        args.run(&config()?)?;

        Ok(())
    }
}