You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
1.7 KiB
91 lines
1.7 KiB
package checker
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/openimsdk/open-im-server/tools/codescan/config"
|
|
)
|
|
|
|
type CheckResult struct {
|
|
FilePath string
|
|
Lines []int
|
|
}
|
|
|
|
func checkFileForChineseComments(filePath string) ([]CheckResult, error) {
|
|
file, err := os.Open(filePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
var results []CheckResult
|
|
scanner := bufio.NewScanner(file)
|
|
reg := regexp.MustCompile(`[\p{Han}]+`)
|
|
lineNumber := 0
|
|
|
|
var linesWithChinese []int
|
|
for scanner.Scan() {
|
|
lineNumber++
|
|
if reg.FindString(scanner.Text()) != "" {
|
|
linesWithChinese = append(linesWithChinese, lineNumber)
|
|
}
|
|
}
|
|
|
|
if len(linesWithChinese) > 0 {
|
|
results = append(results, CheckResult{
|
|
FilePath: filePath,
|
|
Lines: linesWithChinese,
|
|
})
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
func WalkDirAndCheckComments(cfg config.Config) error {
|
|
var allResults []CheckResult
|
|
err := filepath.Walk(cfg.Directory, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
for _, fileType := range cfg.FileTypes {
|
|
if filepath.Ext(path) == fileType {
|
|
results, err := checkFileForChineseComments(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(results) > 0 {
|
|
allResults = append(allResults, results...)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(allResults) > 0 {
|
|
var errMsg strings.Builder
|
|
errMsg.WriteString("Files containing Chinese comments:\n")
|
|
for _, result := range allResults {
|
|
errMsg.WriteString(fmt.Sprintf("%s: Lines %v\n", result.FilePath, result.Lines))
|
|
}
|
|
return fmt.Errorf(errMsg.String())
|
|
}
|
|
|
|
return nil
|
|
}
|