Skip to content

Commit

Permalink
Merge pull request #1453 from visualfc/gengo_check
Browse files Browse the repository at this point in the history
gop: go add -t check mode
  • Loading branch information
xushiwei authored Oct 8, 2023
2 parents 908c373 + 3612d2d commit 3d6a59e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
11 changes: 6 additions & 5 deletions cmd/internal/gengo/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

// Package gengo implements the ``gop go'' command.
// Package gengo implements the gop go command.
package gengo

import (
Expand All @@ -37,8 +37,9 @@ var Cmd = &base.Command{
}

var (
flagVerbose = flag.Bool("v", false, "print verbose information.")
flag = &Cmd.Flag
flag = &Cmd.Flag
flagVerbose = flag.Bool("v", false, "print verbose information.")
flagCheckMode = flag.Bool("t", false, "check mode, no generate gop_autogen.go")
)

func init() {
Expand Down Expand Up @@ -69,9 +70,9 @@ func runCmd(cmd *base.Command, args []string) {
for _, proj := range projs {
switch v := proj.(type) {
case *gopprojs.DirProj:
_, _, err = gop.GenGo(v.Dir, nil, true)
_, _, err = gop.GenGoEx(v.Dir, nil, true, *flagCheckMode)
case *gopprojs.PkgPathProj:
_, _, err = gop.GenGoPkgPath("", v.Path, nil, true)
_, _, err = gop.GenGoPkgPathEx("", v.Path, nil, true, *flagCheckMode)
default:
log.Panicln("`gop go` doesn't support", reflect.TypeOf(v))
}
Expand Down
26 changes: 18 additions & 8 deletions gengo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,26 @@ const (
// -----------------------------------------------------------------------------

func GenGo(dir string, conf *Config, genTestPkg bool) (string, bool, error) {
return GenGoEx(dir, conf, genTestPkg, false)
}

func GenGoEx(dir string, conf *Config, genTestPkg bool, checkOnly bool) (string, bool, error) {
recursively := strings.HasSuffix(dir, "/...")
if recursively {
dir = dir[:len(dir)-4]
}
return dir, recursively, genGoDir(dir, conf, genTestPkg, recursively)
return dir, recursively, genGoDir(dir, conf, genTestPkg, checkOnly, recursively)
}

func genGoDir(dir string, conf *Config, genTestPkg, recursively bool) (err error) {
func genGoDir(dir string, conf *Config, genTestPkg, checkOnly, recursively bool) (err error) {
if recursively {
var list errors.List
fn := func(path string, d fs.DirEntry, err error) error {
if err == nil && d.IsDir() {
if strings.HasPrefix(d.Name(), "_") { // skip _
return filepath.SkipDir
}
if e := genGoIn(path, conf, genTestPkg, true); e != nil {
if e := genGoIn(path, conf, genTestPkg, checkOnly, true); e != nil {
list.Add(e)
}
}
Expand All @@ -66,18 +70,20 @@ func genGoDir(dir string, conf *Config, genTestPkg, recursively bool) (err error
}
return list.ToError()
}
return genGoIn(dir, conf, genTestPkg, false)
return genGoIn(dir, conf, genTestPkg, false, false)
}

func genGoIn(dir string, conf *Config, genTestPkg, prompt bool, gen ...*bool) (err error) {
func genGoIn(dir string, conf *Config, genTestPkg, checkOnly, prompt bool, gen ...*bool) (err error) {
out, test, err := LoadDir(dir, conf, genTestPkg, prompt)
if err != nil {
if err == syscall.ENOENT { // no Go+ source files
return nil
}
return errors.NewWith(err, `LoadDir(dir, conf, genTestPkg, prompt)`, -5, "gop.LoadDir", dir, conf, genTestPkg, prompt)
}

if checkOnly {
return nil
}
os.MkdirAll(dir, 0755)
file := filepath.Join(dir, autoGenFile)
err = out.WriteFile(file)
Expand Down Expand Up @@ -114,6 +120,10 @@ const (
)

func GenGoPkgPath(workDir, pkgPath string, conf *Config, allowExtern bool) (localDir string, recursively bool, err error) {
return GenGoPkgPathEx(workDir, pkgPath, conf, allowExtern, false)
}

func GenGoPkgPathEx(workDir, pkgPath string, conf *Config, allowExtern bool, checkOnly bool) (localDir string, recursively bool, err error) {
recursively = strings.HasSuffix(pkgPath, "/...")
if recursively {
pkgPath = pkgPath[:len(pkgPath)-4]
Expand All @@ -125,7 +135,7 @@ func GenGoPkgPath(workDir, pkgPath string, conf *Config, allowExtern bool) (loca
os.Chmod(dir, modWritable)
defer os.Chmod(dir, modReadonly)
localDir = dir
err = genGoDir(dir, conf, false, recursively)
err = genGoDir(dir, conf, false, checkOnly, recursively)
}, func(e error) {
err = e
})
Expand All @@ -143,7 +153,7 @@ func GenGoPkgPath(workDir, pkgPath string, conf *Config, allowExtern bool) (loca
os.Chmod(localDir, modWritable)
defer os.Chmod(localDir, modReadonly)
}
err = genGoDir(localDir, conf, false, recursively)
err = genGoDir(localDir, conf, false, checkOnly, recursively)
return
}

Expand Down
2 changes: 1 addition & 1 deletion imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,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, true, &gen)
err = genGoIn(dir, &Config{Gop: p.gop, Importer: p, Fset: p.fset}, false, false, true, &gen)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Tidy(dir string, gop *env.Gop) (err error) {
}

conf := &Config{DontUpdateGoMod: true, Gop: gop}
err = genGoDir(modRoot, conf, true, true)
err = genGoDir(modRoot, conf, true, false, true)
if err != nil {
return errors.NewWith(err, `genGoDir(modRoot, conf, true, true)`, -2, "gop.genGoDir", modRoot, conf, true, true)
}
Expand Down

0 comments on commit 3d6a59e

Please sign in to comment.