-
Notifications
You must be signed in to change notification settings - Fork 5
/
project_file_parser.go
89 lines (72 loc) · 1.76 KB
/
project_file_parser.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
package dotnetexecute
import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
"strings"
)
type ProjectFileParser struct{}
func NewProjectFileParser() ProjectFileParser {
return ProjectFileParser{}
}
func (p ProjectFileParser) FindProjectFile(path string) (string, error) {
projectFiles, err := filepath.Glob(filepath.Join(path, "*.csproj"))
if err != nil {
return "", err
}
fsProjFiles, err := filepath.Glob(filepath.Join(path, "*.fsproj"))
if err != nil {
return "", err
}
projectFiles = append(projectFiles, fsProjFiles...)
vbProjFiles, err := filepath.Glob(filepath.Join(path, "*.vbproj"))
if err != nil {
return "", err
}
projectFiles = append(projectFiles, vbProjFiles...)
if len(projectFiles) > 0 {
return projectFiles[0], nil
}
return "", nil
}
func (p ProjectFileParser) NodeIsRequired(path string) (bool, error) {
needsNode, err := findInFile("node ", path)
if err != nil {
return false, err
}
needsNPM, err := findInFile("npm ", path)
if err != nil {
return false, err
}
return needsNode || needsNPM, nil
}
func (p ProjectFileParser) NPMIsRequired(path string) (bool, error) {
return findInFile("npm ", path)
}
func findInFile(str, path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, fmt.Errorf("failed to open %s: %w", path, err)
}
defer file.Close()
var project struct {
Targets []struct {
Execs []struct {
Command string `xml:",attr"`
} `xml:"Exec"`
} `xml:"Target"`
}
err = xml.NewDecoder(file).Decode(&project)
if err != nil {
return false, fmt.Errorf("failed to decode %s: %w", path, err)
}
for _, target := range project.Targets {
for _, exec := range target.Execs {
if strings.HasPrefix(exec.Command, str) {
return true, nil
}
}
}
return false, nil
}