-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathinstall.rs
More file actions
437 lines (381 loc) · 14.6 KB
/
install.rs
File metadata and controls
437 lines (381 loc) · 14.6 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::{
env,
io::{self, IsTerminal, Write},
};
use crossterm::{
cursor,
event::{self, Event, KeyCode, KeyEvent},
execute,
style::{Color, Print, ResetColor, SetForegroundColor},
terminal,
};
use petgraph::stable_graph::StableGraph;
use vite_package_manager::package_manager::{PackageManager, PackageManagerType};
use vite_path::AbsolutePathBuf;
use crate::{
Error, ResolveCommandResult, Workspace,
config::ResolvedTask,
schedule::{ExecutionPlan, ExecutionSummary},
};
/// Install command.
///
/// This is the command that will be executed by the `vite-plus install` command.
///
pub struct InstallCommand {
workspace_root: AbsolutePathBuf,
ignore_replay: bool,
}
/// Install command builder.
///
/// This is a builder pattern for the `vite-plus install` command.
///
pub struct InstallCommandBuilder {
workspace_root: AbsolutePathBuf,
ignore_replay: bool,
}
impl InstallCommand {
pub const fn builder(workspace_root: AbsolutePathBuf) -> InstallCommandBuilder {
InstallCommandBuilder::new(workspace_root)
}
pub async fn execute(self, args: &Vec<String>) -> Result<ExecutionSummary, Error> {
// Handle UnrecognizedPackageManager error and let user select a package manager
let package_manager = match PackageManager::builder(&self.workspace_root).build().await {
Ok(pm) => pm,
Err(Error::UnrecognizedPackageManager) => {
// Prompt user to select a package manager
let selected_type = prompt_package_manager_selection()?;
PackageManager::builder(&self.workspace_root)
.package_manager_type(selected_type)
.build()
.await?
}
Err(e) => return Err(e),
};
let workspace = Workspace::partial_load(self.workspace_root)?;
let resolve_command = package_manager.resolve_install_command(args);
let resolved_task = ResolvedTask::resolve_from_builtin_with_command_result(
&workspace,
"install",
resolve_command.args.iter(),
ResolveCommandResult { bin_path: resolve_command.bin_path, envs: resolve_command.envs },
self.ignore_replay,
Some(package_manager.get_fingerprint_ignores()),
)?;
let mut task_graph: StableGraph<ResolvedTask, ()> = Default::default();
task_graph.add_node(resolved_task);
let summary = ExecutionPlan::plan(task_graph, false)?.execute(&workspace).await?;
workspace.unload().await?;
Ok(summary)
}
}
impl InstallCommandBuilder {
pub const fn new(workspace_root: AbsolutePathBuf) -> Self {
Self { workspace_root, ignore_replay: false }
}
pub const fn ignore_replay(mut self) -> Self {
self.ignore_replay = true;
self
}
pub fn build(self) -> InstallCommand {
InstallCommand { workspace_root: self.workspace_root, ignore_replay: self.ignore_replay }
}
}
/// Common CI environment variables
const CI_ENV_VARS: &[&str] = &[
"CI",
"CONTINUOUS_INTEGRATION",
"GITHUB_ACTIONS",
"GITLAB_CI",
"CIRCLECI",
"TRAVIS",
"JENKINS_URL",
"BUILDKITE",
"DRONE",
"CODEBUILD_BUILD_ID", // AWS CodeBuild
"TF_BUILD", // Azure Pipelines
];
/// Check if running in a CI environment
fn is_ci_environment() -> bool {
CI_ENV_VARS.iter().any(|key| env::var(key).is_ok())
}
/// Interactive menu for selecting a package manager with keyboard navigation
fn interactive_package_manager_menu() -> Result<PackageManagerType, Error> {
let options = [
("pnpm (recommended)", PackageManagerType::Pnpm),
("npm", PackageManagerType::Npm),
("yarn", PackageManagerType::Yarn),
];
let mut selected_index = 0;
// Print header and instructions with proper line breaks
println!("\n📦 No package manager detected. Please select one:");
println!(
" Use ↑↓ arrows to navigate, Enter to select, 1-{} for quick selection",
options.len()
);
println!(" Press Esc, q, or Ctrl+C to cancel installation\n");
// Enable raw mode for keyboard input
terminal::enable_raw_mode()?;
// Clear the selection area and hide cursor
execute!(io::stdout(), cursor::Hide)?;
let result = loop {
// Display menu with current selection
for (i, (name, _)) in options.iter().enumerate() {
execute!(io::stdout(), cursor::MoveToColumn(2))?;
if i == selected_index {
// Highlight selected item
execute!(
io::stdout(),
SetForegroundColor(Color::Cyan),
Print("▶ "),
Print(format!("[{}] ", i + 1)),
Print(name),
ResetColor,
Print(" ← ")
)?;
} else {
execute!(
io::stdout(),
Print(" "),
SetForegroundColor(Color::DarkGrey),
Print(format!("[{}] ", i + 1)),
ResetColor,
Print(name),
Print(" ")
)?;
}
if i < options.len() - 1 {
execute!(io::stdout(), Print("\n"))?;
}
}
// Move cursor back up for next iteration
if options.len() > 1 {
execute!(io::stdout(), cursor::MoveUp((options.len() - 1) as u16))?;
}
// Read keyboard input
if let Event::Key(KeyEvent { code, modifiers, .. }) = event::read()? {
match code {
// Handle Ctrl+C for exit
KeyCode::Char('c') if modifiers.contains(event::KeyModifiers::CONTROL) => {
// Clean up terminal before exiting
terminal::disable_raw_mode()?;
execute!(
io::stdout(),
cursor::Show,
cursor::MoveDown(options.len() as u16),
Print("\n\n"),
SetForegroundColor(Color::Yellow),
Print("⚠ Installation cancelled by user\n"),
ResetColor
)?;
return Err(Error::UserCancelled);
}
KeyCode::Up => {
selected_index = selected_index.saturating_sub(1);
}
KeyCode::Down => {
if selected_index < options.len() - 1 {
selected_index += 1;
}
}
KeyCode::Enter | KeyCode::Char(' ') => {
break Ok(options[selected_index].1);
}
KeyCode::Char('1') => {
break Ok(options[0].1);
}
KeyCode::Char('2') if options.len() > 1 => {
break Ok(options[1].1);
}
KeyCode::Char('3') if options.len() > 2 => {
break Ok(options[2].1);
}
KeyCode::Esc | KeyCode::Char('q') => {
// Exit on escape/quit
terminal::disable_raw_mode()?;
execute!(
io::stdout(),
cursor::Show,
cursor::MoveDown(options.len() as u16),
Print("\n\n"),
SetForegroundColor(Color::Yellow),
Print("⚠ Installation cancelled by user\n"),
ResetColor
)?;
return Err(Error::UserCancelled);
}
_ => {}
}
}
};
// Clean up: disable raw mode and show cursor
terminal::disable_raw_mode()?;
execute!(io::stdout(), cursor::Show, cursor::MoveDown(options.len() as u16), Print("\n"))?;
// Print selection confirmation
if let Ok(pm) = &result {
let name = match pm {
PackageManagerType::Pnpm => "pnpm",
PackageManagerType::Npm => "npm",
PackageManagerType::Yarn => "yarn",
};
println!("\n✓ Selected package manager: {name}\n");
}
result
}
/// Prompt the user to select a package manager
fn prompt_package_manager_selection() -> Result<PackageManagerType, Error> {
// In CI environment, automatically use pnpm without prompting
if is_ci_environment() {
println!("CI environment detected. Using default package manager: pnpm");
return Ok(PackageManagerType::Pnpm);
}
// Check if stdin is a TTY (terminal) - if not, use default
if !io::stdin().is_terminal() {
println!("Non-interactive environment detected. Using default package manager: pnpm");
return Ok(PackageManagerType::Pnpm);
}
// Try interactive menu first, fall back to simple prompt on error
match interactive_package_manager_menu() {
Ok(pm) => Ok(pm),
Err(err) => {
match err {
Error::UserCancelled => Err(err),
// Fallback to simple text prompt if interactive menu fails
_ => simple_text_prompt(),
}
}
}
}
/// Simple text-based prompt as fallback
fn simple_text_prompt() -> Result<PackageManagerType, Error> {
let managers = [
("pnpm", PackageManagerType::Pnpm),
("npm", PackageManagerType::Npm),
("yarn", PackageManagerType::Yarn),
];
println!("\nNo package manager detected. Please select one:");
println!("────────────────────────────────────────────────");
for (i, (name, _)) in managers.iter().enumerate() {
if i == 0 {
println!(" [{}] {} (recommended)", i + 1, name);
} else {
println!(" [{}] {}", i + 1, name);
}
}
print!("\nEnter your choice (1-{}) [default: 1]: ", managers.len());
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let choice = input.trim();
let index = if choice.is_empty() {
0 // Default to pnpm
} else {
choice
.parse::<usize>()
.ok()
.and_then(|n| if n > 0 && n <= managers.len() { Some(n - 1) } else { None })
.unwrap_or(0) // Default to pnpm if invalid input
};
let (name, selected_type) = &managers[index];
println!("✓ Selected package manager: {name}\n");
Ok(*selected_type)
}
#[cfg(test)]
mod tests {
use std::{fs, path::PathBuf};
use serial_test::serial;
use tempfile::TempDir;
use super::*;
/// Helper struct to safely manage environment variables in tests
/// This struct ensures that environment variables are properly restored
/// after the test completes, even if the test panics.
struct EnvGuard {
key: String,
original_value: Option<String>,
}
impl EnvGuard {
fn new(key: &str, value: &str) -> Self {
let original_value = env::var(key).ok();
// SAFETY: This is only used in tests which are run serially,
// preventing data races on environment variables
unsafe {
env::set_var(key, value);
}
Self { key: key.to_string(), original_value }
}
}
impl Drop for EnvGuard {
fn drop(&mut self) {
// SAFETY: This is only used in tests which are run serially,
// preventing data races on environment variables
unsafe {
match &self.original_value {
Some(value) => env::set_var(&self.key, value),
None => env::remove_var(&self.key),
}
}
}
}
#[test]
fn test_install_command_builder_build() {
let workspace_root = AbsolutePathBuf::new(PathBuf::from(if cfg!(windows) {
"C:\\test\\workspace"
} else {
"/test/workspace"
}))
.unwrap();
let command = InstallCommandBuilder::new(workspace_root.clone()).build();
assert_eq!(command.workspace_root, workspace_root);
}
#[ignore = "skip this test for auto run, should be run manually, because it will prompt for user selection"]
#[tokio::test]
async fn test_install_command_with_package_json_without_package_manager() {
let temp_dir = TempDir::new().unwrap();
let workspace_root = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Create a minimal package.json
let package_json = r#"{
"name": "test-package",
"version": "1.0.0"
}"#;
fs::write(workspace_root.join("package.json"), package_json).unwrap();
let command = InstallCommandBuilder::new(workspace_root).build();
assert!(command.execute(&vec![]).await.is_ok());
}
#[tokio::test]
#[cfg(not(windows))] // FIXME
async fn test_install_command_with_package_json_with_package_manager() {
let temp_dir = TempDir::new().unwrap();
let workspace_root = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Create a minimal package.json
let package_json = r#"{
"name": "test-package",
"version": "1.0.0",
"packageManager": "pnpm@10.15.0"
}"#;
fs::write(workspace_root.join("package.json"), package_json).unwrap();
let command = InstallCommandBuilder::new(workspace_root).build();
let result = command.execute(&vec![]).await;
println!("result: {result:?}");
assert!(result.is_ok());
}
#[tokio::test]
async fn test_install_command_execute_with_invalid_workspace() {
let temp_dir = TempDir::new().unwrap();
let workspace_root = AbsolutePathBuf::new(temp_dir.path().join("nonexistent")).unwrap();
let command = InstallCommandBuilder::new(workspace_root).build();
let args = vec![];
let result = command.execute(&args).await;
assert!(matches!(result.unwrap_err(), Error::PackageJsonNotFound(_)));
}
/// Test that in CI environment, we will use pnpm without prompting
#[test]
#[serial] // Run serially to avoid race conditions with environment variables
fn test_prompt_package_manager_in_ci() {
// Use EnvGuard to safely manage the CI environment variable
let _guard = EnvGuard::new("CI", "true");
// Should return pnpm without prompting
let result = prompt_package_manager_selection();
assert!(result.is_ok());
assert_eq!(result.unwrap(), PackageManagerType::Pnpm);
// EnvGuard will automatically restore the original value when dropped
}
}