-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (102 loc) · 2.6 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
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"github.com/charmbracelet/log"
"github.com/google/go-github/v52/github"
"github.com/gregjones/httpcache"
"github.com/opq-osc/Yui/plugin/builder/cmd"
"golang.org/x/oauth2"
"net/http"
"os"
"path/filepath"
"strings"
)
const (
outPath = "dist"
)
type PluginInfo struct {
cmd.BuildMetaInfo
DownloadUrl string
}
func main() {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := &http.Client{
Transport: &oauth2.Transport{
Base: httpcache.NewMemoryCacheTransport(),
Source: ts,
},
}
client := github.NewClient(tc)
release, _, err := client.Repositories.GetLatestRelease(context.Background(), "opq-osc", "Yui-plugins")
if err != nil {
panic(err)
}
dir, err := os.ReadDir("./")
if err != nil {
panic(err)
}
os.MkdirAll(outPath, 0777)
var pluginsInfo []cmd.BuildMetaInfo
for _, v := range dir {
if !v.IsDir() || strings.HasPrefix(v.Name(), ".") {
continue
}
if _, err := os.Stat(filepath.Join(v.Name(), v.Name()+".go")); err != nil {
continue
}
metaBytes, err := os.ReadFile(filepath.Join(v.Name(), "meta.json"))
if err != nil {
log.Error(err)
continue
}
var metaInfo = cmd.BuildMetaInfo{}
err = json.Unmarshal(metaBytes, &metaInfo)
if err != nil {
log.Error(err)
continue
}
err = cmd.RunCmd("go", "run", "-mod=mod", "github.com/opq-osc/Yui/plugin/builder", "build", "-o", filepath.Join(outPath, v.Name()), filepath.Join(v.Name(), v.Name()+".go"))
if err != nil {
panic(err)
}
opqBytes, err := os.ReadFile(filepath.Join(outPath, v.Name()+".opq"))
if err != nil {
panic(err)
}
sha := sha256.New()
sha.Write(opqBytes)
metaInfo.Sha256 = hex.EncodeToString(sha.Sum(nil))
pluginsInfo = append(pluginsInfo, metaInfo)
f, err := os.Open(filepath.Join(outPath, v.Name()+".opq"))
if err != nil {
panic(err)
}
_, _, err = client.Repositories.UploadReleaseAsset(context.Background(), "opq-osc", "Yui-plugins", release.GetID(), &github.UploadOptions{Name: v.Name() + ".opq"}, f)
if err != nil {
log.Error(err)
}
f.Close()
}
pluginsInfoBytes, err := json.Marshal(pluginsInfo)
if err != nil {
panic(err)
}
err = os.WriteFile(filepath.Join(outPath, "meta.json"), pluginsInfoBytes, 0777)
if err != nil {
panic(err)
}
f, err := os.Open(filepath.Join(outPath, "meta.json"))
if err != nil {
panic(err)
}
_, _, err = client.Repositories.UploadReleaseAsset(context.Background(), "opq-osc", "Yui-plugins", release.GetID(), &github.UploadOptions{Name: "meta.json"}, f)
if err != nil {
log.Error(err)
}
f.Close()
}