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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::CargoResult;
use crate::CliResult;

pub fn run(
    config: &cargo::Config,
    manifest_path: &std::path::Path,
    args: &[std::ffi::OsString],
    release: bool,
) -> CargoResult<()> {
    let script = crate::util::script::RawScript::parse_from(manifest_path)?;
    let ws = script.to_workspace(config)?;

    let mut build_config = cargo::core::compiler::BuildConfig::new(
        config,
        None,
        false,
        &[],
        cargo::core::compiler::CompileMode::Build,
    )?;
    build_config.requested_profile =
        cargo::util::interning::InternedString::new(if release { "release" } else { "dev" });
    let compile_opts = cargo::ops::CompileOptions {
        build_config,
        cli_features: cargo::core::resolver::features::CliFeatures::from_command_line(
            &[],
            false,
            true,
        )?,
        spec: cargo::ops::Packages::Default,
        filter: cargo::ops::CompileFilter::Default {
            required_features_filterable: false,
        },
        target_rustdoc_args: None,
        target_rustc_args: None,
        target_rustc_crate_types: None,
        rustdoc_document_private_items: false,
        honor_rust_version: true,
    };

    cargo::ops::run(&ws, &compile_opts, args)
}

pub fn clean(config: &cargo::Config, manifest_path: &std::path::Path) -> CargoResult<()> {
    let opts = cargo::ops::CleanOptions {
        config,
        spec: vec![],
        targets: vec![],
        requested_profile: cargo::util::interning::InternedString::new("dev"),
        profile_specified: false,
        doc: false,
    };
    let script = crate::util::script::RawScript::parse_from(manifest_path)?;
    let ws = script.to_workspace(config)?;
    cargo::ops::clean(&ws, &opts)?;
    Ok(())
}

pub fn test(config: &cargo::Config, manifest_path: &std::path::Path) -> CliResult {
    let script = crate::util::script::RawScript::parse_from(manifest_path)?;
    let ws = script.to_workspace(config)?;

    let mut build_config = cargo::core::compiler::BuildConfig::new(
        config,
        None,
        false,
        &[],
        cargo::core::compiler::CompileMode::Test,
    )?;
    build_config.requested_profile = cargo::util::interning::InternedString::new("test");
    let compile_opts = cargo::ops::CompileOptions {
        build_config,
        cli_features: cargo::core::resolver::features::CliFeatures::from_command_line(
            &[],
            false,
            true,
        )?,
        spec: cargo::ops::Packages::Default,
        filter: cargo::ops::CompileFilter::from_raw_arguments(
            false,
            vec![],
            true,
            vec![],
            false,
            vec![],
            false,
            vec![],
            false,
            false,
        ),
        target_rustdoc_args: None,
        target_rustc_args: None,
        target_rustc_crate_types: None,
        rustdoc_document_private_items: false,
        honor_rust_version: true,
    };

    let ops = cargo::ops::TestOptions {
        no_run: false,
        no_fail_fast: false,
        compile_opts,
    };

    cargo::ops::run_tests(&ws, &ops, &[])
}

pub fn bench(config: &cargo::Config, manifest_path: &std::path::Path) -> CliResult {
    let script = crate::util::script::RawScript::parse_from(manifest_path)?;
    let ws = script.to_workspace(config)?;

    let mut build_config = cargo::core::compiler::BuildConfig::new(
        config,
        None,
        false,
        &[],
        cargo::core::compiler::CompileMode::Bench,
    )?;
    build_config.requested_profile = cargo::util::interning::InternedString::new("bench");
    let compile_opts = cargo::ops::CompileOptions {
        build_config,
        cli_features: cargo::core::resolver::features::CliFeatures::from_command_line(
            &[],
            false,
            true,
        )?,
        spec: cargo::ops::Packages::Default,
        filter: cargo::ops::CompileFilter::from_raw_arguments(
            false,
            vec![],
            true,
            vec![],
            false,
            vec![],
            false,
            vec![],
            false,
            false,
        ),
        target_rustdoc_args: None,
        target_rustc_args: None,
        target_rustc_crate_types: None,
        rustdoc_document_private_items: false,
        honor_rust_version: true,
    };

    let ops = cargo::ops::TestOptions {
        no_run: false,
        no_fail_fast: false,
        compile_opts,
    };

    cargo::ops::run_benches(&ws, &ops, &[])
}