Skip to content

Commit

Permalink
build(publish): parse correclty version
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jun 18, 2024
1 parent 7b79420 commit c48e110
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
14 changes: 6 additions & 8 deletions magefiles/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
type BumpVersion mg.Namespace

// Ts bumps the version of the typescript packages with the given version.
func (BumpVersion) Ts(version string) error {
func (BumpVersion) Ts(v string) error {
fmt.Println("🔖 Bump typescript packages version")

ensureYarn()

err := isVersionTagValid(version)
version, err := parseVersion(v)
if err != nil {
return err
}
Expand All @@ -38,7 +38,7 @@ func (BumpVersion) Ts(version string) error {
err := sh.Run("yarn",
"--cwd", filepath.Join(TS_DIR, pkg.Name()),
"version",
"--new-version", version,
"--new-version", version.String(),
"--allow-same-version",
"--no-git-tag-version")
if err != nil {
Expand All @@ -49,16 +49,14 @@ func (BumpVersion) Ts(version string) error {
}

// Go bumps the version of the go packages with the given version.
func (BumpVersion) Go(version string) error {
func (BumpVersion) Go(v string) error {
fmt.Println("🔖 Bump go packages version")

err := isVersionTagValid(version)
version, err := parseVersion(v)
if err != nil {
return err
}

majorVersion := strings.Split(version, ".")[0]

packages, err := os.ReadDir(GO_DIR)
if err != nil {
return fmt.Errorf("failed to read go directory '%s': %w", GO_DIR, err)
Expand All @@ -79,7 +77,7 @@ func (BumpVersion) Go(version string) error {
moduleNameUnversioned := regexp.
MustCompile(`/v[0-9]+$`).
ReplaceAllString(moduleName, "")
moduleNameVersioned := fmt.Sprintf("%s/%s", moduleNameUnversioned, majorVersion)
moduleNameVersioned := fmt.Sprintf("%s/v%d", moduleNameUnversioned, version.Major)

fmt.Printf(" 🔬 Updating module path to %s\n", moduleNameVersioned)
err = runInPath(filepath.Join(GO_DIR, pkg.Name()), "go",
Expand Down
35 changes: 30 additions & 5 deletions magefiles/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"

"github.com/magefile/mage/sh"
Expand Down Expand Up @@ -38,18 +39,42 @@ func outputInPath(path string, cmd string, args ...string) (_ string, err error)
return sh.Output(cmd, args...)
}

func isVersionTagValid(tag string) error {
type Version struct {
Major int
Minor int
Patch int
}

func parseVersion(tag string) (*Version, error) {
pattern := `v(?P<Major>0|(?:[1-9]\d*))(?:\.(?P<Minor>0|(?:[1-9]\d*))(?:\.(?P<Patch>0|(?:[1-9]\d*)))?(?:\-(?P<PreRelease>[0-9A-Z\.-]+))?(?:\+(?P<Meta>[0-9A-Z\.-]+))?)?`
re, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("failed to compile regex: %w", err)
return nil, fmt.Errorf("failed to compile regex: %w", err)
}

if !re.MatchString(tag) {
return fmt.Errorf("tag version '%s' is not valid, should be 'vX.Y.Z'", tag)
matches := re.FindStringSubmatch(tag)
if matches == nil {
return nil, fmt.Errorf("tag version '%s' is not valid, should be 'vX.Y.Z'", tag)
}
version := &Version{}
for i, name := range re.SubexpNames() {
switch name {
case "Major":
version.Major, err = strconv.Atoi(matches[i])
case "Minor":
version.Minor, err = strconv.Atoi(matches[i])
case "Patch":
version.Patch, err = strconv.Atoi(matches[i])
}
if err != nil {
return nil, fmt.Errorf("failed to parse version %s: %w", name, err)
}
}
return version, nil
}

return nil
func (v *Version) String() string {
return fmt.Sprintf("v%d.%d.%d", v.Major, v.Minor, v.Patch)
}

// EnsureGit ensures that git is installed, if not it panics.
Expand Down

0 comments on commit c48e110

Please sign in to comment.