-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclcode2.go.bk
More file actions
399 lines (354 loc) · 10.5 KB
/
clcode2.go.bk
File metadata and controls
399 lines (354 loc) · 10.5 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
// 支持的编程语言文件扩展名和对应的注释样式
var langExtensions = map[string]struct {
lineComment string // 单行注释前缀
blockStart string // 块注释开始
blockEnd string // 块注释结束
fileExtensions []string // 文件扩展名
}{
"golang": {
lineComment: "//",
blockStart: "/*",
blockEnd: "*/",
fileExtensions: []string{".go"},
},
"java": {
lineComment: "//",
blockStart: "/*",
blockEnd: "*/",
fileExtensions: []string{".java"},
},
"kotlin": {
lineComment: "//",
blockStart: "/*",
blockEnd: "*/",
fileExtensions: []string{".kt", ".kts"},
},
"javascript": {
lineComment: "//",
blockStart: "/*",
blockEnd: "*/",
fileExtensions: []string{".js", ".jsx", ".ts", ".tsx"},
},
"python": {
lineComment: "#",
blockStart: "\"\"\"",
blockEnd: "\"\"\"",
fileExtensions: []string{".py"},
},
"rust": {
lineComment: "//",
blockStart: "/*",
blockEnd: "*/",
fileExtensions: []string{".rs"},
},
"cpp": {
lineComment: "//",
blockStart: "/*",
blockEnd: "*/",
fileExtensions: []string{".cpp", ".cc", ".cxx", ".c++", ".h", ".hpp", ".hxx", ".h++", ".c"},
},
"csharp": {
lineComment: "//",
blockStart: "/*",
blockEnd: "*/",
fileExtensions: []string{".cs"},
},
}
// 要排除的目录名
var excludedDirs = []string{
// Go
"vendor", "node_modules", ".git",
// Java/Kotlin
"build", "target", "out", "bin", ".gradle", "gradle", ".mvn", ".idea",
// JavaScript/TypeScript
"node_modules", "dist", "build", ".next", "out",
// Python
"venv", "env", "__pycache__", ".pytest_cache", "dist", "build", "*.egg-info",
// Rust
"target", "cargo",
// C++
"build", "out", "bin", "lib", "obj", ".vs",
// C#
"bin", "obj", "packages", ".vs",
}
// 文件扩展名 -> 语言类型的映射
var extToLang map[string]string
// 检测文件扩展名是否属于受支持的编程语言
func isSupportedExtension(ext string) bool {
_, exists := extToLang[ext]
return exists
}
// 初始化扩展名映射
func initExtensionMap() {
extToLang = make(map[string]string)
for lang, info := range langExtensions {
for _, ext := range info.fileExtensions {
extToLang[ext] = lang
}
}
}
// 版权注释检测的正则表达式
var copyrightRegex = regexp.MustCompile(`(?i)copyright|©|\(c\)|all rights reserved|license|patent|trademark|proprietary`)
// 检查目录是否被排除
func isExcludedDir(dirName string) bool {
baseName := filepath.Base(dirName)
for _, excluded := range excludedDirs {
if baseName == excluded {
return true
}
}
return false
}
// 处理单个文件
func processFile(filePath string, dryRun bool) error {
ext := filepath.Ext(filePath)
lang, exists := extToLang[ext]
if !exists {
return nil // 不支持的文件类型
}
// 读取文件内容
content, err := os.ReadFile(filePath)
if err != nil {
return err
}
fmt.Printf("检查文件: %s\n", filePath)
// 首先检查整个文件内容是否包含版权信息
contentStr := string(content)
if !copyrightRegex.MatchString(contentStr) {
fmt.Printf(" 没有发现版权信息,跳过\n")
return nil // 文件不包含版权信息,无需处理
}
langInfo := langExtensions[lang]
lines := strings.Split(contentStr, "\n")
// 需要从头部删除的行
linesToRemove := 0
inCommentBlock := false
foundCopyright := false
blockStartLine := -1
// 检查文件开头的空行
firstNonEmptyLine := 0
for i, line := range lines {
if strings.TrimSpace(line) != "" {
firstNonEmptyLine = i
break
}
}
// 检查头部注释
for i := firstNonEmptyLine; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
// 检查是否为源码指令或包声明,则停止检查
if strings.HasPrefix(line, "package ") ||
strings.HasPrefix(line, "import ") ||
strings.HasPrefix(line, "using ") ||
strings.HasPrefix(line, "#include") ||
strings.HasPrefix(line, "#ifndef") ||
strings.HasPrefix(line, "namespace") ||
strings.HasPrefix(line, "public class") ||
strings.HasPrefix(line, "class ") ||
strings.HasPrefix(line, "def ") ||
strings.HasPrefix(line, "function ") ||
strings.HasPrefix(line, "const ") ||
strings.HasPrefix(line, "var ") ||
strings.HasPrefix(line, "let ") {
// 找到实际代码行了,如果此前找到了版权信息,就停止搜索
if foundCopyright {
break
} else {
// 没有找到版权信息,保留原文件
return nil
}
}
// 块注释处理
if !inCommentBlock && strings.HasPrefix(line, langInfo.blockStart) {
inCommentBlock = true
fmt.Println("Block comment start:", line)
// 检查这一行是否包含版权信息
if copyrightRegex.MatchString(line) {
foundCopyright = true
fmt.Printf("Found copyright in START at line %d: %s\n", i, filePath)
}
} else if inCommentBlock {
// 检查块注释中的所有行是否包含版权信息
if !foundCopyright && copyrightRegex.MatchString(line) {
foundCopyright = true
fmt.Printf("Found copyright INSIDE block at line %d: %s\n", i, filePath)
}
// 检查是否到达块注释结束
if strings.Contains(line, langInfo.blockEnd) {
inCommentBlock = false
if foundCopyright {
linesToRemove = i + 1
fmt.Printf("Found copyright END at line %d: %s\n", i, filePath)
}
continue
}
}
// 行注释处理
if strings.HasPrefix(line, langInfo.lineComment) && !inCommentBlock {
if copyrightRegex.MatchString(line) {
foundCopyright = true
linesToRemove = i + 1
fmt.Printf("Found copyright in line comment at line %d: %s\n", i, filePath)
}
} else if !inCommentBlock && line != "" && !strings.HasPrefix(line, langInfo.lineComment) {
// 遇到非注释行,如果已找到版权信息,则停止检查
if foundCopyright {
fmt.Printf("Found code after copyright at line %d\n", i)
break
} else {
// 没找到版权信息就遇到代码行,保留原文件
fmt.Printf("Found code without prior copyright at line %d, skipping file\n", i)
return nil
}
}
// 对于Python的文档字符串特殊处理
if lang == "python" && strings.HasPrefix(line, "\"\"\"") {
// 找到结束的文档字符串
docStringEnd := false
for j := i + 1; j < len(lines) && !docStringEnd; j++ {
if strings.Contains(lines[j], "\"\"\"") {
docStringEnd = true
if copyrightRegex.MatchString(strings.Join(lines[i:j+1], " ")) {
foundCopyright = true
linesToRemove = j + 1
}
i = j
}
}
}
}
// 没有找到版权信息
if !foundCopyright || linesToRemove == 0 {
fmt.Printf(" 没有找到头部版权信息或无法确定要删除的行\n")
return nil
}
// 检查版权信息是否确实在文件头部
codeStarted := false
for i := 0; i < linesToRemove; i++ {
line := strings.TrimSpace(lines[i])
// 如果在版权信息前已经有代码,则不处理
if !strings.HasPrefix(line, langInfo.lineComment) &&
!strings.HasPrefix(line, langInfo.blockStart) &&
!inCommentBlock &&
line != "" &&
!strings.Contains(line, langInfo.blockEnd) {
codeStarted = true
break
}
if strings.HasPrefix(line, langInfo.blockStart) {
inCommentBlock = true
} else if strings.Contains(line, langInfo.blockEnd) {
inCommentBlock = false
}
}
if codeStarted {
fmt.Printf(" 版权信息不在文件头部,跳过\n")
return nil
}
// 重写文件
if !dryRun {
fmt.Printf("► 删除版权信息: %s\n", filePath)
fmt.Printf(" 将删除前 %d 行\n", linesToRemove)
// 准备新内容
newContent := strings.Join(lines[linesToRemove:], "\n")
// 写回文件
return os.WriteFile(filePath, []byte(newContent), 0644)
} else {
fmt.Printf("[试运行] 将删除文件 %s 的版权头部 (%d 行)\n", filePath, linesToRemove)
}
return nil
}
// 递归处理目录
func processDirectory(dirPath string, dryRun bool) error {
fileCount := 0
modifiedCount := 0
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if isExcludedDir(path) {
return filepath.SkipDir
}
return nil
}
ext := filepath.Ext(path)
if isSupportedExtension(ext) {
fileCount++
// 处理文件前记录文件大小
origSize := info.Size()
err := processFile(path, dryRun)
if err != nil {
fmt.Printf("Error processing file %s: %v\n", path, err)
return nil // 继续处理其他文件
}
// 如果不是试运行模式,检查文件大小是否变化
if !dryRun {
newInfo, err := os.Stat(path)
if err == nil && newInfo.Size() != origSize {
modifiedCount++
}
}
}
return nil
})
if !dryRun {
fmt.Printf("\n处理完成! 共扫描 %d 个文件,修改了 %d 个文件\n", fileCount, modifiedCount)
}
return err
}
func main() {
initExtensionMap()
dirPath := flag.String("d", ".", "要处理的项目目录路径")
dryRun := flag.Bool("t", false, "是否进行试运行(不实际修改文件)")
excludeDirsStr := flag.String("e", "", "额外要排除的目录,以逗号分隔")
help := flag.Bool("h", false, "显示帮助信息")
verbose := flag.Bool("v", false, "显示详细信息")
flag.Parse()
if *help {
fmt.Println("用法: clscode -d <目录> [-t] [-e <排除目录>] [-v] [-h]")
fmt.Println(" -d <目录> 指定要处理的项目目录")
fmt.Println(" -t 试运行模式,不实际修改文件")
fmt.Println(" -e <排除目录> 额外要排除的目录,以逗号分隔")
fmt.Println(" -v 显示详细处理信息")
fmt.Println(" -h 显示此帮助信息")
fmt.Println("\n支持以下语言:")
languages := make([]string, 0, len(langExtensions))
for lang := range langExtensions {
languages = append(languages, lang)
}
fmt.Println(strings.Join(languages, ", "))
os.Exit(0)
}
// 添加用户指定的排除目录
if *excludeDirsStr != "" {
userExcludedDirs := strings.Split(*excludeDirsStr, ",")
for _, dir := range userExcludedDirs {
excludedDirs = append(excludedDirs, strings.TrimSpace(dir))
}
}
// 确保目录存在
if _, err := os.Stat(*dirPath); os.IsNotExist(err) {
fmt.Printf("目录不存在: %s\n", *dirPath)
os.Exit(1)
}
fmt.Printf("开始处理目录: %s\n", *dirPath)
if *dryRun {
fmt.Println("运行模式: 试运行 (不会实际修改文件)")
}
// 处理目录
err := processDirectory(*dirPath, *dryRun)
if err != nil {
fmt.Printf("处理过程中出错: %v\n", err)
os.Exit(1)
}
}