mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-02-11 12:17:04 +00:00
38 lines
899 B
Go
38 lines
899 B
Go
package rp
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
InlineUpdateMarker = "x-releaser-pleaser-version"
|
|
)
|
|
|
|
var (
|
|
SemVerRegex = regexp.MustCompile(`(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(-(?<preRelease>[\w.]+))?(\+(?<build>[-\w.]+))?`)
|
|
)
|
|
|
|
type Updater interface {
|
|
UpdateContent(content, version string) string
|
|
}
|
|
|
|
type GenericUpdater struct{}
|
|
|
|
func (u *GenericUpdater) UpdateContent(content, version string) string {
|
|
// TODO: use buffered input/output
|
|
output := strings.Builder{}
|
|
output.Grow(len(content))
|
|
for _, line := range strings.Split(content, "\n") {
|
|
if strings.Contains(line, InlineUpdateMarker) {
|
|
// We strip the "v" prefix to avoid adding/removing it from the users input.
|
|
line = SemVerRegex.ReplaceAllLiteralString(line, strings.TrimPrefix(version, "v"))
|
|
}
|
|
|
|
output.WriteString(line)
|
|
// TODO: Fix added newline
|
|
output.WriteByte('\n')
|
|
}
|
|
|
|
return output.String()
|
|
}
|