Skip to content

Commit

Permalink
Makes changes needed for 1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
ForestEckhardt committed Dec 4, 2024
1 parent a0b7152 commit 313fe38
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 397 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ file that looks like the following:
# The Dotnet Publish CNB does not support non-required metadata options.
```

## Configuration

### `BP_DOTNET_PROJECT_PATH`
To specify a project subdirectory to be used as the root of the app, please use
the `BP_DOTNET_PROJECT_PATH` environment variable at build time either directly
(e.g. `pack build my-app --env BP_DOTNET_PROJECT_PATH=./src/my-app`) or through a
[`project.toml` file](https://github.com/buildpacks/spec/blob/main/extensions/project-descriptor.md).

```shell
BP_DOTNET_PROJECT_PATH=./src/my-app
```

## Usage
To package this buildpack for consumption:
```
$ ./scripts/package.sh
```
This builds the buildpack's source using GOOS=linux by default. You can supply another value as the first argument to package.sh.

## Specifying a project path

To specify a project subdirectory to be used as the root of the app, please use
the BP_DOTNET_PROJECT_PATH environment variable at build time either directly
(e.g. pack build my-app --env BP_DOTNET_PROJECT_PATH=./src/my-app) or through a
project.toml file.
16 changes: 0 additions & 16 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"time"

"github.com/Masterminds/semver"
"github.com/Netflix/go-env"
"github.com/mattn/go-shellwords"
"github.com/paketo-buildpacks/packit/v2"
Expand Down Expand Up @@ -66,7 +65,6 @@ func Build(
symlinker SymlinkManager,
publishProcess PublishProcess,
slicer Slicer,
buildpackYMLParser BuildpackYMLParser,
clock chronos.Clock,
logger scribe.Emitter,
sbomGenerator SBOMGenerator,
Expand All @@ -84,20 +82,6 @@ func Build(
}
logger.Debug.Break()

if config.ProjectPath == "" {
var err error
config.ProjectPath, err = buildpackYMLParser.ParseProjectPath(filepath.Join(context.WorkingDir, "buildpack.yml"))
if err != nil {
return packit.BuildResult{}, err
}

if config.ProjectPath != "" {
nextMajorVersion := semver.MustParse(context.BuildpackInfo.Version).IncMajor()
logger.Subprocess("WARNING: Setting the project path through buildpack.yml will be deprecated soon in Dotnet Publish Buildpack v%s", nextMajorVersion.String())
logger.Subprocess("Please specify the project path through the $BP_DOTNET_PROJECT_PATH environment variable instead. See README.md or the documentation on paketo.io for more information.")
}
}

tempDir, err := os.MkdirTemp("", "dotnet-publish-output")
if err != nil {
return packit.BuildResult{}, fmt.Errorf("could not create temp directory: %w", err)
Expand Down
46 changes: 9 additions & 37 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
homeDir string
layersDir string

bindingResolver *fakes.BindingResolver
buildpackYMLParser *fakes.BuildpackYMLParser
publishProcess *fakes.PublishProcess
sbomGenerator *fakes.SBOMGenerator
slicer *fakes.Slicer
sourceRemover *fakes.SourceRemover
symlinker *fakes.SymlinkManager
logger scribe.Emitter
bindingResolver *fakes.BindingResolver
publishProcess *fakes.PublishProcess
sbomGenerator *fakes.SBOMGenerator
slicer *fakes.Slicer
sourceRemover *fakes.SourceRemover
symlinker *fakes.SymlinkManager
logger scribe.Emitter

build packit.BuildFunc
)
Expand All @@ -52,17 +51,12 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
homeDir, err = os.MkdirTemp("", "home-dir")
Expect(err).NotTo(HaveOccurred())

Expect(os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0600)).To(Succeed())

symlinker = &fakes.SymlinkManager{}
sourceRemover = &fakes.SourceRemover{}
publishProcess = &fakes.PublishProcess{}
bindingResolver = &fakes.BindingResolver{}
slicer = &fakes.Slicer{}

buildpackYMLParser = &fakes.BuildpackYMLParser{}
buildpackYMLParser.ParseProjectPathCall.Returns.ProjectFilePath = "some/project/path"

slicer.SliceCall.Returns.Pkgs = packit.Slice{Paths: []string{"some-package.dll"}}
slicer.SliceCall.Returns.EarlyPkgs = packit.Slice{Paths: []string{"some-release-candidate-package.dll"}}
slicer.SliceCall.Returns.Projects = packit.Slice{Paths: []string{"some-project.dll"}}
Expand All @@ -89,7 +83,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
symlinker,
publishProcess,
slicer,
buildpackYMLParser,
chronos.DefaultClock,
logger,
sbomGenerator,
Expand Down Expand Up @@ -183,19 +176,17 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(symlinker.UnlinkCall.CallCount).To(Equal(0))

Expect(publishProcess.ExecuteCall.Receives.WorkingDir).To(Equal(workingDir))
Expect(publishProcess.ExecuteCall.Receives.ProjectPath).To(Equal("some/project/path"))
Expect(publishProcess.ExecuteCall.Receives.ProjectPath).To(Equal(""))
Expect(publishProcess.ExecuteCall.Receives.OutputPath).To(MatchRegexp(`dotnet-publish-output\d+`))
Expect(publishProcess.ExecuteCall.Receives.Debug).To(BeTrue())
Expect(publishProcess.ExecuteCall.Receives.Flags).To(Equal([]string{"--publishflag", "value"}))

Expect(slicer.SliceCall.Receives.AssetsFile).To(Equal(filepath.Join(workingDir, "some/project/path", "obj", "project.assets.json")))
Expect(slicer.SliceCall.Receives.AssetsFile).To(Equal(filepath.Join(workingDir, "obj", "project.assets.json")))

Expect(sbomGenerator.GenerateCall.Receives.Dir).To(Equal(workingDir))

Expect(buffer.String()).To(ContainSubstring("Some Buildpack 0.0.1"))
Expect(buffer.String()).To(ContainSubstring("Executing build process"))
Expect(buffer.String()).To(ContainSubstring("WARNING: Setting the project path through buildpack.yml will be deprecated soon in Dotnet Publish Buildpack v1.0.0"))
Expect(buffer.String()).To(ContainSubstring("Please specify the project path through the $BP_DOTNET_PROJECT_PATH environment variable instead. See README.md or the documentation on paketo.io for more information."))
})

context("the cache layer is empty", func() {
Expand Down Expand Up @@ -288,7 +279,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
symlinker,
publishProcess,
slicer,
buildpackYMLParser,
chronos.DefaultClock,
logger,
sbomGenerator,
Expand Down Expand Up @@ -398,7 +388,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
symlinker,
publishProcess,
slicer,
buildpackYMLParser,
chronos.DefaultClock,
logger,
sbomGenerator,
Expand All @@ -424,22 +413,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

context("failure cases", func() {

context("when the buildpack.yml can not be parsed", func() {
it.Before(func() {
buildpackYMLParser.ParseProjectPathCall.Returns.Err = errors.New("some-error")
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
BuildpackInfo: packit.BuildpackInfo{
Version: "0.0.1",
},
})
Expect(err).To(MatchError("some-error"))
})
})

context("dotnet publish flags cannot be parsed", func() {
it.Before(func() {
build = dotnetpublish.Build(
Expand All @@ -450,7 +423,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
symlinker,
publishProcess,
slicer,
buildpackYMLParser,
chronos.DefaultClock,
logger,
sbomGenerator,
Expand Down
15 changes: 1 addition & 14 deletions detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,8 @@ type ProjectParser interface {
NPMIsRequired(path string) (bool, error)
}

//go:generate faux --interface BuildpackYMLParser --output fakes/buildpack_yml_parser.go
type BuildpackYMLParser interface {
ParseProjectPath(path string) (projectFilePath string, err error)
}

func Detect(config Configuration, parser ProjectParser, buildpackYMLParser BuildpackYMLParser) packit.DetectFunc {
func Detect(config Configuration, parser ProjectParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
if config.ProjectPath == "" {
var err error
config.ProjectPath, err = buildpackYMLParser.ParseProjectPath(filepath.Join(context.WorkingDir, "buildpack.yml"))
if err != nil {
return packit.DetectResult{}, fmt.Errorf("failed to parse buildpack.yml: %w", err)
}
}

projectFilePath, err := parser.FindProjectFile(filepath.Join(context.WorkingDir, config.ProjectPath))
if err != nil {
return packit.DetectResult{}, err
Expand Down
75 changes: 3 additions & 72 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dotnetpublish_test

import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -19,10 +18,9 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

projectParser *fakes.ProjectParser
buildpackYMLParser *fakes.BuildpackYMLParser
workingDir string
detect packit.DetectFunc
projectParser *fakes.ProjectParser
workingDir string
detect packit.DetectFunc
)

it.Before(func() {
Expand All @@ -34,12 +32,9 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
projectParser.FindProjectFileCall.Returns.String = filepath.Join(workingDir, "app.csproj")
projectParser.ParseVersionCall.Returns.String = "6.0.0"

buildpackYMLParser = &fakes.BuildpackYMLParser{}

detect = dotnetpublish.Detect(
dotnetpublish.Configuration{},
projectParser,
buildpackYMLParser,
)
})

Expand Down Expand Up @@ -76,7 +71,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
}))

Expect(buildpackYMLParser.ParseProjectPathCall.Receives.Path).To(Equal(filepath.Join(workingDir, "buildpack.yml")))
Expect(projectParser.FindProjectFileCall.Receives.Root).To(Equal(workingDir))
Expect(projectParser.ParseVersionCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expand Down Expand Up @@ -123,7 +117,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
}))

Expect(buildpackYMLParser.ParseProjectPathCall.Receives.Path).To(Equal(filepath.Join(workingDir, "buildpack.yml")))
Expect(projectParser.FindProjectFileCall.Receives.Root).To(Equal(workingDir))
Expect(projectParser.ParseVersionCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expand Down Expand Up @@ -178,7 +171,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
}))

Expect(buildpackYMLParser.ParseProjectPathCall.Receives.Path).To(Equal(filepath.Join(workingDir, "buildpack.yml")))
Expect(projectParser.FindProjectFileCall.Receives.Root).To(Equal(workingDir))
Expect(projectParser.ParseVersionCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expand All @@ -192,7 +184,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
detect = dotnetpublish.Detect(
dotnetpublish.Configuration{ProjectPath: "src/proj1"},
projectParser,
buildpackYMLParser,
)
})

Expand Down Expand Up @@ -229,53 +220,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
}))

Expect(buildpackYMLParser.ParseProjectPathCall.CallCount).To(Equal(0))
Expect(projectParser.FindProjectFileCall.Receives.Root).To(Equal(filepath.Join(workingDir, "src/proj1")))
Expect(projectParser.ParseVersionCall.Receives.Path).To(Equal(filepath.Join(workingDir, "src/proj1", "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "src/proj1", "app.csproj")))
Expect(projectParser.NPMIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "src/proj1", "app.csproj")))
})
})
context("when the .csproj file is not at the base of the directory and project_path is set in buildpack.yml", func() {
it.Before(func() {
buildpackYMLParser.ParseProjectPathCall.Returns.ProjectFilePath = "src/proj1"
projectParser.FindProjectFileCall.Returns.String = filepath.Join(workingDir, "src/proj1", "app.csproj")
})

it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})

it("finds the projfile and passes detection", func() {
result, err := detect(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: "dotnet-application"},
},
Requires: []packit.BuildPlanRequirement{
{
Name: "dotnet-sdk",
Metadata: dotnetpublish.BuildPlanMetadata{
Version: "6.0.*",
VersionSource: "app.csproj",
Build: true,
},
},
{
Name: "icu",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
},
},
}))

Expect(buildpackYMLParser.ParseProjectPathCall.Receives.Path).To(Equal(filepath.Join(workingDir, "buildpack.yml")))
Expect(projectParser.FindProjectFileCall.Receives.Root).To(Equal(filepath.Join(workingDir, "src/proj1")))
Expect(projectParser.ParseVersionCall.Receives.Path).To(Equal(filepath.Join(workingDir, "src/proj1", "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "src/proj1", "app.csproj")))
Expand All @@ -284,19 +228,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
})

context("failure cases", func() {
context("when buildpack.yml cannot be parsed", func() {
it.Before(func() {
buildpackYMLParser.ParseProjectPathCall.Returns.Err = fmt.Errorf("parsing error")
})

it("fails detection", func() {
_, err := detect(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).To(MatchError("failed to parse buildpack.yml: parsing error"))
})
})

context("when finding project file returns an error", func() {
it.Before(func() {
projectParser.FindProjectFileCall.Returns.Error = errors.New("some project file error")
Expand Down
37 changes: 0 additions & 37 deletions dotnet_buildpack_yml_parser.go

This file was deleted.

Loading

0 comments on commit 313fe38

Please sign in to comment.