-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdetect.go
189 lines (166 loc) · 5.31 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
package dotnetexecute
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Netflix/go-env"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
type BuildPlanMetadata struct {
Version string `toml:"version,omitempty"`
VersionSource string `toml:"version-source,omitempty"`
Launch bool `toml:"launch"`
}
//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)
NodeIsRequired(path string) (bool, error)
}
// Detect will return a packit.DetectFunc that will be invoked during the
// detect phase of the buildpack lifecycle.
//
// Detection will contribute a Build Plan that requires different things
// depending on the type of app being built. See Configuration for details
// on how environment variable configuration influences detection.
//
// # Source Code Apps
//
// The buildpack will require .NET Core ASP.NET Runtime at launch-time. It will
// require ICU at launch time. It will require Nodejs at launch time if the app
// relies on JavaScript components.
//
// # Framework-dependent Deployments
//
// The buildpack will require the .NET Core ASP.NET Runtime at launch-time to
// run the framework-dependent app. It will require ICU at launch time. It will
// require Nodejs if the app relies on JavaScript components.
//
// # Framework-dependent Executables
//
// The buildpack will require the .NET Core ASP.NET Runtime at launch-time to
// run the framework-dependent app. It will require ICU at launch time. It will
// require Nodejs at launch time if the app relies on JavaScript components.
//
// Self-contained Executables
// The buildpack will require ICU at launch time. It will require Nodejs at
// launch time if the app relies on JavaScript components.
func Detect(
config Configuration,
logger scribe.Emitter,
configParser ConfigParser,
projectParser ProjectParser,
) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
logger.Debug.Process("Build configuration:")
es, err := env.Marshal(&config)
if err != nil {
// not tested
return packit.DetectResult{}, fmt.Errorf("parsing build configuration: %w", err)
}
for envVar := range es {
// for bug https://github.com/Netflix/go-env/issues/23
if !strings.Contains(envVar, "=") {
logger.Debug.Subprocess("%s: %s", envVar, es[envVar])
}
}
logger.Debug.Break()
requirements := []packit.BuildPlanRequirement{}
if config.LiveReloadEnabled {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "watchexec",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
}
if config.DebugEnabled {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "vsdbg",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
}
root := context.WorkingDir
if config.ProjectPath != "" {
root = filepath.Join(root, config.ProjectPath)
}
logger.Debug.Process("Looking for .NET project files in '%s'", root)
runtimeConfig, err := configParser.Parse(filepath.Join(root, "*.runtimeconfig.json"))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return packit.DetectResult{}, err
}
// FDE + FDD cases
if runtimeConfig.RuntimeVersion != "" {
logger.Debug.Subprocess("Detected '%s'", filepath.Join(root, fmt.Sprintf("%s.runtimeconfig.json", runtimeConfig.AppName)))
logger.Debug.Break()
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-core-aspnet-runtime",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
}
projectFile, err := projectParser.FindProjectFile(root)
if err != nil {
return packit.DetectResult{}, err
}
if runtimeConfig.Path == "" && projectFile == "" {
return packit.DetectResult{}, packit.Fail.WithMessage("no *.runtimeconfig.json or project file found")
}
if projectFile != "" {
logger.Debug.Subprocess("Detected '%s'", projectFile)
logger.Debug.Break()
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-application",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-core-aspnet-runtime",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
nodeIsRequired, err := projectParser.NodeIsRequired(projectFile)
if err != nil {
return packit.DetectResult{}, err
}
if nodeIsRequired {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "node",
Metadata: BuildPlanMetadata{
VersionSource: filepath.Base(projectFile),
Launch: true,
},
})
}
}
// ICU will always be append onto the build plan requirements
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "icu",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
logger.Debug.Process("Returning build plan")
logger.Debug.Subprocess("Requirements:")
for _, req := range requirements {
logger.Debug.Action(req.Name)
}
logger.Debug.Break()
return packit.DetectResult{
Plan: packit.BuildPlan{
Requires: requirements,
},
}, nil
}
}