seperate gui / cli builds

This commit is contained in:
olebeck 2023-03-06 15:50:36 +01:00
parent fbb2305d1c
commit ee156a1271
11 changed files with 180 additions and 74 deletions

View File

@ -19,6 +19,11 @@ jobs:
go-version: '1.20'
check-latest: true
cache: true
- name: Setup Golang with cache
uses: magnetikonline/action-golang-cache@v3
with:
go-version-file: go.mod
- uses: awalsh128/cache-apt-pkgs-action@latest
with:
@ -45,11 +50,10 @@ jobs:
- name: build
id: build
run: |
python build.py
run: python build.py
- name: Deploy with rsync
run: rsync -avz ./updates/ olebeck@${{ secrets.SSH_HOST }}:/var/www/updates/bedrocktool/
run: rsync -avz ./updates/ olebeck@${{ secrets.SSH_HOST }}:/var/www/updates/
- name: 'Get Previous tag'
id: previoustag

1
.gitignore vendored
View File

@ -21,6 +21,7 @@ keys.db
/builds/
/updates/
/fyne-cross/
/tmp/
packets.log.gpg
customdata.json

124
build.py
View File

@ -1,15 +1,19 @@
import subprocess, re, sys, os, shutil, json, binascii, hashlib, gzip
import subprocess, re, sys, os, shutil, json, binascii, hashlib, gzip, stat
VER_RE = re.compile(r"v(\d\.\d+\.\d+)")
NAME = "bedrocktool"
APP_ID = "yuv.pink.bedrocktool"
TAG = subprocess.run(["git", "describe", "--exclude", "r-*", "--tags", "--always"], stdout=subprocess.PIPE).stdout.decode("utf8").split("\n")[0]
if TAG == "":
TAG = "v0.0.0"
VER = VER_RE.match(TAG).group(1)
CI = not not os.getenv("GITLAB_CI")
with open("./utils/resourcepack-ace.go") as f:
PACK_SUPPORT = f.read(7) == "package"
with open("./utils/resourcepack-ace.go", "rb") as f:
PACK_SUPPORT = f.read(7) == b"package"
print(f"Pack Support: {PACK_SUPPORT}")
@ -18,13 +22,17 @@ LDFLAGS = f"-s -w -X github.com/bedrock-tool/bedrocktool/utils.Version={TAG}"
PLATFORMS = [
("windows", ["386", "amd64"], ".exe"),
("linux", ["386", "amd64", "arm", "arm64"], ""),
("darwin", ["amd64", "arm64"], ""),
#("darwin", ["amd64", "arm64"], ""),
]
platform_filter = ""
if len(sys.argv) > 1:
platform_filter = sys.argv[1]
if os.path.exists("./tmp"):
shutil.rmtree("./tmp")
os.mkdir("./tmp")
if os.path.exists("./builds"):
shutil.rmtree("./builds")
os.mkdir("./builds")
@ -32,44 +40,88 @@ os.mkdir("./builds")
if os.path.exists("./updates"):
shutil.rmtree("./updates")
os.mkdir("./updates")
os.mkdir(f"./updates/{TAG}")
for (platform_name, archs, ext) in PLATFORMS:
if platform_filter and platform_filter != platform_name:
continue
print(f"Building {platform_name}")
exe_name = f"{NAME}{ext}"
args = [
"fyne-cross", platform_name,
"-app-version", VER,
"-arch", ",".join(archs),
"-ldflags", LDFLAGS,
"-name", exe_name,
"-env", "GOVCS=*:off"
]
if platform_name == "windows":
args.append("-console")
if platform_name == "darwin":
args.extend(["-app-id", APP_ID])
for GUI in [False, True]:
print(f"Building {platform_name} gui: {GUI}")
SUB1 = '-gui' if GUI else ''
exe_name = f"{NAME}{SUB1}{ext}"
args.append("./cmd/bedrocktool")
out = subprocess.run(args)
out.check_returncode()
env = ["GOVCS=*:off"]
GOFLAGS = []
if not PACK_SUPPORT:
GOFLAGS.append("-overlay=overlay.json")
for arch in archs:
exe_path = f"./fyne-cross/bin/{platform_name}-{arch}/{exe_name}"
with open(exe_path, "rb") as f:
exe_data = f.read()
sha = binascii.b2a_base64(hashlib.sha256(exe_data).digest()).decode("utf8").split("\n")[0]
if GUI:
if len(GOFLAGS):
env.append(f"GOFLAGS={' '.join(GOFLAGS)}")
args = [
"fyne-cross", platform_name,
"-app-version", VER,
"-arch", ",".join(archs),
"-ldflags", LDFLAGS + f" -X github.com/bedrock-tool/bedrocktool/utils.CmdName=bedrocktool-gui",
"-name", exe_name,
"-tags", "gui",
"-debug"
]
for e in env:
args.extend(["-env", e])
if platform_name == "windows":
args.append("-console")
if platform_name == "darwin":
args.extend(["-app-id", APP_ID])
args.append("./cmd/bedrocktool")
out = subprocess.run(args)
out.check_returncode()
else:
for arch in archs:
out_path = f"./tmp/{platform_name}-{arch}/{exe_name}"
os.makedirs(os.path.dirname(out_path), exist_ok=True)
env.extend([f"GOOS={platform_name}", f"GOARCH={arch}"])
env.append("CGO_ENABLED=0")
args = [
"go", "build",
"-ldflags", LDFLAGS,
"-trimpath",
"-v",
"-o", out_path,
]
args.extend(GOFLAGS)
args.append("./cmd/bedrocktool")
print(args)
out = subprocess.run(args)
out.check_returncode()
with open(f"./updates/{platform_name}-{arch}.json", "w") as f:
f.write(json.dumps({
"Version": TAG,
"Sha256": sha,
}, indent=2))
for arch in archs:
if GUI:
exe_path = f"./fyne-cross/bin/{platform_name}-{arch}/{exe_name}"
else:
exe_path = f"./tmp/{platform_name}-{arch}/{exe_name}"
with gzip.open(f"./updates/{TAG}/{platform_name}-{arch}.gz", "wb") as f:
f.write(exe_data)
with open(exe_path, "rb") as f:
exe_data = f.read()
sha = binascii.b2a_base64(hashlib.sha256(exe_data).digest()).decode("utf8").split("\n")[0]
exe_out_path = f"./builds/{NAME}-{platform_name}-{arch}-{TAG}{SUB1}{ext}"
with open(exe_out_path, "wb") as f:
f.write(exe_data)
os.chmod(exe_out_path, os.stat(exe_out_path).st_mode | stat.S_IEXEC)
with open(f"./builds/{NAME}-{platform_name}-{arch}-{TAG}{ext}", "wb") as f:
f.write(exe_data)
updates_dir = f"./updates/{NAME}{SUB1}"
os.makedirs(updates_dir, exist_ok=True)
with open(f"{updates_dir}/{platform_name}-{arch}.json", "w") as f:
f.write(json.dumps({
"Version": TAG,
"Sha256": sha,
}, indent=2))
os.makedirs(f"{updates_dir}/{TAG}", exist_ok=True)
with gzip.open(f"{updates_dir}/{TAG}/{platform_name}-{arch}.gz", "wb") as f:
f.write(exe_data)
if not GUI:
os.remove(exe_path)

