forked from paketo-buildpacks/pipenv-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.go
108 lines (94 loc) · 2.76 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
package pipenvinstall
import (
"errors"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
// BuildPlanMetadata is the buildpack-specific data included in build plan
// requirements.
type BuildPlanMetadata struct {
// Build denotes the dependency is needed at build-time.
Build bool `toml:"build"`
// Version denotes the version to request.
Version string `toml:"version"`
// VersionSource denotes the source of version request.
VersionSource string `toml:"version-source"`
}
//go:generate faux --interface Parser --output fakes/parser.go
// Parser will parse python version out of Pipfile.lock.
type Parser interface {
ParseVersion(path string) (version string, err 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 provides site-packages,
// and requires cpython and pipenv at build.
func Detect(pipfileParser, pipfileLockParser Parser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
exists, err := fs.Exists(filepath.Join(context.WorkingDir, "Pipfile"))
if err != nil {
return packit.DetectResult{}, err
}
if !exists {
return packit.DetectResult{}, packit.Fail.WithMessage("no 'Pipfile' found")
}
cpythonRequirement := packit.BuildPlanRequirement{
Name: CPython,
Metadata: BuildPlanMetadata{
Build: true,
},
}
lockFileExists, err := fs.Exists(filepath.Join(context.WorkingDir, "Pipfile.lock"))
if err != nil {
return packit.DetectResult{}, packit.Fail.WithMessage("failed trying to stat Pipfile.lock: %w", err)
}
if lockFileExists {
cpythonVersion, err := pipfileLockParser.ParseVersion(context.WorkingDir)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return packit.DetectResult{}, err
}
}
if cpythonVersion != "" {
cpythonRequirement.Metadata = BuildPlanMetadata{
Build: true,
Version: cpythonVersion,
VersionSource: "Pipfile.lock",
}
}
} else {
cpythonVersion, err := pipfileParser.ParseVersion(context.WorkingDir)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return packit.DetectResult{}, err
}
}
if cpythonVersion != "" {
cpythonRequirement.Metadata = BuildPlanMetadata{
Build: true,
Version: cpythonVersion,
VersionSource: "Pipfile",
}
}
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: SitePackages},
},
Requires: []packit.BuildPlanRequirement{
cpythonRequirement,
{
Name: Pipenv,
Metadata: BuildPlanMetadata{
Build: true,
},
},
},
},
}, nil
}
}