Skip to content

Commit

Permalink
feat: ignore go build constraints (#22)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: mod will now ignore go build constraints and upgrade/downgrade
                 Semantic Import Versioning in all .go files.
                 The flag `buildflags` has been removed.
  • Loading branch information
pascal-hofmann committed Sep 21, 2023
1 parent d88d15f commit 899a558
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
11 changes: 2 additions & 9 deletions cmd/mod/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ import (
)

func main() {
buildFlags := &cli.StringSliceFlag{
Name: "buildflags",
Usage: "build flags to pass to the go compiler. Most commonly use for build flags ie. 'mod upgrade -buildflags=-tags=dev'",
}
app := &cli.App{
Name: "mod",
Usage: "upgrade/downgrade semantic import versioning",
Flags: []cli.Flag{buildFlags},
Commands: []*cli.Command{
{
Name: "upgrade",
Expand All @@ -33,7 +28,6 @@ func main() {
Name: "mod-name",
Value: "",
},
buildFlags,
},
},
{
Expand All @@ -46,7 +40,6 @@ func main() {
Name: "mod-name",
Value: "",
},
buildFlags,
},
},
{
Expand Down Expand Up @@ -76,11 +69,11 @@ func main() {
}

func upgrade(c *cli.Context) error {
return major.Run(".", "upgrade", c.String("mod-name"), c.Int("tag"), c.StringSlice("buildflags"))
return major.Run(".", "upgrade", c.String("mod-name"), c.Int("tag"))
}

func downgrade(c *cli.Context) error {
return major.Run(".", "downgrade", c.String("mod-name"), 0, c.StringSlice("buildflags"))
return major.Run(".", "downgrade", c.String("mod-name"), 0)
}

func migrateDeps(c *cli.Context) error {
Expand Down
2 changes: 2 additions & 0 deletions example/subpkg/subpkg.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build something

package subpkg

import (
Expand Down
29 changes: 20 additions & 9 deletions major/major.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
Expand All @@ -20,7 +21,8 @@ import (

// Run upgrades or downgrades a module path and
// all of its dependencies.
func Run(dir, op, modName string, tag int, buildFlags []string) error {
func Run(dir, op, modName string, tag int) error {

client := true
var modFile *modfile.File
modFile, err := mod.GetModFile(dir)
Expand All @@ -39,7 +41,7 @@ func Run(dir, op, modName string, tag int, buildFlags []string) error {
case "downgrade":
newModPath = getPrevious(modName, separator)
}
c := &packages.Config{Mode: packages.LoadSyntax, Tests: true, Dir: dir, BuildFlags: buildFlags}
c := &packages.Config{Mode: packages.NeedName | packages.NeedFiles, Tests: true, Dir: dir}
pkgs, err := packages.Load(c, "./...")
if err != nil {
return errors.Wrap(err, "could not load package")
Expand All @@ -66,7 +68,7 @@ func Run(dir, op, modName string, tag int, buildFlags []string) error {
if err != nil {
return errors.Wrap(err, "could not format go.mod file with new import path")
}
err = ioutil.WriteFile(filepath.Join(dir, "go.mod"), bts, 0660)
err = os.WriteFile(filepath.Join(dir, "go.mod"), bts, 0660)
if err != nil {
return errors.Wrap(err, "could not rewrite go.mod file")
}
Expand Down Expand Up @@ -146,18 +148,27 @@ func getPrevious(s, sep string) string {
}

func updateImportPath(p *packages.Package, old, new, sep string, files map[string]struct{}) error {
for _, syn := range p.Syntax {
goFileName := p.Fset.File(syn.Pos()).Name()

goFileNames := append(p.GoFiles, p.IgnoredFiles...)
for _, goFileName := range goFileNames {

if _, ok := files[goFileName]; ok {
continue
}
files[goFileName] = struct{}{}

fset := token.NewFileSet()
parsed, err := parser.ParseFile(fset, goFileName, nil, parser.ParseComments)
if err != nil {
return errors.Wrapf(err, "could not parse go file %v", goFileName)
}

var rewritten bool
for _, i := range syn.Imports {
for _, i := range parsed.Imports {
imp := strings.Replace(i.Path.Value, `"`, ``, 2)
if strings.HasPrefix(imp, fmt.Sprintf("%s%s", old, sep)) || imp == old {
newImp := strings.Replace(imp, old, new, 1)
rewrote := astutil.RewriteImport(p.Fset, syn, imp, newImp)
rewrote := astutil.RewriteImport(fset, parsed, imp, newImp)
if rewrote {
rewritten = true
}
Expand All @@ -171,7 +182,7 @@ func updateImportPath(p *packages.Package, old, new, sep string, files map[strin
if err != nil {
return errors.Wrapf(err, "could not create go file %v", goFileName)
}
err = format.Node(f, p.Fset, syn)
err = format.Node(f, fset, parsed)
f.Close()
if err != nil {
return errors.Wrapf(err, "could not rewrite go file %v", goFileName)
Expand Down
2 changes: 1 addition & 1 deletion migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func migrate(path string, gc *github.Client, test bool) error {
}
t := getTag(dir)
fmt.Printf("upgrading %v to v%v\n", path, t)
if err := major.Run(dir, "upgrade", "", t, nil); err != nil {
if err := major.Run(dir, "upgrade", "", t); err != nil {
return errors.Wrap(err, "err upgrading import paths")
}

Expand Down

0 comments on commit 899a558

Please sign in to comment.