-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
main.go
126 lines (110 loc) · 2.63 KB
/
main.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Binary dltl fetches .tl schema from remote repo.
package main
import (
"bytes"
"crypto/sha256"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
"github.com/gotd/tl"
)
func mergeSchema(target, s *tl.Schema) {
Definitions:
for _, d := range s.Definitions {
for _, targetD := range target.Definitions {
if targetD.Definition.ID == d.Definition.ID {
continue Definitions
}
}
target.Definitions = append(target.Definitions, d)
}
Classes:
for _, c := range s.Classes {
for _, targetC := range target.Classes {
if targetC.Name == c.Name {
continue Classes
}
}
target.Classes = append(target.Classes, c)
}
}
func main() {
var (
name = flag.String("f", "api.tl", "file name to download; api.tl or mtproto.tl")
base = flag.String("base", "https://raw.githubusercontent.com/telegramdesktop/tdesktop", "base url")
branch = flag.String("branch", "dev", "branch to use")
dir = flag.String("dir", "Telegram/Resources/tl", "directory of schemas")
out = flag.String("o", "", "output file name (blank to stdout)")
merge = flag.String("merge", "", "path to schema(s) to merge with, comma-separated")
)
flag.Parse()
u, err := url.Parse(*base)
if err != nil {
panic(err)
}
u.Path = path.Join(u.Path, *branch, *dir, *name)
res, err := http.Get(u.String())
if err != nil {
panic(err)
}
defer func() { _ = res.Body.Close() }()
if res.StatusCode/100 != 2 {
panic(fmt.Sprintf("status code %d", res.StatusCode))
}
// Parsing in-place.
h := sha256.New()
s, err := tl.Parse(io.TeeReader(res.Body, h))
if err != nil {
panic(err)
}
if *merge != "" {
for _, mergeName := range strings.Split(*merge, ",") {
data, err := os.ReadFile(mergeName)
if err != nil {
panic(err)
}
m, err := tl.Parse(bytes.NewReader(data))
if err != nil {
panic(err)
}
mergeSchema(s, m)
}
}
var outWriter io.Writer = os.Stdout
if *out != "" {
w, err := os.Create(*out)
if err != nil {
panic(err)
}
defer func() {
if err := w.Close(); err != nil {
panic(err)
}
}()
outWriter = w
}
// Writing header to avoid manual edit.
var b strings.Builder
b.WriteString("// Code generated by ./cmd/dltl, DO NOT EDIT.\n")
b.WriteString("//\n")
b.WriteString(fmt.Sprintf("// Source: %s\n", u))
if *merge != "" {
b.WriteString(fmt.Sprintf("// Merge: %s\n", *merge))
}
if s.Layer > 0 {
b.WriteString(fmt.Sprintf("// Layer: %d\n", s.Layer))
}
b.WriteString(fmt.Sprintf("// SHA256: %x\n", h.Sum(nil)))
b.WriteRune('\n')
if _, err := io.WriteString(outWriter, b.String()); err != nil {
panic(err)
}
if _, err := s.WriteTo(outWriter); err != nil {
panic(err)
}
}