Go: Add methods to GoModule for the tidy and vendor commands

These ensure that the module path is used automatically
This commit is contained in:
Michael B. Gale
2024-05-08 11:41:57 +01:00
parent 881b2586e1
commit d344f720aa
2 changed files with 13 additions and 2 deletions

View File

@@ -168,7 +168,7 @@ func tryUpdateGoModAndGoSum(workspace project.GoWorkspace) {
beforeGoSumFileInfo, beforeGoSumErr := os.Stat(goSumPath)
// run `go mod tidy -e`
cmd := toolchain.TidyModule(goModDir)
cmd := goMod.Tidy()
res := util.RunCmd(cmd)
if !res {
@@ -428,7 +428,7 @@ func installDependencies(workspace project.GoWorkspace) {
path := filepath.Dir(module.Path)
if util.DirExists(filepath.Join(path, "vendor")) {
vendor := toolchain.VendorModule(path)
vendor := module.Vendor()
log.Printf("Synchronizing vendor file using `go mod vendor` in %s.\n", path)
util.RunCmd(vendor)
}

View File

@@ -3,6 +3,7 @@ package project
import (
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
@@ -48,6 +49,16 @@ func (module *GoModule) RequiredGoVersion() util.SemVer {
}
}
// Runs `go mod tidy` for this module.
func (module *GoModule) Tidy() *exec.Cmd {
return toolchain.TidyModule(filepath.Dir(module.Path))
}
// Runs `go mod vendor -e` for this module.
func (module *GoModule) Vendor() *exec.Cmd {
return toolchain.VendorModule(filepath.Dir(module.Path))
}
// Represents information about a Go project workspace: this may either be a folder containing
// a `go.work` file or a collection of `go.mod` files.
type GoWorkspace struct {