1- use std:: { ffi :: OsStr , sync:: Arc } ;
1+ use std:: sync:: Arc ;
22
3- use clap:: { Parser , Subcommand } ;
3+ use clap:: Parser ;
44use vite_path:: AbsolutePath ;
55use vite_str:: Str ;
66use vite_task_graph:: { TaskSpecifier , query:: TaskQueryKind } ;
77use vite_task_plan:: plan_request:: { PlanOptions , PlanRequest , QueryPlanRequest } ;
88
9- /// Represents the CLI arguments handled by vite-task, including both built-in (like run) and custom subcommands (like lint).
10- #[ derive( Debug ) ]
11- pub struct TaskCLIArgs < CustomSubcommand : Subcommand > {
12- pub ( crate ) original : Arc < [ Str ] > ,
13- pub ( crate ) parsed : ParsedTaskCLIArgs < CustomSubcommand > ,
9+ #[ derive( Debug , Clone , clap:: Subcommand ) ]
10+ pub enum CacheSubcommand {
11+ /// Clean up all the cache
12+ Clean ,
1413}
1514
16- impl < CustomSubcommand : Subcommand > TaskCLIArgs < CustomSubcommand > {
17- /// Inspect the custom subcommand (like lint/install). Returns `None` if it's built-in subcommand
18- /// The caller should not use this method to actually handle the custom subcommand. Instead, it should
19- /// private TaskSynthesizer to Session so that vite-task can handle custom subcommands consistently from
20- /// both direct CLI invocations and invocations in task scripts.
21- ///
22- /// This method is provided only to make it possible for the caller to behave differently BEFORE and AFTER the session.
23- /// For example, vite+ needs this method to skip auto-install when the custom subcommand is already `install`.
24- pub fn custom_subcommand ( & self ) -> Option < & CustomSubcommand > {
25- match & self . parsed {
26- ParsedTaskCLIArgs :: BuiltIn ( _) => None ,
27- ParsedTaskCLIArgs :: Custom ( custom) => Some ( custom) ,
28- }
29- }
30- }
31-
32- /// Represents the overall CLI arguments, containing three kinds of subcommands:
33- /// 1. Built-in subcommands handled by vite-task (like run)
34- /// 2. Custom subcommands handled by vite-task with the help of TaskSyntheizer (like lint)
35- /// 3. Custom subcommands not handled by vite-task (like vite+ commands without cache)
36- pub enum CLIArgs < CustomSubcommand : Subcommand , NonTaskSubcommand : Subcommand > {
37- /// Subcommands handled by vite task, including built-in (like run) and custom (like lint)
38- Task ( TaskCLIArgs < CustomSubcommand > ) ,
39- /// Custom subcommands not handled by vite task (like vite+ commands without cache)
40- NonTask ( NonTaskSubcommand ) ,
41- }
15+ /// Arguments for the `run` subcommand.
16+ #[ derive( Debug , clap:: Args ) ]
17+ pub struct RunCommand {
18+ /// `packageName#taskName` or `taskName`.
19+ pub task_specifier : TaskSpecifier ,
4220
43- impl < CustomSubcommand : Subcommand , NonTaskSubcommand : Subcommand >
44- CLIArgs < CustomSubcommand , NonTaskSubcommand >
45- {
46- /// Get the original CLI arguments
47- pub fn try_parse_from (
48- args : impl Iterator < Item = impl AsRef < str > > ,
49- ) -> Result < Self , clap:: Error > {
50- #[ derive( Debug , clap:: Parser ) ]
51- enum ParsedCLIArgs < CustomSubcommand : Subcommand , NonTaskSubcommand : Subcommand > {
52- /// subcommands handled by vite task
53- #[ command( flatten) ]
54- Task ( ParsedTaskCLIArgs < CustomSubcommand > ) ,
21+ /// Run tasks found in all packages in the workspace, in topological order based on package dependencies.
22+ #[ clap( default_value = "false" , short, long) ]
23+ pub recursive : bool ,
5524
56- /// subcommands that are not handled by vite task
57- #[ command( flatten) ]
58- NonTask ( NonTaskSubcommand ) ,
59- }
25+ /// Run tasks found in the current package and all its transitive dependencies, in topological order based on package dependencies.
26+ #[ clap( default_value = "false" , short, long) ]
27+ pub transitive : bool ,
6028
61- let args = args. map ( |arg| Str :: from ( arg. as_ref ( ) ) ) . collect :: < Arc < [ Str ] > > ( ) ;
62- let parsed_cli_args = ParsedCLIArgs :: < CustomSubcommand , NonTaskSubcommand > :: try_parse_from (
63- args. iter ( ) . map ( |s| OsStr :: new ( s. as_str ( ) ) ) ,
64- ) ?;
65-
66- Ok ( match parsed_cli_args {
67- ParsedCLIArgs :: Task ( parsed_task_cli_args) => {
68- Self :: Task ( TaskCLIArgs { original : args, parsed : parsed_task_cli_args } )
69- }
70- ParsedCLIArgs :: NonTask ( non_task_subcommand) => Self :: NonTask ( non_task_subcommand) ,
71- } )
72- }
73- }
29+ /// Do not run dependencies specified in `dependsOn` fields.
30+ #[ clap( default_value = "false" , long) ]
31+ pub ignore_depends_on : bool ,
7432
75- #[ derive( Debug , Parser ) ]
76- pub ( crate ) enum ParsedTaskCLIArgs < CustomSubcommand : Subcommand > {
77- /// subcommands provided by vite task, like `run`
78- #[ clap( flatten) ]
79- BuiltIn ( BuiltInCommand ) ,
80- /// custom subcommands provided by vite+, like `lint`
81- #[ clap( flatten) ]
82- Custom ( CustomSubcommand ) ,
33+ /// Additional arguments to pass to the tasks
34+ #[ clap( trailing_var_arg = true , allow_hyphen_values = true ) ]
35+ pub additional_args : Vec < Str > ,
8336}
8437
8538/// vite task CLI subcommands
86- #[ derive( Debug , Subcommand ) ]
87- pub ( crate ) enum BuiltInCommand {
39+ #[ derive( Debug , Parser ) ]
40+ pub enum Command {
8841 /// Run tasks
89- Run {
90- /// `packageName#taskName` or `taskName`.
91- task_specifier : TaskSpecifier ,
92-
93- /// Run tasks found in all packages in the workspace, in topological order based on package dependencies.
94- #[ clap( default_value = "false" , short, long) ]
95- recursive : bool ,
96-
97- /// Run tasks found in the current package and all its transitive dependencies, in topological order based on package dependencies.
98- #[ clap( default_value = "false" , short, long) ]
99- transitive : bool ,
100-
101- /// Do not run dependencies specified in `dependsOn` fields.
102- #[ clap( default_value = "false" , long) ]
103- ignore_depends_on : bool ,
104-
105- /// Additional arguments to pass to the tasks
106- #[ clap( trailing_var_arg = true , allow_hyphen_values = true ) ]
107- additional_args : Vec < Str > ,
42+ Run ( RunCommand ) ,
43+ /// Manage the task cache
44+ Cache {
45+ #[ clap( subcommand) ]
46+ subcmd : CacheSubcommand ,
10847 } ,
10948}
11049
@@ -117,50 +56,45 @@ pub enum CLITaskQueryError {
11756 PackageNameSpecifiedWithRecursive { package_name : Str , task_name : Str } ,
11857}
11958
120- impl BuiltInCommand {
121- /// Convert to `TaskQuery `, or return an error if invalid.
59+ impl RunCommand {
60+ /// Convert to `PlanRequest `, or return an error if invalid.
12261 pub fn into_plan_request (
12362 self ,
12463 cwd : & Arc < AbsolutePath > ,
12564 ) -> Result < PlanRequest , CLITaskQueryError > {
126- match self {
127- Self :: Run {
128- task_specifier,
129- recursive,
130- transitive,
131- ignore_depends_on,
132- additional_args,
133- } => {
134- let include_explicit_deps = !ignore_depends_on;
135-
136- let query_kind = if recursive {
137- if transitive {
138- return Err ( CLITaskQueryError :: RecursiveTransitiveConflict ) ;
139- }
140- let task_name = if let Some ( package_name) = task_specifier. package_name {
141- return Err ( CLITaskQueryError :: PackageNameSpecifiedWithRecursive {
142- package_name,
143- task_name : task_specifier. task_name ,
144- } ) ;
145- } else {
146- task_specifier. task_name
147- } ;
148- TaskQueryKind :: Recursive { task_names : [ task_name] . into ( ) }
149- } else {
150- TaskQueryKind :: Normal {
151- task_specifiers : [ task_specifier] . into ( ) ,
152- cwd : Arc :: clone ( cwd) ,
153- include_topological_deps : transitive,
154- }
155- } ;
156- Ok ( PlanRequest :: Query ( QueryPlanRequest {
157- query : vite_task_graph:: query:: TaskQuery {
158- kind : query_kind,
159- include_explicit_deps,
160- } ,
161- plan_options : PlanOptions { extra_args : additional_args. into ( ) } ,
162- } ) )
65+ let RunCommand {
66+ task_specifier,
67+ recursive,
68+ transitive,
69+ ignore_depends_on,
70+ additional_args,
71+ } = self ;
72+
73+ let include_explicit_deps = !ignore_depends_on;
74+
75+ let query_kind = if recursive {
76+ if transitive {
77+ return Err ( CLITaskQueryError :: RecursiveTransitiveConflict ) ;
78+ }
79+ let task_name = if let Some ( package_name) = task_specifier. package_name {
80+ return Err ( CLITaskQueryError :: PackageNameSpecifiedWithRecursive {
81+ package_name,
82+ task_name : task_specifier. task_name ,
83+ } ) ;
84+ } else {
85+ task_specifier. task_name
86+ } ;
87+ TaskQueryKind :: Recursive { task_names : [ task_name] . into ( ) }
88+ } else {
89+ TaskQueryKind :: Normal {
90+ task_specifiers : [ task_specifier] . into ( ) ,
91+ cwd : Arc :: clone ( cwd) ,
92+ include_topological_deps : transitive,
16393 }
164- }
94+ } ;
95+ Ok ( PlanRequest :: Query ( QueryPlanRequest {
96+ query : vite_task_graph:: query:: TaskQuery { kind : query_kind, include_explicit_deps } ,
97+ plan_options : PlanOptions { extra_args : additional_args. into ( ) } ,
98+ } ) )
16599 }
166100}
0 commit comments