@@ -2,6 +2,7 @@ package builder
2
2
3
3
import (
4
4
"bytes"
5
+ "fmt"
5
6
"math/rand"
6
7
"os"
7
8
"os/exec"
@@ -11,7 +12,9 @@ import (
11
12
"strings"
12
13
"syscall"
13
14
15
+ "github.com/hashicorp/go-version"
14
16
"github.com/roadrunner-server/velox"
17
+ "github.com/roadrunner-server/velox/builder/templates"
15
18
"go.uber.org/zap"
16
19
)
17
20
@@ -48,11 +51,18 @@ func NewBuilder(rrTmpPath string, modules []*velox.ModulesInfo, out string, log
48
51
}
49
52
50
53
// Build builds a RR based on the provided modules info
51
- func (b * Builder ) Build () error { //nolint:gocyclo
52
- t := new (Template )
53
- t .Entries = make ([]* Entry , len (b .modules ))
54
+ func (b * Builder ) Build (rrModule string ) error { //nolint:gocyclo
55
+ t := new (templates.Template )
56
+
57
+ module , err := validateModule (rrModule )
58
+ if err != nil {
59
+ return err
60
+ }
61
+
62
+ t .ModuleVersion = module
63
+ t .Entries = make ([]* templates.Entry , len (b .modules ))
54
64
for i := 0 ; i < len (b .modules ); i ++ {
55
- t .Entries [i ] = & Entry {
65
+ t .Entries [i ] = & templates. Entry {
56
66
Module : b .modules [i ].ModuleName ,
57
67
Prefix : randStringBytes (5 ),
58
68
Structure : pluginStructureStr ,
@@ -62,9 +72,21 @@ func (b *Builder) Build() error { //nolint:gocyclo
62
72
}
63
73
64
74
buf := new (bytes.Buffer )
65
- err := compileTemplate (buf , t )
66
- if err != nil {
67
- return err
75
+
76
+ // compatibility with the version 2
77
+ switch t .ModuleVersion {
78
+ case velox .V2023 :
79
+ err = templates .CompileTemplateV2023 (buf , t )
80
+ if err != nil {
81
+ return err
82
+ }
83
+ case velox .V2 :
84
+ err = templates .CompileTemplateV2 (buf , t )
85
+ if err != nil {
86
+ return err
87
+ }
88
+ default :
89
+ return fmt .Errorf ("unknown module version: %s" , t .ModuleVersion )
68
90
}
69
91
70
92
b .log .Debug ("[RESULTING TEMPLATE]" , zap .String ("template" , buf .String ()))
@@ -109,9 +131,20 @@ func (b *Builder) Build() error { //nolint:gocyclo
109
131
110
132
buf .Reset ()
111
133
112
- err = compileGoModTemplate (buf , t )
113
- if err != nil {
114
- return err
134
+ // compatibility with the version 2
135
+ switch t .ModuleVersion {
136
+ case velox .V2023 :
137
+ err = templates .CompileGoModTemplate2023 (buf , t )
138
+ if err != nil {
139
+ return err
140
+ }
141
+ case velox .V2 :
142
+ err = templates .CompileGoModTemplateV2 (buf , t )
143
+ if err != nil {
144
+ return err
145
+ }
146
+ default :
147
+ return fmt .Errorf ("unknown module version: %s" , t .ModuleVersion )
115
148
}
116
149
117
150
_ , err = goModFile .Write (buf .Bytes ())
@@ -139,7 +172,7 @@ func (b *Builder) Build() error { //nolint:gocyclo
139
172
}
140
173
}
141
174
142
- // upgrade to 1.19
175
+ // upgrade to 1.20
143
176
err = b .goModTidyCmd ()
144
177
if err != nil {
145
178
return err
@@ -165,6 +198,26 @@ func (b *Builder) Build() error { //nolint:gocyclo
165
198
return nil
166
199
}
167
200
201
+ func (b * Builder ) Write (d []byte ) (int , error ) {
202
+ b .log .Debug ("[STDERR OUTPUT]" , zap .ByteString ("log" , d ))
203
+ return len (d ), nil
204
+ }
205
+
206
+ func validateModule (module string ) (string , error ) {
207
+ if module == "master" {
208
+ // default branch
209
+ return velox .V2023 , nil
210
+ }
211
+
212
+ v , err := version .NewVersion (module )
213
+ if err != nil {
214
+ return "" , err
215
+ }
216
+
217
+ // return major version (v2, v2023, etc)
218
+ return fmt .Sprintf ("v%d" , v .Segments ()[0 ]), nil
219
+ }
220
+
168
221
func randStringBytes (n int ) string {
169
222
b := make ([]byte , n )
170
223
for i := range b {
@@ -208,8 +261,8 @@ func (b *Builder) goBuildCmd(out string) error {
208
261
}
209
262
210
263
func (b * Builder ) goModTidyCmd () error {
211
- b .log .Info ("[EXECUTING CMD]" , zap .String ("cmd" , "go mod tidy -go=1.19 " ))
212
- cmd := exec .Command ("go" , "mod" , "tidy" , "-go=1.19 " )
264
+ b .log .Info ("[EXECUTING CMD]" , zap .String ("cmd" , "go mod tidy -go=1.20 " ))
265
+ cmd := exec .Command ("go" , "mod" , "tidy" , "-go=1.20 " )
213
266
cmd .Stderr = b
214
267
err := cmd .Start ()
215
268
if err != nil {
@@ -238,14 +291,14 @@ func (b *Builder) goGetMod(repo, hash string) error {
238
291
return nil
239
292
}
240
293
241
- func (b * Builder ) getDepsReplace (repl string ) []* Entry {
294
+ func (b * Builder ) getDepsReplace (repl string ) []* templates. Entry {
242
295
b .log .Info ("[REPLACING DEPENDENCIES]" , zap .String ("dependency" , repl ))
243
296
modFile , err := os .ReadFile (path .Join (repl , goModStr ))
244
297
if err != nil {
245
298
return nil
246
299
}
247
300
248
- var result []* Entry //nolint:prealloc
301
+ var result []* templates. Entry //nolint:prealloc
249
302
replaces := replaceRegexp .FindAllStringSubmatch (string (modFile ), - 1 )
250
303
for i := 0 ; i < len (replaces ); i ++ {
251
304
split := strings .Split (strings .TrimSpace (replaces [i ][0 ]), " => " )
@@ -261,7 +314,7 @@ func (b *Builder) getDepsReplace(repl string) []*Entry {
261
314
moduleReplace = path .Join (repl , moduleReplace )
262
315
}
263
316
264
- result = append (result , & Entry {
317
+ result = append (result , & templates. Entry {
265
318
Module : moduleName ,
266
319
Replace : moduleReplace ,
267
320
})
@@ -298,8 +351,3 @@ func moveFile(from, to string) error {
298
351
299
352
return toFile .Close ()
300
353
}
301
-
302
- func (b * Builder ) Write (d []byte ) (int , error ) {
303
- b .log .Debug ("[STDERR OUTPUT]" , zap .ByteString ("log" , d ))
304
- return len (d ), nil
305
- }
0 commit comments