Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of PackageContext and SubpackageContext #819

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,8 @@ func (b *Build) PopulateWorkspace(ctx context.Context) error {
})
}

func (sp SubpackageContext) ShouldRun(pb *PipelineBuild) (bool, error) {
if sp.Subpackage.If == "" {
func (pb *PipelineBuild) ShouldRun(sp config.Subpackage) (bool, error) {
if sp.If == "" {
return true, nil
}

Expand All @@ -936,7 +936,7 @@ func (sp SubpackageContext) ShouldRun(pb *PipelineBuild) (bool, error) {
return mutated[nk], nil
}

result, err := cond.Evaluate(sp.Subpackage.If, lookupWith)
result, err := cond.Evaluate(sp.If, lookupWith)
if err != nil {
return false, fmt.Errorf("evaluating subpackage if-conditional: %w", err)
}
Expand All @@ -955,7 +955,8 @@ func (b *Build) BuildPackage(ctx context.Context) error {

b.Summarize()

pkg := NewPackageContext(&b.Configuration.Package)
pkg := &b.Configuration.Package

pb := PipelineBuild{
Build: b,
Package: pkg,
Expand All @@ -979,9 +980,9 @@ func (b *Build) BuildPackage(ctx context.Context) error {
}

for _, spkg := range b.Configuration.Subpackages {
spkgctx := NewSubpackageContext(&spkg)
pb.Subpackage = spkgctx
for _, p := range spkgctx.Subpackage.Pipeline {
spkg := spkg
pb.Subpackage = &spkg
for _, p := range spkg.Pipeline {
pctx := NewPipelineContext(&p, b.Logger)
if err := pctx.ApplyNeeds(&pb); err != nil {
return fmt.Errorf("unable to apply pipeline requirements: %w", err)
Expand Down Expand Up @@ -1061,20 +1062,20 @@ func (b *Build) BuildPackage(ctx context.Context) error {

// run any pipelines for subpackages
for _, sp := range b.Configuration.Subpackages {
spctx := NewSubpackageContext(&sp)
sp := sp
if !b.IsBuildLess() {
b.Logger.Printf("running pipeline for subpackage %s", sp.Name)
pb.Subpackage = spctx
pb.Subpackage = &sp

result, err := spctx.ShouldRun(&pb)
result, err := pb.ShouldRun(sp)
if err != nil {
return err
}
if !result {
continue
}

for _, p := range spctx.Subpackage.Pipeline {
for _, p := range sp.Pipeline {
pctx := NewPipelineContext(&p, b.Logger)
if _, err := pctx.Run(ctx, &pb); err != nil {
return fmt.Errorf("unable to run pipeline: %w", err)
Expand Down Expand Up @@ -1124,14 +1125,14 @@ func (b *Build) BuildPackage(ctx context.Context) error {

// generate SBOMs for subpackages
for _, sp := range b.Configuration.Subpackages {
sp := sp
langs := []string{}

spctx := NewSubpackageContext(&sp)
if !b.IsBuildLess() {
b.Logger.Printf("generating SBOM for subpackage %s", sp.Name)
pb.Subpackage = spctx
pb.Subpackage = &sp

result, err := spctx.ShouldRun(&pb)
result, err := pb.ShouldRun(sp)
if err != nil {
return err
}
Expand Down Expand Up @@ -1172,24 +1173,24 @@ func (b *Build) BuildPackage(ctx context.Context) error {
}

// emit main package
if err := pkg.Emit(ctx, &pb); err != nil {
if err := pb.Emit(ctx, pkg); err != nil {
return fmt.Errorf("unable to emit package: %w", err)
}

// emit subpackages
for _, sp := range b.Configuration.Subpackages {
spctx := NewSubpackageContext(&sp)
pb.Subpackage = spctx
sp := sp
pb.Subpackage = &sp

result, err := spctx.ShouldRun(&pb)
result, err := pb.ShouldRun(sp)
if err != nil {
return err
}
if !result {
continue
}

if err := spctx.Emit(ctx, &pb); err != nil {
if err := pb.Emit(ctx, pkgFromSub(&sp)); err != nil {
return fmt.Errorf("unable to emit package: %w", err)
}
}
Expand Down Expand Up @@ -1219,10 +1220,10 @@ func (b *Build) BuildPackage(ctx context.Context) error {
apkFiles = append(apkFiles, filepath.Join(packageDir, pkgFileName))

for _, subpkg := range b.Configuration.Subpackages {
spctx := NewSubpackageContext(&subpkg)
pb.Subpackage = spctx
subpkg := subpkg
pb.Subpackage = &subpkg

result, err := spctx.ShouldRun(&pb)
result, err := pb.ShouldRun(subpkg)
if err != nil {
return err
}
Expand Down
83 changes: 28 additions & 55 deletions pkg/build/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,9 @@ func min(l, r int) int {
return r
}

type PackageContext struct {
Package *config.Package
}

func NewPackageContext(pkg *config.Package) *PackageContext {
return &PackageContext{
Package: pkg,
}
}

type SubpackageContext struct {
Subpackage *config.Subpackage
}

// Create a new subpackage context
func NewSubpackageContext(pkg *config.Subpackage) *SubpackageContext {
return &SubpackageContext{
Subpackage: pkg,
}
}

type PackageBuild struct {
Build *Build
Origin *PackageContext
Origin *config.Package
PackageName string
OriginName string
InstalledSize int64
Expand All @@ -101,46 +80,37 @@ type PackageBuild struct {
Commit string
}

func (pkg *PackageContext) Emit(ctx context.Context, pb *PipelineBuild) error {
ctx, span := otel.Tracer("melange").Start(ctx, "Emit")
defer span.End()

fakesp := config.Subpackage{
Name: pkg.Package.Name,
Dependencies: pkg.Package.Dependencies,
Options: pkg.Package.Options,
Scriptlets: pkg.Package.Scriptlets,
Description: pkg.Package.Description,
URL: pkg.Package.URL,
Commit: pkg.Package.Commit,
func pkgFromSub(sub *config.Subpackage) *config.Package {
return &config.Package{
Name: sub.Name,
Dependencies: sub.Dependencies,
Options: sub.Options,
Scriptlets: sub.Scriptlets,
Description: sub.Description,
URL: sub.URL,
Commit: sub.Commit,
}
fakespctx := SubpackageContext{
Subpackage: &fakesp,
}
return fakespctx.Emit(ctx, pb)
}

func (spkg *SubpackageContext) Emit(ctx context.Context, pb *PipelineBuild) error {
pkgctx := NewPackageContext(&pb.Build.Configuration.Package)

func (pb *PipelineBuild) Emit(ctx context.Context, pkg *config.Package) error {
pc := PackageBuild{
Build: pb.Build,
Origin: pkgctx,
PackageName: spkg.Subpackage.Name,
OriginName: spkg.Subpackage.Name,
Origin: &pb.Build.Configuration.Package,
PackageName: pkg.Name,
OriginName: pkg.Name,
OutDir: filepath.Join(pb.Build.OutDir, pb.Build.Arch.ToAPK()),
Logger: pb.Build.Logger,
Dependencies: spkg.Subpackage.Dependencies,
Dependencies: pkg.Dependencies,
Arch: pb.Build.Arch.ToAPK(),
Options: spkg.Subpackage.Options,
Scriptlets: spkg.Subpackage.Scriptlets,
Description: spkg.Subpackage.Description,
URL: spkg.Subpackage.URL,
Commit: spkg.Subpackage.Commit,
Options: pkg.Options,
Scriptlets: pkg.Scriptlets,
Description: pkg.Description,
URL: pkg.URL,
Commit: pkg.Commit,
}

if !pb.Build.StripOriginName {
pc.OriginName = pc.Origin.Package.Name
pc.OriginName = pc.Origin.Name
}

return pc.EmitPackage(ctx)
Expand All @@ -160,12 +130,12 @@ func (pc *PackageBuild) AppendBuildLog(dir string) error {
defer f.Close()

// separate with pipe so it is easy to parse
_, err = f.WriteString(fmt.Sprintf("%s|%s|%s|%s-r%d\n", pc.Arch, pc.OriginName, pc.PackageName, pc.Origin.Package.Version, pc.Origin.Package.Epoch))
_, err = f.WriteString(fmt.Sprintf("%s|%s|%s|%s-r%d\n", pc.Arch, pc.OriginName, pc.PackageName, pc.Origin.Version, pc.Origin.Epoch))
return err
}

func (pc *PackageBuild) Identity() string {
return fmt.Sprintf("%s-%s-r%d", pc.PackageName, pc.Origin.Package.Version, pc.Origin.Package.Epoch)
return fmt.Sprintf("%s-%s-r%d", pc.PackageName, pc.Origin.Version, pc.Origin.Epoch)
}

func (pc *PackageBuild) Filename() string {
Expand All @@ -178,7 +148,7 @@ func (pc *PackageBuild) WorkspaceSubdir() string {

var controlTemplate = `# Generated by melange.
pkgname = {{.PackageName}}
pkgver = {{.Origin.Package.Version}}-r{{.Origin.Package.Epoch}}
pkgver = {{.Origin.Version}}-r{{.Origin.Epoch}}
arch = {{.Arch}}
size = {{.InstalledSize}}
origin = {{.OriginName}}
Expand All @@ -188,7 +158,7 @@ commit = {{.Commit}}
{{- if ne .Build.SourceDateEpoch.Unix 0 }}
builddate = {{ .Build.SourceDateEpoch.Unix }}
{{- end}}
{{- range $copyright := .Origin.Package.Copyright }}
{{- range $copyright := .Origin.Copyright }}
license = {{ $copyright.License }}
{{- end }}
{{- range $dep := .Dependencies.Runtime }}
Expand Down Expand Up @@ -447,6 +417,9 @@ func (pc *PackageBuild) wantSignature() bool {
}

func (pc *PackageBuild) EmitPackage(ctx context.Context) error {
ctx, span := otel.Tracer("melange").Start(ctx, "EmitPackage")
defer span.End()

err := os.MkdirAll(pc.WorkspaceSubdir(), 0o755)
if err != nil {
return fmt.Errorf("unable to ensure workspace exists: %w", err)
Expand Down
14 changes: 6 additions & 8 deletions pkg/build/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ func Test_removeSelfProvidedDeps_WithEmptyProvides(t *testing.T) {
}

func Test_GenerateControlData(t *testing.T) {
pkgctx := NewPackageContext(
&config.Package{
Version: "1.2.3",
Epoch: 4,
},
)
pkg := &config.Package{
Version: "1.2.3",
Epoch: 4,
}

tests := []struct {
name string
Expand All @@ -73,7 +71,7 @@ func Test_GenerateControlData(t *testing.T) {
Build: &Build{
SourceDateEpoch: time.Unix(0, 0),
},
Origin: pkgctx,
Origin: pkg,
PackageName: "glibc",
Arch: "aarch64",
InstalledSize: 666,
Expand All @@ -100,7 +98,7 @@ datahash = baadf00d
Build: &Build{
SourceDateEpoch: time.Unix(12345678, 0),
},
Origin: pkgctx,
Origin: pkg,
PackageName: "glibc",
Arch: "aarch64",
InstalledSize: 666,
Expand Down
18 changes: 9 additions & 9 deletions pkg/build/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func NewPipelineContext(p *config.Pipeline, log apko_log.Logger) *PipelineContex

type PipelineBuild struct {
Build *Build
Package *PackageContext
Subpackage *SubpackageContext
Package *config.Package
Subpackage *config.Subpackage
}

func (pctx *PipelineContext) Identity() string {
Expand Down Expand Up @@ -98,12 +98,12 @@ func MutateWith(pb *PipelineBuild, with map[string]string) (map[string]string, e

func substitutionMap(pb *PipelineBuild) (map[string]string, error) {
nw := map[string]string{
config.SubstitutionPackageName: pb.Package.Package.Name,
config.SubstitutionPackageVersion: pb.Package.Package.Version,
config.SubstitutionPackageEpoch: strconv.FormatUint(pb.Package.Package.Epoch, 10),
config.SubstitutionPackageName: pb.Package.Name,
config.SubstitutionPackageVersion: pb.Package.Version,
config.SubstitutionPackageEpoch: strconv.FormatUint(pb.Package.Epoch, 10),
config.SubstitutionPackageFullVersion: fmt.Sprintf("%s-r%s", config.SubstitutionPackageVersion, config.SubstitutionPackageEpoch),
config.SubstitutionTargetsDestdir: fmt.Sprintf("/home/build/melange-out/%s", pb.Package.Package.Name),
config.SubstitutionTargetsContextdir: fmt.Sprintf("/home/build/melange-out/%s", pb.Package.Package.Name),
config.SubstitutionTargetsDestdir: fmt.Sprintf("/home/build/melange-out/%s", pb.Package.Name),
config.SubstitutionTargetsContextdir: fmt.Sprintf("/home/build/melange-out/%s", pb.Package.Name),
config.SubstitutionHostTripletGnu: pb.Build.BuildTripletGnu(),
config.SubstitutionHostTripletRust: pb.Build.BuildTripletRust(),
config.SubstitutionCrossTripletGnuGlibc: pb.Build.Arch.ToTriplet("gnu"),
Expand All @@ -128,11 +128,11 @@ func substitutionMap(pb *PipelineBuild) (map[string]string, error) {
}

if pb.Subpackage != nil {
nw[config.SubstitutionSubPkgDir] = fmt.Sprintf("/home/build/melange-out/%s", pb.Subpackage.Subpackage.Name)
nw[config.SubstitutionSubPkgDir] = fmt.Sprintf("/home/build/melange-out/%s", pb.Subpackage.Name)
nw[config.SubstitutionTargetsContextdir] = nw[config.SubstitutionSubPkgDir]
}

packageNames := []string{pb.Package.Package.Name}
packageNames := []string{pb.Package.Name}
for _, sp := range pb.Build.Configuration.Subpackages {
packageNames = append(packageNames, sp.Name)
}
Expand Down
Loading
Loading