forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move executable.go into its own package,
- 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
Showing
29 changed files
with
2,147 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.