macdevkit-cli 0.1.5

A Rust CLI wrapper for MacDevKit setup script
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
438
439
440
441
442
443
444
445
446
use clap::{Parser, Subcommand};
use colored::*;
use dialoguer::{theme::ColorfulTheme, Confirm, Select};
use std::process::Command;
use which::which;

mod script_handler;
use script_handler::ScriptHandler;

#[derive(Parser)]
#[command(name = "macdevkit")]
#[command(about = "MacDevKit: A comprehensive setup tool for macOS development environments", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// Run the full setup with interactive prompts
    Setup,
    /// Install Xcode Command Line Tools
    Xcode,
    /// Install Homebrew
    Brew,
    /// Install and configure Git
    Git,
    /// Generate SSH key
    Ssh,
    /// Install Visual Studio Code
    Vscode,
    /// Install Node.js via NVM
    Node,
    /// Install iTerm2
    Iterm,
    /// Install Oh My Zsh
    Zsh,
    /// Install Docker
    Docker,
    /// Install additional developer tools
    DevTools,
    /// Install useful applications
    Apps,
    /// Configure macOS settings
    MacOS,
    /// Create development workspace
    Workspace,
}

fn main() {
    print_welcome();
    
    let cli = Cli::parse();
    
    match &cli.command {
        Some(Commands::Setup) => run_full_setup(),
        Some(Commands::Xcode) => install_xcode_tools(),
        Some(Commands::Brew) => install_homebrew(),
        Some(Commands::Git) => install_git(),
        Some(Commands::Ssh) => generate_ssh_key(),
        Some(Commands::Vscode) => install_vscode(),
        Some(Commands::Node) => install_node(),
        Some(Commands::Iterm) => install_iterm(),
        Some(Commands::Zsh) => install_oh_my_zsh(),
        Some(Commands::Docker) => install_docker(),
        Some(Commands::DevTools) => install_dev_tools(),
        Some(Commands::Apps) => install_apps(),
        Some(Commands::MacOS) => configure_macos(),
        Some(Commands::Workspace) => create_workspace(),
        None => run_interactive_menu(),
    }
}

