1//! Baseline interface of Tasks in Zed: all tasks in Zed are intended to use those for implementing their own logic.
2#![deny(missing_docs)]
3
4pub mod oneshot_source;
5pub mod static_source;
6mod vscode_format;
7
8use collections::HashMap;
9use gpui::ModelContext;
10use static_source::RevealStrategy;
11use std::any::Any;
12use std::path::{Path, PathBuf};
13use std::sync::Arc;
14pub use vscode_format::VsCodeTaskFile;
15
16/// Task identifier, unique within the application.
17/// Based on it, task reruns and terminal tabs are managed.
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub struct TaskId(pub String);
20
21/// Contains all information needed by Zed to spawn a new terminal tab for the given task.
22#[derive(Debug, Clone)]
23pub struct SpawnInTerminal {
24 /// Id of the task to use when determining task tab affinity.
25 pub id: TaskId,
26 /// Human readable name of the terminal tab.
27 pub label: String,
28 /// Executable command to spawn.
29 pub command: String,
30 /// Arguments to the command.
31 pub args: Vec<String>,
32 /// Current working directory to spawn the command into.
33 pub cwd: Option<PathBuf>,
34 /// Env overrides for the command, will be appended to the terminal's environment from the settings.
35 pub env: HashMap<String, String>,
36 /// Whether to use a new terminal tab or reuse the existing one to spawn the process.
37 pub use_new_terminal: bool,
38 /// Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish.
39 pub allow_concurrent_runs: bool,
40 /// What to do with the terminal pane and tab, after the command was started.
41 pub reveal: RevealStrategy,
42}
43
44type VariableName = String;
45type VariableValue = String;
46
47/// Container for predefined environment variables that describe state of Zed at the time the task was spawned.
48#[derive(Clone, Debug, Default, PartialEq, Eq)]
49pub struct TaskVariables(pub HashMap<VariableName, VariableValue>);
50
51impl FromIterator<(String, String)> for TaskVariables {
52 fn from_iter<T: IntoIterator<Item = (String, String)>>(iter: T) -> Self {
53 Self(HashMap::from_iter(iter))
54 }
55}
56
57/// Keeps track of the file associated with a task and context of tasks execution (i.e. current file or current function)
58#[derive(Clone, Debug, Default, PartialEq, Eq)]
59pub struct TaskContext {
60 /// A path to a directory in which the task should be executed.
61 pub cwd: Option<PathBuf>,
62 /// Additional environment variables associated with a given task.
63 pub task_variables: TaskVariables,
64}
65
66/// Represents a short lived recipe of a task, whose main purpose
67/// is to get spawned.
68pub trait Task {
69 /// Unique identifier of the task to spawn.
70 fn id(&self) -> &TaskId;
71 /// Human readable name of the task to display in the UI.
72 fn name(&self) -> &str;
73 /// Task's current working directory. If `None`, current project's root will be used.
74 fn cwd(&self) -> Option<&str>;
75 /// Sets up everything needed to spawn the task in the given directory (`cwd`).
76 /// If a task is intended to be spawned in the terminal, it should return the corresponding struct filled with the data necessary.
77 fn exec(&self, cx: TaskContext) -> Option<SpawnInTerminal>;
78}
79
80/// [`Source`] produces tasks that can be scheduled.
81///
82/// Implementations of this trait could be e.g. [`StaticSource`] that parses tasks from a .json files and provides process templates to be spawned;
83/// another one could be a language server providing lenses with tests or build server listing all targets for a given project.
84pub trait TaskSource: Any {
85 /// A way to erase the type of the source, processing and storing them generically.
86 fn as_any(&mut self) -> &mut dyn Any;
87 /// Collects all tasks available for scheduling, for the path given.
88 fn tasks_for_path(
89 &mut self,
90 path: Option<&Path>,
91 cx: &mut ModelContext<Box<dyn TaskSource>>,
92 ) -> Vec<Arc<dyn Task>>;
93}