From 3a8ea2c256f01b670694c6d0b2701ba1ffecfde2 Mon Sep 17 00:00:00 2001 From: Ralf Pannemans Date: Fri, 16 Aug 2024 12:28:34 +0200 Subject: [PATCH] Request cpython if there is a node-gyp dependency --- constants.go | 1 + detect.go | 35 +++++++++++++++++++++++++++-------- detect_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/constants.go b/constants.go index 201cd521..c885d961 100644 --- a/constants.go +++ b/constants.go @@ -4,6 +4,7 @@ const ( NodeModules = "node_modules" Node = "node" Npm = "npm" + Cpython = "cpython" LayerNameNodeModules = "modules" LayerNameCache = "npm-cache" diff --git a/detect.go b/detect.go index 2dc18e4b..ee65e612 100644 --- a/detect.go +++ b/detect.go @@ -39,6 +39,10 @@ func Detect() packit.DetectFunc { } version := pkg.GetVersion() + _, nodeGypInDep := pkg.Dependencies["node-gyp"] + _, nodeGypInDevDep := pkg.DevDependencies["node-gyp"] + pythonNeeded := nodeGypInDep || nodeGypInDevDep + nodeDependency := packit.BuildPlanRequirement{ Name: Node, Metadata: BuildPlanMetadata{ @@ -54,21 +58,36 @@ func Detect() packit.DetectFunc { } } - return packit.DetectResult{ + npmDependency := packit.BuildPlanRequirement{ + Name: Npm, + Metadata: BuildPlanMetadata{ + Build: true, + }, + } + + cPythonDependency := packit.BuildPlanRequirement{ + Name: Cpython, + Metadata: BuildPlanMetadata{ + Build: true, + }, + } + + result := packit.DetectResult{ Plan: packit.BuildPlan{ Provides: []packit.BuildPlanProvision{ {Name: NodeModules}, }, Requires: []packit.BuildPlanRequirement{ nodeDependency, - { - Name: Npm, - Metadata: BuildPlanMetadata{ - Build: true, - }, - }, + npmDependency, }, }, - }, nil + } + + if pythonNeeded { + result.Plan.Requires = append(result.Plan.Requires, cPythonDependency) + } + + return result, nil } } diff --git a/detect_test.go b/detect_test.go index 8c1b02ba..1f1d50cc 100644 --- a/detect_test.go +++ b/detect_test.go @@ -63,6 +63,47 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { })) }) + context("when the package.json declares a node-gyp dependency", func() { + it.Before(func() { + Expect(os.WriteFile(filePath, []byte(`{ + "dependencies": { + "node-gyp": "1.2.3" + } + }`), 0600)).To(Succeed()) + }) + + it("returns a plan requesting cpython", func() { + result, err := detect(packit.DetectContext{ + WorkingDir: workingDir, + }) + Expect(err).NotTo(HaveOccurred()) + Expect(result.Plan).To(Equal(packit.BuildPlan{ + Provides: []packit.BuildPlanProvision{ + {Name: npminstall.NodeModules}, + }, + Requires: []packit.BuildPlanRequirement{ + { + Name: npminstall.Node, + Metadata: npminstall.BuildPlanMetadata{ + Build: true, + }, + }, + { + Name: npminstall.Npm, + Metadata: npminstall.BuildPlanMetadata{ + Build: true, + }, + }, + { + Name: npminstall.Cpython, + Metadata: npminstall.BuildPlanMetadata{ + Build: true, + }, + }, + }, + })) + }) + }) context("when the package.json does not declare a node engine version", func() { it.Before(func() {