-
Notifications
You must be signed in to change notification settings - Fork 4
/
detect.go
102 lines (87 loc) · 2.41 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
package dotnetpublish
import (
"fmt"
"path/filepath"
"github.com/Masterminds/semver"
"github.com/paketo-buildpacks/packit/v2"
)
type BuildPlanMetadata struct {
Version string `toml:"version,omitempty"`
VersionSource string `toml:"version-source,omitempty"`
Build bool `toml:"build"`
Launch bool `toml:"launch"`
}
//go:generate faux --interface ProjectParser --output fakes/project_parser.go
type ProjectParser interface {
FindProjectFile(root string) (string, error)
ParseVersion(path string) (string, error)
NodeIsRequired(path string) (bool, error)
NPMIsRequired(path string) (bool, error)
}
func Detect(config Configuration, parser ProjectParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
projectFilePath, err := parser.FindProjectFile(filepath.Join(context.WorkingDir, config.ProjectPath))
if err != nil {
return packit.DetectResult{}, err
}
if projectFilePath == "" {
return packit.DetectResult{}, packit.Fail.WithMessage("no project file found")
}
version, err := parser.ParseVersion(projectFilePath)
if err != nil {
return packit.DetectResult{}, err
}
semver, err := semver.NewVersion(version)
if err != nil {
return packit.DetectResult{}, err
}
requirements := []packit.BuildPlanRequirement{
{
Name: "dotnet-sdk",
Metadata: BuildPlanMetadata{
Build: true,
Version: fmt.Sprintf("%d.%d.*", semver.Major(), semver.Minor()),
VersionSource: filepath.Base(projectFilePath),
},
},
}
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "icu",
Metadata: BuildPlanMetadata{
Build: true,
},
})
nodeReq, err := parser.NodeIsRequired(projectFilePath)
if err != nil {
return packit.DetectResult{}, err
}
if nodeReq {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "node",
Metadata: BuildPlanMetadata{
Build: true,
},
})
}
npmReq, err := parser.NPMIsRequired(projectFilePath)
if err != nil {
return packit.DetectResult{}, err
}
if npmReq {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "npm",
Metadata: BuildPlanMetadata{
Build: true,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: "dotnet-application"},
},
Requires: requirements,
},
}, nil
}
}