Git hook triggered via local commit or Gitea's one. It runs Hugo against its dedicated repository with Markdown files and related assets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

61 lines
1.8 KiB

package main
import (
"fmt"
"hash/crc32"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/PumpkinSeed/cage"
"github.com/gohugoio/hugo/commands"
hg "github.com/gohugoio/hugo/common/hugo"
"github.com/spf13/viper"
)
func hugoContext(hugo *Hugo, gitRepoPath string) {
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(filepath.Join(hugo.SourceDir))
err := viper.ReadInConfig()
check(err)
gitRepoPathChecksum := strconv.FormatUint(uint64(crc32.ChecksumIEEE([]byte(gitRepoPath))), 16)
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
check(err)
hugoTitle := reg.ReplaceAllString(viper.GetString("title"), "")
hugo.PublicHTMLName = strings.ToLower(hugoTitle) + "-" + gitRepoPathChecksum
hugo.CatalogName = viper.GetString("params.sandpointsCatalogName")
hugo.CatalogPrefix = viper.GetString("params.sandpointsCatalogPrefix")
hugo.DestinationDir = filepath.Join(hugo.SourceDir, "public")
}
func hugoRun(hugoCommands []string) []string {
logs := cage.Start()
hugoCommand := hugoCommands
goHugo := commands.Execute(hugoCommand)
cage.Stop(logs)
if goHugo.Err != nil {
check(goHugo.Err)
runtime.Goexit()
}
return logs.Data
}
func hugoRender(hugo *Hugo, hook *Hook) string {
// fmt.Println("hugo", "-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics")
hugoCommands := []string{"-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics"}
if hook.Offline {
hugoCommands = append(hugoCommands, []string{"-e", "offline"}...)
} else if hook.Context == "PostReceive" {
hugoCommands = append(hugoCommands, []string{"-e", "gitea"}...)
}
logs := hugoRun(hugoCommands)
lgs := fmt.Sprintf("~~~~~~~ Hugo's logs (v%.2f)~~~~~~~\n", hg.CurrentVersion.Number)
for _, l := range logs {
lgs = lgs + l + "\n"
}
return lgs
}