Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gop run: support build dir #1748

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions build_install_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
// -----------------------------------------------------------------------------

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

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

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

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

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

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

// -----------------------------------------------------------------------------

// If no go.mod and used Go+, use GOPROOT as buildDir.
func getBuildDir(conf *Config) string {
if conf != nil && conf.GopDeps != nil && *conf.GopDeps != 0 {
return conf.Gop.Root
}
return ""
}

// RunDir runs an application from a Go+ package directory.
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
func RunDir(dir string, args []string, conf *Config, run *gocmd.RunConfig) (err error) {
_, _, err = GenGo(dir, conf, false)
if err != nil {
return errors.NewWith(err, `GenGo(dir, conf, false)`, -2, "gop.GenGo", dir, conf, false)
}
return gocmd.RunDir(dir, args, run)
return gocmd.RunDir(getBuildDir(conf), dir, args, run)
}

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

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

// -----------------------------------------------------------------------------

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

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

// TestFiles tests specified Go+ files.
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
func TestFiles(files []string, conf *Config, test *gocmd.TestConfig) (err error) {
files, err = GenGoFiles("", files, conf)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func runCmd(cmd *base.Command, args []string) {
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
if !conf.Mod.HasModfile() { // if no go.mod, check GopDeps
conf.GopDeps = new(int)
}
confCmd := &gocmd.Config{Gop: conf.Gop}
confCmd.Flags = pass.Args
run(proj, args, !noChdir, conf, confCmd)
Expand Down
9 changes: 2 additions & 7 deletions gengo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ const (
// -----------------------------------------------------------------------------

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

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

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

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

// GenGoFiles generates gop_autogen.go for specified Go+ files.
// if conf != nil && conf.Context == nil, it will be set with `gox.NewContext()`.
func GenGoFiles(autogen string, files []string, conf *Config) (result []string, err error) {
func GenGoFiles(autogen string, files []string, conf *Config) (outFiles []string, err error) {
if conf == nil {
conf = new(Config)
}
Expand All @@ -296,11 +291,11 @@ func GenGoFiles(autogen string, files []string, conf *Config) (result []string,
err = errors.NewWith(err, `LoadFiles(files, conf)`, -2, "gop.LoadFiles", files, conf)
return
}
result = append(result, autogen)
err = out.WriteFile(autogen)
if err != nil {
err = errors.NewWith(err, `out.WriteFile(autogen)`, -2, "(*gox.Package).WriteFile", out, autogen)
}
outFiles = []string{autogen}
return
}

Expand Down
22 changes: 12 additions & 10 deletions imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type Importer struct {
mod *gopmod.Module
gop *env.Gop
fset *token.FileSet
flags GenFlags

Flags GenFlags // can change this for loading Go+ modules
}

func NewImporter(mod *gopmod.Module, gop *env.Gop, fset *token.FileSet) *Importer {
Expand All @@ -48,19 +49,20 @@ func NewImporter(mod *gopmod.Module, gop *env.Gop, fset *token.FileSet) *Importe
mod = gopmod.Default
}
dir := ""
if hasModfile(mod) {
if mod.HasModfile() {
dir = mod.Root()
}
impFrom := packages.NewImporter(fset, dir)
return &Importer{mod: mod, gop: gop, impFrom: impFrom, fset: fset, flags: defaultFlags}
return &Importer{mod: mod, gop: gop, impFrom: impFrom, fset: fset, Flags: defaultFlags}
}

const (
gopMod = "github.com/goplus/gop"
)

func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
const (
gop = "github.com/goplus/gop"
)
if strings.HasPrefix(pkgPath, gop) {
if suffix := pkgPath[len(gop):]; suffix == "" || suffix[0] == '/' {
if strings.HasPrefix(pkgPath, gopMod) {
if suffix := pkgPath[len(gopMod):]; suffix == "" || suffix[0] == '/' {
gopRoot := p.gop.Root
if suffix == "/cl/internal/gop-in-go/foo" {
if err = p.genGoExtern(gopRoot+suffix, false); err != nil {
Expand All @@ -70,7 +72,7 @@ func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
return p.impFrom.ImportFrom(pkgPath, gopRoot, 0)
}
}
if mod := p.mod; hasModfile(mod) {
if mod := p.mod; mod.HasModfile() {
ret, e := mod.Lookup(pkgPath)
if e != nil {
if isPkgInMod(pkgPath, "github.com/qiniu/x") {
Expand Down Expand Up @@ -113,7 +115,7 @@ func (p *Importer) genGoExtern(dir string, isExtern bool) (err error) {
defer os.Chmod(dir, modReadonly)
}
gen := false
err = genGoIn(dir, &Config{Gop: p.gop, Importer: p, Fset: p.fset}, false, p.flags, &gen)
err = genGoIn(dir, &Config{Gop: p.gop, Importer: p, Fset: p.fset}, false, p.Flags, &gen)
if err != nil {
return
}
Expand Down
36 changes: 22 additions & 14 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ type Config struct {

Filter func(fs.FileInfo) bool

// If not nil, it is used for returning result of checks Go+ dependencies.
// see https://pkg.go.dev/github.com/goplus/gox#File.CheckGopDeps
GopDeps *int

IgnoreNotatedError bool
DontUpdateGoMod bool
}
Expand Down Expand Up @@ -177,11 +181,6 @@ func FilterNoTestFiles(fi fs.FileInfo) bool {
return !strings.HasSuffix(fname, suffix)
}

func hasModfile(mod *gopmod.Module) bool {
f := mod.File
return f != nil && f.Syntax != nil
}

// -----------------------------------------------------------------------------

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

func saveWithGopMod(mod *gopmod.Module, gop *env.Gop, out, test *gox.Package, conf *Config) {
if !conf.DontUpdateGoMod && mod.HasModfile() {
func afterLoad(mod *gopmod.Module, gop *env.Gop, out, test *gox.Package, conf *Config) {
if mod.Path() == gopMod { // nothing to do for Go+ itself
return
}
updateMod := !conf.DontUpdateGoMod && mod.HasModfile()
if updateMod || conf.GopDeps != nil {
flags := checkGopDeps(out)
if test != nil {
flags |= checkGopDeps(test)
if conf.GopDeps != nil { // for `gop run`
*conf.GopDeps = flags
}
if flags != 0 {
mod.SaveWithGopMod(gop, flags)
if updateMod {
if test != nil {
flags |= checkGopDeps(test)
}
if flags != 0 {
mod.SaveWithGopMod(gop, flags)
}
}
}
}
Expand All @@ -287,7 +295,7 @@ func checkGopDeps(pkg *gox.Package) (flags int) {
}

func relativeBaseOf(mod *gopmod.Module) string {
if hasModfile(mod) {
if mod.HasModfile() {
return mod.Root()
}
dir, _ := os.Getwd()
Expand Down Expand Up @@ -347,7 +355,7 @@ func LoadFiles(dir string, files []string, conf *Config) (out *gox.Package, err
}
break
}
saveWithGopMod(mod, gop, out, nil, conf)
afterLoad(mod, gop, out, nil, conf)
return
}

Expand Down
12 changes: 6 additions & 6 deletions x/gocmd/build_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,35 @@ package gocmd
type InstallConfig = Config

func Install(arg string, conf *InstallConfig) (err error) {
return doWithArgs("install", conf, arg)
return doWithArgs("", "install", conf, arg)
}

func InstallFiles(files []string, conf *InstallConfig) (err error) {
return doWithArgs("install", conf, files...)
return doWithArgs("", "install", conf, files...)
}

// -----------------------------------------------------------------------------

type BuildConfig = Config

func Build(arg string, conf *BuildConfig) (err error) {
return doWithArgs("build", conf, arg)
return doWithArgs("", "build", conf, arg)
}

func BuildFiles(files []string, conf *BuildConfig) (err error) {
return doWithArgs("build", conf, files...)
return doWithArgs("", "build", conf, files...)
}

// -----------------------------------------------------------------------------

type TestConfig = Config

func Test(arg string, conf *TestConfig) (err error) {
return doWithArgs("test", conf, arg)
return doWithArgs("", "test", conf, arg)
}

func TestFiles(files []string, conf *TestConfig) (err error) {
return doWithArgs("test", conf, files...)
return doWithArgs("", "test", conf, files...)
}

// -----------------------------------------------------------------------------
3 changes: 2 additions & 1 deletion x/gocmd/gocmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Config struct {

// -----------------------------------------------------------------------------

func doWithArgs(op string, conf *Config, args ...string) (err error) {
func doWithArgs(dir, op string, conf *Config, args ...string) (err error) {
if conf == nil {
conf = new(Config)
}
Expand All @@ -50,6 +50,7 @@ func doWithArgs(op string, conf *Config, args ...string) (err error) {
exargs = append(exargs, conf.Flags...)
exargs = append(exargs, args...)
cmd := exec.Command(goCmd, exargs...)
cmd.Dir = dir
run := conf.Run
if run == nil {
run = runCmd
Expand Down
Loading