github.com/windmeup/goreleaser@v1.21.95/internal/pipe/semver/semver.go (about)

     1  package semver
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/Masterminds/semver/v3"
     6  	"strings"
     7  
     8  	"github.com/windmeup/goreleaser/pkg/context"
     9  )
    10  
    11  // Pipe is a global hook pipe.
    12  type Pipe struct{}
    13  
    14  // String is the name of this pipe.
    15  func (Pipe) String() string {
    16  	return "parsing tag"
    17  }
    18  
    19  // Run executes the hooks.
    20  func (Pipe) Run(ctx *context.Context) error {
    21  	version, err := monorepo(ctx)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	sv, err := semver.NewVersion(version)
    26  	if err != nil {
    27  		return fmt.Errorf("failed to parse tag '%s' as semver: %w", ctx.Git.CurrentTag, err)
    28  	}
    29  	ctx.Version = strings.TrimPrefix(version, "v")
    30  	ctx.Semver = context.Semver{
    31  		Major:      sv.Major(),
    32  		Minor:      sv.Minor(),
    33  		Patch:      sv.Patch(),
    34  		Prerelease: sv.Prerelease(),
    35  	}
    36  	return nil
    37  }
    38  
    39  func monorepo(ctx *context.Context) (string, error) {
    40  	currentTag := ctx.Git.CurrentTag
    41  	if prefix := ctx.Config.Monorepo.TagPrefix; !strings.HasPrefix(currentTag, prefix) {
    42  		return "", fmt.Errorf("failed to parse monorepo tag '%s': must starts with '%s'", currentTag, prefix)
    43  	}
    44  	parts := strings.Split(currentTag, "/")
    45  	return parts[len(parts)-1], nil
    46  }