Skip to content

Commit

Permalink
Remove top-level version from BOM
Browse files Browse the repository at this point in the history
resolves: buildpacks#357

Signed-off-by: Yael Harel <yharel@vmware.com>
  • Loading branch information
Yael Harel committed Aug 5, 2020
1 parent 2462fd4 commit 3ac2eda
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
36 changes: 34 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package lifecycle

import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/buildpacks/lifecycle/api"

"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"

"github.com/BurntSushi/toml"

"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/layers"
)
Expand All @@ -19,6 +21,7 @@ type Builder struct {
LayersDir string
PlatformDir string
BuildpacksDir string
PlatformAPI *api.Version
Env BuildEnv
Group BuildpackGroup
Plan BuildPlan
Expand Down Expand Up @@ -143,6 +146,18 @@ func (b *Builder) Build() (*BuildMetadata, error) {
labels = append(labels, launch.Labels...)
}

comparisonVersion := api.MustParse("0.4")
if b.PlatformAPI.Compare(comparisonVersion) < 0 {
//plaformApiVersion is less than comparisonVersion
for _, bomEntry := range bom {
bomEntry.convertMetadataToVersion()
}
} else {
for _, bomEntry := range bom {
bomEntry.convertVersionToMetadata()
}
}

return &BuildMetadata{
BOM: bom,
Buildpacks: b.Group.Group,
Expand Down Expand Up @@ -191,6 +206,23 @@ func (p buildpackPlan) has(entry BuildPlanEntry) bool {
return false
}

func (bom *BOMEntry) convertMetadataToVersion() {
if version, ok := bom.Metadata["version"]; ok {
bom.Version = fmt.Sprintf("%v", version)
delete(bom.Metadata, "version")
}
}

func (bom *BOMEntry) convertVersionToMetadata() {
if bom.Version != "" {
if bom.Metadata == nil {
bom.Metadata = make(map[string]interface{})
}
bom.Metadata["version"] = bom.Version
bom.Version = ""
}
}

func setupEnv(env BuildEnv, layersDir string) error {
if err := eachDir(layersDir, func(path string) error {
if !isBuild(path + ".toml") {
Expand Down
2 changes: 2 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lifecycle_test
import (
"bytes"
"fmt"
"github.com/buildpacks/lifecycle/api"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -66,6 +67,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
LayersDir: layersDir,
PlatformDir: platformDir,
BuildpacksDir: buildpacksDir,
PlatformAPI: api.MustParse("0.3"),
Env: env,
Group: lifecycle.BuildpackGroup{
Group: []lifecycle.Buildpack{
Expand Down
3 changes: 3 additions & 0 deletions cmd/lifecycle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"github.com/buildpacks/lifecycle/api"
"log"
"os"

Expand All @@ -27,6 +28,7 @@ type buildArgs struct {
layersDir string
appDir string
platformDir string
platformAPI string
}

func (b *buildCmd) Init() {
Expand Down Expand Up @@ -70,6 +72,7 @@ func (ba buildArgs) build(group lifecycle.BuildpackGroup, plan lifecycle.BuildPl
LayersDir: ba.layersDir,
PlatformDir: ba.platformDir,
BuildpacksDir: ba.buildpacksDir,
PlatformAPI: api.MustParse(ba.platformAPI),
Env: env.NewBuildEnv(os.Environ()),
Group: group,
Plan: plan,
Expand Down
2 changes: 1 addition & 1 deletion cmd/lifecycle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
case "restorer":
cmd.Run(&restoreCmd{}, false)
case "builder":
cmd.Run(&buildCmd{}, false)
cmd.Run(&buildCmd{buildArgs: buildArgs{platformAPI: platformAPI}}, false)
case "exporter":
cmd.Run(&exportCmd{exportArgs: exportArgs{platformAPI: platformAPI}}, false)
case "rebaser":
Expand Down
2 changes: 1 addition & 1 deletion detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (be BuildPlanEntry) noOpt() BuildPlanEntry {

type Require struct {
Name string `toml:"name" json:"name"`
Version string `toml:"version" json:"version"`
Version string `toml:"version,omitempty" json:"version,omitempty"`
Metadata map[string]interface{} `toml:"metadata" json:"metadata"`
}

Expand Down

0 comments on commit 3ac2eda

Please sign in to comment.