aboutsummaryrefslogtreecommitdiff
path: root/cmd/generator/main.go
blob: 20dbc5f71951abf810ed30673ead4aa80ba45754 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main

import (
	"fmt"
	"net/http"
	"os"
	"strings"
	"text/template"

	"github.com/nihei9/maleeni/ucd"
)

func main() {
	err := gen()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}
}

func gen() error {
	var propValAliases *ucd.PropertyValueAliases
	{
		resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/PropertyValueAliases.txt")
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		propValAliases, err = ucd.ParsePropertyValueAliases(resp.Body)
		if err != nil {
			return err
		}
	}
	var unicodeData *ucd.UnicodeData
	{
		resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/UnicodeData.txt")
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		unicodeData, err = ucd.ParseUnicodeData(resp.Body, propValAliases)
		if err != nil {
			return err
		}
	}
	var scripts *ucd.Scripts
	{
		resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/Scripts.txt")
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		scripts, err = ucd.ParseScripts(resp.Body, propValAliases)
		if err != nil {
			return err
		}
	}
	var propList *ucd.PropList
	{
		resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/PropList.txt")
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		propList, err = ucd.ParsePropList(resp.Body)
		if err != nil {
			return err
		}
	}
	tmpl, err := template.ParseFiles("../ucd/codepoint.go.tmpl")
	if err != nil {
		return err
	}
	var b strings.Builder
	err = tmpl.Execute(&b, struct {
		GeneratorName        string
		UnicodeData          *ucd.UnicodeData
		Scripts              *ucd.Scripts
		PropList             *ucd.PropList
		PropertyValueAliases *ucd.PropertyValueAliases
	}{
		GeneratorName:        "generator/main.go",
		UnicodeData:          unicodeData,
		Scripts:              scripts,
		PropList:             propList,
		PropertyValueAliases: propValAliases,
	})
	if err != nil {
		return err
	}
	f, err := os.OpenFile("../ucd/codepoint.go", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
	if err != nil {
		return err
	}
	defer f.Close()
	fmt.Fprintf(f, b.String())
	return nil
}