2017-04-13 10:59:05 -04:00
|
|
|
// Copyright 2017-present The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-01-03 14:09:03 -05:00
|
|
|
// Package releaser implements a set of utilities to help automate the
|
|
|
|
// Hugo release process.
|
2017-04-13 10:59:05 -04:00
|
|
|
package releaser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2017-07-05 19:32:55 -04:00
|
|
|
"strings"
|
2017-04-13 10:59:05 -04:00
|
|
|
|
2020-12-18 12:20:12 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hexec"
|
2018-11-26 04:11:22 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hugo"
|
2017-04-13 10:59:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const commitPrefix = "releaser:"
|
|
|
|
|
2024-03-15 12:25:52 -04:00
|
|
|
// New initializes a ReleaseHandler.
|
2022-08-05 10:35:24 -04:00
|
|
|
func New(skipPush, try bool, step int) (*ReleaseHandler, error) {
|
|
|
|
if step < 1 || step > 2 {
|
|
|
|
return nil, fmt.Errorf("step must be 1 or 2")
|
|
|
|
}
|
2017-04-13 10:59:05 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
prefix := "release-"
|
|
|
|
branch, err := git("rev-parse", "--abbrev-ref", "HEAD")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-29 05:47:37 -04:00
|
|
|
branch = strings.TrimSpace(branch)
|
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
if !strings.HasPrefix(branch, prefix) {
|
|
|
|
return nil, fmt.Errorf("branch %q is not a release branch", branch)
|
2017-09-10 11:14:02 -04:00
|
|
|
}
|
2017-04-13 10:59:05 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
version := strings.TrimPrefix(branch, prefix)
|
2017-09-10 11:14:02 -04:00
|
|
|
version = strings.TrimPrefix(version, "v")
|
2022-08-29 05:47:37 -04:00
|
|
|
|
|
|
|
logf("Branch: %s|Version: v%s\n", branch, version)
|
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
rh := &ReleaseHandler{branchVersion: version, skipPush: skipPush, try: try, step: step}
|
2017-07-05 03:43:47 -04:00
|
|
|
|
|
|
|
if try {
|
|
|
|
rh.git = func(args ...string) (string, error) {
|
2022-08-05 10:35:24 -04:00
|
|
|
logln("git", strings.Join(args, " "))
|
2017-07-05 03:43:47 -04:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rh.git = git
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
return rh, nil
|
2017-04-13 10:59:05 -04:00
|
|
|
}
|
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
// ReleaseHandler provides functionality to release a new version of Hugo.
|
|
|
|
// Test this locally without doing an actual release:
|
|
|
|
// go run -tags release main.go release --skip-publish --try -r 0.90.0
|
|
|
|
// Or a variation of the above -- the skip-publish flag makes sure that any changes are performed to the local Git only.
|
|
|
|
type ReleaseHandler struct {
|
|
|
|
branchVersion string
|
2017-04-13 10:59:05 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
// 1 or 2.
|
|
|
|
step int
|
2021-12-08 03:23:18 -05:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
// No remote pushes.
|
|
|
|
skipPush bool
|
|
|
|
|
|
|
|
// Just simulate, no actual changes.
|
|
|
|
try bool
|
|
|
|
|
|
|
|
git func(args ...string) (string, error)
|
|
|
|
}
|
2017-04-13 10:59:05 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
// Run creates a new release.
|
|
|
|
func (r *ReleaseHandler) Run() error {
|
|
|
|
newVersion, finalVersion := r.calculateVersions()
|
2017-04-13 10:59:05 -04:00
|
|
|
version := newVersion.String()
|
|
|
|
tag := "v" + version
|
2019-07-30 03:02:03 -04:00
|
|
|
mainVersion := newVersion
|
|
|
|
mainVersion.PatchLevel = 0
|
2017-04-13 10:59:05 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
defer r.gitPush()
|
2017-05-20 03:58:08 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
if r.step == 1 {
|
|
|
|
if err := r.bumpVersions(newVersion); err != nil {
|
|
|
|
return err
|
2017-05-20 03:58:08 -04:00
|
|
|
}
|
2017-04-13 10:59:05 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
if _, err := r.git("commit", "-a", "-m", fmt.Sprintf("%s Bump versions for release of %s\n\n[ci skip]", commitPrefix, newVersion)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-20 04:11:23 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
// The above commit will be the target for this release, so print it to the console in a env friendly way.
|
|
|
|
sha, err := git("rev-parse", "HEAD")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-03 03:29:59 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
// Hugoreleaser will do the actual release using these values.
|
|
|
|
if err := r.replaceInFile("hugoreleaser.env",
|
|
|
|
`HUGORELEASER_TAG=(\S*)`, "HUGORELEASER_TAG="+tag,
|
|
|
|
`HUGORELEASER_COMMITISH=(\S*)`, "HUGORELEASER_COMMITISH="+sha,
|
|
|
|
); err != nil {
|
2017-09-10 11:14:02 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-08-05 10:35:24 -04:00
|
|
|
logf("HUGORELEASER_TAG=%s\n", tag)
|
|
|
|
logf("HUGORELEASER_COMMITISH=%s\n", sha)
|
2017-06-16 04:11:02 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
return nil
|
2017-04-13 10:59:05 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 03:43:47 -04:00
|
|
|
if err := r.bumpVersions(finalVersion); err != nil {
|
2017-04-13 10:59:05 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-05 05:20:48 -04:00
|
|
|
if _, err := r.git("commit", "-a", "-m", fmt.Sprintf("%s Prepare repository for %s\n\n[ci skip]", commitPrefix, finalVersion)); err != nil {
|
|
|
|
return err
|
2017-04-13 10:59:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-26 04:11:22 -05:00
|
|
|
func (r *ReleaseHandler) bumpVersions(ver hugo.Version) error {
|
2017-04-13 10:59:05 -04:00
|
|
|
toDev := ""
|
|
|
|
|
|
|
|
if ver.Suffix != "" {
|
2017-09-10 11:14:02 -04:00
|
|
|
toDev = ver.Suffix
|
2017-04-13 10:59:05 -04:00
|
|
|
}
|
|
|
|
|
2018-11-26 04:11:22 -05:00
|
|
|
if err := r.replaceInFile("common/hugo/version_current.go",
|
2022-05-18 07:12:42 -04:00
|
|
|
`Minor:(\s*)(\d*),`, fmt.Sprintf(`Minor:${1}%d,`, ver.Minor),
|
2022-05-16 04:50:17 -04:00
|
|
|
`PatchLevel:(\s*)(\d*),`, fmt.Sprintf(`PatchLevel:${1}%d,`, ver.PatchLevel),
|
|
|
|
`Suffix:(\s*)".*",`, fmt.Sprintf(`Suffix:${1}"%s",`, toDev)); err != nil {
|
2017-04-13 10:59:05 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var minVersion string
|
|
|
|
if ver.Suffix != "" {
|
|
|
|
// People use the DEV version in daily use, and we cannot create new themes
|
|
|
|
// with the next version before it is released.
|
|
|
|
minVersion = ver.Prev().String()
|
|
|
|
} else {
|
|
|
|
minVersion = ver.String()
|
|
|
|
}
|
|
|
|
|
2017-07-05 03:43:47 -04:00
|
|
|
if err := r.replaceInFile("commands/new.go",
|
2017-04-13 10:59:05 -04:00
|
|
|
`min_version = "(.*)"`, fmt.Sprintf(`min_version = "%s"`, minVersion)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
func (r ReleaseHandler) calculateVersions() (hugo.Version, hugo.Version) {
|
|
|
|
newVersion := hugo.MustParseVersion(r.branchVersion)
|
|
|
|
finalVersion := newVersion.Next()
|
|
|
|
finalVersion.PatchLevel = 0
|
|
|
|
|
|
|
|
if newVersion.Suffix != "-test" {
|
|
|
|
newVersion.Suffix = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
finalVersion.Suffix = "-DEV"
|
|
|
|
|
|
|
|
return newVersion, finalVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ReleaseHandler) gitPush() {
|
|
|
|
if r.skipPush {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := r.git("push", "origin", "HEAD"); err != nil {
|
|
|
|
log.Fatal("push failed:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 03:43:47 -04:00
|
|
|
func (r *ReleaseHandler) replaceInFile(filename string, oldNew ...string) error {
|
2021-12-08 03:23:18 -05:00
|
|
|
filename = filepath.FromSlash(filename)
|
|
|
|
fi, err := os.Stat(filename)
|
2017-04-13 10:59:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-05 03:43:47 -04:00
|
|
|
if r.try {
|
2022-08-05 10:35:24 -04:00
|
|
|
logf("Replace in %q: %q\n", filename, oldNew)
|
2017-07-05 03:43:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
b, err := os.ReadFile(filename)
|
2017-04-13 10:59:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newContent := string(b)
|
|
|
|
|
|
|
|
for i := 0; i < len(oldNew); i += 2 {
|
|
|
|
re := regexp.MustCompile(oldNew[i])
|
|
|
|
newContent = re.ReplaceAllString(newContent, oldNew[i+1])
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
return os.WriteFile(filename, []byte(newContent), fi.Mode())
|
2017-04-13 10:59:05 -04:00
|
|
|
}
|
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
func git(args ...string) (string, error) {
|
|
|
|
cmd, _ := hexec.SafeCommand("git", args...)
|
|
|
|
out, err := cmd.CombinedOutput()
|
2017-04-13 10:59:05 -04:00
|
|
|
if err != nil {
|
2022-08-05 10:35:24 -04:00
|
|
|
return "", fmt.Errorf("git failed: %q: %q (%q)", err, out, args)
|
2017-04-13 10:59:05 -04:00
|
|
|
}
|
2022-08-05 10:35:24 -04:00
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func logf(format string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, format, args...)
|
|
|
|
}
|
2017-09-10 11:14:02 -04:00
|
|
|
|
2022-08-05 10:35:24 -04:00
|
|
|
func logln(args ...interface{}) {
|
|
|
|
fmt.Fprintln(os.Stderr, args...)
|
2017-09-10 11:14:02 -04:00
|
|
|
}
|