-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtask_command.rs
More file actions
114 lines (105 loc) · 3.92 KB
/
task_command.rs
File metadata and controls
114 lines (105 loc) · 3.92 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
use std::fmt::Display;
use bincode::{Decode, Encode};
use diff::Diff;
use serde::{Deserialize, Serialize};
use vite_path::{AbsolutePath, RelativePathBuf};
use vite_str::Str;
use super::{CommandFingerprint, ResolvedTaskCommand, TaskConfig};
use crate::{Error, cmd::TaskParsedCommand, execute::TaskEnvs};
#[derive(Encode, Decode, Serialize, Deserialize, Debug, PartialEq, Eq, Diff, Clone)]
#[diff(attr(#[derive(Debug)]))]
#[serde(untagged)]
pub enum TaskCommand {
ShellScript(Str),
Parsed(TaskParsedCommand),
}
impl Display for TaskCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ShellScript(command) => Display::fmt(&command, f),
Self::Parsed(parsed_command) => Display::fmt(&parsed_command, f),
}
}
}
impl From<TaskCommand> for TaskConfig {
fn from(command: TaskCommand) -> Self {
Self {
command,
cwd: RelativePathBuf::empty(),
cacheable: true,
inputs: Default::default(),
envs: Default::default(),
pass_through_envs: Default::default(),
fingerprint_ignores: Default::default(),
}
}
}
impl TaskCommand {
pub fn need_skip_cache(&self) -> bool {
matches!(self, Self::Parsed(parsed_command) if parsed_command.program == "vite" || (parsed_command.program.ends_with("vite.js") && parsed_command.args.first() == Some(&("dev".into()))))
}
// Whether the command starts a inner runner.
pub fn has_inner_runner(&self) -> bool {
let Self::Parsed(parsed_command) = self else {
return false;
};
if parsed_command.program != "vite" {
return false;
}
let Some(subcommand) = parsed_command.args.first() else {
return false;
};
matches!(subcommand.as_str(), "run" | "lint" | "fmt" | "build" | "test" | "lib")
}
}
#[derive(Encode, Decode, Debug, Serialize, Deserialize, PartialEq, Eq, Diff, Clone)]
#[diff(attr(#[derive(Debug)]))]
pub struct ResolvedTaskConfig {
pub config_dir: RelativePathBuf,
pub config: TaskConfig,
}
impl ResolvedTaskConfig {
pub(crate) fn resolve_command(
&self,
base_dir: &AbsolutePath,
task_args: &[Str],
) -> Result<ResolvedTaskCommand, Error> {
let cwd = self.config_dir.join(&self.config.cwd);
let command = if task_args.is_empty() {
self.config.command.clone()
} else {
match &self.config.command {
TaskCommand::ShellScript(command_script) => {
let command_script =
std::iter::once(command_script.clone())
.chain(task_args.iter().map(|arg| {
shell_escape::escape(arg.as_str().into()).as_ref().into()
}))
.collect::<Vec<_>>()
.join(" ")
.into();
TaskCommand::ShellScript(command_script)
}
TaskCommand::Parsed(parsed_command) => {
let mut parsed_command = parsed_command.clone();
parsed_command.args.extend_from_slice(task_args);
TaskCommand::Parsed(parsed_command)
}
}
};
let task_envs = TaskEnvs::resolve(std::env::vars_os(), base_dir, self)?;
Ok(ResolvedTaskCommand {
fingerprint: CommandFingerprint {
cwd,
command,
envs_without_pass_through: task_envs
.envs_without_pass_through
.into_iter()
.collect(),
pass_through_envs: self.config.pass_through_envs.iter().cloned().collect(),
fingerprint_ignores: self.config.fingerprint_ignores.clone(),
},
all_envs: task_envs.all_envs,
})
}
}