Skip to content

Commit

Permalink
[#113]: feature: significantly increase build speed
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Oct 8, 2023
2 parents f850ca2 + 548e636 commit ba6d0f0
Show file tree
Hide file tree
Showing 13 changed files with 421 additions and 103 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
# Added by cargo
**/target
**/plugins_dev.toml
.vscode
.vscode
rr
42 changes: 18 additions & 24 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ 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,
Prefix: randStringBytes(5),
Structure: pluginStructureStr,
Version: b.modules[i].Version,
Replace: b.modules[i].Replace,
Module: b.modules[i].ModuleName,
Prefix: randStringBytes(5),
Structure: pluginStructureStr,
PseudoVersion: b.modules[i].PseudoVersion,
Replace: b.modules[i].Replace,
}
}

buf := new(bytes.Buffer)

// compatibility with the version 2
// compatibility with version 2
switch t.ModuleVersion {
case velox.V2023:
err = templates.CompileTemplateV2023(buf, t)
Expand Down Expand Up @@ -131,7 +131,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo

buf.Reset()

// compatibility with the version 2
// compatibility with version 2
switch t.ModuleVersion {
case velox.V2023:
err = templates.CompileGoModTemplate2023(buf, t)
Expand All @@ -147,6 +147,8 @@ 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()))

_, err = goModFile.Write(buf.Bytes())
if err != nil {
return err
Expand All @@ -160,16 +162,9 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return err
}

for i := 0; i < len(t.Entries); i++ {
// go get only deps w/o replace
if t.Entries[i].Replace != "" {
t.Entries = append(t.Entries, b.getDepsReplace(t.Entries[i].Replace)...)
continue
}
err = b.goGetMod(t.Entries[i].Module, t.Entries[i].Version)
if err != nil {
return err
}
err = b.goModDowloadCmd()
if err != nil {
return err
}

err = b.goModTidyCmd()
Expand Down Expand Up @@ -259,9 +254,9 @@ func (b *Builder) goBuildCmd(out string) error {
return nil
}

func (b *Builder) goModTidyCmd() error {
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod tidy"))
cmd := exec.Command("go", "mod", "tidy")
func (b *Builder) goModDowloadCmd() error {
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod download"))
cmd := exec.Command("go", "mod", "download")
cmd.Stderr = b
err := cmd.Start()
if err != nil {
Expand All @@ -274,11 +269,10 @@ func (b *Builder) goModTidyCmd() error {
return nil
}

func (b *Builder) goGetMod(repo, hash string) error {
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go get "+repo+"@"+hash))
cmd := exec.Command("go", "get", repo+"@"+hash) //nolint:gosec
func (b *Builder) goModTidyCmd() error {
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod tidy"))
cmd := exec.Command("go", "mod", "tidy")
cmd.Stderr = b

err := cmd.Start()
if err != nil {
return err
Expand Down
24 changes: 24 additions & 0 deletions builder/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ package container
import (
"github.com/roadrunner-server/informer/v4"
"github.com/roadrunner-server/resetter/v4"
aba "github.com/roadrunner-server/some_plugin"
abc "github.com/roadrunner-server/some_plugin/v2"
abd "github.com/roadrunner-server/some_plugin/v22234"
ab "github.com/roadrunner-server/rpc/v4"
cd "github.com/roadrunner-server/http/v4"
ef "github.com/roadrunner-server/grpc/v4"
Expand All @@ -30,6 +33,9 @@ func Plugins() []any {
&resetter.Plugin{},
// std and custom plugins
&aba.Plugin{},
&abc.Plugin{},
&abd.Plugin{},
&ab.Plugin{},
&cd.Plugin{},
&ef.Plugin{},
Expand All @@ -44,6 +50,24 @@ func TestCompile(t *testing.T) {
Entries: make([]*templates.Entry, 0, 10),
}

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

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

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

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/rpc/v4",
Structure: "Plugin{}",
Expand Down
9 changes: 6 additions & 3 deletions builder/templates/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ 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
Prefix string
Version string
// Replace directive, should include path
// 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)
PseudoVersion string
// Replace directive, should include a path
Replace string
}

Expand Down
10 changes: 8 additions & 2 deletions builder/templates/templateV2023.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ module github.com/roadrunner-server/roadrunner/{{.ModuleVersion}}
go 1.21
toolchain go1.21.0
toolchain go1.21.1
require (
github.com/buger/goterm v1.0.4
github.com/dustin/go-humanize v1.0.1
github.com/joho/godotenv v1.5.1
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.2
go.uber.org/automaxprocs v1.5.2
github.com/roadrunner-server/informer/v4 latest
github.com/roadrunner-server/resetter/v4 latest
// Go module pseudo-version
{{range $v := .Entries}}{{$v.Module}} {{$v.PseudoVersion}}
{{end}}
)
replace (
Expand Down
2 changes: 1 addition & 1 deletion cmd/vx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
// os.Args[0] always contains path to the executable, like foo/bar/rr -> rr
// os.Args[0] always contains a path to the executable, like foo/bar/rr -> rr
cmd := cli.NewCommand(filepath.Base(os.Args[0]))
err := cmd.Execute()
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions github/modinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package github

import (
"fmt"
"regexp"
"strconv"
"time"

m "golang.org/x/mod/module"
)

var vr = regexp.MustCompile("/v(\\d+)$") //nolint:gosimple

// here we accept a module name and return the version
// e.g.: github.com/roadrunner-server/logger/v2 => v2
func parseModuleInfo(module string, t time.Time, rev string) string {
match := vr.FindStringSubmatch(module)
var version string
if len(match) > 1 {
if !IsDigit(match[1]) {
return m.PseudoVersion("", "", t, rev)
}

version = fmt.Sprintf("v%s", match[1])
}

return m.PseudoVersion(version, "", t, rev)
}

func IsDigit(num string) bool {
_, err := strconv.ParseInt(num, 10, 64)
return err == nil
}
62 changes: 62 additions & 0 deletions github/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package github

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type tm struct {
module string
tm time.Time
sha string
expect string
}

func TestParse(t *testing.T) {
tn, err := time.Parse("20060102150405", "20231008162055")
require.NoError(t, err)

tests := []tm{
{
module: "github.com/roadrunner-server/logger/va",
tm: tn,
sha: "1234567890",
expect: "v0.0.0-20231008162055-1234567890",
},
{
module: "github.com/roadrunner-server/logger/v2",
tm: tn,
sha: "1234567890",
expect: "v2.0.0-20231008162055-1234567890",
},
{
module: "github.com/roadrunner-server/logger/v2222222222222",
tm: tn,
sha: "1234567890",
expect: "v2222222222222.0.0-20231008162055-1234567890",
},
{
module: "github.com/roadrunner-server/logger",
tm: tn,
sha: "1234567890",
expect: "v0.0.0-20231008162055-1234567890",
},
{
module: "github.com/roadrunner-server/logger/v2",
tm: tn,
sha: "",
expect: "v2.0.0-20231008162055-",
},
{
expect: "v0.0.0-00010101000000-",
},
}

for _, tt := range tests {
out := parseModuleInfo(tt.module, tt.tm, tt.sha)
assert.Equal(t, tt.expect, out)
}
}
Loading

0 comments on commit ba6d0f0

Please sign in to comment.