View File

@ -11,8 +11,6 @@ import (
"runtime/debug"
"syscall"
"fyne.io/fyne/v2/widget"
"github.com/bedrock-tool/bedrocktool/gui"
"github.com/bedrock-tool/bedrocktool/locale"
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/bedrock-tool/bedrocktool/utils/crypt"
@ -21,19 +19,20 @@ import (
_ "github.com/bedrock-tool/bedrocktool/subcommands"
_ "github.com/bedrock-tool/bedrocktool/subcommands/skins"
_ "github.com/bedrock-tool/bedrocktool/subcommands/world"
_ "github.com/bedrock-tool/bedrocktool/ui/gui"
"github.com/google/subcommands"
"github.com/sirupsen/logrus"
)
type CLI struct {
gui.UI
utils.UI
}
func (c *CLI) Init() {
}
func (c *CLI) SetOptions() bool {
func (c *CLI) SetOptions(context.Context) bool {
flag.Parse()
return false
}
@ -97,17 +96,17 @@ func main() {
subcommands.ImportantFlag("preload")
subcommands.HelpCommand()
var ui gui.UI
var ui utils.UI
if len(os.Args) < 2 {
ui = gui.NewGUI()
ui = utils.MakeGui()
utils.Options.IsInteractive = true
} else {
ui = &CLI{}
}
ui.Init()
if ui.SetOptions() {
if ui.SetOptions(ctx) {
return
}
@ -233,18 +232,10 @@ func (c *CreateCustomDataCMD) Execute(_ context.Context, f *flag.FlagSet, _ ...i
return 0
}
func (c *TransCMD) SettingsUI() *widget.Form {
return nil
}
func (c *TransCMD) MainWindow() error {
return nil
}
func (c *CreateCustomDataCMD) SettingsUI() *widget.Form {
return nil
}
func (c *CreateCustomDataCMD) MainWindow() error {
return nil
}

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/bedrock-tool/bedrocktool
go 1.19
go 1.20
//replace github.com/sandertv/gophertunnel => ./gophertunnel
replace github.com/sandertv/gophertunnel => github.com/olebeck/gophertunnel v1.27.4-1

View File

@ -1,9 +0,0 @@
package gui
import "context"
type UI interface {
Init()
SetOptions() bool
Execute(context.Context) error
}

View File

@ -52,7 +52,9 @@ func (s *SkinPack) AddSkin(skin *Skin) bool {
func (s *SkinPack) Save(fpath, serverName string) error {
os.MkdirAll(fpath, 0o755)
var skinsJson []skinEntry
var skinsJson struct {
Skins []skinEntry `json:"skins"`
}
geometryJson := map[string]SkinGeometry{}
for _, s2 := range s.skins { // write skin texture
@ -92,7 +94,7 @@ func (s *SkinPack) Save(fpath, serverName string) error {
entry.Geometry = geometryName
}
}
skinsJson = append(skinsJson, entry)
skinsJson.Skins = append(skinsJson.Skins, entry)
}
if len(geometryJson) > 0 { // geometry.json

View File

@ -1,3 +1,5 @@
//go:build gui
package gui
import (
@ -72,16 +74,12 @@ var settings = map[string]func(utils.Command) *widget.Form{
}
type GUI struct {
UI
utils.UI
selected binding.String
}
func NewGUI() *GUI {
return &GUI{}
}
func (g *GUI) SetOptions() bool {
func (g *GUI) SetOptions(ctx context.Context) bool {
a := app.New()
w := a.NewWindow("Bedrocktool")
@ -167,3 +165,9 @@ func (g *GUI) Execute(ctx context.Context) error {
cmd.Execute(ctx, nil)
return nil
}
func init() {
utils.MakeGui = func() utils.UI {
return &GUI{}
}
}

1
ui/gui/stub.go Normal file
View File

@ -0,0 +1 @@
package gui

57
utils/iui.go Normal file
View File

@ -0,0 +1,57 @@
package utils
import (
"context"
"flag"
"fmt"
"os"
"strings"
"github.com/bedrock-tool/bedrocktool/locale"
"github.com/google/subcommands"
)
type UI interface {
Init()
SetOptions(context.Context) bool
Execute(context.Context) error
}
type InteractiveCLI struct {
UI
}
func (c *InteractiveCLI) Init() {
}
func (c *InteractiveCLI) SetOptions(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
fmt.Println(locale.Loc("available_commands", nil))
for name, cmd := range ValidCMDs {
fmt.Printf("\t%s\t%s\n", name, cmd.Synopsis())
}
fmt.Println(locale.Loc("use_to_run_command", nil))
cmd, cancelled := UserInput(ctx, locale.Loc("input_command", nil))
if cancelled {
return true
}
_cmd := strings.Split(cmd, " ")
os.Args = append(os.Args, _cmd...)
}
flag.Parse()
return false
}
func (c *InteractiveCLI) Execute(ctx context.Context) error {
subcommands.Execute(ctx)
return nil
}
var MakeGui = func() UI {
return &InteractiveCLI{}
}

View File

@ -1,8 +1,11 @@
package utils
import "github.com/sanbornm/go-selfupdate/selfupdate"
import (
"github.com/sanbornm/go-selfupdate/selfupdate"
)
var Version string
var CmdName = "bedrocktool"
const updateServer = "https://updates.yuv.pink/"
@ -11,5 +14,5 @@ var Updater = &selfupdate.Updater{
ApiURL: updateServer,
BinURL: updateServer,
Dir: "update/",
CmdName: "bedrocktool", // app name
CmdName: CmdName,
}