forked from paketo-buildpacks/dotnet-execute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_file_parser.go
165 lines (137 loc) · 3.73 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
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
package dotnetexecute
import (
"encoding/xml"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"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) 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
}
}
// This regular expression matches on 'net<x>.<y>',
// 'net<x>.<y>-<platform>' & 'netcoreapp<x>.<y>'
targetFrameworkRe := regexp.MustCompile(`net(?:coreapp)?(\d\.\d)(?:\w+)?`)
for _, group := range project.PropertyGroups {
matches := targetFrameworkRe.FindStringSubmatch(group.TargetFramework)
if len(matches) == 2 {
return fmt.Sprintf("%s.0", matches[1]), nil
}
}
return "", errors.New("failed to find version in project file: missing TargetFramework property")
}
func (p ProjectFileParser) 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"`
ItemGroups []struct {
PackageReferences []struct {
Include string `xml:"Include,attr"`
Version string `xml:"Version,attr"`
} `xml:"PackageReference"`
} `xml:"ItemGroup"`
}
err = xml.NewDecoder(file).Decode(&project)
if err != nil {
return false, fmt.Errorf("failed to decode %s: %w", path, err)
}
if project.SDK == "Microsoft.NET.Sdk.Web" {
return true, nil
}
for _, ig := range project.ItemGroups {
for _, pr := range ig.PackageReferences {
if pr.Include == "Microsoft.AspNetCore.App" || pr.Include == "Microsoft.AspNetCore.All" {
return true, nil
}
}
}
return false, 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
}