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
use std::process::Command;
use std::fs;
use std::path::Path;
use std::io::Write;
use colored::*;

pub struct ScriptHandler {
    wrapper_script_path: String,
}

impl ScriptHandler {
    pub fn new() -> Self {
        // Primary path: Use the wrapper script that's copied during build
        let primary_path = format!("{}/init_wrapper.sh", env!("OUT_DIR"));
        
        // 记录脚本路径用于调试
       // println!("{}", format!("Script path: {}", primary_path).cyan());
        
        ScriptHandler {
            wrapper_script_path: primary_path,
        }
    }

    pub fn run_section(&self, section: &str) -> Result<bool, String> {
        println!("{}", format!("\n==== Running {} Section ====\n", section.to_uppercase()).blue());
        
        // 检查脚本是否存在
        let script_path = Path::new(&self.wrapper_script_path);
        
        if script_path.exists() {
            // 如果脚本存在,使用脚本
       //     println!("{}", format!("Using script: {}", self.wrapper_script_path).cyan());
            
            // 确保脚本是可执行的
            #[cfg(unix)]
            {
                let _ = Command::new("chmod")
                    .args(["+x", &self.wrapper_script_path])
                    .status()
                    .map_err(|e| format!("Failed to set script permissions: {}", e));
            }
            
            // 运行脚本并传递部分参数
            let output = Command::new(&self.wrapper_script_path)
                .arg(section.to_lowercase())
                .status()
                .map_err(|e| format!("Failed to execute script: {}", e))?;
            
            return Ok(output.success());
        } else {
            // 如果脚本不存在,直接在Rust中实现相应功能
        //    println!("{}", "Using built-in implementation...".cyan());
            return self.handle_section_internally(section);
        }
    }
    
    // 在Rust代码中直接实现各个部分的功能
    fn handle_section_internally(&self, section: &str) -> Result<bool, String> {
        match section.to_lowercase().as_str() {
            "xcode" => self.handle_xcode_section(),
            "brew" => self.handle_brew_section(),
            "git" => self.handle_git_section(),
            "ssh" => self.handle_ssh_section(),
            "vscode" => self.handle_vscode_section(),
            "node" => self.handle_node_section(),
            "iterm" => self.handle_iterm_section(),
            "zsh" => self.handle_zsh_section(),
            "docker" => self.handle_docker_section(),
            "devtools" => self.handle_devtools_section(),
            "apps" => self.handle_apps_section(),
            "macos" => self.handle_macos_section(),
            "workspace" => self.handle_workspace_section(),
            _ => Err(format!("Unknown section: {}", section)),
        }
    }
    
    // 实现各个部分的功能
    fn handle_xcode_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing Xcode Command Line Tools ====\n".blue());
        
        // 检查xcode command line tools是否已安装
        let xcode_select_check = Command::new("xcode-select")
            .arg("-p")
            .output()
            .map_err(|e| format!("Failed to check xcode-select: {}", e))?;
        
