Removed old impl

This commit is contained in:
2026-06-12 04:51:38 +08:00
parent 4fcd58b5ed
commit f532ada7c4
63 changed files with 717 additions and 1359 deletions
+60 -5
View File
@@ -1,13 +1,16 @@
package main
import (
"crypto/md5"
"encoding/hex"
"flag"
"log"
"net/http"
"strings"
"github.com/tgckpg/botanres-go/internal/generated"
"github.com/tgckpg/botanres-go/internal/resolver"
"github.com/tgckpg/resolver-go/internal/closure"
"github.com/tgckpg/resolver-go/internal/generated"
"github.com/tgckpg/resolver-go/internal/resolver"
)
func main() {
@@ -20,13 +23,24 @@ func main() {
log.Fatal(err)
}
h := handler{r: r}
h := handler{
r: r,
closure: closure.NewCompileCache(2),
}
http.HandleFunc("/", h.index)
log.Printf("botan-api listening on %s", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}
type handler struct{ r *resolver.Resolver }
type handler struct {
r *resolver.Resolver
closure *closure.CompileCache
}
func hashStrings(parts []string) string {
sum := md5.Sum([]byte(strings.Join(parts, "|")))
return hex.EncodeToString(sum[:])
}
func (h handler) index(w http.ResponseWriter, req *http.Request) {
path := strings.Trim(req.URL.Path, "/")
@@ -61,7 +75,46 @@ func (h handler) index(w http.ResponseWriter, req *http.Request) {
return
}
log.Println(res.JSFiles)
if outMode == resolver.ModeJS {
fileHashes := make([]string, 0, len(res.JSFiles))
for _, f := range res.JSFiles {
fileHashes = append(fileHashes, f.JSHash)
}
hash := hashStrings(fileHashes)
if compiled, ok := h.closure.Get(hash); ok {
w.Header().Set("Content-Type", "application/javascript")
w.Header().Set("X-Botan-Compiled", "hit")
w.Write(compiled)
return
}
jsFiles := make([]string, 0, len(res.JSFiles))
for _, f := range res.JSFiles {
jsFiles = append(jsFiles, f.Src)
}
jsExterns, err := h.r.GetExterns(generated.Externs)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
h.closure.Enqueue(closure.CompileJob{
Hash: res.Hash,
Mode: "js",
ExternSources: jsExterns,
JSSources: []closure.SourceInput{
{
Name: "botanjs-" + res.Hash + ".js",
Source: string(res.Content),
},
},
Defines: map[string]any{
"DEBUG": false,
},
})
}
// Compatibility flags:
// rjs/rcss/ojs/ocss => content
@@ -73,5 +126,7 @@ func (h handler) index(w http.ResponseWriter, req *http.Request) {
return
}
w.Header().Set("Content-Type", res.ContentType)
w.Header().Set("X-Botan-Compiled", "miss")
_, _ = w.Write(res.Content)
}
@@ -7,7 +7,7 @@ import (
"os"
"strings"
"github.com/tgckpg/botanres-go/internal/classmap"
"github.com/tgckpg/resolver-go/internal/classmap"
)
func main() {
@@ -36,9 +36,9 @@ func main() {
func render(pkg string, m *classmap.Map) string {
var b strings.Builder
b.WriteString("// Code generated by botan-gen; DO NOT EDIT.\n")
b.WriteString("// Code generated by classmap-gen; DO NOT EDIT.\n")
b.WriteString("package " + pkg + "\n\n")
b.WriteString("import \"github.com/tgckpg/botanres-go/internal/classmap\"\n\n")
b.WriteString("import \"github.com/tgckpg/resolver-go/internal/classmap\"\n\n")
b.WriteString("var ClassMap = &classmap.Map{\nSymbols: map[string]classmap.Symbol{\n")
for _, s := range classmap.SortedSymbols(m) {
fmt.Fprintf(&b, "%q: {Name:%q, Kind:%q, Parent:%q, Imports:%#v, Resource: classmap.Resource{Src:%q, JSHash:%q, CSSHash:%q}},\n",
@@ -61,6 +61,6 @@ func dir(path string) string {
}
func fatal(err error) {
fmt.Fprintln(os.Stderr, "botan-gen:", err)
fmt.Fprintln(os.Stderr, "classmap-gen:", err)
os.Exit(1)
}
+87
View File
@@ -0,0 +1,87 @@
package main
import (
"flag"
"fmt"
"go/format"
"io/fs"
"os"
"path/filepath"
"strings"
)
func main() {
src := flag.String("src", "./src", "BotanJS source root")
out := flag.String("out", "internal/generated/externs_gen.go", "generated Go output")
pkg := flag.String("pkg", "generated", "generated package name")
flag.Parse()
if err := os.MkdirAll(filepath.Dir(*out), 0o755); err != nil {
fatal(err)
}
code, err := render(*pkg, *src)
if err != nil {
fatal(err)
}
fmted, err := format.Source([]byte(code))
if err != nil {
_, _ = os.Stderr.WriteString(code)
fatal(err)
}
if err := os.WriteFile(*out, fmted, 0o644); err != nil {
fatal(err)
}
}
func render(pkg string, root string) (string, error) {
var b strings.Builder
root = filepath.Clean(root)
b.WriteString("// Code generated by externs-gen; DO NOT EDIT.\n")
b.WriteString("package " + pkg + "\n\n")
b.WriteString("var Externs = []string{\n")
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if filepath.Ext(path) != ".js" {
return nil
}
if filepath.Base(filepath.Dir(path)) != "externs" {
return nil
}
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
// Generated data should be slash-normalized even on Windows.
rel = filepath.ToSlash(rel)
fmt.Fprintf(&b, "\t%q,\n", rel)
return nil
})
if err != nil {
return "", err
}
b.WriteString("}\n")
return b.String(), nil
}
func fatal(err error) {
fmt.Fprintln(os.Stderr, "externs-gen:", err)
os.Exit(1)
}