Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop requiring .NET runtime / ASP.NET during build #330

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import (
"os"
"path/filepath"

"github.com/Masterminds/semver"
"github.com/paketo-buildpacks/packit/v2"
)

type BuildPlanMetadata struct {
Version string `toml:"version,omitempty"`
Build bool `toml:"build"`
Launch bool `toml:"launch"`
Version string `toml:"version,omitempty"`
VersionSource string `toml:"version-source,omitempty"`
Build bool `toml:"build"`
Launch bool `toml:"launch"`
}

//go:generate faux --interface ProjectParser --output fakes/project_parser.go
type ProjectParser interface {
FindProjectFile(root string) (string, error)
ASPNetIsRequired(path string) (bool, error)
ParseVersion(path string) (string, error)
NodeIsRequired(path string) (bool, error)
NPMIsRequired(path string) (bool, error)
}
Expand Down Expand Up @@ -49,17 +51,23 @@ func Detect(parser ProjectParser, buildpackYMLParser BuildpackYMLParser) packit.
return packit.DetectResult{}, packit.Fail.WithMessage("no project file found")
}

version, err := parser.ParseVersion(projectFilePath)
if err != nil {
return packit.DetectResult{}, err
}

semver, err := semver.NewVersion(version)
if err != nil {
return packit.DetectResult{}, err
}

requirements := []packit.BuildPlanRequirement{
{
Name: "dotnet-sdk",
Metadata: BuildPlanMetadata{
Build: true,
},
},
{
Name: "dotnet-runtime",
Metadata: BuildPlanMetadata{
Build: true,
Build: true,
Version: fmt.Sprintf("%d.%d.*", semver.Major(), semver.Minor()),
VersionSource: filepath.Base(projectFilePath),
},
},
{
Expand All @@ -70,20 +78,6 @@ func Detect(parser ProjectParser, buildpackYMLParser BuildpackYMLParser) packit.
},
}

aspNetReq, err := parser.ASPNetIsRequired(projectFilePath)
if err != nil {
return packit.DetectResult{}, err
}

if aspNetReq {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "dotnet-aspnetcore",
Metadata: BuildPlanMetadata{
Build: true,
},
})
}

nodeReq, err := parser.NodeIsRequired(projectFilePath)
if err != nil {
return packit.DetectResult{}, err
Expand Down
119 changes: 24 additions & 95 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {

projectParser = &fakes.ProjectParser{}
projectParser.FindProjectFileCall.Returns.String = filepath.Join(workingDir, "app.csproj")
projectParser.ParseVersionCall.Returns.String = "3.1.0"

buildpackYMLParser = &fakes.BuildpackYMLParser{}

Expand All @@ -56,13 +57,9 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
{
Name: "dotnet-sdk",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
{
Name: "dotnet-runtime",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
Version: "3.1.*",
VersionSource: "app.csproj",
Build: true,
},
},
{
Expand All @@ -77,63 +74,11 @@ 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.ASPNetIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.ParseVersionCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NPMIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
})

context("when aspnet is required", func() {
it.Before(func() {
projectParser.ASPNetIsRequiredCall.Returns.Bool = true
})

it("requires aspnet in the build plan", 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{
Build: true,
},
},
{
Name: "dotnet-runtime",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
{
Name: "icu",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
{
Name: "dotnet-aspnetcore",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
},
},
}))

Expect(buildpackYMLParser.ParseProjectPathCall.Receives.Path).To(Equal(filepath.Join(workingDir, "buildpack.yml")))
Expect(projectParser.FindProjectFileCall.Receives.Root).To(Equal(workingDir))
Expect(projectParser.ASPNetIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NPMIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
})
})