        if xcode_select_check.status.success() {
            println!("{}", "✓ Xcode Command Line Tools already installed".green());
            println!("{}", "Xcode Command Line Tools installation completed".green());
            return Ok(true);
        } else {
            println!("{}", "Installing Xcode Command Line Tools...".cyan());
            
            // 触发安装
            let install_result = Command::new("xcode-select")
                .args(["--install"])
                .status()
                .map_err(|e| format!("Failed to run xcode-select --install: {}", e))?;
            
            if install_result.success() {
                println!("{}", "Xcode Command Line Tools installation triggered".green());
                println!("Please wait for the installation to complete.");
                println!("{}", "Xcode Command Line Tools installation completed".green());
                return Ok(true);
            } else {
                return Err("Failed to install Xcode Command Line Tools".to_string());
            }
        }
    }
    
    fn handle_brew_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing Homebrew ====\n".blue());
        
        // 检查Homebrew是否已安装
        let brew_check = Command::new("which")
            .arg("brew")
            .output()
            .map_err(|e| format!("Failed to check brew: {}", e))?;
        
        if brew_check.status.success() {
            println!("{}", "✓ Homebrew already installed".green());
            
            // 更新Homebrew
            let _ = Command::new("brew")
                .arg("update")
                .status()
                .map_err(|e| format!("Failed to update Homebrew: {}", e))?;
            
            println!("{}", "Homebrew updated".green());
            return Ok(true);
        } else {
            println!("{}", "Installing Homebrew...".cyan());
            
            // 安装Homebrew
            let install_cmd = r#"/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)""#;
            
            let install_result = Command::new("bash")
                .args(["-c", install_cmd])
                .status()
                .map_err(|e| format!("Failed to install Homebrew: {}", e))?;
            
            if install_result.success() {
                println!("{}", "Homebrew installed".green());
                
                // 在Apple Silicon Mac上添加Homebrew到PATH
                if std::env::consts::ARCH == "aarch64" {
                    println!("Adding Homebrew to PATH for Apple Silicon Mac...");
                    
                    // 将Homebrew添加到zprofile
                    let home = std::env::var("HOME").unwrap_or_else(|_| String::from("."));
                    let zprofile_path = format!("{}/.zprofile", home);
                    
                    let profile_content = "\neval \"$(/opt/homebrew/bin/brew shellenv)\"\n";
                    
                    // 使用标准文件操作方式追加内容
                    match std::fs::OpenOptions::new()
                        .create(true)
                        .append(true)
                        .open(&zprofile_path)
                    {
                        Ok(mut file) => {
                            match file.write_all(profile_content.as_bytes()) {
                                Ok(_) => {
                                    println!("{}", "Homebrew added to PATH for Apple Silicon Mac".green());
                                },
                                Err(e) => {
                                    println!("{}", format!("Warning: Could not update .zprofile: {}", e).yellow());
                                }
                            }
                        },
                        Err(e) => {
                            println!("{}", format!("Warning: Could not open .zprofile: {}", e).yellow());
                        }
                    }
                }
                
                return Ok(true);
            } else {
                return Err("Failed to install Homebrew".to_string());
            }
        }
    }
    
    fn handle_git_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing and configuring Git ====\n".blue());
        
        // 检查Git是否已安装
        let git_check = Command::new("which")
            .arg("git")
            .output()
            .map_err(|e| format!("Failed to check git: {}", e))?;
        
        if !git_check.status.success() {
            println!("{}", "Installing Git...".cyan());
            
            // 使用Homebrew安装Git
            let install_result = Command::new("brew")
                .args(["install", "git"])
                .status()
                .map_err(|e| format!("Failed to install Git: {}", e))?;
            
            if !install_result.success() {
                return Err("Failed to install Git".to_string());
            }
            
            println!("{}", "Git installed".green());
        } else {
            println!("{}", "✓ Git already installed".green());
        }
        
        // 简单的Git配置检查
        let git_user_name = Command::new("git")
            .args(["config", "--global", "user.name"])
            .output()
            .map_err(|e| format!("Failed to check git config: {}", e))?;
        
        if git_user_name.stdout.is_empty() {
            println!("Git needs to be configured");
            println!("{}", "Git configuration should be done manually.".yellow());
            println!("Please run: git config --global user.name \"Your Name\"");
            println!("Please run: git config --global user.email \"your.email@example.com\"");
        } else {
            println!("{}", "✓ Git already configured".green());
        }
        
        println!("{}", "Git setup completed".green());
        Ok(true)
    }
    
    // 其他部分的实现(简化版,可根据需要扩展)
    fn handle_ssh_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== SSH Key Generation ====\n".blue());
        println!("{}", "This feature requires interactive input and is better performed via the original script.".yellow());
        println!("Please run the original script directly for this feature.");
        Ok(true)
    }
    
    fn handle_vscode_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing Visual Studio Code ====\n".blue());
        
        // 检查VS Code是否已安装
        let vscode_check = Command::new("which")
            .arg("code")
            .output()
            .map_err(|e| format!("Failed to check VS Code: {}", e))?;
        
        if vscode_check.status.success() {
            println!("{}", "✓ VS Code already installed".green());
        } else {
            println!("{}", "Installing VS Code...".cyan());
            
            // 使用Homebrew安装VS Code
            let install_result = Command::new("brew")
                .args(["install", "--cask", "visual-studio-code"])
                .status()
                .map_err(|e| format!("Failed to install VS Code: {}", e))?;
            
            if install_result.success() {
                println!("{}", "VS Code installed".green());
            } else {
                return Err("Failed to install VS Code".to_string());
            }
        }
        
        println!("{}", "VS Code setup completed".green());
        Ok(true)
    }
    
    fn handle_node_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Node.js Setup ====\n".blue());
        println!("{}", "This feature requires interactive input and is better performed via the original script.".yellow());
        println!("Please run the original script directly for this feature.");
        Ok(true)
    }
    
    fn handle_iterm_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing iTerm2 ====\n".blue());
        
        // 使用Homebrew安装iTerm2
        let install_result = Command::new("brew")
            .args(["install", "--cask", "iterm2"])
            .status()
            .map_err(|e| format!("Failed to install iTerm2: {}", e))?;
        
        if install_result.success() {
            println!("{}", "iTerm2 installed".green());
        } else {
            return Err("Failed to install iTerm2".to_string());
        }
        
        Ok(true)
    }
    
    fn handle_zsh_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing Oh My Zsh ====\n".blue());
        
        // 检查Oh My Zsh是否已安装
        let home = std::env::var("HOME").unwrap_or_else(|_| String::from("."));
        let omz_path = format!("{}/.oh-my-zsh", home);
        
        if Path::new(&omz_path).exists() {
            println!("{}", "✓ Oh My Zsh already installed".green());
            return Ok(true);
        }
        
        println!("{}", "Installing Oh My Zsh...".cyan());
        println!("{}", "This requires running a curl command and is better performed via the original script.".yellow());
        println!("To install Oh My Zsh, please run:");
        println!("sh -c \"$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)\"");
        
        Ok(true)
    }
    
    fn handle_docker_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing Docker ====\n".blue());
        
        // 使用Homebrew安装Docker
        let install_result = Command::new("brew")
            .args(["install", "--cask", "docker"])
            .status()
            .map_err(|e| format!("Failed to install Docker: {}", e))?;
        
        if install_result.success() {
            println!("{}", "Docker installed".green());
            println!("Please launch Docker Desktop to complete the setup.");
        } else {
            return Err("Failed to install Docker".to_string());
        }
        
        Ok(true)
    }
    
    fn handle_devtools_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing Developer Tools ====\n".blue());
        
        // 简单的开发工具安装列表
        let tools = [
            "jq", "ripgrep", "fd", "bat", "exa", "httpie", "htop"
        ];
        
        for tool in tools.iter() {
            println!("Installing {}...", tool);
            
            let install_result = Command::new("brew")
                .args(["install", tool])
                .status()
                .map_err(|e| format!("Failed to install {}: {}", tool, e))?;
            
            if install_result.success() {
                println!("{}", format!("{} installed", tool).green());
            } else {
                println!("{}", format!("Failed to install {}", tool).red());
            }
        }
        
        println!("{}", "Developer tools installation completed".green());
        Ok(true)
    }
    
    fn handle_apps_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Installing Applications ====\n".blue());
        
        println!("{}", "This feature requires interactive selection and is better performed via the original script.".yellow());
        println!("Please run the original script directly for this feature.");
        
        Ok(true)
    }
    
    fn handle_macos_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Configuring macOS Settings ====\n".blue());
        
        // 示例:配置Finder显示隐藏文件
        println!("Setting Finder to show hidden files...");
        let _ = Command::new("defaults")
            .args(["write", "com.apple.finder", "AppleShowAllFiles", "-bool", "true"])
            .status();
        
        println!("Setting Finder to show path bar...");
        let _ = Command::new("defaults")
            .args(["write", "com.apple.finder", "ShowPathbar", "-bool", "true"])
            .status();
        
        println!("Setting Finder to show status bar...");
        let _ = Command::new("defaults")
            .args(["write", "com.apple.finder", "ShowStatusBar", "-bool", "true"])
            .status();
        
        println!("Disabling press-and-hold for keys in favor of key repeat...");
        let _ = Command::new("defaults")
            .args(["write", "NSGlobalDomain", "ApplePressAndHoldEnabled", "-bool", "false"])
            .status();
        
        println!("Setting a faster keyboard repeat rate...");
        let _ = Command::new("defaults")
            .args(["write", "NSGlobalDomain", "KeyRepeat", "-int", "2"])
            .status();
        
        let _ = Command::new("defaults")
            .args(["write", "NSGlobalDomain", "InitialKeyRepeat", "-int", "15"])
            .status();
        
        println!("Disabling auto-correct...");
        let _ = Command::new("defaults")
            .args(["write", "NSGlobalDomain", "NSAutomaticSpellingCorrectionEnabled", "-bool", "false"])
            .status();
        
        println!("Restarting Finder to apply changes...");
        let _ = Command::new("killall")
            .arg("Finder")
            .status();
        
        let _ = Command::new("killall")
            .arg("SystemUIServer")
            .status();
        
        println!("{}", "macOS settings configured".green());
        Ok(true)
    }
    
    fn handle_workspace_section(&self) -> Result<bool, String> {
        println!("{}", "\n==== Creating Development Workspace ====\n".blue());
        
        let home = std::env::var("HOME").unwrap_or_else(|_| String::from("."));
        let workspace_path = format!("{}/Workspace", home);
        
        // 创建Workspace目录
        match fs::create_dir_all(&workspace_path) {
            Ok(_) => {
                println!("{}", format!("✓ Created workspace directory at {}", workspace_path).green());
                Ok(true)
            },
            Err(e) => {
                Err(format!("Failed to create workspace directory: {}", e))
            }
        }
    }
}