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

feat: ignore go build constraints #22

Merged
merged 1 commit into from
Sep 21, 2023
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
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