-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.go
120 lines (101 loc) · 3.81 KB
/
codegen.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
package main
import (
"bufio"
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"
)
func main() {
// Check if the version argument is provided
if len(os.Args) < 2 {
// If not, print an error message and exit
fmt.Println("Error: Missing version argument")
os.Exit(1)
}
// Get the version argument from the command line
version := os.Args[1]
// Construct the URL for the emoji test data based on the version
url := fmt.Sprintf("https://unicode.org/Public/emoji/%s/emoji-test.txt", version)
// Fetch the emoji test data from the Unicode website
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Create a new scanner to read the response body line by line
scanner := bufio.NewScanner(resp.Body)
// Create a new file to write the emoji list to
versionClean := "v"
zeroPatch := strings.HasSuffix(version, ".0")
if zeroPatch {
versionClean += strings.TrimSuffix(version, ".0")
if err := os.MkdirAll(versionClean, 0755); err != nil {
log.Fatal(err)
}
} else {
versionClean += strings.ReplaceAll(version, ".", "")
if err := os.MkdirAll(fmt.Sprintf("v%s", version), 0755); err != nil {
log.Fatal(err)
}
}
var f *os.File
if zeroPatch {
f, err = os.Create(fmt.Sprintf("%s/%s.go", versionClean, versionClean))
} else {
f, err = os.Create(fmt.Sprintf("v%s/%s.go", version, versionClean))
}
if err != nil {
log.Fatal(err)
}
defer f.Close() // Close the file when the function returns
// Write the package declaration and a warning not to edit the file to the new file
fmt.Fprintln(f, fmt.Sprintf("//go:generate go run codegen/codegen.go %s", version))
fmt.Fprintln(f, fmt.Sprintf("package %s", versionClean))
fmt.Fprintln(f, "")
fmt.Fprintln(f, "// THIS FILE IS GENERATED BY GO GENERATE. DO NOT EDIT.")
fmt.Fprintln(f, "")
// Compile a regular expression to match the lines containing emoji data
// re := regexp.MustCompile(`^([0-9A-F ]+)\s+; fully-qualified\s+# (.*) E[0-9.]*\.?[0-9]* (.*)$`)
re := regexp.MustCompile(`^([0-9A-F ]+)\s+; fully-qualified\s+# (.*?)(?: E[0-9.]*\.?[0-9]*)? (.*)$`)
// Loop through each line in the response body
for scanner.Scan() {
line := scanner.Text()
// Skip comment lines and empty lines
if strings.HasPrefix(line, "#") || strings.TrimSpace(line) == "" {
continue
}
// Try to match the line with the regular expression
matches := re.FindStringSubmatch(line)
// If the line matches the regular expression, extract the emoji character and name
if len(matches) == 4 {
emojiChar := matches[2]
emojiName := matches[3]
// Create a constant name for the emoji by converting the name to uppercase and replacing spaces and special characters with underscores
constName := "EMOJI_" + strings.ToUpper(strings.ReplaceAll(emojiName, " - ", "_"))
constName = strings.ReplaceAll(constName, " ", "_")
constName = strings.ReplaceAll(constName, "-", "_")
constName = strings.ReplaceAll(constName, ":", "")
constName = strings.ReplaceAll(constName, ",", "")
constName = strings.ReplaceAll(constName, "'", "")
constName = strings.ReplaceAll(constName, "’", "")
constName = strings.ReplaceAll(constName, "(", "")
constName = strings.ReplaceAll(constName, ")", "")
constName = strings.ReplaceAll(constName, ".", "")
constName = strings.ReplaceAll(constName, "!", "")
constName = strings.ReplaceAll(constName, "“", "")
constName = strings.ReplaceAll(constName, "”", "")
constName = strings.ReplaceAll(constName, "#", "HASHTAG")
constName = strings.ReplaceAll(constName, "*", "STAR")
constName = strings.ReplaceAll(constName, "&", "AND")
// Write the constant declaration to the new file
fmt.Fprintf(f, "const %s = \"%s\"\n", constName, emojiChar)
}
}
// Check for any errors that occurred while scanning the response body
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}