Skip to content

Commit

Permalink
Correctly upgrade sdk/go.mod during major version bumps (#129)
Browse files Browse the repository at this point in the history
* Correctly upgrade sdk/go.mod during major version bumps

* Simplify
  • Loading branch information
iwahbe authored Aug 31, 2023
1 parent 2149a46 commit 31867f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
67 changes: 25 additions & 42 deletions upgrade/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"path"
"path/filepath"
"reflect"
"regexp"
"strings"

Expand Down Expand Up @@ -550,19 +551,18 @@ func MajorVersionBump(ctx Context, goMod *GoMod, target *UpstreamUpgradeTarget,
return nil
}

prev := "provider"
prev := ""
if repo.currentVersion.Major() > 1 {
prev += fmt.Sprintf("/v%d", repo.currentVersion.Major())
}

nextMajorVersion := fmt.Sprintf("v%d", repo.currentVersion.Major()+1)
next := fmt.Sprintf("/v%d", repo.currentVersion.IncMajor().Major())

// Replace s in file, where {} is interpolated into the old and new provider
// component of the path.
replaceInFile := func(desc, path, s string) step.Step {
return UpdateFile(desc+" in "+path, path, func(src []byte) ([]byte, error) {
old := strings.ReplaceAll(s, "{}", prev)
new := strings.ReplaceAll(s, "{}", "provider/"+nextMajorVersion)
new := strings.ReplaceAll(s, "{}", next)
return bytes.ReplaceAll(src, []byte(old), []byte(new)), nil
})
}
Expand All @@ -571,30 +571,23 @@ func MajorVersionBump(ctx Context, goMod *GoMod, target *UpstreamUpgradeTarget,
return step.Combined("Increment Major Version",
step.F("Next major version", func() (string, error) {
// This step displays the next major version to the user.
return nextMajorVersion, nil
return repo.currentVersion.IncMajor().String(), nil
}),
replaceInFile("Update PROVIDER_PATH", "Makefile",
"PROVIDER_PATH := {}").In(&repo.root),
"PROVIDER_PATH := provider{}",
).In(&repo.root),
replaceInFile("Update -X Version", ".goreleaser.yml",
"github.com/pulumi/"+name+"/{}/pkg").In(&repo.root),
"github.com/pulumi/"+name+"/provider{}/pkg",
).In(&repo.root),
replaceInFile("Update -X Version", ".goreleaser.prerelease.yml",
"github.com/pulumi/"+name+"/{}/pkg").In(&repo.root),
replaceInFile("Update Go Module", "go.mod",
"module github.com/pulumi/"+name+"/{}").In(repo.providerDir()),
step.F("Update sdk/go.mod", func() (string, error) {
path := "sdk/go.mod"
f, err := os.ReadFile(path)
if err != nil {
return "", err
}
mod := fmt.Sprintf("github.com/%s/%s/sdk", repo.org, repo.name)
var prev string
if m := repo.currentVersion.Major(); m > 1 {
prev = fmt.Sprintf("/%d", m)
}
f = bytes.ReplaceAll(f, []byte(mod+prev), []byte(mod+nextMajorVersion))
return "", os.WriteFile(path, f, 0600)
}).In(&repo.root),
"github.com/pulumi/"+name+"/provider{}/pkg",
).In(&repo.root),
replaceInFile("Update Go Module (provider)", "go.mod",
"module github.com/pulumi/"+name+"/provider{}",
).In(repo.providerDir()),
replaceInFile("Update Go Module (sdk)", "go.mod",
"module github.com/pulumi/"+name+"/sdk{}",
).In(repo.sdkDir()),
step.F("Update Go Imports", func() (string, error) {
var filesUpdated int
var fn filepath.WalkFunc = func(path string, info fs.FileInfo, err error) error {
Expand All @@ -608,8 +601,8 @@ func MajorVersionBump(ctx Context, goMod *GoMod, target *UpstreamUpgradeTarget,
}

new := bytes.ReplaceAll(data,
[]byte("github.com/pulumi/"+name+"/"+prev),
[]byte("github.com/pulumi/"+name+"/"+"provider/"+nextMajorVersion),
[]byte("github.com/pulumi/"+name+"/provider"+prev),
[]byte("github.com/pulumi/"+name+"/provider"+next),
)

if !goMod.Kind.IsPatched() && !goMod.Kind.IsForked() {
Expand All @@ -625,20 +618,7 @@ func MajorVersionBump(ctx Context, goMod *GoMod, target *UpstreamUpgradeTarget,
}
}

// If the length changed, then something changed
updated := len(data) != len(new)
if !updated {
// If the length stayed the same, we can check
// each bit.
for i := 0; i < len(data); i++ {
if data[i] != new[i] {
updated = true
break
}
}
}

if updated {
if !reflect.DeepEqual(data, new) {
filesUpdated++
return os.WriteFile(path, new, info.Mode().Perm())
}
Expand Down Expand Up @@ -770,11 +750,14 @@ func UpdateFile(desc, path string, f func([]byte) ([]byte, error)) step.Step {
if err != nil {
return "", err
}
bytes, err = f(bytes)
newBytes, err := f(bytes)
if err != nil {
return "", err
}
return "", os.WriteFile(path, bytes, stats.Mode().Perm())
if reflect.DeepEqual(newBytes, bytes) {
return "no change", nil
}
return "", os.WriteFile(path, newBytes, stats.Mode().Perm())
})
}

Expand Down
5 changes: 5 additions & 0 deletions upgrade/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (p ProviderRepo) examplesDir() *string {
return &dir
}

func (p ProviderRepo) sdkDir() *string {
dir := filepath.Join(p.root, "sdk")
return &dir
}

type GoMod struct {
Kind RepoKind
Upstream module.Version
Expand Down

0 comments on commit 31867f7

Please sign in to comment.