Skip to content

Commit b91068d

Browse files
authored
Merge pull request #1748 from xushiwei/q
gop run: support build dir
2 parents 662e0e7 + e96bf79 commit b91068d

File tree

8 files changed

+98
-58
lines changed

8 files changed

+98
-58
lines changed

build_install_run.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
// -----------------------------------------------------------------------------
2828

2929
// InstallDir installs a Go+ package directory.
30-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
3130
func InstallDir(dir string, conf *Config, install *gocmd.InstallConfig) (err error) {
3231
_, _, err = GenGo(dir, conf, false)
3332
if err != nil {
@@ -37,7 +36,6 @@ func InstallDir(dir string, conf *Config, install *gocmd.InstallConfig) (err err
3736
}
3837

3938
// InstallPkgPath installs a Go+ package.
40-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
4139
func InstallPkgPath(workDir, pkgPath string, conf *Config, install *gocmd.InstallConfig) (err error) {
4240
localDir, recursively, err := GenGoPkgPath(workDir, pkgPath, conf, true)
4341
if err != nil {
@@ -56,7 +54,6 @@ func cwdParam(recursively bool) string {
5654
}
5755

5856
// InstallFiles installs specified Go+ files.
59-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
6057
func InstallFiles(files []string, conf *Config, install *gocmd.InstallConfig) (err error) {
6158
files, err = GenGoFiles("", files, conf)
6259
if err != nil {
@@ -80,7 +77,6 @@ func chdir(dir string) string {
8077
// -----------------------------------------------------------------------------
8178

8279
// BuildDir builds a Go+ package directory.
83-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
8480
func BuildDir(dir string, conf *Config, build *gocmd.BuildConfig) (err error) {
8581
_, _, err = GenGo(dir, conf, false)
8682
if err != nil {
@@ -90,7 +86,6 @@ func BuildDir(dir string, conf *Config, build *gocmd.BuildConfig) (err error) {
9086
}
9187

9288
// BuildPkgPath builds a Go+ package.
93-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
9489
func BuildPkgPath(workDir, pkgPath string, conf *Config, build *gocmd.BuildConfig) (err error) {
9590
localDir, recursively, err := GenGoPkgPath(workDir, pkgPath, conf, false)
9691
if err != nil {
@@ -102,7 +97,6 @@ func BuildPkgPath(workDir, pkgPath string, conf *Config, build *gocmd.BuildConfi
10297
}
10398

10499
// BuildFiles builds specified Go+ files.
105-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
106100
func BuildFiles(files []string, conf *Config, build *gocmd.BuildConfig) (err error) {
107101
files, err = GenGoFiles("", files, conf)
108102
if err != nil {
@@ -128,18 +122,24 @@ func restoreDirAndMod(old string, mod os.FileMode) {
128122

129123
// -----------------------------------------------------------------------------
130124

125+
// If no go.mod and used Go+, use GOPROOT as buildDir.
126+
func getBuildDir(conf *Config) string {
127+
if conf != nil && conf.GopDeps != nil && *conf.GopDeps != 0 {
128+
return conf.Gop.Root
129+
}
130+
return ""
131+
}
132+
131133
// RunDir runs an application from a Go+ package directory.
132-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
133134
func RunDir(dir string, args []string, conf *Config, run *gocmd.RunConfig) (err error) {
134135
_, _, err = GenGo(dir, conf, false)
135136
if err != nil {
136137
return errors.NewWith(err, `GenGo(dir, conf, false)`, -2, "gop.GenGo", dir, conf, false)
137138
}
138-
return gocmd.RunDir(dir, args, run)
139+
return gocmd.RunDir(getBuildDir(conf), dir, args, run)
139140
}
140141

141142
// RunPkgPath runs an application from a Go+ package.
142-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
143143
func RunPkgPath(pkgPath string, args []string, chDir bool, conf *Config, run *gocmd.RunConfig) (err error) {
144144
localDir, recursively, err := GenGoPkgPath("", pkgPath, conf, true)
145145
if err != nil {
@@ -153,23 +153,21 @@ func RunPkgPath(pkgPath string, args []string, chDir bool, conf *Config, run *go
153153
defer os.Chdir(old)
154154
localDir = "."
155155
}
156-
return gocmd.RunDir(localDir, args, run)
156+
return gocmd.RunDir("", localDir, args, run)
157157
}
158158

159159
// RunFiles runs an application from specified Go+ files.
160-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
161160
func RunFiles(autogen string, files []string, args []string, conf *Config, run *gocmd.RunConfig) (err error) {
162161
files, err = GenGoFiles(autogen, files, conf)
163162
if err != nil {
164163
return errors.NewWith(err, `GenGoFiles(autogen, files, conf)`, -2, "gop.GenGoFiles", autogen, files, conf)
165164
}
166-
return gocmd.RunFiles(files, args, run)
165+
return gocmd.RunFiles(getBuildDir(conf), files, args, run)
167166
}
168167

169168
// -----------------------------------------------------------------------------
170169

171170
// TestDir tests a Go+ package directory.
172-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
173171
func TestDir(dir string, conf *Config, test *gocmd.TestConfig) (err error) {
174172
_, _, err = GenGo(dir, conf, true)
175173
if err != nil {
@@ -179,7 +177,6 @@ func TestDir(dir string, conf *Config, test *gocmd.TestConfig) (err error) {
179177
}
180178

181179
// TestPkgPath tests a Go+ package.
182-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
183180
func TestPkgPath(workDir, pkgPath string, conf *Config, test *gocmd.TestConfig) (err error) {
184181
localDir, recursively, err := GenGoPkgPath(workDir, pkgPath, conf, false)
185182
if err != nil {
@@ -191,7 +188,6 @@ func TestPkgPath(workDir, pkgPath string, conf *Config, test *gocmd.TestConfig)
191188
}
192189

193190
// TestFiles tests specified Go+ files.
194-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
195191
func TestFiles(files []string, conf *Config, test *gocmd.TestConfig) (err error) {
196192
files, err = GenGoFiles("", files, conf)
197193
if err != nil {

cmd/internal/run/run.go

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ func runCmd(cmd *base.Command, args []string) {
8484
if err != nil {
8585
log.Panicln("gop.NewDefaultConf:", err)
8686
}
87+
if !conf.Mod.HasModfile() { // if no go.mod, check GopDeps
88+
conf.GopDeps = new(int)
89+
}
8790
confCmd := &gocmd.Config{Gop: conf.Gop}
8891
confCmd.Flags = pass.Args
8992
run(proj, args, !noChdir, conf, confCmd)

gengo.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ const (
4949
// -----------------------------------------------------------------------------
5050

5151
// GenGo generates gop_autogen.go for a Go+ package directory.
52-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
5352
func GenGo(dir string, conf *Config, genTestPkg bool) (string, bool, error) {
5453
return GenGoEx(dir, conf, genTestPkg, 0)
5554
}
5655

5756
// GenGoEx generates gop_autogen.go for a Go+ package directory.
58-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
5957
func GenGoEx(dir string, conf *Config, genTestPkg bool, flags GenFlags) (string, bool, error) {
6058
recursively := strings.HasSuffix(dir, "/...")
6159
if recursively {
@@ -214,7 +212,6 @@ const (
214212
)
215213

216214
// GenGoPkgPath generates gop_autogen.go for a Go+ package.
217-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
218215
func GenGoPkgPath(workDir, pkgPath string, conf *Config, allowExtern bool) (localDir string, recursively bool, err error) {
219216
return GenGoPkgPathEx(workDir, pkgPath, conf, allowExtern, 0)
220217
}
@@ -233,7 +230,6 @@ func remotePkgPath(pkgPath string, conf *Config, recursively bool, flags GenFlag
233230
}
234231

235232
// GenGoPkgPathEx generates gop_autogen.go for a Go+ package.
236-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
237233
func GenGoPkgPathEx(workDir, pkgPath string, conf *Config, allowExtern bool, flags GenFlags) (localDir string, recursively bool, err error) {
238234
recursively = strings.HasSuffix(pkgPath, "/...")
239235
if recursively {
@@ -276,8 +272,7 @@ func remotePkgPathDo(pkgPath string, doSth func(pkgDir, modDir string), onErr fu
276272
// -----------------------------------------------------------------------------
277273

278274
// GenGoFiles generates gop_autogen.go for specified Go+ files.
279-
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
280-
func GenGoFiles(autogen string, files []string, conf *Config) (result []string, err error) {
275+
func GenGoFiles(autogen string, files []string, conf *Config) (outFiles []string, err error) {
281276
if conf == nil {
282277
conf = new(Config)
283278
}
@@ -296,11 +291,11 @@ func GenGoFiles(autogen string, files []string, conf *Config) (result []string,
296291
err = errors.NewWith(err, `LoadFiles(files, conf)`, -2, "gop.LoadFiles", files, conf)
297292
return
298293
}
299-
result = append(result, autogen)
300294
err = out.WriteFile(autogen)
301295
if err != nil {
302296
err = errors.NewWith(err, `out.WriteFile(autogen)`, -2, "(*gox.Package).WriteFile", out, autogen)
303297
}
298+
outFiles = []string{autogen}
304299
return
305300
}
306301

imp.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ type Importer struct {
3737
mod *gopmod.Module
3838
gop *env.Gop
3939
fset *token.FileSet
40-
flags GenFlags
40+
41+
Flags GenFlags // can change this for loading Go+ modules
4142
}
4243

4344
func NewImporter(mod *gopmod.Module, gop *env.Gop, fset *token.FileSet) *Importer {
@@ -48,19 +49,20 @@ func NewImporter(mod *gopmod.Module, gop *env.Gop, fset *token.FileSet) *Importe
4849
mod = gopmod.Default
4950
}
5051
dir := ""
51-
if hasModfile(mod) {
52+
if mod.HasModfile() {
5253
dir = mod.Root()
5354
}
5455
impFrom := packages.NewImporter(fset, dir)
55-
return &Importer{mod: mod, gop: gop, impFrom: impFrom, fset: fset, flags: defaultFlags}
56+
return &Importer{mod: mod, gop: gop, impFrom: impFrom, fset: fset, Flags: defaultFlags}
5657
}
5758

59+
const (
60+
gopMod = "github.com/goplus/gop"
61+
)
62+
5863
func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
59-
const (
60-
gop = "github.com/goplus/gop"
61-
)
62-
if strings.HasPrefix(pkgPath, gop) {
63-
if suffix := pkgPath[len(gop):]; suffix == "" || suffix[0] == '/' {
64+
if strings.HasPrefix(pkgPath, gopMod) {
65+
if suffix := pkgPath[len(gopMod):]; suffix == "" || suffix[0] == '/' {
6466
gopRoot := p.gop.Root
6567
if suffix == "/cl/internal/gop-in-go/foo" {
6668
if err = p.genGoExtern(gopRoot+suffix, false); err != nil {
@@ -70,7 +72,7 @@ func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
7072
return p.impFrom.ImportFrom(pkgPath, gopRoot, 0)
7173
}
7274
}
73-
if mod := p.mod; hasModfile(mod) {
75+
if mod := p.mod; mod.HasModfile() {
7476
ret, e := mod.Lookup(pkgPath)
7577
if e != nil {
7678
if isPkgInMod(pkgPath, "github.com/qiniu/x") {
@@ -113,7 +115,7 @@ func (p *Importer) genGoExtern(dir string, isExtern bool) (err error) {
113115
defer os.Chmod(dir, modReadonly)
114116
}
115117
gen := false
116-
err = genGoIn(dir, &Config{Gop: p.gop, Importer: p, Fset: p.fset}, false, p.flags, &gen)
118+
err = genGoIn(dir, &Config{Gop: p.gop, Importer: p, Fset: p.fset}, false, p.Flags, &gen)
117119
if err != nil {
118120
return
119121
}

load.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ type Config struct {
123123

124124
Filter func(fs.FileInfo) bool
125125

126+
// If not nil, it is used for returning result of checks Go+ dependencies.
127+
// see https://pkg.go.dev/github.com/goplus/gox#File.CheckGopDeps
128+
GopDeps *int
129+
126130
IgnoreNotatedError bool
127131
DontUpdateGoMod bool
128132
}
@@ -177,11 +181,6 @@ func FilterNoTestFiles(fi fs.FileInfo) bool {
177181
return !strings.HasSuffix(fname, suffix)
178182
}
179183

180-
func hasModfile(mod *gopmod.Module) bool {
181-
f := mod.File
182-
return f != nil && f.Syntax != nil
183-
}
184-
185184
// -----------------------------------------------------------------------------
186185

187186
// LoadDir loads Go+ packages from a specified directory.
@@ -263,18 +262,27 @@ func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (ou
263262
if genTestPkg && pkgTest != nil {
264263
test, err = cl.NewPackage("", pkgTest, clConf)
265264
}
266-
saveWithGopMod(mod, gop, out, test, conf)
265+
afterLoad(mod, gop, out, test, conf)
267266
return
268267
}
269268

270-
func saveWithGopMod(mod *gopmod.Module, gop *env.Gop, out, test *gox.Package, conf *Config) {
271-
if !conf.DontUpdateGoMod && mod.HasModfile() {
269+
func afterLoad(mod *gopmod.Module, gop *env.Gop, out, test *gox.Package, conf *Config) {
270+
if mod.Path() == gopMod { // nothing to do for Go+ itself
271+
return
272+
}
273+
updateMod := !conf.DontUpdateGoMod && mod.HasModfile()
274+
if updateMod || conf.GopDeps != nil {
272275
flags := checkGopDeps(out)
273-
if test != nil {
274-
flags |= checkGopDeps(test)
276+
if conf.GopDeps != nil { // for `gop run`
277+
*conf.GopDeps = flags
275278
}
276-
if flags != 0 {
277-
mod.SaveWithGopMod(gop, flags)
279+
if updateMod {
280+
if test != nil {
281+
flags |= checkGopDeps(test)
282+
}
283+
if flags != 0 {
284+
mod.SaveWithGopMod(gop, flags)
285+
}
278286
}
279287
}
280288
}
@@ -287,7 +295,7 @@ func checkGopDeps(pkg *gox.Package) (flags int) {
287295
}
288296

289297
func relativeBaseOf(mod *gopmod.Module) string {
290-
if hasModfile(mod) {
298+
if mod.HasModfile() {
291299
return mod.Root()
292300
}
293301
dir, _ := os.Getwd()
@@ -347,7 +355,7 @@ func LoadFiles(dir string, files []string, conf *Config) (out *gox.Package, err
347355
}
348356
break
349357
}
350-
saveWithGopMod(mod, gop, out, nil, conf)
358+
afterLoad(mod, gop, out, nil, conf)
351359
return
352360
}
353361

x/gocmd/build_install.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,35 @@ package gocmd
2121
type InstallConfig = Config
2222

2323
func Install(arg string, conf *InstallConfig) (err error) {
24-
return doWithArgs("install", conf, arg)
24+
return doWithArgs("", "install", conf, arg)
2525
}
2626

2727
func InstallFiles(files []string, conf *InstallConfig) (err error) {
28-
return doWithArgs("install", conf, files...)
28+
return doWithArgs("", "install", conf, files...)
2929
}
3030

3131
// -----------------------------------------------------------------------------
3232

3333
type BuildConfig = Config
3434

3535
func Build(arg string, conf *BuildConfig) (err error) {
36-
return doWithArgs("build", conf, arg)
36+
return doWithArgs("", "build", conf, arg)
3737
}
3838

3939
func BuildFiles(files []string, conf *BuildConfig) (err error) {
40-
return doWithArgs("build", conf, files...)
40+
return doWithArgs("", "build", conf, files...)
4141
}
4242

4343
// -----------------------------------------------------------------------------
4444

4545
type TestConfig = Config
4646

4747
func Test(arg string, conf *TestConfig) (err error) {
48-
return doWithArgs("test", conf, arg)
48+
return doWithArgs("", "test", conf, arg)
4949
}
5050

5151
func TestFiles(files []string, conf *TestConfig) (err error) {
52-
return doWithArgs("test", conf, files...)
52+
return doWithArgs("", "test", conf, files...)
5353
}
5454

5555
// -----------------------------------------------------------------------------

x/gocmd/gocmd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Config struct {
3636

3737
// -----------------------------------------------------------------------------
3838

39-
func doWithArgs(op string, conf *Config, args ...string) (err error) {
39+
func doWithArgs(dir, op string, conf *Config, args ...string) (err error) {
4040
if conf == nil {
4141
conf = new(Config)
4242
}
@@ -50,6 +50,7 @@ func doWithArgs(op string, conf *Config, args ...string) (err error) {
5050
exargs = append(exargs, conf.Flags...)
5151
exargs = append(exargs, args...)
5252
cmd := exec.Command(goCmd, exargs...)
53+
cmd.Dir = dir
5354
run := conf.Run
5455
if run == nil {
5556
run = runCmd

0 commit comments

Comments
 (0)