fn print_welcome() {
    println!("{}", r#"
    __  ___          ____             __ __ _ __ 
   /  |/  /___ _____/ __ \___ _   __/ //_/(_) /_
  / /|_/ / __ `/ __/ / / / _ \ | / / ,<  / / __/
 / /  / / /_/ / /_/ /_/ /  __/ |/ / /| |/ / /_  
/_/  /_/\__,_/\__/_____/\___/|___/_/ |_/_/\__/  
                                                 
"#.bright_blue());
    
    println!("{}", "Welcome to MacDevKit - Your Ultimate macOS Development Environment Setup Tool".yellow());
    println!();
    println!("{}", "This CLI tool will help you:".cyan());
    println!("  {}  Install essential developer tools", "".green());
    println!("  {}  Configure your development environment", "".green());
    println!("  {}  Set up programming languages and frameworks", "".green());
    println!("  {}  Install useful applications", "".green());
    println!("  {}  Optimize your macOS settings", "".green());
    println!();
}

fn run_interactive_menu() {
    let options = vec![
        "Full Setup",
        "Install Xcode Command Line Tools",
        "Install Homebrew",
        "Install and configure Git",
        "Generate SSH key",
        "Install Visual Studio Code",
        "Install Node.js via NVM",
        "Install iTerm2",
        "Install Oh My Zsh",
        "Install Docker",
        "Install additional developer tools",
        "Install useful applications",
        "Configure macOS settings",
        "Create development workspace",
        "Exit",
    ];
    
    let selection = Select::with_theme(&ColorfulTheme::default())
        .with_prompt("Select an option")
        .default(0)
        .items(&options)
        .interact()
        .unwrap();
    
    match selection {
        0 => run_full_setup(),
        1 => install_xcode_tools(),
        2 => install_homebrew(),
        3 => install_git(),
        4 => generate_ssh_key(),
        5 => install_vscode(),
        6 => install_node(),
        7 => install_iterm(),
        8 => install_oh_my_zsh(),
        9 => install_docker(),
        10 => install_dev_tools(),
        11 => install_apps(),
        12 => configure_macos(),
        13 => create_workspace(),
        14 => println!("{}", "Goodbye!".green()),
        _ => unreachable!(),
    }
}

fn run_full_setup() {
    println!("{}", "\n==== Running Full Setup ====\n".blue());
    
    // Run all steps sequentially with confirmation for each
    if confirm_step("Install Xcode Command Line Tools") {
        install_xcode_tools();
    }
    
    if confirm_step("Install Homebrew") {
        install_homebrew();
    }
    
    if confirm_step("Install and Configure Git") {
        install_git();
    }
    
    if confirm_step("Generate SSH Key") {
        generate_ssh_key();
    }
    
    if confirm_step("Install Visual Studio Code") {
        install_vscode();
    }
    
    if confirm_step("Install Node.js via NVM") {
        install_node();
    }
    
    if confirm_step("Install iTerm2") {
        install_iterm();
    }
    
    if confirm_step("Install Oh My Zsh") {
        install_oh_my_zsh();
    }
    
    if confirm_step("Install Docker") {
        install_docker();
    }
    
    if confirm_step("Install Additional Developer Tools") {
        install_dev_tools();
    }
    
    if confirm_step("Install Useful Applications") {
        install_apps();
    }
    
    if confirm_step("Configure macOS Settings") {
        configure_macos();
    }
    
    if confirm_step("Create Development Workspace") {
        create_workspace();
    }
    
    println!("{}", "\n==== Setup Complete! ====\n".blue());
    println!("{}", "Your Mac has been set up for development.".green());
    println!("{}", "Some changes may require a restart to take effect.".yellow());
    println!("{}", "Enjoy your new development environment!".green());
    
    if confirm_restart() {
        restart_computer();
    }
}

fn confirm_step(step_name: &str) -> bool {
    Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt(format!("Do you want to {}?", step_name))
        .default(true)
        .interact()
        .unwrap_or(false)
}

fn confirm_restart() -> bool {
    Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("Do you want to restart your computer now?")
        .default(false)
        .interact()
        .unwrap_or(false)
}

fn command_exists(command: &str) -> bool {
    which(command).is_ok()
}

// Implementation of step functions using ScriptHandler
fn install_xcode_tools() {
    let script_handler = ScriptHandler::new();
    match script_handler.run_section("xcode") {
        Ok(success) => {
            if success {
                println!("{}", "Xcode Command Line Tools installation completed".green());
            } else {
                println!("{}", "Xcode Command Line Tools installation failed".red());
            }
        },
        Err(e) => {
            println!("{}", format!("Error: {}", e).red());
            
            // Fallback to direct implementation
            if command_exists("xcode-select") {
                println!("{}", "✓ Xcode Command Line Tools already installed".green());
            } else {
                println!("{}", "Installing Xcode Command Line Tools...".cyan());
                let _ = Command::new("xcode-select")
                    .args(["--install"])
                    .status();
                println!("{}", "Xcode Command Line Tools installation triggered".green());
                println!("Please wait for the installation to complete.");
            }
        }
    }
}

fn install_homebrew() {
    let script_handler = ScriptHandler::new();
    match script_handler.run_section("brew") {
        Ok(success) => {
            if success {
                println!("{}", "Homebrew installation completed".green());
            } else {
                println!("{}", "Homebrew installation failed".red());
            }
        },
        Err(e) => {
            println!("{}", format!("Error: {}", e).red());
            
            // Fallback to direct implementation
            if command_exists("brew") {
                println!("{}", "✓ Homebrew already installed".green());
                
                let _ = Command::new("brew")
                    .arg("update")
                    .status();
                println!("{}", "Homebrew updated".green());
            } else {
                println!("{}", "Installing Homebrew...".cyan());
                let brew_install_script = "/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"";
                let _ = Command::new("bash")
                    .args(["-c", brew_install_script])
                    .status();
                println!("{}", "Homebrew installed".green());
            }
        }
    }
}

fn install_git() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("git") {
        println!("{}", format!("Error: {}", e).red());
        
        // Fallback implementation
        if command_exists("git") {
            println!("{}", "✓ Git already installed".green());
        } else {
            println!("{}", "Installing Git...".cyan());
            let _ = Command::new("brew")
                .args(["install", "git"])
                .status();
            println!("{}", "Git installed".green());
        }
        
        // Configure Git if not already
        let username_output = Command::new("git")
            .args(["config", "--global", "user.name"])
            .output();
        
        // 检查是否有输出,而不是使用 unwrap_or_else 直接创建 Output
        let has_username = username_output.is_ok() && 
            !username_output.as_ref().unwrap().stdout.is_empty();
        
        if !has_username {
            // Let user enter git configuration
            let git_username = dialoguer::Input::<String>::new()
                .with_prompt("Enter your Git username")
                .interact()
                .unwrap_or_else(|_| String::from(""));
                
            let _ = Command::new("git")
                .args(["config", "--global", "user.name", &git_username])
                .status();
                
            let git_email = dialoguer::Input::<String>::new()
                .with_prompt("Enter your Git email")
                .interact()
                .unwrap_or_else(|_| String::from(""));
                
            let _ = Command::new("git")
                .args(["config", "--global", "user.email", &git_email])
                .status();
                
            // Set some sensible Git defaults
            let _ = Command::new("git")
                .args(["config", "--global", "init.defaultBranch", "main"])
                .status();
                
            let _ = Command::new("git")
                .args(["config", "--global", "core.editor", "code --wait"])
                .status();
                
            let _ = Command::new("git")
                .args(["config", "--global", "pull.rebase", "false"])
                .status();
                
            println!("{}", "Git configured".green());
        } else {
            println!("{}", "✓ Git already configured".green());
        }
    }
}

// Implement the remaining functions using the ScriptHandler
fn generate_ssh_key() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("ssh") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn install_vscode() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("vscode") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn install_node() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("node") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn install_iterm() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("iterm") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn install_oh_my_zsh() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("zsh") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn install_docker() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("docker") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn install_dev_tools() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("devtools") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn install_apps() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("apps") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn configure_macos() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("macos") {
        println!("{}", format!("Error: {}", e).red());
        // Fallback implementation would go here
    }
}

fn create_workspace() {
    let script_handler = ScriptHandler::new();
    if let Err(e) = script_handler.run_section("workspace") {
        println!("{}", format!("Error: {}", e).red());
        
        // Fallback implementation
        let home_dir = std::env::var("HOME").unwrap_or_else(|_| String::from("~"));
        let _ = Command::new("mkdir")
            .args(["-p", &format!("{}/Workspace", home_dir)])
            .status();
        println!("{}", "Created ~/Workspace directory".green());
    }
}

fn restart_computer() {
    println!("{}", "Restarting your computer now...".cyan());
    let _ = Command::new("sudo")
        .args(["shutdown", "-r", "now"])
        .status();
}