-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.go
74 lines (62 loc) · 1.67 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
package rake
import (
"fmt"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
type BuildPlanMetadata struct {
Launch bool `toml:"launch"`
}
func Detect(gemfileParser Parser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
rakefiles := []string{"Rakefile", "Rakefile.rb", "rakefile", "rakefile.rb"}
rakeFileExists := false
for _, file := range rakefiles {
exists, err := fs.Exists(filepath.Join(context.WorkingDir, file))
if err != nil {
return packit.DetectResult{}, fmt.Errorf("failed to stat %s: %w", file, err)
}
if exists {
rakeFileExists = true
break
}
}
if !rakeFileExists {
return packit.DetectResult{}, packit.Fail.WithMessage("no 'Rakefile', 'Rakefile.rb', 'rakefile', or 'rakefile.rb' file found")
}
hasRakeGem, err := gemfileParser.Parse(filepath.Join(context.WorkingDir, "Gemfile"))
if err != nil {
return packit.DetectResult{}, fmt.Errorf("failed to parse Gemfile: %w", err)
}
requirements := []packit.BuildPlanRequirement{}
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "mri",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
if hasRakeGem {
requirements = append(
requirements,
packit.BuildPlanRequirement{
Name: "gems",
Metadata: BuildPlanMetadata{
Launch: true,
},
},
packit.BuildPlanRequirement{
Name: "bundler",
Metadata: BuildPlanMetadata{
Launch: true,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{},
Requires: requirements,
},
}, nil
}
}