-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.go
196 lines (183 loc) · 3.98 KB
/
lint.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
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
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"sync"
"sort"
"os/exec"
"bytes"
)
type IProj interface {
GetName() string
GetFiles() []IncludedFile
}
type IncludedFile struct {
Include string `xml:"Include,attr"`
}
type Proj struct {
TargetName string `xml:"PropertyGroup>TargetName"`
ConfigurationType string `xml:"PropertyGroup>ConfigurationType"`
ProjectName string `xml:"PropertyGroup>ProjectName"`
RootNamespace string `xml:"PropertyGroup>RootNamespace"`
Filename string
Files []IncludedFile `xml:"ItemGroup>ClCompile"`
}
func (p Proj) GetName() string {
if filepath.Base(p.Filename) == "dirs.proj" {
return "dir dir"
}
name := p.TargetName
if len(name) < 1 {
name = p.ProjectName
}
if len(name) < 1 {
name = p.RootNamespace
}
if len(name) < 1 {
name = filepath.Base(p.Filename)
var ext = filepath.Ext(p.Filename)
name = name[0 : len(name)-len(ext)]
}
if p.ConfigurationType == "StaticLibrary" {
name = name + " lib"
} else if p.ConfigurationType == "DynamicLibrary" {
name = name + " dll"
} else if p.ConfigurationType == "Library" {
name = name + " dll"
} else if p.ConfigurationType == "Application" {
name = name + " exe"
} else if p.ConfigurationType == "Driver" {
name = name + " drv"
} else {
name = name + " unknown"
}
return name
}
func (p Proj) GetFiles() []IncludedFile {
return p.Files
}
func ParseProject(fpath string) (IProj, error) {
var p Proj
p.Filename = fpath
f, err := os.Open(fpath)
if err != nil {
return nil, err
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
if err = xml.Unmarshal(content, &p); err != nil {
return nil, err
}
return p, nil
}
func WalkMatch(root string, exts []string) ([]string, error) {
var matches []string
err := filepath.WalkDir(root, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if info.Name() == "node_modules" || info.Name() == "obj" || info.Name() == "objd" {
return filepath.SkipDir
}
return nil
}
matched := false
fileext := filepath.Ext(path)
for _, ext := range exts {
if fileext == ext {
matched = true
break
}
}
if matched {
matches = append(matches, path)
}
return nil
})
if err != nil {
return nil, err
}
return matches, nil
}
func PrintProjects(prj <- chan string) {
for {
if line, ok := <- prj; ok {
fmt.Println(line)
} else {
break
}
}
}
func PreProcessProject(projfile string) error {
tmpdir, err := os.MkdirTemp("", "msbuild*")
if err != nil {
return err
}
matching, err := filepath.Glob(projfile)
if err == nil {
fmt.Println("Matching:", matching)
}
fi, err := ioutil.ReadDir(filepath.Dir(projfile))
if err == nil {
for _, f := range fi {
fmt.Println("Files:", f.Name())
}
}
// defer os.RemoveAll(tmpdir)
preprocessed := filepath.Join(tmpdir, "preprocessed.vcxproj")
fmt.Println("Preprocessing", projfile, "to", preprocessed)
c := exec.Command("msbuild", "/pp:" + preprocessed, filepath.Base(projfile))
c.Dir = filepath.Dir(projfile)
var outb, errb bytes.Buffer
c.Stdout = &outb
c.Stderr = &errb
err = c.Run()
if err != nil {
fmt.Println(errb.String())
return err
}
p, err := ParseProject(preprocessed)
if err != nil {
return err
}
fmt.Println(p.GetName(), p.GetFiles())
return nil
}
func main() {
err := PreProcessProject(os.Args[1])
if err != nil {
fmt.Println(err)
if false {
files, err := WalkMatch("client/onedrive", []string{".vcxproj", ".csproj", ".proj"})
if err != nil {
panic("Can't find project files")
}
sort.Strings(files)
projects := make(chan string)
go PrintProjects(projects)
defer func() {
close(projects)
}()
wg := sync.WaitGroup{}
for _, file := range files {
wg.Add(1)
go func(file string){
p, err := ParseProject(file)
if err == nil {
projects <- p.GetName()+" "+filepath.Dir(file)
}
wg.Done()
}(file)
}
wg.Wait()
}
}
}