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