This commit is contained in:
josc146
2023-06-01 16:54:21 +08:00
parent 06a125b8d7
commit b63370928d
15 changed files with 85 additions and 31 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/minio/selfupdate"
@@ -13,7 +14,9 @@ import (
// App struct
type App struct {
ctx context.Context
ctx context.Context
exDir string
cmdPrefix string
}
// NewApp creates a new App application struct
@@ -25,6 +28,14 @@ func NewApp() *App {
// so we can call the runtime methods
func (a *App) OnStartup(ctx context.Context) {
a.ctx = ctx
ex, _ := os.Executable()
a.exDir = filepath.Dir(ex)
a.cmdPrefix = ""
if runtime.GOOS == "darwin" {
a.exDir += "/../../../"
a.cmdPrefix = "cd " + a.exDir + " && "
}
a.downloadLoop()
}

View File

@@ -9,7 +9,7 @@ import (
)
func (a *App) DownloadFile(path string, url string) error {
_, err := grab.Get(path, url)
_, err := grab.Get(a.exDir+path, url)
if err != nil {
return err
}
@@ -81,7 +81,7 @@ func (a *App) AddToDownloadList(path string, url string) {
downloadList = append(downloadList, DownloadStatus{
resp: nil,
Name: filepath.Base(path),
Path: path,
Path: a.exDir + path,
Url: url,
Downloading: true,
})

View File

@@ -2,7 +2,7 @@ package backend_golang
import (
"encoding/json"
"fmt"
"errors"
"io"
"os"
"os/exec"
@@ -18,14 +18,14 @@ func (a *App) SaveJson(fileName string, jsonData any) error {
return err
}
if err := os.WriteFile(fileName, text, 0644); err != nil {
if err := os.WriteFile(a.exDir+fileName, text, 0644); err != nil {
return err
}
return nil
}
func (a *App) ReadJson(fileName string) (any, error) {
file, err := os.ReadFile(fileName)
file, err := os.ReadFile(a.exDir + fileName)
if err != nil {
return nil, err
}
@@ -40,7 +40,7 @@ func (a *App) ReadJson(fileName string) (any, error) {
}
func (a *App) FileExists(fileName string) bool {
_, err := os.Stat(fileName)
_, err := os.Stat(a.exDir + fileName)
return err == nil
}
@@ -52,7 +52,7 @@ type FileInfo struct {
}
func (a *App) ReadFileInfo(fileName string) (FileInfo, error) {
info, err := os.Stat(fileName)
info, err := os.Stat(a.exDir + fileName)
if err != nil {
return FileInfo{}, err
}
@@ -65,7 +65,7 @@ func (a *App) ReadFileInfo(fileName string) (FileInfo, error) {
}
func (a *App) ListDirFiles(dirPath string) ([]FileInfo, error) {
files, err := os.ReadDir(dirPath)
files, err := os.ReadDir(a.exDir + dirPath)
if err != nil {
return nil, err
}
@@ -87,7 +87,7 @@ func (a *App) ListDirFiles(dirPath string) ([]FileInfo, error) {
}
func (a *App) DeleteFile(path string) error {
err := os.Remove(path)
err := os.Remove(a.exDir + path)
if err != nil {
return err
}
@@ -95,18 +95,18 @@ func (a *App) DeleteFile(path string) error {
}
func (a *App) CopyFile(src string, dst string) error {
sourceFile, err := os.Open(src)
sourceFile, err := os.Open(a.exDir + src)
if err != nil {
return err
}
defer sourceFile.Close()
err = os.MkdirAll(dst[:strings.LastIndex(dst, "/")], 0755)
err = os.MkdirAll(a.exDir+dst[:strings.LastIndex(dst, "/")], 0755)
if err != nil {
return err
}
destFile, err := os.Create(dst)
destFile, err := os.Create(a.exDir + dst)
if err != nil {
return err
}
@@ -120,7 +120,7 @@ func (a *App) CopyFile(src string, dst string) error {
}
func (a *App) OpenFileFolder(path string) error {
absPath, err := filepath.Abs(path)
absPath, err := filepath.Abs(a.exDir + path)
if err != nil {
return err
}
@@ -131,10 +131,16 @@ func (a *App) OpenFileFolder(path string) error {
if err != nil {
return err
}
return nil
case "darwin":
fmt.Println("Running on macOS")
cmd := exec.Command("open", "-R", absPath)
err := cmd.Run()
if err != nil {
return err
}
return nil
case "linux":
fmt.Println("Running on Linux")
println("unsupported OS")
}
return nil
return errors.New("unsupported OS")
}

View File

@@ -26,7 +26,7 @@ func (a *App) ConvertModel(python string, modelPath string, strategy string, out
if err != nil {
return "", err
}
return Cmd(python, "./backend-python/convert_model.py", "--in", modelPath, "--out", outPath, "--strategy", strategy)
return Cmd(python, "./backend-python/convert_model.py", "--in", modelPath, "--out", outPath, "--strategy", `"`+strategy+`"`)
}
func (a *App) DepCheck(python string) error {
@@ -37,7 +37,7 @@ func (a *App) DepCheck(python string) error {
if err != nil {
return err
}
out, err := exec.Command(python, "./backend-python/dep_check.py").CombinedOutput()
out, err := exec.Command(python, a.exDir+"./backend-python/dep_check.py").CombinedOutput()
if err != nil {
return errors.New("DepCheck Error: " + string(out))
}
@@ -63,7 +63,11 @@ func (a *App) InstallPyDep(python string, cnMirror bool) (string, error) {
if err != nil {
return "", err
}
_, err = Cmd(python, "-m", "pip", "install", "torch==1.13.1", "torchvision==0.14.1", "torchaudio==0.13.1", "--index-url", "https://download.pytorch.org/whl/cu117")
if runtime.GOOS == "windows" {
_, err = Cmd(python, "-m", "pip", "install", "torch==1.13.1", "torchvision==0.14.1", "torchaudio==0.13.1", "--index-url", "https://download.pytorch.org/whl/cu117")
} else {
_, err = Cmd(python, "-m", "pip", "install", "torch", "torchvision", "torchaudio")
}
if err != nil {
return "", err
}

View File

@@ -15,12 +15,8 @@ import (
)
func Cmd(args ...string) (string, error) {
path, err := filepath.Abs(args[0])
if err != nil {
return "", err
}
args[0] = path
if runtime.GOOS == "windows" {
switch platform := runtime.GOOS; platform {
case "windows":
_, err := os.Stat("cmd-helper.bat")
if err != nil {
if err := os.WriteFile("./cmd-helper.bat", []byte("start %*"), 0644); err != nil {
@@ -31,13 +27,33 @@ func Cmd(args ...string) (string, error) {
if err != nil {
return "", err
}
path, err := filepath.Abs(args[0])
if err != nil {
return "", err
}
args[0] = path
cmd := exec.Command(cmdHelper, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
return string(out), nil
} else {
case "darwin":
ex, err := os.Executable()
if err != nil {
return "", err
}
exDir := filepath.Dir(ex) + "/../../../"
cmd := exec.Command("osascript", "-e", `tell application "Terminal" to do script "`+"cd "+exDir+" && "+strings.Join(args, " ")+`"`)
err = cmd.Start()
if err != nil {
return "", err
}
cmd.Wait()
return "", nil
case "linux":
cmd := exec.Command(args[0], args[1:]...)
err := cmd.Start()
if err != nil {
@@ -46,9 +62,19 @@ func Cmd(args ...string) (string, error) {
cmd.Wait()
return "", nil
}
return "", errors.New("unsupported OS")
}
func CopyEmbed(efs embed.FS) error {
prefix := ""
if runtime.GOOS == "darwin" {
ex, err := os.Executable()
if err != nil {
return err
}
prefix = filepath.Dir(ex) + "/../../../"
}
err := fs.WalkDir(efs, ".", func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
@@ -61,6 +87,7 @@ func CopyEmbed(efs embed.FS) error {
return err
}
path = prefix + path
err = os.MkdirAll(path[:strings.LastIndex(path, "/")], 0755)
if err != nil {
return err