-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdetect.go
83 lines (70 loc) · 2.22 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
package bundler
import (
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2"
)
//go:generate faux --interface VersionParser --output fakes/version_parser.go
type VersionParser interface {
ParseVersion(path string) (version string, err error)
}
type BuildPlanMetadata struct {
VersionSource string `toml:"version-source"`
Version string `toml:"version"`
}
func Detect(buildpackYMLParser, gemfileLockParser VersionParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
var requirements []packit.BuildPlanRequirement
// If versions are provided via BP_BUNDLER_VERSION, buildpack.yml, and/or Gemfile.lock:
// Detection will pass all versions as build plan requirements.
// The build phase is responsible for using a priority mapping to select correct version.
// This will allow for greater clarity in log output if the user has set version through multiple configurations.
// check $BP_BUNDLER_VERSION
version := os.Getenv("BP_BUNDLER_VERSION")
if version != "" {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Bundler,
Metadata: BuildPlanMetadata{
VersionSource: "BP_BUNDLER_VERSION",
Version: version,
},
})
}
// check buildpack.yml
version, err := buildpackYMLParser.ParseVersion(filepath.Join(context.WorkingDir, BuildpackYMLSource))
if err != nil {
return packit.DetectResult{}, err
}
if version != "" {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Bundler,
Metadata: BuildPlanMetadata{
VersionSource: BuildpackYMLSource,
Version: version,
},
})
}
// check Gemfile.lock
version, err = gemfileLockParser.ParseVersion(filepath.Join(context.WorkingDir, GemfileLockSource))
if err != nil {
return packit.DetectResult{}, err
}
if version != "" {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Bundler,
Metadata: BuildPlanMetadata{
VersionSource: GemfileLockSource,
Version: version,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: Bundler},
},
Requires: requirements,
},
}, nil
}
}