-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlaunch.rs
More file actions
226 lines (205 loc) · 7.28 KB
/
Copy pathlaunch.rs
File metadata and controls
226 lines (205 loc) · 7.28 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
218
219
220
221
222
223
224
225
226
use std::{
ffi::OsString,
fmt::Display,
path::{Path, PathBuf},
str::FromStr,
};
use clap::ValueEnum;
use color_eyre::eyre::{self, Result, bail, eyre};
use log::{info, trace};
use serde::{Deserialize, Serialize};
use crate::workspace::{DevContainer, Workspace};
pub const LAUNCH_DETECT: &str = "detect";
pub const LAUNCH_FORCE_CONTAINER: &str = "force-container";
pub const LAUNCH_FORCE_CLASSIC: &str = "force-classic";
/// Set the dev container launch strategy of vscode.
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
ValueEnum,
Serialize,
Deserialize,
)]
pub enum ContainerStrategy {
/// Use dev container if it was detected
#[default]
Detect,
/// Force open with dev container, even if no config was found
ForceContainer,
/// Ignore dev container
ForceClassic,
}
impl FromStr for ContainerStrategy {
type Err = eyre::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
LAUNCH_DETECT => Ok(Self::Detect),
LAUNCH_FORCE_CONTAINER => Ok(Self::ForceContainer),
LAUNCH_FORCE_CLASSIC => Ok(Self::ForceClassic),
_ => Err(eyre!("Invalid launch behavior: {}", s)),
}
}
}
impl Display for ContainerStrategy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Detect => f.write_str(LAUNCH_DETECT),
Self::ForceContainer => f.write_str(LAUNCH_FORCE_CONTAINER),
Self::ForceClassic => f.write_str(LAUNCH_FORCE_CLASSIC),
}
}
}
/// The launch behavior that is used to start vscode (saved in the history file)
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Behavior {
/// The strategy to use for launching the container.
pub strategy: ContainerStrategy,
/// Additional arguments to pass to the editor.
pub args: Vec<OsString>,
/// The editor command to use (e.g. "code", "code-insiders", "cursor")
#[serde(default = "default_editor_command")]
pub command: String,
}
fn default_editor_command() -> String {
"code".to_string()
}
/// Formats the editor name based on the command for display in messages.
fn format_editor_name(command: &str) -> String {
match command.to_lowercase().as_str() {
"code" => "Visual Studio Code".to_string(),
"code-insiders" => "Visual Studio Code Insiders".to_string(),
"cursor" => "Cursor".to_string(),
"codium" => "VSCodium".to_string(),
"positron" => "Positron".to_string(),
_ => format!("'{command}'"),
}
}
/// The configuration for the launch behavior
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Setup {
/// The workspace configuration.
workspace: Workspace,
/// The behavior configuration.
behavior: Behavior,
/// Whether to perform a dry run, not actually launching the editor.
dry_run: bool,
}
impl Setup {
pub fn new(workspace: Workspace, behavior: Behavior, dry_run: bool) -> Self {
Self {
workspace,
behavior,
dry_run,
}
}
/// Selects the dev container that should be used.
fn detect(&self, config: Option<PathBuf>) -> Result<Option<DevContainer>> {
let name = self.workspace.name.clone();
if let Some(config) = config {
let config_log = config.display();
trace!("Dev container set by path: {config_log}");
Ok(Some(DevContainer::from_config(config.as_path(), &name)?))
} else {
let configs = self.workspace.find_dev_container_configs();
let dev_containers = self.workspace.load_dev_containers(&configs)?;
match configs.len() {
0 => {
trace!("No dev container specified.");
Ok(None)
}
1 => {
trace!("Selected the only existing dev container.");
Ok(dev_containers.into_iter().next())
}
_ => Ok(Some(
crate::ui::pick_devcontainer(dev_containers)?
.ok_or_else(|| eyre!("Dev container selection cancelled"))?,
)),
}
}
}
/// Launches vscode with the given configuration.
/// Returns the dev container that was used, if any.
pub fn launch(
self,
config: Option<PathBuf>,
subfolder: Option<&Path>,
) -> Result<Option<DevContainer>> {
let editor_name = format_editor_name(&self.behavior.command);
if self.workspace.remote_host.is_some() {
match self.behavior.strategy {
ContainerStrategy::ForceContainer => {
info!(
"Opening remote workspace over SSH with {editor_name}; use VS Code Dev Containers on the remote host to reopen in a container..."
);
}
_ => {
info!("Opening remote workspace over SSH with {editor_name}...");
}
}
self.workspace.open_classic(
self.behavior.args,
self.dry_run,
&self.behavior.command,
)?;
return Ok(None);
}
match self.behavior.strategy {
ContainerStrategy::Detect => {
let dev_container = self.detect(config)?;
if let Some(ref dev_container) = dev_container {
info!("Opening dev container with {editor_name}...");
self.workspace.open(
self.behavior.args,
self.dry_run,
dev_container,
&self.behavior.command,
subfolder,
)?;
} else {
info!("No dev container found, opening on host system with {editor_name}...");
self.workspace.open_classic(
self.behavior.args,
self.dry_run,
&self.behavior.command,
)?;
}
Ok(dev_container)
}
ContainerStrategy::ForceContainer => {
let dev_container = self.detect(config)?;
if let Some(ref dev_container) = dev_container {
info!("Force opening dev container with {editor_name}...");
self.workspace.open(
self.behavior.args,
self.dry_run,
dev_container,
&self.behavior.command,
subfolder,
)?;
} else {
bail!(
"No dev container found, but was forced to open it using dev containers."
);
}
Ok(dev_container)
}
ContainerStrategy::ForceClassic => {
info!("Opening without dev containers using {editor_name}...");
self.workspace.open_classic(
self.behavior.args,
self.dry_run,
&self.behavior.command,
)?;
Ok(None)
}
}
}
}