Skip to content

Commit

Permalink
Add support for project descriptor v0.2
Browse files Browse the repository at this point in the history
Signed-off-by: Sambhav Kothari <skothari44@bloomberg.net>
  • Loading branch information
sambhav authored and Tyler Phelan committed Aug 24, 2021
1 parent 0c846f7 commit d873401
Show file tree
Hide file tree
Showing 2 changed files with 362 additions and 145 deletions.
77 changes: 56 additions & 21 deletions pkg/cnb/project_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ import (

const defaultProjectDescriptorPath = "project.toml"

func ProcessProjectDescriptor(appDir, descriptorPath, platformDir string, logger *log.Logger) error {
var (
d descriptor
)

file := filepath.Join(appDir, defaultProjectDescriptorPath)
func ProcessProjectDescriptor(appDir,descriptorPath, platformDir string, logger *log.Logger) error {
filePath := filepath.Join(appDir, defaultProjectDescriptorPath)
if descriptorPath != "" {
file = filepath.Join(appDir, descriptorPath)
filePath = filepath.Join(appDir, descriptorPath)
}

if _, err := os.Stat(file); os.IsNotExist(err) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
if descriptorPath != "" {
return fmt.Errorf("project descriptor path set but no file found: %s", descriptorPath)
}
Expand All @@ -31,24 +26,50 @@ func ProcessProjectDescriptor(appDir, descriptorPath, platformDir string, logger
return fmt.Errorf("unable to determine if project descriptor file exists: %w", err)
}

_, err := toml.DecodeFile(file, &d)
d, err := parseProjectDescriptor(filePath, logger)
if err != nil {
return err
}
if d.Build.Buildpacks != nil {
if d.Buildpacks != nil {
logger.Println("info: buildpacks provided in project descriptor file will be ignored")
}

if d.Build.Builder != "" {
if d.Builder != "" {
logger.Println("info: builder provided in project descriptor file will be ignored")
}
if err := processFiles(appDir, d); err != nil {
return err
}
return serializeEnvVars(d.Build.Env, platformDir)
return serializeEnvVars(d.Env, platformDir)
}

func processFiles(appDir string, d descriptor) error {
func parseProjectDescriptor(filePath string, logger *log.Logger) (build, error) {
var dv2 descriptorV2
if _, err := toml.DecodeFile(filePath, &dv2); err != nil {
return build{}, err
}

if dv2.Project.SchemaVersion != "" {
if dv2.Project.SchemaVersion == "0.2" {
// Normalizing the buildpacks table to a common schema
dv2.IO.Buildpacks.Buildpacks = dv2.IO.Buildpacks.Group
dv2.IO.Buildpacks.Group = nil
return dv2.IO.Buildpacks, nil
} else {
logger.Println(fmt.Sprintf("warning: project descriptor version %s is unsupported and %s will be ignored", dv2.Project.SchemaVersion, filePath))
return build{}, nil
}
}
var d descriptor
if _, err := toml.DecodeFile(filePath, &d); err != nil {
return build{}, err
}
// Removing groups from v1 descriptor if it exists
d.Build.Group = nil
return d.Build, nil
}

func processFiles(appDir string, d build) error {
fileFilter, err := getFileFilter(d)
if err != nil {
return err
Expand Down Expand Up @@ -77,19 +98,19 @@ func processFiles(appDir string, d descriptor) error {
})
}

func getFileFilter(d descriptor) (func(string) bool, error) {
if d.Build.Exclude != nil && d.Build.Include != nil {
return nil, fmt.Errorf("%s: cannot have both include and exclude defined", defaultProjectDescriptorPath)
func getFileFilter(d build) (func(string) bool, error) {
if d.Exclude != nil && d.Include != nil {
return nil, fmt.Errorf("project descriptor cannot have both include and exclude defined")
}

if len(d.Build.Exclude) > 0 {
excludes := ignore.CompileIgnoreLines(d.Build.Exclude...)
if len(d.Exclude) > 0 {
excludes := ignore.CompileIgnoreLines(d.Exclude...)
return func(fileName string) bool {
return !excludes.MatchesPath(fileName)
}, nil
}
if len(d.Build.Include) > 0 {
includes := ignore.CompileIgnoreLines(d.Build.Include...)
if len(d.Include) > 0 {
includes := ignore.CompileIgnoreLines(d.Include...)
return includes.MatchesPath, nil
}

Expand All @@ -106,10 +127,24 @@ type build struct {
Include []string `toml:"include"`
Exclude []string `toml:"exclude"`
Buildpacks []buildpack `toml:"buildpacks"`
Group []buildpack `toml:"group"`
Builder string `toml:"builder"`
Env []envVariable `toml:"env"`
}

type descriptor struct {
Build build `toml:"build"`
}

type descriptorV2 struct {
Project project `toml:"_"`
IO ioTable `toml:"io"`
}

type project struct {
SchemaVersion string `toml:"schema-version"`
}

type ioTable struct {
Buildpacks build `toml:"buildpacks"`
}
Loading

0 comments on commit d873401

Please sign in to comment.