Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
Rewrite per RFC 0001 (#60)
Browse files Browse the repository at this point in the history
Signed-off-by: Arjun Sreedharan <[email protected]>
  • Loading branch information
arjun024 authored Jul 21, 2020
1 parent 81c3d69 commit f2c33a8
Show file tree
Hide file tree
Showing 67 changed files with 1,295 additions and 2,115 deletions.
Empty file added .packit
Empty file.
37 changes: 14 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Dep Cloud Native Buildpack

The Go Dep CNB builds a Go application binary, using the
[`dep`](https://golang.github.io/dep/docs/introduction.html) tool to package
dependencies.
The Dep CNB provides the
[`dep`](https://golang.github.io/dep/docs/introduction.html) executable. The
buildpack installs dep onto the `$PATH` which makes it available for subsequent
buildpacks and/or the final container image.

## Integration

The Dep CNB provides dep as a dependency. Downstream
buildpacks can require the node dependency by generating a [Build Plan
The Dep CNB provides `dep` as a dependency. Downstream
buildpacks can require the dep dependency by generating a [Build Plan
TOML](https://github.com/buildpacks/spec/blob/master/buildpack.md#build-plan-toml)
file that looks like the following:

Expand All @@ -26,10 +27,16 @@ file that looks like the following:
[requires.metadata]

# Setting the build flag to true will ensure that the Dep
# depdendency is available on the $PATH for subsequent buildpacks during
# dependency is available on the $PATH for subsequent buildpacks during
# their build phase. If you are writing a buildpack that needs to run Dep
# during its build process, this flag should be set to true.
build = true

# Setting the launch flag to true will ensure that the Dep
# dependency is available on the $PATH for the running application. If you are
# writing an application that needs to run Dep at runtime, this flag should
# be set to true.
launch = true
```

## Usage
Expand All @@ -42,20 +49,4 @@ This builds the buildpack's Go source using GOOS=linux by default. You can suppl

## `buildpack.yml` Configuration

The `dep` requires a `buildpack.yml` file in the root of the application directory, and must contain the `import-path` directive.

```yaml
go:
# this sets the go import-path (required)
import-path: hubgit.net/user/app

# this allows you to override the location of the main package of the app
targets: ["./cmd/web"]

# this allows you to set Go ldflags for compilation
ldflags:
main.version: v1.2.3
main.sha: 1234567
```
See `integration/testdata/` subfolders for examples.
The dep buildpack does not support configurations via `buildpack.yml`.
105 changes: 105 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package dep

import (
"path/filepath"
"time"

"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/chronos"
"github.com/paketo-buildpacks/packit/postal"
)

//go:generate faux --interface EntryResolver --output fakes/entry_resolver.go
type EntryResolver interface {
Resolve([]packit.BuildpackPlanEntry) packit.BuildpackPlanEntry
}

//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
Install(dependency postal.Dependency, cnbPath, layerPath string) error
}

//go:generate faux --interface BuildPlanRefinery --output fakes/build_plan_refinery.go
type BuildPlanRefinery interface {
BillOfMaterials(postal.Dependency) packit.BuildpackPlanEntry
}

func Build(
entries EntryResolver,
dependencies DependencyManager,
planRefinery BuildPlanRefinery,
clock chronos.Clock,
logger LogEmitter,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)

entry := entries.Resolve(context.Plan.Entries)

dependency, err := dependencies.Resolve(
filepath.Join(context.CNBPath, "buildpack.toml"),
entry.Name,
entry.Version,
context.Stack)
if err != nil {
return packit.BuildResult{}, err
}

bom := planRefinery.BillOfMaterials(dependency)

depLayer, err := context.Layers.Get(Dep)
if err != nil {
return packit.BuildResult{}, err
}

depLayer.Launch = entry.Metadata["launch"] == true
depLayer.Build = entry.Metadata["build"] == true
depLayer.Cache = entry.Metadata["build"] == true

cachedSHA, ok := depLayer.Metadata[DependencyCacheKey].(string)
if ok && cachedSHA == dependency.SHA256 {
logger.Process("Reusing cached layer %s", depLayer.Path)
logger.Break()

return packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{bom},
},
Layers: []packit.Layer{depLayer},
}, nil
}

logger.Process("Executing build process")

err = depLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}

logger.Subprocess("Installing Dep")

duration, err := clock.Measure(func() error {
return dependencies.Install(dependency, context.CNBPath, depLayer.Path)
})
if err != nil {
return packit.BuildResult{}, err
}

logger.Action("Completed in %s", duration.Round(time.Millisecond))

depLayer.Metadata = map[string]interface{}{
DependencyCacheKey: dependency.SHA256,
"built_at": clock.Now().Format(time.RFC3339Nano),
}

return packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{bom},
},
Layers: []packit.Layer{
depLayer,
},
}, nil
}
}
Loading

0 comments on commit f2c33a8

Please sign in to comment.