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

Add support for Project Descriptor 0.2 #1241

Merged
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
8 changes: 4 additions & 4 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/logging"
"github.com/buildpacks/pack/pkg/archive"
"github.com/buildpacks/pack/project"
projectCommon "github.com/buildpacks/pack/project/common"
Copy link
Contributor Author

@YousefHaggyHeroku YousefHaggyHeroku Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a better name for this 🤷 , maybe just common? thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong feeling over here!

)

const (
Expand Down Expand Up @@ -151,7 +151,7 @@ type BuildOptions struct {
ProjectDescriptorBaseDir string

// ProjectDescriptor describes the project and any configuration specific to the project
ProjectDescriptor project.Descriptor
ProjectDescriptor projectCommon.Descriptor

// The lifecycle image that will be used for the analysis, restore and export phases
// when using an untrusted builder.
Expand Down Expand Up @@ -368,7 +368,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
return c.logImageNameAndSha(ctx, opts.Publish, imageRef)
}

func getFileFilter(descriptor project.Descriptor) (func(string) bool, error) {
func getFileFilter(descriptor projectCommon.Descriptor) (func(string) bool, error) {
if len(descriptor.Build.Exclude) > 0 {
excludes := ignore.CompileIgnoreLines(descriptor.Build.Exclude...)
return func(fileName string) bool {
Expand Down Expand Up @@ -920,7 +920,7 @@ func parseDigestFromImageID(id imgutil.Identifier) string {
return fmt.Sprintf("sha256:%s", digest)
}

func createInlineBuildpack(bp project.Buildpack, stackID string) (string, error) {
func createInlineBuildpack(bp projectCommon.Buildpack, stackID string) (string, error) {
pathToInlineBuilpack, err := ioutil.TempDir("", "inline-cnb")
if err != nil {
return pathToInlineBuilpack, err
Expand Down
40 changes: 20 additions & 20 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
ilogging "github.com/buildpacks/pack/internal/logging"
rg "github.com/buildpacks/pack/internal/registry"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/project"
projectCommon "github.com/buildpacks/pack/project/common"
h "github.com/buildpacks/pack/testhelpers"
)

Expand Down Expand Up @@ -1237,9 +1237,9 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
Image: "some/app",
Builder: defaultBuilderName,
ClearCache: true,
ProjectDescriptor: project.Descriptor{
Build: project.Build{
Buildpacks: []project.Buildpack{{
ProjectDescriptor: projectCommon.Descriptor{
Build: projectCommon.Build{
Buildpacks: []projectCommon.Buildpack{{
URI: server.URL(),
}},
},
Expand Down Expand Up @@ -1303,11 +1303,11 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
Image: "some/app",
Builder: defaultBuilderName,
ClearCache: true,
ProjectDescriptor: project.Descriptor{
Build: project.Build{
Buildpacks: []project.Buildpack{{
ProjectDescriptor: projectCommon.Descriptor{
Build: projectCommon.Build{
Buildpacks: []projectCommon.Buildpack{{
ID: "my/inline",
Script: project.Script{
Script: projectCommon.Script{
API: "0.4",
Inline: "touch foo.txt",
},
Expand Down Expand Up @@ -1338,11 +1338,11 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
Image: "some/app",
Builder: defaultBuilderName,
ClearCache: true,
ProjectDescriptor: project.Descriptor{
Build: project.Build{
Buildpacks: []project.Buildpack{{
ProjectDescriptor: projectCommon.Descriptor{
Build: projectCommon.Build{
Buildpacks: []projectCommon.Buildpack{{
ID: "my/inline",
Script: project.Script{
Script: projectCommon.Script{
Inline: "touch foo.txt",
},
}},
Expand All @@ -1359,10 +1359,10 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
Image: "some/app",
Builder: defaultBuilderName,
ClearCache: true,
ProjectDescriptor: project.Descriptor{
Build: project.Build{
Buildpacks: []project.Buildpack{{
Script: project.Script{
ProjectDescriptor: projectCommon.Descriptor{
Build: projectCommon.Build{
Buildpacks: []projectCommon.Buildpack{{
Script: projectCommon.Script{
API: "0.4",
Inline: "touch foo.txt",
},
Expand All @@ -1380,12 +1380,12 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
Image: "some/app",
Builder: defaultBuilderName,
ClearCache: true,
ProjectDescriptor: project.Descriptor{
Build: project.Build{
Buildpacks: []project.Buildpack{{
ProjectDescriptor: projectCommon.Descriptor{
Build: projectCommon.Build{
Buildpacks: []projectCommon.Buildpack{{
ID: "buildpack.1.id",
Version: "buildpack.1.version",
Script: project.Script{
Script: projectCommon.Script{
Inline: "touch foo.txt",
},
}},
Expand Down
7 changes: 4 additions & 3 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/logging"
"github.com/buildpacks/pack/project"
projectCommon "github.com/buildpacks/pack/project/common"
)

type BuildFlags struct {
Expand Down Expand Up @@ -253,7 +254,7 @@ func addEnvVar(env map[string]string, item string) map[string]string {
return env
}

func parseProjectToml(appPath, descriptorPath string) (project.Descriptor, string, error) {
func parseProjectToml(appPath, descriptorPath string) (projectCommon.Descriptor, string, error) {
actualPath := descriptorPath
computePath := descriptorPath == ""

Expand All @@ -263,9 +264,9 @@ func parseProjectToml(appPath, descriptorPath string) (project.Descriptor, strin

if _, err := os.Stat(actualPath); err != nil {
if computePath {
return project.Descriptor{}, "", nil
return projectCommon.Descriptor{}, "", nil
}
return project.Descriptor{}, "", errors.Wrap(err, "stat project descriptor")
return projectCommon.Descriptor{}, "", errors.Wrap(err, "stat project descriptor")
}

descriptor, err := project.ReadProjectDescriptor(actualPath)
Expand Down
37 changes: 21 additions & 16 deletions internal/commands/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"reflect"
"testing"

"github.com/buildpacks/lifecycle/api"

pubcfg "github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/project"
projectCommon "github.com/buildpacks/pack/project/common"

"github.com/golang/mock/gomock"
"github.com/heroku/color"
Expand Down Expand Up @@ -501,16 +503,17 @@ version = "1.0"

it("should build an image with configuration in descriptor", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(project.Descriptor{
Project: project.Project{
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(projectCommon.Descriptor{
Project: projectCommon.Project{
Name: "Sample",
},
Build: project.Build{
Buildpacks: []project.Buildpack{{
Build: projectCommon.Build{
Buildpacks: []projectCommon.Buildpack{{
ID: "example/lua",
Version: "1.0",
}},
},
SchemaVersion: api.MustParse("0.1"),
})).
Return(nil)

Expand Down Expand Up @@ -594,20 +597,21 @@ builder = "my-builder"

it("should use project.toml in source repo", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(project.Descriptor{
Project: project.Project{
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(projectCommon.Descriptor{
Project: projectCommon.Project{
Name: "Sample",
},
Build: project.Build{
Buildpacks: []project.Buildpack{{
Build: projectCommon.Build{
Buildpacks: []projectCommon.Buildpack{{
ID: "example/lua",
Version: "1.0",
}},
Env: []project.EnvVar{{
Env: []projectCommon.EnvVar{{
Name: "KEY1",
Value: "VALUE1",
}},
},
SchemaVersion: api.MustParse("0.1"),
})).
Return(nil)

Expand Down Expand Up @@ -637,20 +641,21 @@ builder = "my-builder"

it("should use specified descriptor", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(project.Descriptor{
Project: project.Project{
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(projectCommon.Descriptor{
Project: projectCommon.Project{
Name: "Sample",
},
Build: project.Build{
Buildpacks: []project.Buildpack{{
Build: projectCommon.Build{
Buildpacks: []projectCommon.Buildpack{{
ID: "example/lua",
Version: "1.0",
}},
Env: []project.EnvVar{{
Env: []projectCommon.EnvVar{{
Name: "KEY1",
Value: "VALUE1",
}},
},
SchemaVersion: api.MustParse("0.1"),
})).
Return(nil)

Expand Down Expand Up @@ -803,7 +808,7 @@ func EqBuildOptionsWithAdditionalTags(additionalTags []string) gomock.Matcher {
}
}

func EqBuildOptionsWithProjectDescriptor(descriptor project.Descriptor) gomock.Matcher {
func EqBuildOptionsWithProjectDescriptor(descriptor projectCommon.Descriptor) gomock.Matcher {
return buildOptionsMatcher{
description: fmt.Sprintf("Descriptor=%s", descriptor),
equals: func(o pack.BuildOptions) bool {
Expand Down
48 changes: 48 additions & 0 deletions project/common/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package common

import (
"github.com/buildpacks/lifecycle/api"

"github.com/buildpacks/pack/internal/dist"
)

type Script struct {
API string `toml:"api"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are internal only representations right? Meaning these won't be Marshalled(), Unmarshalled() to the filesystem?

If so, then these toml:"..." annotations won't be used.

Copy link
Contributor Author

@YousefHaggyHeroku YousefHaggyHeroku Aug 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are meant to be primarily internal representations. However, currently v01/package.go and v02/package.go reference these structs for Unmarshalling some of the unchanged attributes (ex: v02 uses common.Buildpack).

I'm not sure whether we should just copy over the structs and remove struct tags from common.go or just leave as is, I'm comfortable with either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm comfortable straddling both sides and treating common primarily as the internal representations with the (un)marshalling until there is a deviation between different versions.

Inline string `toml:"inline"`
Shell string `toml:"shell"`
}
type Buildpack struct {
ID string `toml:"id"`
Version string `toml:"version"`
URI string `toml:"uri"`
Script Script `toml:"script"`
}

type EnvVar struct {
Name string `toml:"name"`
Value string `toml:"value"`
}

type Build struct {
Include []string `toml:"include"`
Exclude []string `toml:"exclude"`
Buildpacks []Buildpack `toml:"buildpacks"`
Env []EnvVar `toml:"env"`
Builder string `toml:"builder"`
}

type Project struct {
Name string `toml:"name"`
Licenses []dist.License `toml:"licenses"`
}

type Descriptor struct {
Project Project `toml:"project"`
Build Build `toml:"build"`
Metadata map[string]interface{} `toml:"metadata"`
SchemaVersion *api.Version
}

type ProjectDescriptorSchema interface {
DescriptorFromToml(projectTomlContents string) (Descriptor, error)
}
Loading