From 83dbccc73e63abf39fe7e8455625cb237d60b18d Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Wed, 27 May 2020 15:18:59 -0700 Subject: [PATCH] Handle More Node Cases Previously, this implementation handle the straight forward riff.toml with a .js file listed case, but it did not handle the implicit cases where a package.json file existed, but no artifact was explicitly defined. This change updates the implementation to include that functionality. Signed-off-by: Ben Hale --- buildpack.toml | 2 ++ node/detect.go | 28 ++++++++++++++++++------ node/detect_test.go | 52 +++++++++++++++++++++++++++++++++++++++------ node/function.go | 3 ++- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/buildpack.toml b/buildpack.toml index 3c96edb..21c9669 100644 --- a/buildpack.toml +++ b/buildpack.toml @@ -32,10 +32,12 @@ id = "org.cloudfoundry.stacks.cflinuxfs3" [[metadata.configurations]] name = "RIFF" description = "whether this is a riff function without a riff.toml file" +build = true [[metadata.configurations]] name = "RIFF_ARTIFACT" description = "the artifact to invoke" +build = true [[metadata.dependencies]] id = "invoker" diff --git a/node/detect.go b/node/detect.go index 1985050..5352ad7 100644 --- a/node/detect.go +++ b/node/detect.go @@ -18,6 +18,8 @@ package node import ( "fmt" + "os" + "path/filepath" "github.com/buildpacks/libcnb" "github.com/paketo-buildpacks/libpak" @@ -35,8 +37,7 @@ func (Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error) {Name: "riff-node"}, }, Requires: []libcnb.BuildPlanRequire{ - {Name: "node"}, - {Name: "node_modules", Metadata: map[string]interface{}{"build": true, "launch": true}}, + {Name: "node", Metadata: map[string]interface{}{"build": true}}, {Name: "streaming-http-adapter"}, }, }, @@ -50,12 +51,25 @@ func (Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error) if ok, err := libfnbuildpack.IsRiff(context.Application.Path, cr); err != nil { return libcnb.DetectResult{}, fmt.Errorf("unable to determine if application is riff\n%w", err) - } else if ok { - metadata, err := libfnbuildpack.Metadata(context.Application.Path, cr) - if err != nil { - return libcnb.DetectResult{}, fmt.Errorf("uanble to read riff metadata\n%w", err) - } + } else if !ok { + return result, nil + } + + metadata, err := libfnbuildpack.Metadata(context.Application.Path, cr) + if err != nil { + return libcnb.DetectResult{}, fmt.Errorf("unable to read riff metadata\n%w", err) + } + + if s, ok := metadata["artifact"].(string); ok && filepath.Ext(s) == ".js" { + result.Plans[0].Requires = append(result.Plans[0].Requires, libcnb.BuildPlanRequire{ + Name: "riff-node", + Metadata: metadata, + }) + } + if _, err := os.Stat(filepath.Join(context.Application.Path, "package.json")); err != nil && !os.IsNotExist(err) { + return libcnb.DetectResult{}, fmt.Errorf("unable to stat package.json\n%w", err) + } else if err == nil { result.Plans[0].Requires = append(result.Plans[0].Requires, libcnb.BuildPlanRequire{ Name: "riff-node", Metadata: metadata, diff --git a/node/detect_test.go b/node/detect_test.go index a2570bc..9f06177 100644 --- a/node/detect_test.go +++ b/node/detect_test.go @@ -56,8 +56,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { {Name: "riff-node"}, }, Requires: []libcnb.BuildPlanRequire{ - {Name: "node"}, - {Name: "node_modules", Metadata: map[string]interface{}{"build": true, "launch": true}}, + {Name: "node", Metadata: map[string]interface{}{"build": true}}, {Name: "streaming-http-adapter"}, }, }, @@ -65,7 +64,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { })) }) - it("passes with with riff.toml", func() { + it("passes with riff.toml and non-.js artifact", func() { Expect(ioutil.WriteFile(filepath.Join(ctx.Application.Path, "riff.toml"), []byte(` artifact = "test-artifact" `), 0644)) @@ -78,10 +77,51 @@ artifact = "test-artifact" {Name: "riff-node"}, }, Requires: []libcnb.BuildPlanRequire{ - {Name: "node"}, - {Name: "node_modules", Metadata: map[string]interface{}{"build": true, "launch": true}}, + {Name: "node", Metadata: map[string]interface{}{"build": true}}, {Name: "streaming-http-adapter"}, - {Name: "riff-node", Metadata: map[string]interface{}{"artifact": "test-artifact"}}, + }, + }, + }, + })) + }) + + it("passes with with riff.toml and .js artifact", func() { + Expect(ioutil.WriteFile(filepath.Join(ctx.Application.Path, "riff.toml"), []byte(` +artifact = "test-artifact.js" +`), 0644)) + + Expect(detect.Detect(ctx)).To(Equal(libcnb.DetectResult{ + Pass: true, + Plans: []libcnb.BuildPlan{ + { + Provides: []libcnb.BuildPlanProvide{ + {Name: "riff-node"}, + }, + Requires: []libcnb.BuildPlanRequire{ + {Name: "node", Metadata: map[string]interface{}{"build": true}}, + {Name: "streaming-http-adapter"}, + {Name: "riff-node", Metadata: map[string]interface{}{"artifact": "test-artifact.js"}}, + }, + }, + }, + })) + }) + + it("passes with with riff.toml and package.json", func() { + Expect(ioutil.WriteFile(filepath.Join(ctx.Application.Path, "riff.toml"), []byte{}, 0644)) + Expect(ioutil.WriteFile(filepath.Join(ctx.Application.Path, "package.json"), []byte{}, 0644)) + + Expect(detect.Detect(ctx)).To(Equal(libcnb.DetectResult{ + Pass: true, + Plans: []libcnb.BuildPlan{ + { + Provides: []libcnb.BuildPlanProvide{ + {Name: "riff-node"}, + }, + Requires: []libcnb.BuildPlanRequire{ + {Name: "node", Metadata: map[string]interface{}{"build": true}}, + {Name: "streaming-http-adapter"}, + {Name: "riff-node", Metadata: map[string]interface{}{}}, }, }, }, diff --git a/node/function.go b/node/function.go index 0710f86..9a7e84e 100644 --- a/node/function.go +++ b/node/function.go @@ -22,6 +22,7 @@ import ( "github.com/buildpacks/libcnb" "github.com/paketo-buildpacks/libpak" "github.com/paketo-buildpacks/libpak/bard" + "github.com/projectriff/libfnbuildpack" ) type Function struct { @@ -32,7 +33,7 @@ type Function struct { func NewFunction(applicationPath string, artifactPath string) (Function, error) { return Function{ - LayerContributor: libpak.NewLayerContributor(bard.FormatIdentity("NodeJS", artifactPath), + LayerContributor: libpak.NewLayerContributor(libfnbuildpack.FormatFunction("NodeJS", artifactPath), map[string]interface{}{"artifact": artifactPath}), Path: filepath.Join(applicationPath, artifactPath), }, nil