Skip to content

Commit

Permalink
move executable.go into its own package,
Browse files Browse the repository at this point in the history
- port over cnb library from node-engine rewrite
- update tests a bit to remove os.Args bug
[#169918669]

Co-authored-by: Daniel Thornton <[email protected]>
  • Loading branch information
thitch97 and dwillist committed Nov 27, 2019
1 parent a77405d commit 0f20fa6
Show file tree
Hide file tree
Showing 29 changed files with 2,147 additions and 164 deletions.
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/packit.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package packit

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/BurntSushi/toml"
"github.com/cloudfoundry/packit/internal"
)

type BuildFunc func(BuildContext) (BuildResult, error)

type BuildContext struct {
CNBPath string
Stack string
WorkingDir string
Plan BuildpackPlan
Layers Layers
}

type BuildResult struct {
Plan BuildpackPlan
Layers []Layer
Processes []Process
}

type Process struct {
Type string `toml:"type"`
Command string `toml:"command"`
Args []string `toml:"args"`
Direct bool `toml:"direct"`
}

type BuildpackPlanEntry struct {
Name string `toml:"name"`
Version string `toml:"version"`
Metadata map[string]interface{} `toml:"metadata"`
}

type BuildpackPlan struct {
Entries []BuildpackPlanEntry `toml:"entries"`
}

func Build(f BuildFunc, options ...Option) {
config := Config{
exitHandler: internal.NewExitHandler(),
args: os.Args,
tomlWriter: internal.NewTOMLWriter(),
envWriter: internal.NewEnvironmentWriter(),
}

for _, option := range options {
config = option(config)
}

var (
layersPath = config.args[1]
planPath = config.args[3]
)

pwd, err := os.Getwd()
if err != nil {
config.exitHandler.Error(err)
return
}

var plan BuildpackPlan
_, err = toml.DecodeFile(planPath, &plan)
if err != nil {
config.exitHandler.Error(err)
return
}

result, err := f(BuildContext{
CNBPath: filepath.Clean(strings.TrimSuffix(config.args[0], filepath.Join("bin", "build"))),
Stack: os.Getenv("CNB_STACK_ID"),
WorkingDir: pwd,
Plan: plan,
Layers: Layers{
Path: layersPath,
},
})
if err != nil {
config.exitHandler.Error(err)
return
}

err = config.tomlWriter.Write(planPath, result.Plan)
if err != nil {
config.exitHandler.Error(err)
return
}

for _, layer := range result.Layers {
err = config.tomlWriter.Write(filepath.Join(layersPath, fmt.Sprintf("%s.toml", layer.Name)), layer)
if err != nil {
config.exitHandler.Error(err)
return
}

err = config.envWriter.Write(filepath.Join(layer.Path, "env"), layer.SharedEnv)
if err != nil {
config.exitHandler.Error(err)
return
}

err = config.envWriter.Write(filepath.Join(layer.Path, "env.launch"), layer.LaunchEnv)
if err != nil {
config.exitHandler.Error(err)
return
}

err = config.envWriter.Write(filepath.Join(layer.Path, "env.build"), layer.BuildEnv)
if err != nil {
config.exitHandler.Error(err)
return
}
}

if len(result.Processes) > 0 {
var launch struct {
Processes []Process `toml:"processes"`
}
launch.Processes = result.Processes

err = config.tomlWriter.Write(filepath.Join(layersPath, "launch.toml"), launch)
if err != nil {
config.exitHandler.Error(err)
return
}
}
}
Loading

0 comments on commit 0f20fa6

Please sign in to comment.