Skip to content

Commit

Permalink
feature: simplify velox build configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Valery Piashchynski <[email protected]>
  • Loading branch information
rustatian committed Jun 21, 2024
1 parent 1020826 commit a4d97fd
Show file tree
Hide file tree
Showing 19 changed files with 281 additions and 340 deletions.
95 changes: 58 additions & 37 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package builder
import (
"bytes"
"fmt"
"log/slog"
"math/rand"
"os"
"os/exec"
Expand All @@ -11,11 +12,11 @@ import (
"regexp"
"strings"
"syscall"
"time"

"github.com/hashicorp/go-version"
"github.com/roadrunner-server/velox/v2024"
"github.com/roadrunner-server/velox/v2024/builder/templates"
"go.uber.org/zap"
)

const (
Expand All @@ -36,16 +37,18 @@ type Builder struct {
rrTempPath string
out string
modules []*velox.ModulesInfo
log *zap.Logger
buildArgs []string
log *slog.Logger
debug bool
rrVersion string
}

func NewBuilder(rrTmpPath string, modules []*velox.ModulesInfo, out string, log *zap.Logger, buildArgs []string) *Builder {
func NewBuilder(rrTmpPath string, modules []*velox.ModulesInfo, out, rrVersion string, debug bool, log *slog.Logger) *Builder {
return &Builder{
rrTempPath: rrTmpPath,
modules: modules,
buildArgs: buildArgs,
out: out,
debug: debug,
rrVersion: rrVersion,
log: log,
}
}
Expand All @@ -63,9 +66,10 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
t.Entries = make([]*templates.Entry, len(b.modules))
for i := 0; i < len(b.modules); i++ {
t.Entries[i] = &templates.Entry{
Module: b.modules[i].ModuleName,
Module: b.modules[i].ModuleName,
// we need to set prefix to avoid collisions
Prefix: randStringBytes(5),
Structure: pluginStructureStr,
StructureName: pluginStructureStr,
PseudoVersion: b.modules[i].PseudoVersion,
Replace: b.modules[i].Replace,
}
Expand Down Expand Up @@ -94,7 +98,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
}

b.log.Debug("[RESULTING TEMPLATE]", zap.String("template", buf.String()))
b.log.Debug("[RESULTING TEMPLATE]", slog.String("template", buf.String()))

f, err := os.Open(b.rrTempPath)
if err != nil {
Expand All @@ -108,7 +112,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
}

for i := 0; i < len(files); i++ {
b.log.Info("[CLEANING UP]", zap.String("file/folder", files[i]))
b.log.Info("[CLEANING UP]", slog.String("file/folder", files[i]))
_ = os.RemoveAll(files[i])
}
}()
Expand Down Expand Up @@ -157,7 +161,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
}

b.log.Debug("[RESULTING TEMPLATE]", zap.String("template", buf.String()))
b.log.Debug("[RESULTING TEMPLATE]", slog.String("template", buf.String()))

_, err = goModFile.Write(buf.Bytes())
if err != nil {
Expand All @@ -166,7 +170,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo

buf.Reset()

b.log.Info("[SWITCHING WORKING DIR]", zap.String("wd", b.rrTempPath))
b.log.Info("[SWITCHING WORKING DIR]", slog.String("wd", b.rrTempPath))
err = syscall.Chdir(b.rrTempPath)
if err != nil {
return err
Expand All @@ -182,7 +186,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return err
}

b.log.Info("[CHECKING OUTPUT DIR]", zap.String("dir", b.out))
b.log.Info("[CHECKING OUTPUT DIR]", slog.String("dir", b.out))
err = os.MkdirAll(b.out, os.ModeDir)
if err != nil {
return err
Expand All @@ -193,7 +197,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return err
}

b.log.Info("[MOVING EXECUTABLE]", zap.String("file", filepath.Join(b.rrTempPath, executableName)), zap.String("to", filepath.Join(b.out, executableName)))
b.log.Info("[MOVING EXECUTABLE]", slog.String("file", filepath.Join(b.rrTempPath, executableName)), slog.String("to", filepath.Join(b.out, executableName)))
err = moveFile(filepath.Join(b.rrTempPath, executableName), filepath.Join(b.out, executableName))
if err != nil {
return err
Expand All @@ -203,7 +207,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
}

func (b *Builder) Write(d []byte) (int, error) {
b.log.Debug("[STDERR OUTPUT]", zap.ByteString("log", d))
b.log.Debug("[STDERR OUTPUT]", slog.Any("log", d))
return len(d), nil
}

Expand Down Expand Up @@ -232,25 +236,42 @@ func randStringBytes(n int) string {

func (b *Builder) goBuildCmd(out string) error {
var cmd *exec.Cmd
if len(b.buildArgs) != 0 {
buildCmdArgs := make([]string, 0, len(b.buildArgs)+5)
buildCmdArgs = append(buildCmdArgs, "build")
// verbose
buildCmdArgs = append(buildCmdArgs, "-v")
// build args
buildCmdArgs = append(buildCmdArgs, b.buildArgs...)
// output file
buildCmdArgs = append(buildCmdArgs, "-o")
// path
buildCmdArgs = append(buildCmdArgs, out)
// path to main.go
buildCmdArgs = append(buildCmdArgs, rrMainGo)
cmd = exec.Command("go", buildCmdArgs...)
} else {
cmd = exec.Command("go", "build", "-o", out, rrMainGo)
}

b.log.Info("[EXECUTING CMD]", zap.String("cmd", cmd.String()))

buildCmdArgs := make([]string, 0, 5)
buildCmdArgs = append(buildCmdArgs, "build", "-v", "-trimpath")

// var ld []string
switch b.debug {
case true:
// debug flags
// turn off optimizations
buildCmdArgs = append(buildCmdArgs, "-gcflags", "-N")
// turn off inlining
buildCmdArgs = append(buildCmdArgs, "-gcflags", "-l")
// build with debug tags
buildCmdArgs = append(buildCmdArgs, "-tags", "debug")
case false:
buildCmdArgs = append(buildCmdArgs, "-ldflags", "-s")
}

// LDFLAGS for version and build time, always appended
buildCmdArgs = append(buildCmdArgs, "-ldflags")
buildCmdArgs = append(buildCmdArgs, fmt.Sprintf(
"-X github.com/roadrunner-server/roadrunner/v2024/internal/meta.version=%s -X github.com/roadrunner-server/roadrunner/v2024/internal/meta.buildTime=%s",
b.rrVersion,
time.Now().UTC().Format(time.RFC3339)),
)

// output
buildCmdArgs = append(buildCmdArgs, "-o")
// path
buildCmdArgs = append(buildCmdArgs, out)
// path to main.go
buildCmdArgs = append(buildCmdArgs, rrMainGo)

cmd = exec.Command("go", buildCmdArgs...)

b.log.Info("[EXECUTING CMD]", slog.String("cmd", cmd.String()))
cmd.Stderr = b
cmd.Stdout = b
err := cmd.Start()
Expand All @@ -265,7 +286,7 @@ func (b *Builder) goBuildCmd(out string) error {
}

func (b *Builder) goModDowloadCmd() error {
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod download"))
b.log.Info("[EXECUTING CMD]", slog.String("cmd", "go mod download"))
cmd := exec.Command("go", "mod", "download")
cmd.Stderr = b
err := cmd.Start()
Expand All @@ -280,7 +301,7 @@ func (b *Builder) goModDowloadCmd() error {
}

func (b *Builder) goModTidyCmd() error {
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod tidy"))
b.log.Info("[EXECUTING CMD]", slog.String("cmd", "go mod tidy"))
cmd := exec.Command("go", "mod", "tidy")
cmd.Stderr = b
err := cmd.Start()
Expand All @@ -295,7 +316,7 @@ func (b *Builder) goModTidyCmd() error {
}

func (b *Builder) getDepsReplace(repl string) []*templates.Entry {
b.log.Info("[REPLACING DEPENDENCIES]", zap.String("dependency", repl))
b.log.Info("[REPLACING DEPENDENCIES]", slog.String("dependency", repl))
modFile, err := os.ReadFile(path.Join(repl, goModStr))
if err != nil {
return nil
Expand All @@ -306,7 +327,7 @@ func (b *Builder) getDepsReplace(repl string) []*templates.Entry {
for i := 0; i < len(replaces); i++ {
split := strings.Split(strings.TrimSpace(replaces[i][0]), " => ")
if len(split) != 2 {
b.log.Error("not enough split args", zap.String("replace", replaces[i][0]))
b.log.Error("not enough split args", slog.String("replace", replaces[i][0]))
continue
}

Expand Down
4 changes: 2 additions & 2 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package builder

import (
"log/slog"
"os"
"path"
"testing"

"github.com/roadrunner-server/velox/v2024"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -81,7 +81,7 @@ func setup() *Builder {
"dummy_multiple_absolute_remote": []byte(replaceGoModMultipleRemote),
}

b := NewBuilder("/tmp", []*velox.ModulesInfo{}, "", zap.NewNop(), []string{})
b := NewBuilder("/tmp", []*velox.ModulesInfo{}, "", "v2024.1.0", false, slog.Default())

b.modules = []*velox.ModulesInfo{
{
Expand Down
59 changes: 33 additions & 26 deletions builder/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package builder

import (
"bytes"
"strings"
"testing"

"github.com/roadrunner-server/velox/v2024/builder/templates"
Expand All @@ -21,7 +22,7 @@ import (
cd "github.com/roadrunner-server/http/v4"
ef "github.com/roadrunner-server/grpc/v4"
jk "github.com/roadrunner-server/logger/v4"
)
func Plugins() []any {
Expand All @@ -31,7 +32,7 @@ func Plugins() []any {
&informer.Plugin{},
// resetter plugin (./rr reset)
&resetter.Plugin{},
// std and custom plugins
&aba.Plugin{},
&abc.Plugin{},
Expand All @@ -40,7 +41,6 @@ func Plugins() []any {
&cd.Plugin{},
&ef.Plugin{},
&jk.Plugin{},
}
}
`
Expand All @@ -51,47 +51,54 @@ func TestCompile(t *testing.T) {
}

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/some_plugin",
Structure: "Plugin{}",
Prefix: "aba",
Module: "github.com/roadrunner-server/some_plugin",
StructureName: "Plugin{}",
Prefix: "aba",
})

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/some_plugin/v2",
Structure: "Plugin{}",
Prefix: "abc",
Module: "github.com/roadrunner-server/some_plugin/v2",
StructureName: "Plugin{}",
Prefix: "abc",
})

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/some_plugin/v22234",
Structure: "Plugin{}",
Prefix: "abd",
Module: "github.com/roadrunner-server/some_plugin/v22234",
StructureName: "Plugin{}",
Prefix: "abd",
})

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/rpc/v4",
Structure: "Plugin{}",
Prefix: "ab",
Module: "github.com/roadrunner-server/rpc/v4",
StructureName: "Plugin{}",
Prefix: "ab",
})
tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/http/v4",
Structure: "Plugin{}",
Prefix: "cd",
Module: "github.com/roadrunner-server/http/v4",
StructureName: "Plugin{}",
Prefix: "cd",
})
tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/grpc/v4",
Structure: "Plugin{}",
Prefix: "ef",
Module: "github.com/roadrunner-server/grpc/v4",
StructureName: "Plugin{}",
Prefix: "ef",
})
tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/logger/v4",
Structure: "Plugin{}",
Prefix: "jk",
Module: "github.com/roadrunner-server/logger/v4",
StructureName: "Plugin{}",
Prefix: "jk",
})

buf := new(bytes.Buffer)
err := templates.CompileTemplateV2023(buf, tt)
err := templates.CompileTemplateV2024(buf, tt)
require.NoError(t, err)

require.Equal(t, res, buf.String())
bufstr := buf.String()
bufstr = strings.ReplaceAll(bufstr, "\t", "")
bufstr = strings.ReplaceAll(bufstr, "\n", "")

resstr := strings.ReplaceAll(res, "\t", "")
resstr = strings.ReplaceAll(resstr, "\n", "")

require.Equal(t, resstr, bufstr)
}
5 changes: 3 additions & 2 deletions builder/templates/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
// Entry represents all info about module
type Entry struct {
// Module is the module name (github.com/roadrunner-server/logger/v2)
Module string
Structure string
Module string
// StructureName is the structure name of the plugin (Plugin{})
StructureName string
// Prefix is the prefix for the plugin to avoid collisions
Prefix string
// PseudoVersion is the pseudo version of the module (v0.0.0-20210101000000-000000000000)
Expand Down
4 changes: 2 additions & 2 deletions builder/templates/templateV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func Plugins() []any {
&informer.Plugin{},
// resetter plugin (./rr reset)
&resetter.Plugin{},
// std and custom plugins
{{range $v := .Entries}}&{{$v.Prefix}}.{{$v.Structure}},
{{range $v := .Entries}}&{{$v.Prefix}}.{{$v.StructureName}},
{{end}}
}
}
Expand Down
Loading

0 comments on commit a4d97fd

Please sign in to comment.