forked from paketo-buildpacks/dotnet-execute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
195 lines (166 loc) · 4.95 KB
/
detect.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
package dotnetexecute
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/paketo-buildpacks/packit"
)
//go:generate faux --interface BuildpackConfigParser --output fakes/buildpack_config_parser.go
type BuildpackConfigParser interface {
ParseProjectPath(path string) (projectPath string, err error)
}
//go:generate faux --interface ConfigParser --output fakes/config_parser.go
type ConfigParser interface {
Parse(glob string) (RuntimeConfig, error)
}
//go:generate faux --interface ProjectParser --output fakes/project_parser.go
type ProjectParser interface {
FindProjectFile(root string) (string, error)
ParseVersion(path string) (string, error)
ASPNetIsRequired(path string) (bool, error)
NodeIsRequired(path string) (bool, error)
}
func Detect(buildpackYMLParser BuildpackConfigParser, configParser ConfigParser, projectParser ProjectParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
root := context.WorkingDir
path, err := buildpackYMLParser.ParseProjectPath(filepath.Join(context.WorkingDir, "buildpack.yml"))
if err != nil {
return packit.DetectResult{}, fmt.Errorf("failed to parse buildpack.yml: %w", err)
}
if path != "" {
root = filepath.Join(root, path)
}
requirements := []packit.BuildPlanRequirement{
{
Name: "icu",
Metadata: map[string]interface{}{
"launch": true,
},
},
}
config, err := configParser.Parse(filepath.Join(root, "*.runtimeconfig.json"))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return packit.DetectResult{}, err
}
// FDE + FDD cases
if config.Version != "" {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-runtime",
Metadata: map[string]interface{}{
"version": config.Version,
"version-source": "runtimeconfig.json",
"launch": true,
},
})
// Only make SDK available at launch if there is no executable (FDD case only)
if !config.Executable {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-sdk",
Metadata: map[string]interface{}{
"version": getSDKVersion(config.Version),
"version-source": "runtimeconfig.json",
},
})
}
if config.UsesASPNet {
requirements = append(requirements, packit.BuildPlanRequirement{
// When aspnet buildpack is rewritten per RFC0001, change to "dotnet-aspnet"
Name: "dotnet-aspnetcore",
Metadata: map[string]interface{}{
"version": config.Version,
"version-source": "runtimeconfig.json",
"launch": true,
},
})
}
}
projectFile, err := projectParser.FindProjectFile(root)
if err != nil {
return packit.DetectResult{}, err
}
if config.Path == "" && projectFile == "" {
return packit.DetectResult{}, packit.Fail.WithMessage("no *.runtimeconfig.json or project file found")
}
if projectFile != "" {
version, err := projectParser.ParseVersion(projectFile)
if err != nil {
return packit.DetectResult{}, err
}
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-application",
Metadata: map[string]interface{}{
"launch": true,
},
})
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-runtime",
Metadata: map[string]interface{}{
"version": version,
"version-source": filepath.Base(projectFile),
"launch": true,
},
})
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-sdk",
Metadata: map[string]interface{}{
"version": getSDKVersion(version),
"version-source": filepath.Base(projectFile),
},
})
aspNetIsRequired, err := projectParser.ASPNetIsRequired(projectFile)
if err != nil {
return packit.DetectResult{}, err
}
if aspNetIsRequired {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-aspnetcore",
Metadata: map[string]interface{}{
"version": version,
"version-source": filepath.Base(projectFile),
"launch": true,
},
})
}
nodeIsRequired, err := projectParser.NodeIsRequired(projectFile)
if err != nil {
return packit.DetectResult{}, err
}
if nodeIsRequired {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "node",
Metadata: map[string]interface{}{
"version-source": filepath.Base(projectFile),
"launch": true,
},
})
}
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Requires: requirements,
},
}, nil
}
}
func getSDKVersion(version string) string {
if version == "" {
return "*"
}
pieces := strings.SplitN(version, ".", 3)
if len(pieces) < 3 {
pieces = append(pieces, "*")
}
var parts []string
for i, part := range pieces {
if i+1 == len(pieces) {
part = "*"
}
parts = append(parts, part)
if part == "*" {
break
}
}
return strings.Join(parts, ".")
}