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
use std::any::Any;

use crate::{config::Config, Result, Run};

/// Arguments definition of the `dist` subcommand.
#[cfg_attr(doc, doc = include_str!("../../doc/cargo-xtask-dist.md"))]
#[derive(Debug, Clone, Default, clap::Args)]
#[non_exhaustive]
pub struct Dist {
    /// Arguments for the `dist-build` subcommand.
    #[cfg(subcommand_dist_build)]
    #[cfg_attr(docsrs, doc(cfg(feature = "subcommand-dist-build-*")))]
    #[clap(flatten)]
    pub dist_build_args: super::DistBuild,
    /// Arguments for the `dist-archive` subcommand.
    #[clap(flatten)]
    pub dist_archive_args: super::DistArchive,
}

impl Run for Dist {
    fn run(&self, config: &Config) -> Result<()> {
        self.run(config)
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl Dist {
    /// Runs the `dist` subcommand.
    #[tracing::instrument(name = "dist", skip_all, err)]
    pub fn run(&self, config: &Config) -> Result<()> {
        let Self {
            #[cfg(subcommand_dist_build)]
            dist_build_args,
            dist_archive_args,
        } = self;
        let dist_config = config.dist()?;

        let working_dir = dist_config.dist_base_working_directory();
        crate::fs::create_or_cleanup_dir(working_dir)?;

        #[cfg(subcommand_dist_build)]
        dist_build_args.run(config)?;

        dist_archive_args.run(config)?;

        Ok(())
    }
}