-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect.go
97 lines (82 loc) · 2.48 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
package poetryrun
import (
"os"
"path/filepath"
"github.com/paketo-buildpacks/libreload-packit"
"github.com/paketo-buildpacks/packit/v2"
)
//go:generate faux --interface PyProjectParser --output fakes/py_project_parser.go
type Reloader libreload.Reloader
//go:generate faux --interface Reloader --output fakes/reloader.go
// BuildPlanMetadata is the buildpack specific data included in build plan
// requirements.
type BuildPlanMetadata struct {
// Build denotes the dependency is needed at build-time.
Launch bool `toml:"launch"`
}
type PyProjectParser interface {
Parse(string) (string, 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 pip at build.
//
// Detection is contingent on there being one or more scripts to run
// defined in the pyproject.toml under [tool.poetry.scripts]
func Detect(pyProjectParser PyProjectParser, reloader Reloader) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
if shouldDetect, err := shouldDetect(context.WorkingDir, pyProjectParser); err != nil {
return packit.DetectResult{}, err
} else if !shouldDetect {
return packit.DetectResult{}, nil
}
requirements := []packit.BuildPlanRequirement{
{
Name: CPython,
Metadata: BuildPlanMetadata{
Launch: true,
},
},
{
Name: Poetry,
Metadata: BuildPlanMetadata{
Launch: true,
},
},
{
Name: PoetryVenv,
Metadata: BuildPlanMetadata{
Launch: true,
},
},
}
if shouldReload, err := reloader.ShouldEnableLiveReload(); err != nil {
return packit.DetectResult{}, err
} else if shouldReload {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Watchexec,
Metadata: BuildPlanMetadata{
Launch: true,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Requires: requirements,
},
}, nil
}
}
func shouldDetect(workingDir string, pyProjectParser PyProjectParser) (shouldDetect bool, err error) {
if _, hasRunTarget := os.LookupEnv("BP_POETRY_RUN_TARGET"); hasRunTarget {
return true, nil
}
if script, err := pyProjectParser.Parse(filepath.Join(workingDir, "pyproject.toml")); err != nil {
return false, err
} else if script == "" {
return false, packit.Fail.WithMessage("Expects one and exactly one script defined in pyproject.toml")
}
return true, nil
}