forked from paketo-buildpacks/pip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect.go
77 lines (65 loc) · 2.05 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
package pip
import (
"os"
"regexp"
"github.com/paketo-buildpacks/packit/v2"
)
// 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"`
// Launch denotes the dependency is needed at runtime.
Launch bool `toml:"launch"`
// Version denotes the version of a dependency, if there is one.
Version string `toml:"version"`
// VersionSource denotes where dependency version came from (e.g. an environment variable).
VersionSource string `toml:"version-source"`
}
// Detect will return a packit.DetectFunc that will be invoked during the
// detect phase of the buildpack lifecycle.
//
// Detection always passes, and will contribute a Build Plan that provides pip,
// and requires cpython OR python, python_packages, and requirements.
//
// If a version is provided via the $BP_PIP_VERSION environment variable, that
// version of pip will be a requirement.
func Detect() packit.DetectFunc {
return func(_ packit.DetectContext) (packit.DetectResult, error) {
requirements := []packit.BuildPlanRequirement{
{
Name: CPython,
Metadata: BuildPlanMetadata{
Build: true,
},
},
}
pipVersion := os.Getenv("BP_PIP_VERSION")
if pipVersion != "" {
// Pip releases are of the form X.Y rather than X.Y.0, so in order
// to support selecting the exact version X.Y we have to up-convert
// X.Y to X.Y.0.
// Otherwise X.Y would match the latest patch release
// X.Y.Z if it is available.
var xDotYPattern = regexp.MustCompile(`^\d+\.\d+$`)
if xDotYPattern.MatchString(pipVersion) {
pipVersion = pipVersion + ".0"
}
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Pip,
Metadata: BuildPlanMetadata{
VersionSource: "BP_PIP_VERSION",
Version: pipVersion,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: Pip},
},
Requires: requirements,
},
}, nil
}
}