Skip to content

Commit 447bd57

Browse files
adamperlincpuguy83
authored andcommitted
Move build steps to separate build.sh script in generated RPM spec
1 parent 4a469a9 commit 447bd57

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

frontend/rpm/handle_sources.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rpm
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/Azure/dalec"
89
"github.com/Azure/dalec/frontend"
@@ -54,6 +55,45 @@ func HandleSources(wf WorkerFunc) gwclient.BuildFunc {
5455
}
5556
}
5657

58+
func buildScriptSourceState(spec *dalec.Spec) *llb.State {
59+
if len(spec.Build.Steps) == 0 {
60+
return nil
61+
}
62+
63+
script := buildScript(spec)
64+
st := llb.Scratch().File(llb.Mkfile("build.sh", 0755, []byte(script)))
65+
return &st
66+
}
67+
68+
func buildScript(spec *dalec.Spec) string {
69+
b := &strings.Builder{}
70+
71+
t := spec.Build
72+
if len(t.Steps) == 0 {
73+
return ""
74+
}
75+
76+
fmt.Fprintln(b, "#!/bin/sh")
77+
fmt.Fprintln(b, "set -e")
78+
79+
if spec.HasGomods() {
80+
fmt.Fprintln(b, "export GOMODCACHE=\"$(pwd)/"+gomodsName+"\"")
81+
}
82+
83+
envKeys := dalec.SortMapKeys(t.Env)
84+
for _, k := range envKeys {
85+
v := t.Env[k]
86+
fmt.Fprintf(b, "export %s=\"%s\"\n", k, v)
87+
}
88+
89+
for _, step := range t.Steps {
90+
writeStep(b, step)
91+
}
92+
93+
b.WriteString("\n")
94+
return b.String()
95+
}
96+
5797
func Dalec2SourcesLLB(worker llb.State, spec *dalec.Spec, sOpt dalec.SourceOpts, opts ...llb.ConstraintsOpt) ([]llb.State, error) {
5898
sources, err := dalec.Sources(spec, sOpt)
5999
if err != nil {
@@ -83,6 +123,11 @@ func Dalec2SourcesLLB(worker llb.State, spec *dalec.Spec, sOpt dalec.SourceOpts,
83123
out = append(out, st.With(sourceTar(worker, gomodsName, withPG("Tar gomod deps")...)))
84124
}
85125

126+
scriptSt := buildScriptSourceState(spec)
127+
if scriptSt != nil {
128+
out = append(out, *scriptSt)
129+
}
130+
86131
return out, nil
87132
}
88133

frontend/rpm/template.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
)
1616

1717
const gomodsName = "__gomods"
18+
const buildScriptName = "build.sh"
1819

1920
var specTmpl = template.Must(template.New("spec").Funcs(tmplFuncs).Parse(strings.TrimSpace(`
2021
Name: {{.Name}}
@@ -238,8 +239,15 @@ func (w *specWrapper) Sources() (fmt.Stringer, error) {
238239
fmt.Fprintf(b, "Source%d: %s\n", idx, ref)
239240
}
240241

242+
sourceIdx := len(keys)
243+
241244
if w.Spec.HasGomods() {
242-
fmt.Fprintf(b, "Source%d: %s.tar.gz\n", len(keys), gomodsName)
245+
fmt.Fprintf(b, "Source%d: %s.tar.gz\n", sourceIdx, gomodsName)
246+
sourceIdx += 1
247+
}
248+
249+
if len(w.Spec.Build.Steps) > 0 {
250+
fmt.Fprintf(b, "Source%d: %s\n", sourceIdx, buildScriptName)
243251
}
244252

245253
if len(keys) > 0 {
@@ -328,30 +336,14 @@ func writeStep(b *strings.Builder, step dalec.BuildStep) {
328336
func (w *specWrapper) BuildSteps() fmt.Stringer {
329337
b := &strings.Builder{}
330338

331-
t := w.Spec.Build
332-
if len(t.Steps) == 0 {
339+
if len(w.Spec.Build.Steps) == 0 {
333340
return b
334341
}
335342

336343
fmt.Fprintf(b, "%%build\n")
337-
338-
fmt.Fprintln(b, "set -e")
339-
340-
if w.Spec.HasGomods() {
341-
fmt.Fprintln(b, "export GOMODCACHE=\"$(pwd)/"+gomodsName+"\"")
342-
}
343-
344-
envKeys := dalec.SortMapKeys(t.Env)
345-
for _, k := range envKeys {
346-
v := t.Env[k]
347-
fmt.Fprintf(b, "export %s=\"%s\"\n", k, v)
348-
}
349-
350-
for _, step := range t.Steps {
351-
writeStep(b, step)
352-
}
353-
344+
fmt.Fprintf(b, "%%{_sourcedir}/%s\n", buildScriptName)
354345
b.WriteString("\n")
346+
355347
return b
356348
}
357349

0 commit comments

Comments
 (0)