This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen.go
145 lines (124 loc) · 2.68 KB
/
gen.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: Apache-2.0
// +build ignore
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
)
const notoEmojiVersion = "v2020-09-16-unicode13_1"
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
if err := clean(); err != nil {
return err
}
tmp, err := os.MkdirTemp("", "noto-emoji-")
if err != nil {
return err
}
defer os.RemoveAll(tmp)
if err := prepareNotoEmojiFiles(tmp); err != nil {
return err
}
return nil
}
func clean() error {
fmt.Printf("Cleaning Noto Emoji files\n")
if err := os.RemoveAll("png"); err != nil {
return err
}
if err := os.Remove("README-noto-emoji.md"); err != nil {
return err
}
if err := os.Remove("LICENSE-noto-emoji"); err != nil {
return err
}
return nil
}
func prepareNotoEmojiFiles(tmp string) error {
fn := notoEmojiVersion + ".tar.gz"
if e, err := exists(fn); err != nil {
return err
} else if !e {
url := "https://github.com/googlefonts/noto-emoji/archive/refs/tags/" + fn
fmt.Fprintf(os.Stderr, "%s not found: please download it from %s\n", fn, url)
return nil
}
fmt.Printf("Copying %s to %s\n", fn, filepath.Join(tmp, fn))
in, err := os.Open(fn)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(filepath.Join(tmp, fn))
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
fmt.Printf("Extracting %s\n", fn)
cmd := exec.Command("tar", "-xzf", fn)
cmd.Stderr = os.Stderr
cmd.Dir = tmp
if err := cmd.Run(); err != nil {
return err
}
dir := filepath.Join(tmp, "noto-emoji-"+notoEmojiVersion[1:])
fmt.Printf("Copying PNG images\n")
srcpngdir := filepath.Join(dir, "png", "128")
dstpngdir := "image"
if err := os.MkdirAll(dstpngdir, 0755); err != nil {
return err
}
es, err := os.ReadDir(srcpngdir)
if err != nil {
return err
}
for _, e := range es {
if e.IsDir() {
continue
}
src := filepath.Join(srcpngdir, e.Name())
dst := filepath.Join(dstpngdir, e.Name())
if err := os.Rename(src, dst); err != nil {
return err
}
}
fmt.Printf("Copying README.md and LICENSE\n")
for _, f := range []string{"README.md", "LICENSE"} {
infn := filepath.Join(dir, f)
ext := filepath.Ext(f)
outfn := f[:len(f)-len(ext)] + "-noto-emoji" + ext
in, err := os.Open(infn)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(outfn)
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
}
return nil
}
func exists(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}