This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Arjun Sreedharan <[email protected]>
- Loading branch information
Showing
67 changed files
with
1,295 additions
and
2,115 deletions.
There are no files selected for viewing
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
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,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 | ||
} | ||
} |
Oops, something went wrong.