-
Notifications
You must be signed in to change notification settings - Fork 11
/
update.go
164 lines (156 loc) · 4.49 KB
/
update.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
package main
import (
"fmt"
"github.com/otiai10/copy"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
)
func updateUpdater() {
fmt.Println("正在获取updater最新版本号..")
updaterVersion := "v" + recentlyTag("https://github.com/Jrohy/cfw-updater/tags")[0]
if version != updaterVersion {
currentPath, _ := os.Executable()
_, execName := filepath.Split(currentPath)
fmt.Println("发现新版updater: " + updaterVersion)
downloadFileName := "cfw-updater.exe"
if runtime.GOOS == "darwin" {
if runtime.GOARCH == "arm64" {
downloadFileName = "cfw-updater_mac_arm64"
} else {
downloadFileName = "cfw-updater_mac_amd64"
}
}
oldName := strings.ReplaceAll(downloadFileName, "cfw-updater", "old")
newName := strings.ReplaceAll(downloadFileName, "cfw-updater", "new")
downloadFile(fmt.Sprintf("https://github.com/Jrohy/cfw-updater/releases/download/%s/%s", updaterVersion, downloadFileName), newName)
if runtime.GOOS == "darwin" {
exec.Command("chmod", "+x", newName).Run()
}
startBackground()
os.Rename(execName, oldName)
os.Rename(newName, execName)
fmt.Println()
exit("updater更新完成, 请手动重新运行!")
} else {
var c *exec.Cmd
if runtime.GOOS == "darwin" {
c = exec.Command("zsh", "-c", "clear")
} else {
c = exec.Command("cmd", "/c", "cls")
}
c.Stdout = os.Stdout
c.Run()
fmt.Println("cfw-updater " + updaterVersion)
}
fmt.Println()
}
func updateDmgShell(updatePath, dmgPath string) string {
return fmt.Sprintf(`
DMG_PATH="%s"
UPDATE_PATH="%s"
TEMP_PATH="%s"
VOLUME=$(hdiutil attach "$DMG_PATH" | grep Volumes | awk -F " " '{for (i=3;i<=NF;i++)printf("%%s ", $i);print ""}'|awk '$1=$1')
[[ -e "$UPDATE_PATH"/Contents/MacOS/data ]] && sudo cp -rp "$UPDATE_PATH"/Contents/MacOS/data "$TEMP_PATH"/
sudo rm -rf "$UPDATE_PATH"
sudo cp -rf "$VOLUME"/*.app "$UPDATE_PATH"
[[ -e "$TEMP_PATH"/data ]] && sudo mv "$TEMP_PATH"/data "$UPDATE_PATH"/Contents/MacOS/
hdiutil detach "$VOLUME" >/dev/null
sudo xattr -r -d com.apple.quarantine "$UPDATE_PATH"
`, dmgPath, updatePath, tempPath)
}
func updateTransFile(diList []*downloadInfo, stopCh chan struct{}) {
var srcFile string
if strings.Contains(diList[len(diList)-1].fileFullName, "asar") {
srcFile = fullPath("app.asar")
} else {
srcFile = fullPath(path.Join(diList[len(diList)-1].fileName, "app.asar"))
}
if runtime.GOOS == "darwin" {
execCommand(fmt.Sprintf("sudo cp -rp \"%s\" \"%s\"", srcFile, path.Join(ci.rootPath, "Contents/Resources/app.asar")))
} else {
if err := copy.Copy(srcFile, path.Join(ci.rootPath, "resources/app.asar")); err != nil {
close(stopCh)
fmt.Printf("\n\n请尝试以管理员身份运行此程序:\n")
exit(err.Error())
}
}
}
func updateCoreFile(diList []*downloadInfo, stopCh chan struct{}) {
if ci.portableData && runtime.GOOS == "windows" {
if err := copy.Copy(ci.rootPath+"/data", fullPath(diList[0].fileName+"/data")); err != nil {
close(stopCh)
fmt.Printf("\n\n")
exit(err.Error())
}
}
if ci.installType == MacDmg {
startBackground()
execCommand(updateDmgShell(ci.rootPath, fullPath(diList[0].fileFullName)))
} else if ci.installType == WinExe {
startBackground()
go exec.Command(fullPath(diList[0].fileFullName), "/S").Run()
for {
if check := checkCfw(); check != nil {
check.process.Kill()
break
}
}
} else {
for {
if err := copy.Copy(fullPath(diList[0].fileName), ci.rootPath); err == nil {
break
}
}
}
}
func stopCfw(stopCh chan struct{}) {
if runtime.GOOS == "darwin" {
// 提前获取macOS用于更新的密码
fmt.Println("请输入更新所需的密码(有权限无需密码的会跳过):")
execCommand("sudo echo >/dev/null")
fmt.Println()
}
go showProgress("更新cfw中", stopCh)
ci.process.Kill()
if ci.portableData && len(ci.otherProcess) > 1 {
for _, item := range ci.otherProcess {
item.Kill()
}
}
}
func startCfw(stopCh chan struct{}) {
if ci.installType == Win7z {
startBackground()
}
if runtime.GOOS == "darwin" {
go exec.Command(path.Join(ci.rootPath, "Contents/MacOS/Clash for Windows")).Run()
} else {
go exec.Command(path.Join(ci.rootPath, "Clash for Windows.exe")).Run()
}
for {
if checkCfw() != nil {
break
}
}
close(stopCh)
}
func updateCfw(diList []*downloadInfo) {
stopCh := make(chan struct{})
if updateCore || updateTrans {
stopCfw(stopCh)
}
if updateCore {
updateCoreFile(diList, stopCh)
}
if updateTrans {
updateTransFile(diList, stopCh)
}
if updateCore || updateTrans {
startCfw(stopCh)
fmt.Printf("\n\n更新成功!\n\n")
}
}