-
Notifications
You must be signed in to change notification settings - Fork 6
/
detect.go
81 lines (69 loc) · 2.14 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
package poetry
import (
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
type BuildPlanMetadata struct {
VersionSource string `toml:"version-source"`
Build bool `toml:"build"`
Version string `toml:"version"`
}
//go:generate faux --interface PyProjectPythonVersionParser --output fakes/pyproject_parser.go
type PyProjectPythonVersionParser interface {
// ParsePythonVersion extracts `tool.poetry.dependencies.python`
// from pyproject.toml
ParsePythonVersion(string) (string, error)
}
const PyProjectTomlFile = "pyproject.toml"
func Detect(parser PyProjectPythonVersionParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
pyProjectToml := filepath.Join(context.WorkingDir, PyProjectTomlFile)
if exists, err := fs.Exists(pyProjectToml); err != nil {
return packit.DetectResult{}, err
} else if !exists {
return packit.DetectResult{}, packit.Fail.WithMessage("%s is not present", PyProjectTomlFile)
}
pythonVersion, err := parser.ParsePythonVersion(pyProjectToml)
if err != nil {
return packit.DetectResult{}, err
}
if pythonVersion == "" {
return packit.DetectResult{}, packit.Fail.WithMessage("%s must include [tool.poetry.dependencies.python], see https://python-poetry.org/docs/pyproject/#dependencies-and-dev-dependencies", PyProjectTomlFile)
}
requirements := []packit.BuildPlanRequirement{
{
Name: Pip,
Metadata: BuildPlanMetadata{
Build: true,
},
},
{
Name: CPython,
Metadata: BuildPlanMetadata{
Build: true,
Version: pythonVersion,
VersionSource: PyProjectTomlFile,
},
},
}
if version, ok := os.LookupEnv("BP_POETRY_VERSION"); ok {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: PoetryDependency,
Metadata: BuildPlanMetadata{
VersionSource: "BP_POETRY_VERSION",
Version: version,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: PoetryDependency},
},
Requires: requirements,
},
}, nil
}
}