-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_source_code.go
142 lines (131 loc) · 3.73 KB
/
export_source_code.go
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
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package cmd
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/cobra"
"github.com/webx-top/com"
)
// 导出软著源码
var (
sourceCodeFileIgnore *string
sourceCodeMaxLines *uint
sourceCodeFileDir *string
sourceCodeFileExtensions *string
exportSourceCodeToFile *string
)
var exportSourceCodeCmd = &cobra.Command{
Use: "exportsc",
Short: `Export source files without comments`,
RunE: exportSourceCodeRunE,
}
func exportSourceCodeRunE(cmd *cobra.Command, args []string) error {
extentions := strings.Split(strings.ToLower(*sourceCodeFileExtensions), `,`)
f, err := os.Open(*exportSourceCodeToFile)
if err != nil {
if os.IsNotExist(err) {
f, err = os.Create(*exportSourceCodeToFile)
}
if err != nil {
return err
}
}
defer f.Close()
var re *regexp.Regexp
re, err = regexp.Compile(*sourceCodeFileIgnore)
if err != nil {
return err
}
var root string
root, err = filepath.Abs(*sourceCodeFileDir)
if err != nil {
return err
}
var lines uint
err = filepath.Walk(root, func(pPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if re.MatchString(pPath) {
return nil
}
if *sourceCodeMaxLines > 0 && lines > *sourceCodeMaxLines {
return filepath.SkipDir
}
extension := strings.ToLower(filepath.Ext(info.Name()))
var ok bool
for _, ext := range extentions {
if extension == ext {
ok = true
break
}
}
if !ok {
return nil
}
var content string
var commentStarted bool
fmt.Println(`reading file:`, pPath)
err = com.SeekFileLines(pPath, func(line string) error {
lineClean := strings.TrimSpace(line)
if len(lineClean) == 0 {
return nil
}
if strings.HasPrefix(lineClean, `//`) {
return nil
}
switch extension {
case `.php`:
if strings.HasPrefix(lineClean, `#`) {
return nil
}
}
if commentStarted {
if strings.HasSuffix(lineClean, `*/`) {
commentStarted = false
}
return nil
}
if strings.HasPrefix(lineClean, `/*`) {
commentStarted = true
return nil
}
content += line + "\n"
lines++
return nil
})
if err != nil {
return err
}
_, err = f.WriteString("// Location: " + strings.TrimPrefix(pPath, root) + "\n" + content)
return err
})
return err
}
func init() {
rootCmd.AddCommand(exportSourceCodeCmd)
sourceCodeMaxLines = exportSourceCodeCmd.Flags().Uint("maxLines", 3500, "导出的最大行数")
sourceCodeFileIgnore = exportSourceCodeCmd.Flags().String("ignore", "/vendor/|/license/|bindata_assetfs\\.go|/\\.git/|/dbschema/", "忽略文件或文件夹正则表达式")
sourceCodeFileDir = exportSourceCodeCmd.Flags().String("dir", "./", "源文件所在文件夹")
sourceCodeFileExtensions = exportSourceCodeCmd.Flags().String("ext", ".go", "源文件扩展名,多个扩展名用(,)分隔,例如: .go,.php")
exportSourceCodeToFile = exportSourceCodeCmd.Flags().String("output", "./sourceCode.txt", "导出后保存的文件名称")
}