2019-12-10 02:02:15 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2020-12-18 12:20:12 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hexec"
|
|
|
|
|
2019-12-10 02:02:15 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hugio"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-12-04 06:07:54 -05:00
|
|
|
// The current is built with 446a5dcf5a3230ce9832682d8f521071d8a34a2b (go 1.22 dev. Thu Oct 5 12:20:11 2023 -0700)
|
2019-12-10 02:02:15 -05:00
|
|
|
fmt.Println("Forking ...")
|
|
|
|
defer fmt.Println("Done ...")
|
|
|
|
|
|
|
|
cleanFork()
|
|
|
|
|
|
|
|
htmlRoot := filepath.Join(forkRoot, "htmltemplate")
|
|
|
|
|
|
|
|
for _, pkg := range goPackages {
|
|
|
|
copyGoPackage(pkg.dstPkg, pkg.srcPkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range goPackages {
|
|
|
|
doWithGoFiles(pkg.dstPkg, pkg.rewriter, pkg.replacer)
|
|
|
|
}
|
|
|
|
|
|
|
|
goimports(htmlRoot)
|
|
|
|
gofmt(forkRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TODO(bep)
|
2022-03-16 03:48:16 -04:00
|
|
|
goSource = "/Users/bep/dev/go/misc/go/src"
|
2019-12-10 02:02:15 -05:00
|
|
|
forkRoot = "../../tpl/internal/go_templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
type goPackage struct {
|
|
|
|
srcPkg string
|
|
|
|
dstPkg string
|
|
|
|
replacer func(name, content string) string
|
|
|
|
rewriter func(name string)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
textTemplateReplacers = strings.NewReplacer(
|
|
|
|
`"text/template/`, `"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/`,
|
|
|
|
`"internal/fmtsort"`, `"github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort"`,
|
2020-05-23 09:32:27 -04:00
|
|
|
`"internal/testenv"`, `"github.com/gohugoio/hugo/tpl/internal/go_templates/testenv"`,
|
|
|
|
"TestLinkerGC", "_TestLinkerGC",
|
2019-12-10 02:02:15 -05:00
|
|
|
// Rename types and function that we want to overload.
|
|
|
|
"type state struct", "type stateOld struct",
|
2019-12-10 13:56:44 -05:00
|
|
|
"func (s *state) evalFunction", "func (s *state) evalFunctionOld",
|
|
|
|
"func (s *state) evalField(", "func (s *state) evalFieldOld(",
|
2019-11-27 07:42:36 -05:00
|
|
|
"func (s *state) evalCall(", "func (s *state) evalCallOld(",
|
2019-12-18 10:50:34 -05:00
|
|
|
"func isTrue(val reflect.Value) (truth, ok bool) {", "func isTrueOld(val reflect.Value) (truth, ok bool) {",
|
2019-12-10 02:02:15 -05:00
|
|
|
)
|
|
|
|
|
2020-05-23 09:32:27 -04:00
|
|
|
testEnvReplacers = strings.NewReplacer(
|
|
|
|
`"internal/cfg"`, `"github.com/gohugoio/hugo/tpl/internal/go_templates/cfg"`,
|
|
|
|
)
|
|
|
|
|
2019-12-10 02:02:15 -05:00
|
|
|
htmlTemplateReplacers = strings.NewReplacer(
|
|
|
|
`. "html/template"`, `. "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"`,
|
|
|
|
`"html/template"`, `template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"`,
|
|
|
|
"\"text/template\"\n", "template \"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate\"\n",
|
|
|
|
`"html/template"`, `htmltemplate "html/template"`,
|
|
|
|
`"fmt"`, `htmltemplate "html/template"`,
|
2021-01-29 11:20:55 -05:00
|
|
|
`t.Skip("this test currently fails with -race; see issue #39807")`, `// t.Skip("this test currently fails with -race; see issue #39807")`,
|
2019-12-10 02:02:15 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
func commonReplace(name, content string) string {
|
|
|
|
if strings.HasSuffix(name, "_test.go") {
|
|
|
|
content = strings.Replace(content, "package template\n", `// +build go1.13,!windows
|
|
|
|
|
|
|
|
package template
|
|
|
|
`, 1)
|
|
|
|
content = strings.Replace(content, "package template_test\n", `// +build go1.13
|
|
|
|
|
|
|
|
package template_test
|
|
|
|
`, 1)
|
|
|
|
|
|
|
|
content = strings.Replace(content, "package parse\n", `// +build go1.13
|
|
|
|
|
|
|
|
package parse
|
|
|
|
`, 1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
var goPackages = []goPackage{
|
2020-12-02 07:23:25 -05:00
|
|
|
{
|
|
|
|
srcPkg: "text/template", dstPkg: "texttemplate",
|
|
|
|
replacer: func(name, content string) string { return textTemplateReplacers.Replace(commonReplace(name, content)) },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
srcPkg: "html/template", dstPkg: "htmltemplate", replacer: func(name, content string) string {
|
|
|
|
if strings.HasSuffix(name, "content.go") {
|
|
|
|
// Remove template.HTML types. We need to use the Go types.
|
|
|
|
content = removeAll(`(?s)// Strings of content.*?\)\n`, content)
|
|
|
|
}
|
2019-12-10 02:02:15 -05:00
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
content = commonReplace(name, content)
|
2019-12-10 02:02:15 -05:00
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
return htmlTemplateReplacers.Replace(content)
|
|
|
|
},
|
2019-12-10 02:02:15 -05:00
|
|
|
rewriter: func(name string) {
|
|
|
|
for _, s := range []string{"CSS", "HTML", "HTMLAttr", "JS", "JSStr", "URL", "Srcset"} {
|
|
|
|
rewrite(name, fmt.Sprintf("%s -> htmltemplate.%s", s, s))
|
|
|
|
}
|
|
|
|
rewrite(name, `"text/template/parse" -> "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"`)
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{srcPkg: "internal/fmtsort", dstPkg: "fmtsort", rewriter: func(name string) {
|
2019-12-10 02:02:15 -05:00
|
|
|
rewrite(name, `"internal/fmtsort" -> "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort"`)
|
|
|
|
}},
|
2020-12-02 07:23:25 -05:00
|
|
|
{
|
|
|
|
srcPkg: "internal/testenv", dstPkg: "testenv",
|
2020-05-23 09:32:27 -04:00
|
|
|
replacer: func(name, content string) string { return testEnvReplacers.Replace(content) }, rewriter: func(name string) {
|
|
|
|
rewrite(name, `"internal/testenv" -> "github.com/gohugoio/hugo/tpl/internal/go_templates/testenv"`)
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{srcPkg: "internal/cfg", dstPkg: "cfg", rewriter: func(name string) {
|
2020-05-23 09:32:27 -04:00
|
|
|
rewrite(name, `"internal/cfg" -> "github.com/gohugoio/hugo/tpl/internal/go_templates/cfg"`)
|
|
|
|
}},
|
2019-12-10 02:02:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var fs = afero.NewOsFs()
|
|
|
|
|
|
|
|
// Removes all non-Hugo files in the go_templates folder.
|
|
|
|
func cleanFork() {
|
|
|
|
must(filepath.Walk(filepath.Join(forkRoot), func(path string, info os.FileInfo, err error) error {
|
|
|
|
if !info.IsDir() && len(path) > 10 && !strings.Contains(path, "hugo") {
|
|
|
|
must(fs.Remove(path))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func must(err error, what ...string) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(what, " ERROR: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyGoPackage(dst, src string) {
|
|
|
|
from := filepath.Join(goSource, src)
|
|
|
|
to := filepath.Join(forkRoot, dst)
|
|
|
|
fmt.Println("Copy", from, "to", to)
|
|
|
|
must(hugio.CopyDir(fs, from, to, func(s string) bool { return true }))
|
|
|
|
}
|
|
|
|
|
|
|
|
func doWithGoFiles(dir string,
|
|
|
|
rewrite func(name string),
|
2023-09-21 14:15:31 -04:00
|
|
|
transform func(name, in string) string,
|
|
|
|
) {
|
2019-12-10 02:02:15 -05:00
|
|
|
if rewrite == nil && transform == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
must(filepath.Walk(filepath.Join(forkRoot, dir), func(path string, info os.FileInfo, err error) error {
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(path, ".go") || strings.Contains(path, "hugo_") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Handle", path)
|
|
|
|
|
|
|
|
if rewrite != nil {
|
|
|
|
rewrite(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if transform == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-18 17:43:26 -05:00
|
|
|
data, err := os.ReadFile(path)
|
2019-12-10 02:02:15 -05:00
|
|
|
must(err)
|
|
|
|
f, err := os.Create(path)
|
|
|
|
must(err)
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(transform(path, string(data)))
|
|
|
|
must(err)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeAll(expression, content string) string {
|
|
|
|
re := regexp.MustCompile(expression)
|
|
|
|
return re.ReplaceAllString(content, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func rewrite(filename, rule string) {
|
2020-12-18 12:20:12 -05:00
|
|
|
cmf, _ := hexec.SafeCommand("gofmt", "-w", "-r", rule, filename)
|
2019-12-10 02:02:15 -05:00
|
|
|
out, err := cmf.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("gofmt failed:", string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func goimports(dir string) {
|
2020-12-18 12:20:12 -05:00
|
|
|
cmf, _ := hexec.SafeCommand("goimports", "-w", dir)
|
2019-12-10 02:02:15 -05:00
|
|
|
out, err := cmf.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("goimports failed:", string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func gofmt(dir string) {
|
2020-12-18 12:20:12 -05:00
|
|
|
cmf, _ := hexec.SafeCommand("gofmt", "-w", dir)
|
2019-12-10 02:02:15 -05:00
|
|
|
out, err := cmf.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("gofmt failed:", string(out))
|
|
|
|
}
|
|
|
|
}
|