/
main.go
180 lines (172 loc) · 4.12 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"fmt"
"github.com/orestonce/go2cpp"
"github.com/orestonce/m3u8d"
"github.com/orestonce/m3u8d/m3u8dcpp"
"golang.org/x/text/encoding/simplifiedchinese"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
)
func main() {
BuildCliBinary() // 编译命令行版本
if os.Getenv("GITHUB_ACTIONS") == "" { // 本地编译
CreateLibForQtUi("amd64-static") // 创建Qt需要使用的.a库文件
WriteVersionDotRc("1.5.20")
} else { // github actions 编译
if runtime.GOOS == "darwin" { // 编译darwin版本的dmg
CreateLibForQtUi("amd64-shared")
} else { // 编译windows版本的exe
CreateLibForQtUi("386-static")
}
if len(os.Args) <= 1 || os.Args[1] != "check-only" {
version := strings.TrimPrefix(os.Getenv("GITHUB_REF_NAME"), "v")
WriteVersionDotRc(version)
}
}
}
func BuildCliBinary() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
type buildCfg struct {
GOOS string
GOARCH string
Ext string
}
var list = []buildCfg{
{
GOOS: "linux",
GOARCH: "386",
},
{
GOOS: "linux",
GOARCH: "arm",
},
{
GOOS: "linux",
GOARCH: "mipsle",
},
{
GOOS: "darwin",
GOARCH: "amd64",
},
{
GOOS: "windows",
GOARCH: "386",
Ext: ".exe",
},
}
for _, cfg := range list {
name := "m3u8d_cli_" + cfg.GOOS + "_" + cfg.GOARCH + cfg.Ext
cmd := exec.Command("go", "build", "-trimpath", "-ldflags", "-s -w", "-o", filepath.Join(wd, "bin", name))
cmd.Dir = filepath.Join(wd, "cmd")
cmd.Env = append(os.Environ(), "GOOS="+cfg.GOOS)
cmd.Env = append(cmd.Env, "GOARCH="+cfg.GOARCH)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Println(cmd.Dir)
panic(err)
}
fmt.Println("done", name)
}
}
func CreateLibForQtUi(mode string) {
ctx := go2cpp.NewGo2cppContext(go2cpp.NewGo2cppContext_Req{
CppBaseName: "m3u8d",
EnableQtClass_RunOnUiThread: true,
EnableQtClass_Toast: true,
})
ctx.Generate1(m3u8dcpp.StartDownload)
ctx.Generate1(m3u8dcpp.CloseOldEnv)
ctx.Generate1(m3u8dcpp.GetStatus)
ctx.Generate1(m3u8dcpp.WaitDownloadFinish)
ctx.Generate1(m3u8d.GetWd)
ctx.Generate1(m3u8d.ParseCurlStr)
ctx.Generate1(m3u8d.RunDownload_Req_ToCurlStr)
ctx.Generate1(m3u8d.GetFileNameFromUrl)
ctx.Generate1(m3u8dcpp.MergeTsDir)
ctx.Generate1(m3u8dcpp.MergeStop)
ctx.Generate1(m3u8dcpp.MergeGetProgressPercent)
if mode == "amd64-static" {
ctx.MustCreateAmd64LibraryInDir("m3u8d-qt")
} else if mode == "386-static" {
ctx.MustCreate386LibraryInDir("m3u8d-qt")
} else if mode == "amd64-shared" {
ctx.MustCreateAmd64CSharedInDir("m3u8d-qt")
} else {
panic(mode)
}
}
func WriteVersionDotRc(version string) {
tmp := strings.Split(version, ".")
ok := len(tmp) == 3
for _, v := range tmp {
vi, err := strconv.Atoi(v)
if err != nil {
ok = false
break
}
if vi < 0 {
ok = false
break
}
}
if ok == false {
panic("version invalid: " + strconv.Quote(version))
}
tmp = append(tmp, "0")
v1 := strings.Join(tmp, ",")
// TODO: 这里写中文github action会乱码, 有空研究一下
data := []byte(`IDI_ICON1 ICON "favicon.ico"
#if defined(UNDER_CE)
#include <winbase.h>
#else
#include <winver.h>
#endif
VS_VERSION_INFO VERSIONINFO
FILEVERSION ` + v1 + `
PRODUCTVERSION ` + v1 + `
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS VS_FF_DEBUG
#else
FILEFLAGS 0x0L
#endif
FILEOS VOS__WINDOWS32
FILETYPE VFT_DLL
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "080404b0"
BEGIN
VALUE "ProductVersion", "` + version + `.0\0"
VALUE "ProductName", "m3u8 downloader\0"
VALUE "LegalCopyright", "https://github.com/orestonce/m3u8d\0"
VALUE "FileDescription", "m3u8 downloader\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x804, 1200
END
END
`)
data, err := simplifiedchinese.GBK.NewEncoder().Bytes(data)
if err != nil {
panic(err)
}
err = ioutil.WriteFile("m3u8d-qt/version.rc", data, 0777)
if err != nil {
panic(err)
}
}