tasks.rs

 1use gpui::Context;
 2use project::TaskSourceKind;
 3use remote::ConnectionState;
 4use task::{ResolvedTask, TaskContext, TaskTemplate};
 5
 6use crate::Workspace;
 7
 8pub fn schedule_task(
 9    workspace: &mut Workspace,
10    task_source_kind: TaskSourceKind,
11    task_to_resolve: &TaskTemplate,
12    task_cx: &TaskContext,
13    omit_history: bool,
14    cx: &mut Context<Workspace>,
15) {
16    match workspace.project.read(cx).ssh_connection_state(cx) {
17        None | Some(ConnectionState::Connected) => {}
18        Some(
19            ConnectionState::Connecting
20            | ConnectionState::Disconnected
21            | ConnectionState::HeartbeatMissed
22            | ConnectionState::Reconnecting,
23        ) => {
24            log::warn!("Cannot schedule tasks when disconnected from a remote host");
25            return;
26        }
27    }
28
29    if let Some(spawn_in_terminal) =
30        task_to_resolve.resolve_task(&task_source_kind.to_id_base(), task_cx)
31    {
32        schedule_resolved_task(
33            workspace,
34            task_source_kind,
35            spawn_in_terminal,
36            omit_history,
37            cx,
38        );
39    }
40}
41
42pub fn schedule_resolved_task(
43    workspace: &mut Workspace,
44    task_source_kind: TaskSourceKind,
45    mut resolved_task: ResolvedTask,
46    omit_history: bool,
47    cx: &mut Context<Workspace>,
48) {
49    let debug_config = resolved_task.resolved_debug_adapter_config();
50
51    if let Some(spawn_in_terminal) = resolved_task.resolved.take() {
52        if let Some(debug_config) = debug_config {
53            workspace
54                .debug_task_queue
55                .insert(resolved_task.id.clone(), debug_config);
56        }
57
58        if !omit_history {
59            resolved_task.resolved = Some(spawn_in_terminal.clone());
60            workspace.project().update(cx, |project, cx| {
61                if let Some(task_inventory) =
62                    project.task_store().read(cx).task_inventory().cloned()
63                {
64                    task_inventory.update(cx, |inventory, _| {
65                        inventory.task_scheduled(task_source_kind, resolved_task);
66                    })
67                }
68            });
69        }
70
71        cx.emit(crate::Event::SpawnTask {
72            action: Box::new(spawn_in_terminal),
73        });
74    }
75}