bedrocktool/build.py

156 lines
5.0 KiB
Python
Raw Normal View History

2023-03-08 14:18:05 +00:00
import subprocess, re, sys, os, shutil, json, binascii, hashlib, gzip
2023-03-06 01:03:31 +00:00
2023-03-08 14:18:05 +00:00
VER_RE = re.compile(r"v(\d\.\d+\.\d+)(?:-(\d+)-(\w))?")
2023-03-06 01:03:31 +00:00
NAME = "bedrocktool"
APP_ID = "yuv.pink.bedrocktool"
2023-03-08 14:18:05 +00:00
GIT_TAG = subprocess.run(["git", "describe", "--exclude", "r*", "--tags", "--always"], stdout=subprocess.PIPE).stdout.decode("utf8").split("\n")[0]
if GIT_TAG == "":
GIT_TAG = "v0.0.0"
VER_MATCH = VER_RE.match(GIT_TAG)
VER = VER_MATCH.group(1)
PATCH = VER_MATCH.group(2) or "0"
TAG = f"{VER}-{PATCH}"
2023-03-06 01:03:31 +00:00
2023-03-08 14:18:05 +00:00
print(f"VER: {VER}")
print(f"TAG: {TAG}")
GITHUB_OUTPUT = os.getenv("GITHUB_OUTPUT")
if GITHUB_OUTPUT:
with open(GITHUB_OUTPUT, "a") as f:
f.write(f"release_tag=r{VER}\n")
2023-03-06 01:03:31 +00:00
2023-03-06 14:50:36 +00:00
with open("./subcommands/resourcepack-d/resourcepack-d.go", "rb") as f:
PACK_SUPPORT = f.read(100).count(b"package ") > 0
2023-03-06 01:03:31 +00:00
print(f"Pack Support: {PACK_SUPPORT}")
LDFLAGS = f"-s -w -X github.com/bedrock-tool/bedrocktool/utils.Version={TAG}"
PLATFORMS = [
2023-03-08 11:46:16 +00:00
("windows", ["amd64"], ".exe"),
("linux", ["amd64"], ""),
2023-03-06 14:50:36 +00:00
#("darwin", ["amd64", "arm64"], ""),
2023-03-06 15:47:02 +00:00
("android", ["arm64"], ".apk")
2023-03-06 01:03:31 +00:00
]
platform_filter = ""
arch_filter = ""
2023-03-06 01:03:31 +00:00
if len(sys.argv) > 1:
platform_filter = sys.argv[1]
if len(sys.argv) > 2:
arch_filter = sys.argv[2]
2023-03-06 14:50:36 +00:00
if os.path.exists("./tmp"):
shutil.rmtree("./tmp")
os.mkdir("./tmp")
2023-03-06 01:03:31 +00:00
if os.path.exists("./builds"):
shutil.rmtree("./builds")
os.mkdir("./builds")
if os.path.exists("./updates"):
shutil.rmtree("./updates")
os.mkdir("./updates")
for (platform_name, archs, ext) in PLATFORMS:
if platform_filter and platform_filter != platform_name:
continue
archs = [a for a in archs if arch_filter == "" or a == arch_filter]
if len(archs) == 0:
continue
2023-03-06 14:50:36 +00:00
for GUI in [False, True]:
2023-03-06 15:47:02 +00:00
if platform_name in ["android"] and not GUI:
continue
2023-03-06 14:50:36 +00:00
print(f"Building {platform_name} gui: {GUI}")
SUB1 = '-gui' if GUI else ''
2023-03-06 15:47:02 +00:00
name = f"{NAME}{SUB1}"
2023-03-06 14:50:36 +00:00
tags = []
2023-03-08 14:18:05 +00:00
if PACK_SUPPORT:
tags.append("packs")
2023-03-09 15:24:39 +00:00
env = {
"GOVCS": "*:off"
}
2023-03-06 14:50:36 +00:00
if GUI:
args = [
"fyne-cross", platform_name,
"-app-version", VER,
"-arch", ",".join(archs),
"-ldflags", LDFLAGS + f" -X github.com/bedrock-tool/bedrocktool/utils.CmdName=bedrocktool-gui",
2023-03-06 15:47:02 +00:00
"-name", name,
"-tags", ",".join(["gui"] + tags),
2023-03-06 14:50:36 +00:00
"-debug"
]
2023-03-09 15:24:39 +00:00
for k,v in env.items():
args.extend(["-env", f"{k}={v}"])
2023-03-06 14:50:36 +00:00
if platform_name == "windows":
args.append("-console")
2023-03-06 15:47:02 +00:00
if platform_name in ["android"]:
2023-03-06 14:50:36 +00:00
args.extend(["-app-id", APP_ID])
args.append("./cmd/bedrocktool")
out = subprocess.run(args)
out.check_returncode()
else:
for arch in archs:
2023-03-06 16:03:45 +00:00
out_path = f"./tmp/{platform_name}-{arch}/{name}{ext}"
2023-03-06 14:50:36 +00:00
os.makedirs(os.path.dirname(out_path), exist_ok=True)
2023-03-09 15:24:39 +00:00
env.update({
"GOOS": platform_name,
"GOARCH": arch,
"CGO_ENABLED": "0"
})
2023-03-06 14:50:36 +00:00
args = [
"go", "build",
"-ldflags", LDFLAGS,
"-trimpath",
"-tags", ",".join(tags),
2023-03-06 14:50:36 +00:00
"-v",
"-o", out_path,
]
args.append("./cmd/bedrocktool")
print(args)
2023-03-09 15:24:39 +00:00
env2 = os.environ.copy()
env2.update(env)
out = subprocess.run(args, env=env2)
2023-03-06 14:50:36 +00:00
out.check_returncode()
for arch in archs:
exe_out_path = f"./builds/{NAME}-{platform_name}-{arch}-{TAG}{SUB1}{ext}"
2023-03-06 15:47:02 +00:00
if platform_name == "android":
2023-03-06 16:03:45 +00:00
apk_path = f"./fyne-cross/dist/android-{arch}/{name}{ext}"
2023-03-06 15:47:02 +00:00
#shutil.copy(apk_path, exe_out_path) # dont upload builds yet, its not usable lol
else:
if GUI:
2023-03-06 16:03:45 +00:00
exe_path = f"./fyne-cross/bin/{platform_name}-{arch}/{name}"
2023-03-06 15:47:02 +00:00
else:
2023-03-06 16:03:45 +00:00
exe_path = f"./tmp/{platform_name}-{arch}/{name}{ext}"
2023-03-06 15:47:02 +00:00
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]
shutil.copy(exe_path, exe_out_path)
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)