Skip to content

Commit

Permalink
[*] chore: support auto upating emoji tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-M-C committed Oct 11, 2021
1 parent 6e3aa9d commit 61cf526
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
116 changes: 116 additions & 0 deletions internal/tool/const/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
"strings"
)

func downloadEmoji() {
// list and find latest emoji versions
s, err := httpGet(emojiOfficialURL)
var lines []string
check(err)

if find := strings.SplitN(s, "Parent Directory", 2); len(find) == 1 {
printf("error, cannot find emoji sub directories")
os.Exit(-1)
} else {
lines = strings.Split(find[1], "\n")
}

latest := ""
rep := regexp.MustCompile(`(\d+\.\d+)\/.+20\d\d-[01]\d-\d\d`)
for _, line := range lines {
match := rep.FindStringSubmatch(line)
if len(match) < 2 {
continue
}

v := match[1]
printf("found version: %s", v)

if vercmp(latest, v) < 0 {
latest = v
}
}

printf("latest version: %v", latest)

// get emoji and zwj files
basic, err := httpGet(emojiOfficialURL + latest + "/" + emojiOfficialSeqFile)
check(err)
zwj, err := httpGet(emojiOfficialURL + latest + "/" + emojiOfficialZwjSeqFile)
check(err)

err = ioutil.WriteFile(emojiDataFile, []byte(basic), 0644)
check(err)
err = ioutil.WriteFile(emojiZwjSeqFile, []byte(zwj), 0644)
check(err)

referenceURL = emojiOfficialURL + latest + "/"
}

func vercmp(left, right string) int {
if left == "" {
if right == "" {
return 0
}
return -1
}
if right == "" {
return 1
}

toCode := func(s string) uint32 {
nums := strings.Split(s, ".")
if len(nums) == 1 {
n, _ := strconv.ParseUint(s, 10, 32)
return uint32(n) << 16
}

hi, _ := strconv.ParseUint(nums[0], 10, 32)
lo, _ := strconv.ParseUint(nums[1], 10, 32)
return (uint32(hi) << 16) + uint32(lo)
}

leftCode := toCode(left)
rightCode := toCode(right)

if leftCode < rightCode {
return -1
}
if leftCode > rightCode {
return 1
}
return 0
}

func httpGet(u string) (res string, err error) {
resp, err := http.Get(u)
if err != nil {
return "", fmt.Errorf("http.Get(%s) error: %w", u, err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body)
return "", fmt.Errorf("HTTP status %d, body '%s'", resp.StatusCode, string(b))
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
if errors.Is(err, io.EOF) {
return string(b), nil
}
return "", fmt.Errorf("ioutil.ReadAll error: %w", err)
}

return string(b), nil
}
22 changes: 15 additions & 7 deletions tool/const/main.go → internal/tool/const/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import (
// https://juejin.im/post/5b47564b51882519ec07e9ec
// https://www.jianshu.com/p/9682f8ce1260

// official source file:
// http://www.unicode.org/Public/emoji/14.0/emoji-sequences.txt

const (
emojiDataFile = "../../official/emoji-sequences.txt"
emojiZwjSeqFile = "../../official/emoji-zwj-sequences.txt"
emojiOfficialURL = "https://www.unicode.org/Public/emoji/"

emojiOfficialSeqFile = "emoji-sequences.txt"
emojiOfficialZwjSeqFile = "emoji-zwj-sequences.txt"

emojiDataFile = "../../../official/emoji-sequences.txt"
emojiZwjSeqFile = "../../../official/emoji-zwj-sequences.txt"

emojiDataGoFile = "../../official/emoji-sequences.go"
emojiDataGoFile = "../../../official/emoji-sequences.go"

// typeEmojiBasic = " Basic_Emoji "
// typeEmojiKeycapSequence = " Emoji_Keycap_Sequence "
Expand All @@ -31,6 +33,10 @@ const (
// typeEmojiModifierSequence = " RGI_Emoji_Modifier_Sequence "
)

var (
referenceURL = ""
)

type record struct {
runes []rune
comment string
Expand Down Expand Up @@ -64,6 +70,7 @@ func addRecord(digits []rune, comment string) {
}

func main() {
downloadEmoji()
parseEmojiData(emojiDataFile)
parseEmojiData(emojiZwjSeqFile)
printEmojiDataParam()
Expand Down Expand Up @@ -178,9 +185,10 @@ func printEmojiDataParam() {
}

// file heading
f.WriteString("// Code generated by internal/tool/const. DO NOT EDIT.\n\n")
f.WriteString("// Package official indicates official unicode emoji variables.\n")
f.WriteString("//\n")
f.WriteString("// Reference: https://www.unicode.org/Public/emoji/13.1/emoji-sequences.txt\n")
f.WriteString("// Reference: " + referenceURL + "\n")
f.WriteString("//\n")
f.WriteString("// " + emojiDataFileVer + "\n")
f.WriteString("package official\n\n")
Expand Down
4 changes: 3 additions & 1 deletion official/emoji-sequences.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61cf526

Please sign in to comment.