-
Notifications
You must be signed in to change notification settings - Fork 4
/
dotnet_project_file_parser.go
117 lines (95 loc) · 2.58 KB
/
dotnet_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
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
package dotnetpublish
import (
"encoding/xml"
"errors"
"fmt"
"os"
"strings"
)
type DotnetProjectFileParser struct{}
func NewDotnetProjectFileParser() DotnetProjectFileParser {
return DotnetProjectFileParser{}
}
func (p DotnetProjectFileParser) ParseVersion(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("failed to read project file: %w", err)
}
defer file.Close()
var project struct {
PropertyGroups []struct {
RuntimeFrameworkVersion string
TargetFramework string
} `xml:"PropertyGroup"`
}
err = xml.NewDecoder(file).Decode(&project)
if err != nil {
return "", fmt.Errorf("failed to parse project file: %w", err)
}
for _, group := range project.PropertyGroups {
if group.RuntimeFrameworkVersion != "" {
return group.RuntimeFrameworkVersion, nil
}
}
for _, group := range project.PropertyGroups {
if strings.HasPrefix(group.TargetFramework, "netcoreapp") {
return fmt.Sprintf("%s.0", strings.TrimPrefix(group.TargetFramework, "netcoreapp")), nil
}
}
return "", errors.New("failed to find version in project file: missing TargetFramework property")
}
func (p DotnetProjectFileParser) ASPNetIsRequired(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 {
SDK string `xml:"Sdk,attr"`
}
err = xml.NewDecoder(file).Decode(&project)
if err != nil {
return false, fmt.Errorf("failed to decode %s: %w", path, err)
}
return project.SDK == "Microsoft.NET.Sdk.Web", nil
}
func (p DotnetProjectFileParser) 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 DotnetProjectFileParser) 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
}