-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmod.rs
More file actions
217 lines (188 loc) · 7.62 KB
/
mod.rs
File metadata and controls
217 lines (188 loc) · 7.62 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::sync::Arc;
use clap::Parser;
use vite_path::AbsolutePath;
use vite_str::Str;
use vite_task_graph::{TaskSpecifier, query::TaskQuery};
use vite_task_plan::plan_request::{CacheOverride, PlanOptions, QueryPlanRequest};
use vite_workspace::package_filter::{PackageQueryArgs, PackageQueryError};
/// Controls how task output is displayed.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
pub enum LogMode {
/// Output streams directly to the terminal as tasks produce it.
#[default]
Interleaved,
/// Each line is prefixed with `[packageName#taskName]`.
Labeled,
/// Output is buffered per task and printed as a block after each task completes.
Grouped,
}
#[derive(Debug, Clone, clap::Subcommand)]
pub enum CacheSubcommand {
/// Clean up all the cache
Clean,
}
/// Flags that control how a `run` command selects tasks.
#[derive(Debug, Clone, PartialEq, Eq, clap::Args)]
#[expect(clippy::struct_excessive_bools, reason = "CLI flags are naturally boolean")]
pub struct RunFlags {
#[clap(flatten)]
pub package_query: PackageQueryArgs,
/// Do not run dependencies specified in `dependsOn` fields.
#[clap(default_value = "false", long)]
pub ignore_depends_on: bool,
/// Show full detailed summary after execution.
#[clap(default_value = "false", short = 'v', long)]
pub verbose: bool,
/// Force caching on for all tasks and scripts.
#[clap(long, conflicts_with = "no_cache")]
pub cache: bool,
/// Force caching off for all tasks and scripts.
#[clap(long, conflicts_with = "cache")]
pub no_cache: bool,
/// How task output is displayed.
#[clap(long, default_value = "interleaved")]
pub log: LogMode,
}
impl RunFlags {
#[must_use]
pub const fn cache_override(&self) -> CacheOverride {
if self.cache {
CacheOverride::ForceEnabled
} else if self.no_cache {
CacheOverride::ForceDisabled
} else {
CacheOverride::None
}
}
}
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Public CLI types (clap-parsed)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// Arguments for the `run` subcommand as parsed by clap.
///
/// Contains the `--last-details` flag which is resolved into a separate
/// `ResolvedCommand::RunLastDetails` variant internally.
#[derive(Debug, clap::Parser)]
pub struct RunCommand {
/// `packageName#taskName` or `taskName`. If omitted, lists all available tasks.
pub(crate) task_specifier: Option<Str>,
#[clap(flatten)]
pub(crate) flags: RunFlags,
/// Additional arguments to pass to the tasks
#[clap(trailing_var_arg = true, allow_hyphen_values = true)]
pub(crate) additional_args: Vec<Str>,
/// Display the detailed summary of the last run.
#[clap(long, exclusive = true)]
pub(crate) last_details: bool,
}
/// vite task CLI subcommands as parsed by clap.
///
/// vite task CLI subcommands as parsed by clap.
///
/// Pass directly to `Session::main` or `HandledCommand::ViteTaskCommand`.
/// The `--last-details` flag on the `run` subcommand is resolved internally.
#[derive(Debug, Parser)]
pub enum Command {
/// Run tasks
Run(RunCommand),
/// Manage the task cache
Cache {
#[clap(subcommand)]
subcmd: CacheSubcommand,
},
}
impl Command {
/// Resolve the clap-parsed command into the dispatched [`ResolvedCommand`] enum.
///
/// When `--last-details` is set on the `run` subcommand, this produces
/// [`ResolvedCommand::RunLastDetails`] instead of [`ResolvedCommand::Run`],
/// making the exclusivity enforced at the type level.
#[must_use]
pub(crate) fn into_resolved(self) -> ResolvedCommand {
match self {
Self::Run(run) if run.last_details => ResolvedCommand::RunLastDetails,
Self::Run(run) => ResolvedCommand::Run(run.into_resolved()),
Self::Cache { subcmd } => ResolvedCommand::Cache { subcmd },
}
}
}
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Internal resolved types (used for dispatch — `--last-details` is a separate variant)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// Resolved CLI command for internal dispatch.
///
/// Unlike [`Command`], this enum makes `--last-details` a separate variant
/// ([`ResolvedCommand::RunLastDetails`]) so that it is exclusive at the type level —
/// there is no way to combine it with task execution fields.
#[derive(Debug)]
pub enum ResolvedCommand {
/// Run tasks with the given parameters.
Run(ResolvedRunCommand),
/// Display the saved detailed summary of the last run (`--last-details`).
RunLastDetails,
/// Manage the task cache.
Cache { subcmd: CacheSubcommand },
}
/// Resolved arguments for executing tasks.
///
/// Does not contain `last_details` — that case is represented by
/// [`ResolvedCommand::RunLastDetails`] instead.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedRunCommand {
/// `packageName#taskName` or `taskName`. If omitted, lists all available tasks.
pub task_specifier: Option<Str>,
pub flags: RunFlags,
/// Additional arguments to pass to the tasks.
pub additional_args: Vec<Str>,
}
impl RunCommand {
/// Convert to the resolved run command, stripping the `last_details` flag.
#[must_use]
pub(crate) fn into_resolved(self) -> ResolvedRunCommand {
ResolvedRunCommand {
task_specifier: self.task_specifier,
flags: self.flags,
additional_args: self.additional_args,
}
}
}
#[derive(thiserror::Error, Debug)]
pub enum CLITaskQueryError {
#[error("no task specifier provided")]
MissingTaskSpecifier,
#[error(transparent)]
PackageQuery(#[from] PackageQueryError),
}
impl ResolvedRunCommand {
/// Convert to `QueryPlanRequest`, or return an error if invalid.
///
/// # Errors
///
/// Returns an error if conflicting flags are set or if a `--filter` expression
/// cannot be parsed.
pub fn into_query_plan_request(
self,
cwd: &Arc<AbsolutePath>,
) -> Result<(QueryPlanRequest, bool), CLITaskQueryError> {
let raw_specifier = self.task_specifier.ok_or(CLITaskQueryError::MissingTaskSpecifier)?;
let task_specifier = TaskSpecifier::parse_raw(&raw_specifier);
let cache_override = self.flags.cache_override();
let include_explicit_deps = !self.flags.ignore_depends_on;
let (package_query, is_cwd_only) =
self.flags.package_query.into_package_query(task_specifier.package_name, cwd)?;
Ok((
QueryPlanRequest {
query: TaskQuery {
package_query,
task_name: task_specifier.task_name,
include_explicit_deps,
},
plan_options: PlanOptions {
extra_args: self.additional_args.into(),
cache_override,
},
},
is_cwd_only,
))
}
}