Skip to content

Commit 9b1bda3

Browse files
committed
mv File.Runner => Project.Runner
1 parent 1f14b39 commit 9b1bda3

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

modfile/gop_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ xgo 1.1
171171
172172
project github.com/goplus/spx math
173173
class .spx Sprite
174+
runner github.com/goplus/spx/v2/cmd/spxrun v2.0.1
174175
175176
require (
176177
github.com/ajstarks/svgo v0.0.0-20210927141636-6d70534b1098
@@ -182,7 +183,7 @@ func TestGoModCompat2(t *testing.T) {
182183
gopmod = goxmodSpx2
183184
)
184185
f, err := modfile.ParseLax("go.mod", []byte(gopmod), nil)
185-
if err != nil || len(f.Syntax.Stmt) != 6 {
186+
if err != nil || len(f.Syntax.Stmt) != 7 {
186187
t.Fatal("modfile.ParseLax failed:", f, err)
187188
}
188189

@@ -191,7 +192,7 @@ func TestGoModCompat2(t *testing.T) {
191192
t.Fatal("modfile.ParseLax xgo:", xgo)
192193
}
193194

194-
require := f.Syntax.Stmt[5].(*modfile.LineBlock)
195+
require := f.Syntax.Stmt[6].(*modfile.LineBlock)
195196
if len(require.Token) != 1 || require.Token[0] != "require" {
196197
t.Fatal("modfile.ParseLax require:", require)
197198
}
@@ -345,6 +346,26 @@ import "\?" math
345346
`)
346347
doTestParseErr(t, `gop.mod:2: import must declare after a project definition`, `
347348
import math
349+
`)
350+
doTestParseErr(t, `gop.mod:2: runner must declare after a project definition`, `
351+
runner github.com/goplus/spx/v2/cmd/spxrun
352+
`)
353+
doTestParseErr(t, `gop.mod:3: usage: runner runnerPkgPath [version]`, `
354+
project github.com/goplus/spx math
355+
runner
356+
`)
357+
doTestParseErr(t, `gop.mod:3: invalid quoted string: invalid syntax`, `
358+
project github.com/goplus/spx math
359+
runner "\?"
360+
`)
361+
doTestParseErr(t, `gop.mod:3: invalid syntax`, `
362+
project github.com/goplus/spx math
363+
runner github.com/goplus/spx/v2/cmd/spxrun "\?"
364+
`)
365+
doTestParseErr(t, `gop.mod:4: repeated runner statement`, `
366+
project github.com/goplus/spx math
367+
runner github.com/goplus/spx/v2/cmd/spxrun
368+
runner github.com/goplus/spx/v2/cmd/spxrun
348369
`)
349370
doTestParseErr(t, `gop.mod:2: unknown directive: unknown`, `
350371
unknown .spx

modfile/rule.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ type Compiler struct {
3535
// A Runner is the runner statement that specifies a custom runner for the project.
3636
// Example: runner github.com/goplus/spx/v2/cmd/spxrun
3737
type Runner struct {
38-
Cmd string // the command package path to run the project
39-
Syntax *Line
38+
Path string // package path of the runner
39+
Version string // optional version of the runner
40+
Syntax *Line
4041
}
4142

4243
// A File is the parsed, interpreted form of a gox.mod file.
4344
type File struct {
4445
XGo *XGo
4546
Compiler *Compiler // the underlying go compiler in go.mod (not gox.mod)
4647
Projects []*Project
47-
Runner *Runner // custom runner command
4848
ClassMods []string // calc by require statements in go.mod (not gox.mod)
4949

5050
Syntax *FileSyntax
@@ -91,6 +91,7 @@ type Project struct {
9191
Works []*Class // work class of classfile
9292
PkgPaths []string // package paths of classfile and optional inline-imported packages.
9393
Import []*Import // auto-imported packages
94+
Runner *Runner // custom runner
9495
Syntax *Line
9596
}
9697

@@ -327,21 +328,34 @@ usage: class [-embed -prefix=Prefix] *.workExt WorkClass [WorkPrototype]`, sw)
327328
errorf("usage: import [name] pkgPath")
328329
return
329330
}
330-
case "runner", "run": // "run" for backward compatibility
331-
if f.Runner != nil {
331+
case "runner":
332+
proj := f.proj()
333+
if proj == nil {
334+
errorf("runner must declare after a project definition")
335+
return
336+
}
337+
if proj.Runner != nil {
332338
errorf("repeated runner statement")
333339
return
334340
}
335-
if len(args) != 1 {
336-
errorf("usage: runner cmdPkgPath")
341+
if len(args) < 1 {
342+
errorf("usage: runner runnerPkgPath [version]")
337343
return
338344
}
339-
cmdPath, err := parsePkgPath(&args[0])
345+
runnerPath, err := parsePkgPath(&args[0])
340346
if err != nil {
341347
wrapError(err)
342348
return
343349
}
344-
f.Runner = &Runner{Cmd: cmdPath, Syntax: line}
350+
runnerVer := ""
351+
if len(args) > 1 {
352+
runnerVer, err = parseString(&args[1])
353+
if err != nil {
354+
wrapError(err)
355+
return
356+
}
357+
}
358+
proj.Runner = &Runner{Path: runnerPath, Version: runnerVer, Syntax: line}
345359
default:
346360
if strict {
347361
errorf("unknown directive: %s", verb)

0 commit comments

Comments
 (0)