From 7a6241fb511c4dfbfc4fcc8f611026bdcfe2b47a Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Tue, 21 Apr 2020 10:27:40 -0700 Subject: [PATCH] libbs Update This change updates the buildpack to use libbs which removes duplication of build-system common behaviors. Signed-off-by: Ben Hale --- go.mod | 4 +- go.sum | 2 + sbt/application.go | 193 ---------------------- sbt/application_test.go | 258 ------------------------------ sbt/build.go | 14 +- sbt/build_test.go | 5 +- sbt/cache.go | 67 -------- sbt/cache_test.go | 71 -------- sbt/init_test.go | 2 - sbt/testdata/stub-application.zip | Bin 178 -> 0 bytes 10 files changed, 17 insertions(+), 599 deletions(-) delete mode 100644 sbt/application.go delete mode 100644 sbt/application_test.go delete mode 100644 sbt/cache.go delete mode 100644 sbt/cache_test.go delete mode 100644 sbt/testdata/stub-application.zip diff --git a/go.mod b/go.mod index a118e26..2b7a5c1 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,8 @@ go 1.14 require ( github.com/buildpacks/libcnb v1.8.0 - github.com/mattn/go-shellwords v1.0.10 github.com/onsi/gomega v1.9.0 - github.com/paketo-buildpacks/libjvm v1.10.4 + github.com/paketo-buildpacks/libbs v1.0.0 github.com/paketo-buildpacks/libpak v1.27.5 github.com/sclevine/spec v1.4.0 - github.com/stretchr/testify v1.5.1 ) diff --git a/go.sum b/go.sum index 105d2e0..97f72e5 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,8 @@ github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0 github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.9.0 h1:R1uwffexN6Pr340GtYRIdZmAiN4J+iw6WG4wog1DUXg= github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= +github.com/paketo-buildpacks/libbs v1.0.0 h1:DxsO+0FkgAr8FC+/6CSA8QSeQ8ncI7wNFt0L1m8M/ho= +github.com/paketo-buildpacks/libbs v1.0.0/go.mod h1:PSwqXBDCznpGgNao/L/qbhrZiIV7qrtYFavZ0FrA/DI= github.com/paketo-buildpacks/libjvm v1.10.4 h1:JXXiypzBzW72v3lERdQzAmbvgGd68UEpnjJTBJiel4M= github.com/paketo-buildpacks/libjvm v1.10.4/go.mod h1:Erul55CiZ3hTd9m/dJ1MV0tfa+86pnYIOEUQSbn39jc= github.com/paketo-buildpacks/libpak v1.27.5 h1:gcYvzepgfYjl/Ae7lKqZGasVklC1y9cNAGF4AMPntJk= diff --git a/sbt/application.go b/sbt/application.go deleted file mode 100644 index 21a694c..0000000 --- a/sbt/application.go +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sbt - -import ( - "fmt" - "io/ioutil" - "os" - "path/filepath" - "sort" - "strings" - - "github.com/buildpacks/libcnb" - "github.com/mattn/go-shellwords" - "github.com/paketo-buildpacks/libjvm" - "github.com/paketo-buildpacks/libpak" - "github.com/paketo-buildpacks/libpak/bard" - "github.com/paketo-buildpacks/libpak/crush" - "github.com/paketo-buildpacks/libpak/effect" - "github.com/paketo-buildpacks/libpak/sherpa" -) - -var ( - DefaultArguments = []string{"universal:packageBin"} - DefaultTarget = filepath.Join("target", "universal", "*.zip") -) - -type Application struct { - ApplicationPath string - CachePath string - Command string - Executor effect.Executor - LayerContributor libpak.LayerContributor - Logger bard.Logger - Plan *libcnb.BuildpackPlan -} - -func NewApplication(applicationPath string, cachePath string, command string, plan *libcnb.BuildpackPlan) (Application, error) { - l, err := sherpa.NewFileListing(applicationPath) - if err != nil { - return Application{}, fmt.Errorf("unable to create file listing for %s\n%w", applicationPath, err) - } - expected := map[string][]sherpa.FileEntry{"files": l} - - return Application{ - ApplicationPath: applicationPath, - CachePath: cachePath, - Command: command, - Executor: effect.NewExecutor(), - LayerContributor: libpak.NewLayerContributor("Compiled Application", expected), - Plan: plan, - }, nil -} - -func (a Application) Contribute(layer libcnb.Layer) (libcnb.Layer, error) { - a.Logger.Body(bard.FormatUserConfig("BP_SCALA_BUILD_ARGUMENTS", "the arguments passed to the build system", - strings.Join(DefaultArguments, " "))) - a.Logger.Body(bard.FormatUserConfig("BP_SCALA_BUILT_MODULE", "the module to find application artifact in", "")) - a.Logger.Body(bard.FormatUserConfig("BP_SCALA_BUILT_ARTIFACT", "the built application artifact", DefaultTarget)) - - a.LayerContributor.Logger = a.Logger - - layer, err := a.LayerContributor.Contribute(layer, func() (libcnb.Layer, error) { - arguments, err := a.ResolveArguments() - if err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to resolve arguments\n%w", err) - } - - a.Logger.Bodyf("Executing %s %s", filepath.Base(a.Command), strings.Join(arguments, " ")) - if err := a.Executor.Execute(effect.Execution{ - Command: a.Command, - Args: arguments, - Dir: a.ApplicationPath, - Stdout: a.Logger.InfoWriter(), - Stderr: a.Logger.InfoWriter(), - }); err != nil { - return libcnb.Layer{}, fmt.Errorf("error running build\n%w", err) - } - - artifact, err := a.ResolveArtifact() - if err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to resolve artifact\n%w", err) - } - - in, err := os.Open(artifact) - if err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to open %s\n%w", artifact, err) - } - defer in.Close() - - file := filepath.Join(layer.Path, "application.zip") - if err := sherpa.CopyFile(in, file); err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to copy %s to %s\n%w", artifact, file, err) - } - - layer.Cache = true - return layer, nil - }) - if err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to contribute application layer\n%w", err) - } - - entry := libcnb.BuildpackPlanEntry{ - Name: "sbt", - Metadata: map[string]interface{}{}, - } - a.Plan.Entries = append(a.Plan.Entries, entry) - - entry.Metadata["dependencies"], err = libjvm.NewMavenJARListing(a.CachePath) - if err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to generate dependencies from %s\n%w", a.CachePath, err) - } - - a.Logger.Header("Removing source code") - cs, err := ioutil.ReadDir(a.ApplicationPath) - if err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to list children of %s\n%w", a.ApplicationPath, err) - } - for _, c := range cs { - file := filepath.Join(a.ApplicationPath, c.Name()) - if err := os.RemoveAll(file); err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to remove %s\n%w", file, err) - } - } - - file := filepath.Join(layer.Path, "application.zip") - in, err := os.Open(file) - if err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to open %s\n%w", file, err) - } - defer in.Close() - - if err := crush.ExtractZip(in, a.ApplicationPath, 0); err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to extract %s\n%w", file, err) - } - - return layer, nil -} - -func (Application) Name() string { - return "application" -} - -func (a Application) ResolveArguments() ([]string, error) { - var err error - arguments := DefaultArguments - - if s, ok := os.LookupEnv("BP_SBT_BUILD_ARGUMENTS"); ok { - arguments, err = shellwords.Parse(s) - if err != nil { - return nil, fmt.Errorf("unable to parse arguments from %s\n%w", s, err) - } - } - - return arguments, nil -} - -func (a Application) ResolveArtifact() (string, error) { - pattern := DefaultTarget - if s, ok := os.LookupEnv("BP_SBT_BUILT_MODULE"); ok { - pattern = filepath.Join(s, pattern) - } - if s, ok := os.LookupEnv("BP_SBT_BUILT_ARTIFACT"); ok { - pattern = s - } - - file := filepath.Join(a.ApplicationPath, pattern) - artifacts, err := filepath.Glob(file) - if err != nil { - return "", fmt.Errorf("unable to find files with %s\n%w", pattern, err) - } - - if len(artifacts) != 1 { - sort.Strings(artifacts) - return "", fmt.Errorf("unable to find built artifact in %s, candidates: %s", pattern, artifacts) - } - - return artifacts[0], nil -} diff --git a/sbt/application_test.go b/sbt/application_test.go deleted file mode 100644 index 4281143..0000000 --- a/sbt/application_test.go +++ /dev/null @@ -1,258 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sbt_test - -import ( - "fmt" - "io" - "io/ioutil" - "os" - "path/filepath" - "testing" - - "github.com/buildpacks/libcnb" - . "github.com/onsi/gomega" - "github.com/paketo-buildpacks/libjvm" - "github.com/paketo-buildpacks/libpak/bard" - "github.com/paketo-buildpacks/libpak/effect" - "github.com/paketo-buildpacks/libpak/effect/mocks" - "github.com/paketo-buildpacks/sbt/sbt" - "github.com/sclevine/spec" - "github.com/stretchr/testify/mock" -) - -func testApplication(t *testing.T, context spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - cachePath string - ctx libcnb.BuildContext - application sbt.Application - executor *mocks.Executor - plan *libcnb.BuildpackPlan - ) - - it.Before(func() { - var err error - - ctx.Application.Path, err = ioutil.TempDir("", "application-application") - Expect(err).NotTo(HaveOccurred()) - - ctx.Layers.Path, err = ioutil.TempDir("", "application-layers") - Expect(err).NotTo(HaveOccurred()) - - cachePath, err = ioutil.TempDir("", "application-cache") - Expect(err).NotTo(HaveOccurred()) - - plan = &libcnb.BuildpackPlan{} - - application, err = sbt.NewApplication(ctx.Application.Path, cachePath, "test-command", plan) - Expect(err).NotTo(HaveOccurred()) - - executor = &mocks.Executor{} - application.Executor = executor - }) - - it.After(func() { - Expect(os.RemoveAll(ctx.Application.Path)).To(Succeed()) - Expect(os.RemoveAll(ctx.Layers.Path)).To(Succeed()) - Expect(os.RemoveAll(cachePath)).To(Succeed()) - }) - - it("contributes layer", func() { - in, err := os.Open(filepath.Join("testdata", "stub-application.zip")) - Expect(err).NotTo(HaveOccurred()) - Expect(os.MkdirAll(filepath.Join(ctx.Application.Path, "target", "universal"), 0755)).To(Succeed()) - out, err := os.OpenFile(filepath.Join(ctx.Application.Path, "target", "universal", "stub-application.zip"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) - Expect(err).NotTo(HaveOccurred()) - _, err = io.Copy(out, in) - Expect(err).NotTo(HaveOccurred()) - Expect(in.Close()).To(Succeed()) - Expect(out.Close()).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(cachePath, "test-file-1.1.1.jar"), []byte{}, 0644)).To(Succeed()) - - application.Logger = bard.NewLogger(ioutil.Discard) - executor.On("Execute", mock.Anything).Return(nil) - - layer, err := ctx.Layers.Layer("test-layer") - Expect(err).NotTo(HaveOccurred()) - - layer, err = application.Contribute(layer) - Expect(err).NotTo(HaveOccurred()) - - Expect(layer.Cache).To(BeTrue()) - - e := executor.Calls[0].Arguments[0].(effect.Execution) - Expect(e.Command).To(Equal("test-command")) - Expect(e.Args).To(Equal([]string{"universal:packageBin"})) - Expect(e.Dir).To(Equal(ctx.Application.Path)) - Expect(e.Stdout).NotTo(BeNil()) - Expect(e.Stderr).NotTo(BeNil()) - - Expect(filepath.Join(layer.Path, "application.zip")).To(BeARegularFile()) - Expect(filepath.Join(ctx.Application.Path, "target", "universal", "stub-application.zip")).NotTo(BeAnExistingFile()) - Expect(filepath.Join(ctx.Application.Path, "fixture-marker")).To(BeARegularFile()) - - Expect(plan).To(Equal(&libcnb.BuildpackPlan{ - Entries: []libcnb.BuildpackPlanEntry{ - { - Name: "sbt", - Metadata: map[string]interface{}{ - "dependencies": []libjvm.MavenJAR{ - { - Name: "test-file", - Version: "1.1.1", - SHA256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - }, - }, - }, - }, - }, - })) - }) - - context("ResolveArguments", func() { - it("uses default arguments", func() { - Expect(application.ResolveArguments()).To(Equal([]string{"universal:packageBin"})) - }) - - context("$BP_SBT_BUILD_ARGUMENTS", func() { - - it.Before(func() { - Expect(os.Setenv("BP_SBT_BUILD_ARGUMENTS", "test configured arguments")).To(Succeed()) - }) - - it.After(func() { - Expect(os.Unsetenv("BP_SBT_BUILD_ARGUMENTS")).To(Succeed()) - }) - - it("parses value from $BP_SBT_BUILD_ARGUMENTS", func() { - Expect(application.ResolveArguments()).To(Equal([]string{"test", "configured", "arguments"})) - }) - }) - }) - - context("ResolveArtifact", func() { - it("fails with no files", func() { - _, err := application.ResolveArtifact() - Expect(err).To(MatchError("unable to find built artifact in target/universal/*.zip, candidates: []")) - }) - - it("fails with multiple candidates", func() { - Expect(os.MkdirAll(filepath.Join(ctx.Application.Path, "target", "universal"), 0755)).To(Succeed()) - - for _, f := range []string{"stub-application-1.zip", "stub-application-2.zip", "stub-application-3.zip"} { - in, err := os.Open(filepath.Join("testdata", "stub-application.zip")) - Expect(err).NotTo(HaveOccurred()) - - out, err := os.OpenFile(filepath.Join(ctx.Application.Path, "target", "universal", f), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) - Expect(err).NotTo(HaveOccurred()) - - _, err = io.Copy(out, in) - Expect(err).NotTo(HaveOccurred()) - - Expect(in.Close()).To(Succeed()) - Expect(out.Close()).To(Succeed()) - } - - _, err := application.ResolveArtifact() - Expect(err).To(MatchError( - fmt.Sprintf("unable to find built artifact in target/universal/*.zip, candidates: [%s %s %s]", - filepath.Join(ctx.Application.Path, "target", "universal", "stub-application-1.zip"), - filepath.Join(ctx.Application.Path, "target", "universal", "stub-application-2.zip"), - filepath.Join(ctx.Application.Path, "target", "universal", "stub-application-3.zip")))) - - }) - - it("passes with a single candidate", func() { - Expect(os.MkdirAll(filepath.Join(ctx.Application.Path, "target", "universal"), 0755)).To(Succeed()) - - in, err := os.Open(filepath.Join("testdata", "stub-application.zip")) - Expect(err).NotTo(HaveOccurred()) - - out, err := os.OpenFile(filepath.Join(ctx.Application.Path, "target", "universal", "stub-application.zip"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) - Expect(err).NotTo(HaveOccurred()) - - _, err = io.Copy(out, in) - Expect(err).NotTo(HaveOccurred()) - - Expect(in.Close()).To(Succeed()) - Expect(out.Close()).To(Succeed()) - - Expect(application.ResolveArtifact()).To(Equal(filepath.Join(ctx.Application.Path, "target", "universal", "stub-application.zip"))) - }) - - context("$BP_SBT_BUILT_MODULE", func() { - - it.Before(func() { - Expect(os.Setenv("BP_SBT_BUILT_MODULE", "test-directory")).To(Succeed()) - }) - - it.After(func() { - Expect(os.Unsetenv("BP_SBT_BUILT_MODULE")).To(Succeed()) - }) - - it("passes with $BP_SBT_BUILT_MODULE", func() { - Expect(os.MkdirAll(filepath.Join(ctx.Application.Path, "test-directory", "target", "universal"), 0755)).To(Succeed()) - - in, err := os.Open(filepath.Join("testdata", "stub-application.zip")) - Expect(err).NotTo(HaveOccurred()) - - out, err := os.OpenFile(filepath.Join(ctx.Application.Path, "test-directory", "target", "universal", "stub-application.zip"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) - Expect(err).NotTo(HaveOccurred()) - - _, err = io.Copy(out, in) - Expect(err).NotTo(HaveOccurred()) - - Expect(in.Close()).To(Succeed()) - Expect(out.Close()).To(Succeed()) - - Expect(application.ResolveArtifact()).To(Equal(filepath.Join(ctx.Application.Path, "test-directory", "target", "universal", "stub-application.zip"))) - }) - - }) - - context("$BP_SBT_BUILT_ARTIFACT", func() { - it.Before(func() { - Expect(os.Setenv("BP_SBT_BUILT_ARTIFACT", "test-directory/stub-application.zip")).To(Succeed()) - }) - - it.After(func() { - Expect(os.Unsetenv("BP_SBT_BUILT_ARTIFACT")).To(Succeed()) - }) - - it("passes with BP_SBT_BUILT_ARTIFACT", func() { - Expect(os.MkdirAll(filepath.Join(ctx.Application.Path, "test-directory"), 0755)).To(Succeed()) - - in, err := os.Open(filepath.Join("testdata", "stub-application.zip")) - Expect(err).NotTo(HaveOccurred()) - - out, err := os.OpenFile(filepath.Join(ctx.Application.Path, "test-directory", "stub-application.zip"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) - Expect(err).NotTo(HaveOccurred()) - - _, err = io.Copy(out, in) - Expect(err).NotTo(HaveOccurred()) - - Expect(in.Close()).To(Succeed()) - Expect(out.Close()).To(Succeed()) - - Expect(application.ResolveArtifact()).To(Equal(filepath.Join(ctx.Application.Path, "test-directory", "stub-application.zip"))) - }) - - }) - }) -} diff --git a/sbt/build.go b/sbt/build.go index abe7219..b61cd2e 100644 --- a/sbt/build.go +++ b/sbt/build.go @@ -19,9 +19,11 @@ package sbt import ( "fmt" "os" + "os/user" "path/filepath" "github.com/buildpacks/libcnb" + "github.com/paketo-buildpacks/libbs" "github.com/paketo-buildpacks/libpak" "github.com/paketo-buildpacks/libpak/bard" ) @@ -58,14 +60,20 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) { return libcnb.BuildResult{}, fmt.Errorf("unable to stat %s\n%w", command, err) } - c, err := NewCache() + u, err := user.Current() if err != nil { - return libcnb.BuildResult{}, fmt.Errorf("unable to create cache layer\n%w", err) + return libcnb.BuildResult{}, fmt.Errorf("unable to determine user home directory\n%w", err) } + + c := libbs.Cache{Path: filepath.Join(u.HomeDir, ".sbt")} c.Logger = b.Logger result.Layers = append(result.Layers, c) - a, err := NewApplication(context.Application.Path, c.Path, command, result.Plan) + arg := libbs.NewArgumentResolver("BP_SBT_BUILD_ARGUMENTS", []string{"universal:packageBin"}, b.Logger) + + art := libbs.NewArtifactResolver("BP_SBT_BUILT_ARTIFACT", "BP_SBT_BUILT_MODULE", filepath.Join("target", "universal", "*.zip"), b.Logger) + + a, err := libbs.NewApplication(context.Application.Path, arg, art, c, command, result.Plan) if err != nil { return libcnb.BuildResult{}, fmt.Errorf("unable to create application layer\n%w", err) } diff --git a/sbt/build_test.go b/sbt/build_test.go index 778da4b..2af5515 100644 --- a/sbt/build_test.go +++ b/sbt/build_test.go @@ -24,6 +24,7 @@ import ( "github.com/buildpacks/libcnb" . "github.com/onsi/gomega" + "github.com/paketo-buildpacks/libbs" "github.com/paketo-buildpacks/sbt/sbt" "github.com/sclevine/spec" ) @@ -60,7 +61,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { Expect(result.Layers).To(HaveLen(2)) Expect(result.Layers[0].Name()).To(Equal("cache")) Expect(result.Layers[1].Name()).To(Equal("application")) - Expect(result.Layers[1].(sbt.Application).Command).To(Equal(filepath.Join(ctx.Application.Path, "sbt"))) + Expect(result.Layers[1].(libbs.Application).Command).To(Equal(filepath.Join(ctx.Application.Path, "sbt"))) }) it("contributes distribution", func() { @@ -82,7 +83,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { Expect(result.Layers[0].Name()).To(Equal("sbt")) Expect(result.Layers[1].Name()).To(Equal("cache")) Expect(result.Layers[2].Name()).To(Equal("application")) - Expect(result.Layers[2].(sbt.Application).Command).To(Equal(filepath.Join(ctx.Layers.Path, "sbt", "bin", "sbt"))) + Expect(result.Layers[2].(libbs.Application).Command).To(Equal(filepath.Join(ctx.Layers.Path, "sbt", "bin", "sbt"))) }) } diff --git a/sbt/cache.go b/sbt/cache.go deleted file mode 100644 index f1aea46..0000000 --- a/sbt/cache.go +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sbt - -import ( - "fmt" - "os" - "os/user" - "path/filepath" - - "github.com/buildpacks/libcnb" - "github.com/paketo-buildpacks/libpak/bard" -) - -type Cache struct { - Logger bard.Logger - Path string -} - -func NewCache() (Cache, error) { - u, err := user.Current() - if err != nil { - return Cache{}, fmt.Errorf("unable to determine user home directory\n%w", err) - } - - return Cache{Path: filepath.Join(u.HomeDir, ".sbt")}, nil -} - -func (c Cache) Contribute(layer libcnb.Layer) (libcnb.Layer, error) { - if err := os.MkdirAll(layer.Path, 0755); err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to create layer directory %s\n%w", layer.Path, err) - } - - file := filepath.Dir(c.Path) - if err := os.MkdirAll(file, 0755); err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to create directory %s\n%w", file, err) - } - - if err := os.Symlink(layer.Path, c.Path); os.IsExist(err) { - c.Logger.Body("Cache already exists") - } else if err != nil { - return libcnb.Layer{}, fmt.Errorf("unable to link cache from %s to %s\n%w", layer.Path, c.Path, err) - } else { - c.Logger.Bodyf("Creating cache directory %s", c.Path) - } - - layer.Cache = true - return layer, nil -} - -func (Cache) Name() string { - return "cache" -} diff --git a/sbt/cache_test.go b/sbt/cache_test.go deleted file mode 100644 index f843834..0000000 --- a/sbt/cache_test.go +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sbt_test - -import ( - "io/ioutil" - "os" - "path/filepath" - "testing" - - "github.com/buildpacks/libcnb" - . "github.com/onsi/gomega" - "github.com/paketo-buildpacks/sbt/sbt" - "github.com/sclevine/spec" -) - -func testCache(t *testing.T, context spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - ctx libcnb.BuildContext - path string - ) - - it.Before(func() { - var err error - - path, err = ioutil.TempDir("", "cache") - Expect(err).NotTo(HaveOccurred()) - - ctx.Layers.Path, err = ioutil.TempDir("", "cache-layers") - Expect(err).NotTo(HaveOccurred()) - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - Expect(os.RemoveAll(ctx.Layers.Path)).To(Succeed()) - }) - - it("symlinks to destination if it does not exist", func() { - file := filepath.Join(path, "test") - - layer, err := ctx.Layers.Layer("test-layer") - Expect(err).NotTo(HaveOccurred()) - - layer, err = sbt.Cache{Path: file}.Contribute(layer) - Expect(err).NotTo(HaveOccurred()) - - Expect(layer.Cache).To(BeTrue()) - - fi, err := os.Lstat(file) - Expect(err).NotTo(HaveOccurred()) - Expect(fi.Mode() & os.ModeSymlink).To(Equal(os.ModeSymlink)) - - Expect(os.Readlink(file)).To(Equal(layer.Path)) - }) -} diff --git a/sbt/init_test.go b/sbt/init_test.go index 4f89db9..e3c5c5d 100644 --- a/sbt/init_test.go +++ b/sbt/init_test.go @@ -25,9 +25,7 @@ import ( func TestUnit(t *testing.T) { suite := spec.New("sbt", spec.Report(report.Terminal{})) - suite("Application", testApplication) suite("Build", testBuild) - suite("Cache", testCache) suite("Detect", testDetect) suite("Distribution", testDistribution) suite.Run(t) diff --git a/sbt/testdata/stub-application.zip b/sbt/testdata/stub-application.zip deleted file mode 100644 index 941a44bb2b6bd9d477ed0c7b5b1f0f42bd6abd03..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 178 zcmWIWW@h1H0D*^wlLEjDD8a`d!;qF)QBqozs+*fwl$}}>8p6rI9QJos90-?Ia5FHn xd}U-{U=ac82=HcPl4HhYk_6n~C5<2^y1A?nb1}?hWdo^Z1i}y??E&I2008b-AK(B0