context("when node is required", func() {
it.Before(func() {
projectParser.NodeIsRequiredCall.Returns.Bool = true
Expand All @@ -153,13 +98,9 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
{
Name: "dotnet-sdk",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
{
Name: "dotnet-runtime",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
Version: "3.1.*",
VersionSource: "app.csproj",
Build: true,
},
},
{
Expand All @@ -180,7 +121,7 @@ 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.ASPNetIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.ParseVersionCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NPMIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
})
Expand All @@ -206,13 +147,9 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
{
Name: "dotnet-sdk",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
{
Name: "dotnet-runtime",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
Version: "3.1.*",
VersionSource: "app.csproj",
Build: true,
},
},
{
Expand All @@ -239,7 +176,7 @@ 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.ASPNetIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.ParseVersionCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NodeIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
Expect(projectParser.NPMIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "app.csproj")))
})
Expand Down Expand Up @@ -270,13 +207,9 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
{
Name: "dotnet-sdk",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
{
Name: "dotnet-runtime",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
Version: "3.1.*",
VersionSource: "app.csproj",
Build: true,
},
},
{
Expand All @@ -291,7 +224,7 @@ 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.ASPNetIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "src/proj1", "app.csproj")))
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")))
})
Expand Down Expand Up @@ -320,13 +253,9 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
{
Name: "dotnet-sdk",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
},
},
{
Name: "dotnet-runtime",
Metadata: dotnetpublish.BuildPlanMetadata{
Build: true,
Version: "3.1.*",
VersionSource: "app.csproj",
Build: true,
},
},
{
Expand All @@ -341,7 +270,7 @@ 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(filepath.Join(workingDir, "src/proj1")))
Expect(projectParser.ASPNetIsRequiredCall.Receives.Path).To(Equal(filepath.Join(workingDir, "src/proj1", "app.csproj")))
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")))
})
Expand Down Expand Up @@ -387,14 +316,14 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
})
})

context("when parsing for ASPNet errors", func() {
context("when parsing for SDK version errors", func() {
it.Before(func() {
projectParser.ASPNetIsRequiredCall.Returns.Error = errors.New("parsing-error")
projectParser.ParseVersionCall.Returns.Error = errors.New("parsing-version-error")
})

it("errors", func() {
_, err := detect(packit.DetectContext{WorkingDir: workingDir})
Expect(err).To(MatchError("parsing-error"))
Expect(err).To(MatchError("parsing-version-error"))
})
})

Expand Down
40 changes: 22 additions & 18 deletions dotnet_project_file_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package dotnetpublish

import (
"encoding/xml"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)

Expand Down Expand Up @@ -39,40 +41,42 @@ func (p ProjectFileParser) FindProjectFile(path string) (string, error) {
return "", nil
}

func (p ProjectFileParser) ASPNetIsRequired(path string) (bool, error) {
func (p ProjectFileParser) ParseVersion(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return false, fmt.Errorf("failed to open %s: %w", path, err)
return "", fmt.Errorf("failed to read project file: %w", err)
}
defer file.Close()

var project struct {
SDK string `xml:"Sdk,attr"`
ItemGroups []struct {
PackageReferences []struct {
Include string `xml:"Include,attr"`
Version string `xml:"Version,attr"`
} `xml:"PackageReference"`
} `xml:"ItemGroup"`
PropertyGroups []struct {
RuntimeFrameworkVersion string
TargetFramework string
} `xml:"PropertyGroup"`
}

err = xml.NewDecoder(file).Decode(&project)
if err != nil {
return false, fmt.Errorf("failed to decode %s: %w", path, err)
return "", fmt.Errorf("failed to parse project file: %w", err)
}

if project.SDK == "Microsoft.NET.Sdk.Web" {
return true, nil
for _, group := range project.PropertyGroups {
if group.RuntimeFrameworkVersion != "" {
return group.RuntimeFrameworkVersion, nil
}
}

for _, ig := range project.ItemGroups {
for _, pr := range ig.PackageReferences {
if pr.Include == "Microsoft.AspNetCore.App" || pr.Include == "Microsoft.AspNetCore.All" {
return true, nil
}
// This regular expression matches on 'net<x>.<y>',
// 'net<x>.<y>-<platform>' & 'netcoreapp<x>.<y>'
targetFrameworkRe := regexp.MustCompile(`net(?:coreapp)?(?:(\d\.\d)(?:\-?\w+)?)$`)
for _, group := range project.PropertyGroups {
matches := targetFrameworkRe.FindStringSubmatch(group.TargetFramework)
if len(matches) == 2 {
return fmt.Sprintf("%s.0", matches[1]), nil
}
}
return false, nil

return "", errors.New("failed to find version in project file: missing or invalid TargetFramework property")
}

func (p ProjectFileParser) NodeIsRequired(path string) (bool, error) {
Expand Down
Loading