mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-17 20:44:38 +03:00
chore: move updater
This commit is contained in:
2
go/cmd/updater/.gitignore
vendored
Normal file
2
go/cmd/updater/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/updater
|
||||
/launcher
|
||||
11
go/cmd/updater/go.mod
Normal file
11
go/cmd/updater/go.mod
Normal file
@@ -0,0 +1,11 @@
|
||||
module updater
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/codeclysm/extract v2.2.0+incompatible
|
||||
|
||||
require (
|
||||
github.com/h2non/filetype v1.1.3 // indirect
|
||||
github.com/juju/errors v0.0.0-20220331221717-b38fca44723b // indirect
|
||||
github.com/stretchr/testify v1.7.1 // indirect
|
||||
)
|
||||
19
go/cmd/updater/go.sum
Normal file
19
go/cmd/updater/go.sum
Normal file
@@ -0,0 +1,19 @@
|
||||
github.com/codeclysm/extract v2.2.0+incompatible h1:q3wyckoA30bhUSiwdQezMqVhwd8+WGE64/GL//LtUhI=
|
||||
github.com/codeclysm/extract v2.2.0+incompatible/go.mod h1:2nhFMPHiU9At61hz+12bfrlpXSUrOnK+wR+KlGO4Uks=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
|
||||
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
|
||||
github.com/juju/errors v0.0.0-20220331221717-b38fca44723b h1:AxFeSQJfcm2O3ov1wqAkTKYFsnMw2g1B4PkYujfAdkY=
|
||||
github.com/juju/errors v0.0.0-20220331221717-b38fca44723b/go.mod h1:jMGj9DWF/qbo91ODcfJq6z/RYc3FX3taCBZMCcpI4Ls=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
5
go/cmd/updater/launcher_darwin.go
Normal file
5
go/cmd/updater/launcher_darwin.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func Launcher() {
|
||||
println("?")
|
||||
}
|
||||
77
go/cmd/updater/launcher_linux.go
Normal file
77
go/cmd/updater/launcher_linux.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var local_qt_theme bool
|
||||
|
||||
func Launcher() {
|
||||
log.Println("Running as launcher")
|
||||
wd, _ := filepath.Abs(".")
|
||||
|
||||
_debug := flag.Bool("debug", false, "Debug mode")
|
||||
flag.BoolVar(&local_qt_theme, "theme", false, "Use local QT theme (unstable)")
|
||||
flag.Parse()
|
||||
|
||||
// Find & symlink some Qt Plugin to enable system theme
|
||||
tryLinkQtPlugin("styles", !local_qt_theme)
|
||||
tryLinkQtPlugin("platformthemes", !local_qt_theme)
|
||||
|
||||
cmd := exec.Command("./nekoray", flag.Args()...)
|
||||
cmd.Env = os.Environ()
|
||||
ld_env := "LD_LIBRARY_PATH=" + filepath.Join(wd, "./usr/lib")
|
||||
cmd.Env = append(cmd.Env, ld_env)
|
||||
log.Println(ld_env, cmd)
|
||||
|
||||
if *_debug {
|
||||
cmd.Env = append(cmd.Env, "QT_DEBUG_PLUGINS=1")
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Run()
|
||||
} else {
|
||||
cmd.Start()
|
||||
}
|
||||
}
|
||||
|
||||
func tryLinkQtPlugin(sub string, remove bool) {
|
||||
wd_plugins_sub := filepath.Join("./usr/plugins", sub)
|
||||
|
||||
if Exist(wd_plugins_sub) {
|
||||
if remove {
|
||||
os.RemoveAll(wd_plugins_sub)
|
||||
}
|
||||
} else {
|
||||
if remove {
|
||||
return
|
||||
}
|
||||
|
||||
arch := "x86_64"
|
||||
if runtime.GOARCH == "arm64" {
|
||||
arch = "aarch64"
|
||||
}
|
||||
|
||||
paths := []string{
|
||||
filepath.Join("/usr/lib/qt5/plugins", sub),
|
||||
filepath.Join("/usr/lib64/qt5/plugins", sub),
|
||||
filepath.Join("/usr/lib/"+arch+"-linux-gnu/qt5/plugins", sub),
|
||||
filepath.Join("/usr/lib/qt/plugins", sub),
|
||||
}
|
||||
path := FindExist(paths)
|
||||
if path == "" {
|
||||
log.Println("warning:", sub, "not found")
|
||||
return
|
||||
}
|
||||
|
||||
err := os.Symlink(path, wd_plugins_sub)
|
||||
if err != nil {
|
||||
log.Println("symlink failed:", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
10
go/cmd/updater/launcher_windows.go
Normal file
10
go/cmd/updater/launcher_windows.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func Launcher() {
|
||||
log.Fatalf("launcher is not for your platform", runtime.GOOS)
|
||||
}
|
||||
58
go/cmd/updater/main.go
Normal file
58
go/cmd/updater/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// update & launcher
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
wd := filepath.Dir(exe)
|
||||
os.Chdir(wd)
|
||||
exe = filepath.Base(os.Args[0])
|
||||
log.Println("exe:", exe, "exe dir:", wd)
|
||||
|
||||
if strings.HasPrefix(strings.ToLower(exe), "updater") {
|
||||
if runtime.GOOS == "windows" {
|
||||
if strings.HasPrefix(strings.ToLower(exe), "updater.old") {
|
||||
// 2. "updater.old" update files
|
||||
time.Sleep(time.Second)
|
||||
Updater()
|
||||
// 3. start
|
||||
exec.Command("./nekoray.exe").Start()
|
||||
} else {
|
||||
// 1. nekoray stop it self and run "updater.exe"
|
||||
Copy("./updater.exe", "./updater.old")
|
||||
exec.Command("./updater.old").Start()
|
||||
}
|
||||
} else {
|
||||
// 1. update files
|
||||
Updater()
|
||||
// 2. start
|
||||
Launcher()
|
||||
}
|
||||
return
|
||||
} else if strings.HasPrefix(strings.ToLower(exe), "launcher") {
|
||||
Launcher()
|
||||
return
|
||||
}
|
||||
log.Fatalf("wrong name")
|
||||
}
|
||||
|
||||
func Copy(src string, dst string) {
|
||||
// Read all content of src to data
|
||||
data, _ := ioutil.ReadFile(src)
|
||||
// Write data to dst
|
||||
ioutil.WriteFile(dst, data, 0644)
|
||||
}
|
||||
115
go/cmd/updater/updater.go
Normal file
115
go/cmd/updater/updater.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/codeclysm/extract"
|
||||
)
|
||||
|
||||
func Updater() {
|
||||
pre_cleanup := func() {
|
||||
if runtime.GOOS == "linux" {
|
||||
os.RemoveAll("./usr")
|
||||
}
|
||||
os.RemoveAll("./nekoray_update")
|
||||
}
|
||||
|
||||
// update extract
|
||||
if Exist("./nekoray.zip") {
|
||||
pre_cleanup()
|
||||
log.Println("updating from zip")
|
||||
|
||||
f, err := os.Open("./nekoray.zip")
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
err = extract.Zip(context.Background(), f, "./nekoray_update", nil)
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
f.Close()
|
||||
} else if Exist("./nekoray.tar.gz") {
|
||||
pre_cleanup()
|
||||
log.Println("updating from tar.gz")
|
||||
|
||||
f, err := os.Open("./nekoray.tar.gz")
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
err = extract.Gz(context.Background(), f, "./nekoray_update", nil)
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
f.Close()
|
||||
} else {
|
||||
log.Fatalln("no update")
|
||||
}
|
||||
|
||||
// remove old file
|
||||
removeAll("./*.dll")
|
||||
removeAll("./*.dmp")
|
||||
|
||||
// update move
|
||||
err := Mv("./nekoray_update/nekoray", "./")
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
|
||||
os.RemoveAll("./nekoray_update")
|
||||
os.RemoveAll("./nekoray.zip")
|
||||
os.RemoveAll("./nekoray.tar.gz")
|
||||
}
|
||||
|
||||
func Exist(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func FindExist(paths []string) string {
|
||||
for _, path := range paths {
|
||||
if Exist(path) {
|
||||
return path
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func Mv(src, dst string) error {
|
||||
s, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.IsDir() {
|
||||
es, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, e := range es {
|
||||
err = Mv(filepath.Join(src, e.Name()), filepath.Join(dst, e.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = os.MkdirAll(filepath.Dir(dst), 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Rename(src, dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeAll(glob string) {
|
||||
files, _ := filepath.Glob(glob)
|
||||
for _, f := range files {
|
||||
os.Remove(f)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user