|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +// init_vscode.go - VS Code extension installation for Mendix projects |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "runtime" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +// installVSCodeExtension extracts the embedded .vsix and installs it into VS Code. |
| 16 | +func installVSCodeExtension(projectDir string) { |
| 17 | + // Skip if no embedded vsix data |
| 18 | + if len(vsixData) == 0 { |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + // Write .vsix to the project directory |
| 23 | + vsixPath := filepath.Join(projectDir, ".claude", "vscode-mdl.vsix") |
| 24 | + if err := os.WriteFile(vsixPath, vsixData, 0644); err != nil { |
| 25 | + fmt.Fprintf(os.Stderr, " Warning: could not write VS Code extension: %v\n", err) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + // Try to find the VS Code CLI |
| 30 | + codeCLI := findCodeCLI() |
| 31 | + if codeCLI == "" { |
| 32 | + fmt.Println() |
| 33 | + fmt.Println(" ┌─────────────────────────────────────────────────────────────┐") |
| 34 | + fmt.Println(" │ VS Code MDL extension extracted but could not auto-install │") |
| 35 | + fmt.Println(" │ ('code' command not found on PATH) │") |
| 36 | + fmt.Println(" │ │") |
| 37 | + fmt.Println(" │ To install, either: │") |
| 38 | + fmt.Println(" │ 1. Run in terminal: │") |
| 39 | + fmt.Printf(" │ code --install-extension %s\n", vsixPath) |
| 40 | + fmt.Println(" │ 2. Or in VS Code: Ctrl+Shift+X → ··· → Install from VSIX │") |
| 41 | + fmt.Printf(" │ Select: %s\n", vsixPath) |
| 42 | + fmt.Println(" └─────────────────────────────────────────────────────────────┘") |
| 43 | + fmt.Println() |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + // Install the extension |
| 48 | + cmd := exec.Command(codeCLI, "--install-extension", vsixPath, "--force") |
| 49 | + output, err := cmd.CombinedOutput() |
| 50 | + if err != nil { |
| 51 | + fmt.Fprintf(os.Stderr, " Warning: VS Code extension install failed: %v\n", err) |
| 52 | + if len(output) > 0 { |
| 53 | + fmt.Fprintf(os.Stderr, " %s\n", strings.TrimSpace(string(output))) |
| 54 | + } |
| 55 | + fmt.Printf(" Install manually: %s --install-extension %s\n", codeCLI, vsixPath) |
| 56 | + return |
| 57 | + } |
| 58 | + fmt.Println(" Installed VS Code MDL extension") |
| 59 | + |
| 60 | + // Clean up the extracted .vsix |
| 61 | + os.Remove(vsixPath) |
| 62 | +} |
| 63 | + |
| 64 | +// findCodeCLI looks for the VS Code CLI executable. |
| 65 | +func findCodeCLI() string { |
| 66 | + // 1. Check PATH (works on all platforms when VS Code added to PATH) |
| 67 | + for _, name := range []string{"code", "code-insiders"} { |
| 68 | + if path, err := exec.LookPath(name); err == nil { |
| 69 | + return path |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // 2. Windows: check common install locations |
| 74 | + if runtime.GOOS == "windows" { |
| 75 | + localAppData := os.Getenv("LOCALAPPDATA") |
| 76 | + programFiles := os.Getenv("ProgramFiles") |
| 77 | + candidates := []string{} |
| 78 | + if localAppData != "" { |
| 79 | + candidates = append(candidates, |
| 80 | + filepath.Join(localAppData, "Programs", "Microsoft VS Code", "bin", "code.cmd"), |
| 81 | + filepath.Join(localAppData, "Programs", "Microsoft VS Code Insiders", "bin", "code-insiders.cmd"), |
| 82 | + ) |
| 83 | + } |
| 84 | + if programFiles != "" { |
| 85 | + candidates = append(candidates, |
| 86 | + filepath.Join(programFiles, "Microsoft VS Code", "bin", "code.cmd"), |
| 87 | + filepath.Join(programFiles, "Microsoft VS Code Insiders", "bin", "code-insiders.cmd"), |
| 88 | + ) |
| 89 | + } |
| 90 | + for _, path := range candidates { |
| 91 | + if _, err := os.Stat(path); err == nil { |
| 92 | + return path |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // 3. macOS: check standard application path |
| 98 | + if runtime.GOOS == "darwin" { |
| 99 | + candidates := []string{ |
| 100 | + "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code", |
| 101 | + "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code-insiders", |
| 102 | + } |
| 103 | + for _, path := range candidates { |
| 104 | + if _, err := os.Stat(path); err == nil { |
| 105 | + return path |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return "" |
| 111 | +} |
0 commit comments