forked from chengazhen/gitlens-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
286 lines (240 loc) · 7.52 KB
/
Copy pathmain.go
File metadata and controls
286 lines (240 loc) · 7.52 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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
)
const (
insertCode = `e={user:{id:"88888888-8888-8888-8888-888888888888",name:"Neo",email:"x@x.com",status:"activated",createdDate:"2000-01-01T00:00:00.000Z"},licenses:{paidLicenses:{},effectiveLicenses:{"gitlens-pro":{organizationId:"Linux",latestStatus:"active",latestStartDate:"2024-01-01",latestEndDate:"2999-01-01",reactivationCount:99,nextOptInDate:"2999-01-01"}}},nextOptInDate:"2999-01-01"};`
)
type ExtensionPath struct {
Name string
Path string
Description string
}
// 预设的 VSCode 扩展目录
var predefinedPaths = []ExtensionPath{
{
Name: "VSCode",
Path: filepath.Join(".vscode", "extensions"),
Description: "标准 VSCode",
},
{
Name: "Cursor",
Path: filepath.Join(".cursor", "extensions"),
Description: "Cursor 编辑器",
},
{
Name: "VSCode Insiders",
Path: filepath.Join(".vscode-insiders", "extensions"),
Description: "VSCode 预览版",
},
{
Name: "Windsurf",
Path: filepath.Join(".windsurf", "extensions"),
Description: "Windsurf 编辑器",
},
}
func getExtensionsDir() string {
// 优先使用命令行参数
if len(os.Args) > 2 && os.Args[1] == "--ext-dir" {
return os.Args[2]
}
// 其次使用环境变量
if envDir := os.Getenv("VSCODE_EXTENSIONS_DIR"); envDir != "" {
return envDir
}
// 获取用户主目录
home, err := os.UserHomeDir()
if err != nil {
fmt.Println("警告: 无法获取用户主目录")
home = "."
}
// 显示预设选项
fmt.Println("\n请选择 VSCode 扩展目录:")
for i, path := range predefinedPaths {
fullPath := filepath.Join(home, path.Path)
fmt.Printf("[%d] %s (%s)\n 路径: %s\n", i+1, path.Name, path.Description, fullPath)
}
fmt.Printf("[%d] 自定义路径\n", len(predefinedPaths)+1)
// 获取用户选择
choice := promptForSelection(len(predefinedPaths) + 1)
// 如果选择自定义路径
if choice == len(predefinedPaths)+1 {
fmt.Print("\n请输入自定义扩展目录路径: ")
reader := bufio.NewReader(os.Stdin)
customPath, _ := reader.ReadString('\n')
return strings.TrimSpace(customPath)
}
// 返回选择的预设路径
selectedPath := predefinedPaths[choice-1]
return filepath.Join(home, selectedPath.Path)
}
func main() {
if len(os.Args) > 1 && os.Args[1] == "restore" {
if err := Restore(); err != nil {
fmt.Printf("恢复失败: %v\n", err)
waitForKeyPress()
return
}
fmt.Println("恢复完成! 请重启 VS Code。")
waitForKeyPress()
return
}
// 获取扩展目录
extensionsDir := getExtensionsDir()
fmt.Println(extensionsDir)
// 获取最新版本的 GitLens 目录
extensionPath, err := getLatestGitLensPath(extensionsDir)
if err != nil {
fmt.Printf("查找 GitLens 扩展失败: %v\n", err)
waitForKeyPress()
return
}
// 需要修改的文件列表
filesToModify := []string{
filepath.Join(extensionPath, "dist", "gitlens.js"),
filepath.Join(extensionPath, "dist", "browser", "gitlens.js"),
}
// 处理每个文件
for _, file := range filesToModify {
if err := processFile(file); err != nil {
fmt.Printf("处理文件 %s 失败: %v\n", file, err)
continue
}
}
fmt.Println("\n激活完成! 请重启 VS Code 以使更改生效。")
waitForKeyPress()
}
// 等待用户按键
func waitForKeyPress() {
fmt.Print("\n按回车键退出...")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
func getLatestGitLensPath(extensionsDir string) (string, error) {
entries, err := os.ReadDir(extensionsDir)
if err != nil {
return "", fmt.Errorf("读取扩展目录失败: %v", err)
}
var gitLensDirs []string
pattern := regexp.MustCompile(`^eamodio\.gitlens-\d+\.\d+\.\d+$`)
for _, entry := range entries {
if entry.IsDir() && pattern.MatchString(entry.Name()) {
gitLensDirs = append(gitLensDirs, entry.Name())
}
}
if len(gitLensDirs) == 0 {
return "", fmt.Errorf("未找到 GitLens 扩展")
}
// 按版本号排序
sort.Sort(sort.Reverse(sort.StringSlice(gitLensDirs)))
// 如果只有一个版本,直接返回
if len(gitLensDirs) == 1 {
return filepath.Join(extensionsDir, gitLensDirs[0]), nil
}
// 显示可用版本供用户选择
fmt.Println("\n发现多个 GitLens 版本:")
for i, dir := range gitLensDirs {
fmt.Printf("[%d] %s\n", i+1, dir)
}
selectedVersion := promptForSelection(len(gitLensDirs))
return filepath.Join(extensionsDir, gitLensDirs[selectedVersion-1]), nil
}
// 提示用户选择版本
func promptForSelection(maxChoice int) int {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("\n请选择要激活的 GitLens 版本 (输入数字): ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
choice, err := strconv.Atoi(input)
if err == nil && choice > 0 && choice <= maxChoice {
return choice
}
fmt.Printf("无效的选择,请输入 1 到 %d 之间的数字\n", maxChoice)
}
}
// 添加版本检测函数
func isVersion15(dirName string) bool {
pattern := regexp.MustCompile(`^eamodio\.gitlens-15\.\d+\.\d+$`)
return pattern.MatchString(dirName)
}
// 修改 processFile 函数
func processFile(filePath string) error {
// 检查文件是否存在
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return fmt.Errorf("文件不存在: %s", filePath)
}
// 读取文件内容
content, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("读取文件失败: %v", err)
}
// 创建备份
backupPath := filePath + ".backup"
if _, err := os.Stat(backupPath); os.IsNotExist(err) {
if err := os.WriteFile(backupPath, content, 0644); err != nil {
return fmt.Errorf("创建备份失败: %v", err)
}
fmt.Printf("已创建备份: %s\n", backupPath)
}
// 获取目录名以检查版本
dirName := filepath.Base(filepath.Dir(filepath.Dir(filePath)))
if isVersion15(dirName) {
return processVersion15File(filePath, content)
}
return processVersion16File(filePath, content)
}
// 处理 15.x 版本的文件
func processVersion15File(filePath string, content []byte) error {
contentStr := string(content)
// 替换模式
replacements := map[string]string{
"qn.CommunityWithAccount": "qn.Enterprise",
"qn.Community": "qn.Enterprise",
"qn.Pro": "qn.Enterprise",
}
modified := false
for old, new := range replacements {
if strings.Contains(contentStr, old) {
contentStr = strings.ReplaceAll(contentStr, old, new)
modified = true
fmt.Printf("替换 %s 为 %s\n", old, new)
}
}
if !modified {
return fmt.Errorf("未找到需要替换的内容")
}
// 写入修改后的内容
if err := os.WriteFile(filePath, []byte(contentStr), 0644); err != nil {
return fmt.Errorf("写入文件失败: %v", err)
}
fmt.Printf("成功修改文件: %s\n", filePath)
return nil
}
// 处理 16.x 版本的文件
func processVersion16File(filePath string, content []byte) error {
// 查找匹配模式
pattern := regexp.MustCompile(`let ([a-zA-Z])={id:e\.user\.id,name:`)
matches := pattern.FindStringSubmatch(string(content))
if len(matches) < 2 {
return fmt.Errorf("未找到匹配模式")
}
matchedLetter := matches[1]
exactMatch := fmt.Sprintf("let %s={id:e.user.id,name:", matchedLetter)
// 注入激活代码
newContent := strings.Replace(string(content), exactMatch, insertCode+exactMatch, 1)
// 写入修改后的内容
if err := os.WriteFile(filePath, []byte(newContent), 0644); err != nil {
return fmt.Errorf("写入文件失败: %v", err)
}
fmt.Printf("成功修改文件: %s\n", filePath)
fmt.Printf("匹配的变量: %s\n", matchedLetter)
return nil
}