zenity/internal/zenutil/osa_generator.go

121 lines
2.2 KiB
Go
Raw Normal View History

2022-06-18 07:37:39 -04:00
//go:build generate
//go:generate go run osa_generator.go osascripts/
2020-01-10 20:41:13 -05:00
2020-01-04 22:21:39 -05:00
package main
import (
2021-02-19 12:46:47 -05:00
"bytes"
2022-03-28 15:22:39 -04:00
"encoding/json"
2020-01-04 22:21:39 -05:00
"log"
"os"
"path/filepath"
"strings"
"text/template"
2021-02-19 12:46:47 -05:00
"github.com/dchest/jsmin"
2020-01-04 22:21:39 -05:00
)
func main() {
dir := os.Args[1]
2021-04-22 10:03:08 -04:00
files, err := os.ReadDir(dir)
2020-01-04 22:21:39 -05:00
if err != nil {
log.Fatal(err)
}
var str strings.Builder
2022-03-28 15:22:39 -04:00
funcs := template.FuncMap{"json": json.Marshal}
2020-01-04 22:21:39 -05:00
for _, file := range files {
name := file.Name()
2021-04-22 10:03:08 -04:00
data, err := os.ReadFile(filepath.Join(dir, name))
2021-02-19 12:46:47 -05:00
if err != nil {
log.Fatal(err)
}
2023-01-10 09:26:14 -05:00
if bytes.ContainsRune(data, '`') {
2022-05-06 08:48:31 -04:00
log.Fatalf("illegal character: %s: `", name)
}
2022-03-28 15:22:39 -04:00
_, err = template.New(file.Name()).Funcs(funcs).Parse(string(data))
if err != nil {
log.Fatal(err)
}
2021-02-19 12:46:47 -05:00
data, err = minify(data)
if err != nil {
log.Fatal(err)
}
2021-04-30 14:19:14 -04:00
str.WriteString("\n" + `{{define "`)
str.WriteString(strings.TrimSuffix(name, filepath.Ext(name)))
str.WriteString(`" -}}` + "\n")
str.Write(data)
str.WriteString("\n{{- end}}")
2020-01-04 22:21:39 -05:00
}
2020-01-19 06:57:05 -05:00
out, err := os.Create("osa_generated.go")
2020-01-04 22:21:39 -05:00
if err != nil {
log.Fatal(err)
}
2021-04-30 14:19:14 -04:00
err = generator.Execute(out, str.String())
2020-01-04 22:21:39 -05:00
if err != nil {
log.Fatal(err)
}
err = out.Close()
if err != nil {
log.Fatal(err)
}
}
2021-02-19 12:46:47 -05:00
func minify(data []byte) ([]byte, error) {
var templates [][]byte
var buf []byte
for {
i := bytes.Index(data, []byte("{{"))
if i < 0 {
break
}
j := bytes.Index(data[i+len("{{"):], []byte("}}"))
if j < 0 {
break
}
templates = append(templates, data[i:i+j+len("{{}}")])
buf = append(buf, data[:i]...)
buf = append(buf, []byte("TEMPLATE")...)
data = data[i+j+len("{{}}"):]
}
buf = append(buf, data...)
buf, err := jsmin.Minify(buf)
if err != nil {
return nil, err
}
var res []byte
for _, t := range templates {
i := bytes.Index(buf, []byte("TEMPLATE"))
res = append(res, buf[:i]...)
res = append(res, t...)
buf = buf[i+len("TEMPLATE"):]
}
return append(res, buf...), nil
}
2020-01-04 22:21:39 -05:00
var generator = template.Must(template.New("").Parse(`// Code generated by zenity; DO NOT EDIT.
2022-06-18 07:37:39 -04:00
2022-03-24 10:37:37 -04:00
//go:build darwin
2020-01-04 22:21:39 -05:00
2020-01-19 06:57:05 -05:00
package zenutil
2020-01-04 22:21:39 -05:00
2021-04-08 12:23:17 -04:00
import (
"encoding/json"
"text/template"
)
2020-01-04 22:21:39 -05:00
2022-06-02 07:49:31 -04:00
var scripts = template.Must(template.New("").Funcs(template.FuncMap{"json": func(v any) (string, error) {
2020-01-21 07:03:58 -05:00
b, err := json.Marshal(v)
return string(b), err
2021-04-30 14:19:14 -04:00
}}).Parse(` + "`{{.}}`))\n"))