bedrocktool/build.py

187 lines
5.2 KiB
Python
Raw Permalink 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
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}")
2023-03-24 19:45:53 +00:00
print(flush=True)
2023-03-06 01:03:31 +00:00
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-14 01:07:39 +00:00
#("android", ["arm64"], ".apk")
2023-03-06 01:03:31 +00:00
]
2023-03-06 15:47:02 +00:00
2023-03-15 13:51:31 +00:00
def clean():
2023-03-26 10:45:37 +00:00
shutil.rmtree("./tmp", True)
shutil.rmtree("./builds", True)
shutil.rmtree("./updates", True)
2023-03-15 13:51:31 +00:00
for file in os.listdir("./cmd/bedrocktool"):
if file.endswith(".syso"):
os.remove(f"./cmd/bedrocktool/{file}")
def make_dirs():
os.mkdir("./tmp")
os.mkdir("./builds")
os.mkdir("./updates")
2023-03-15 15:14:57 +00:00
def build_cli(platform: str, arch: str, env_in: dict[str,str], tags: list[str], ldflags, compiled_path: str):
2023-03-15 13:51:31 +00:00
env = {}
env.update(env_in)
env.update({
"GOOS": platform,
"GOARCH": arch,
})
args = [
"go", "build",
2023-03-15 15:14:57 +00:00
"-ldflags", ldflags,
2023-03-15 13:51:31 +00:00
"-trimpath",
"-tags", ",".join(tags),
"-o", compiled_path,
"-v"
]
args.append("./cmd/bedrocktool")
env2 = os.environ.copy()
env2.update(env)
subprocess.run(args, env=env2).check_returncode()
2023-03-15 15:14:57 +00:00
def build_gui(platform: str, arch: str, env, tags: list[str], ldflags, compiled_path: str):
2023-03-15 13:51:31 +00:00
if platform == "windows":
2023-03-15 15:14:57 +00:00
ldflags += " -H=windows"
2023-03-15 13:51:31 +00:00
args = [
"gogio",
"-arch", arch,
"-target", platform,
"-icon", "icon.png",
"-tags", ",".join(tags),
"-ldflags", ldflags,
"-o", compiled_path,
"-x"
]
if platform in ["android", "ios"]:
args.extend(["-appid", APP_ID])
args.append("./cmd/bedrocktool")
env2 = os.environ.copy()
env2.update(env)
subprocess.run(args, env=env2).check_returncode()
def package(platform: str, arch: str, compiled_path: str, GUI: bool, ext: str):
SUB1 = '-gui' if GUI else ''
exe_out_path = f"./builds/{NAME}-{platform}-{arch}-{TAG}{SUB1}{ext}"
# create hash and copy
with open(compiled_path, "rb") as f:
exe_data = f.read()
sha = binascii.b2a_base64(hashlib.sha256(exe_data).digest()).decode("utf8").split("\n")[0]
shutil.copy(compiled_path, exe_out_path)
# create update
updates_dir = f"./updates/{NAME}{SUB1}"
os.makedirs(updates_dir, exist_ok=True)
with open(f"{updates_dir}/{platform}-{arch}.json", "w") as f:
f.write(json.dumps({
"Version": TAG,
"Sha256": sha,
}, indent=2))
# write update data
os.makedirs(f"{updates_dir}/{TAG}", exist_ok=True)
with gzip.open(f"{updates_dir}/{TAG}/{platform}-{arch}.gz", "wb") as f:
f.write(exe_data)
os.remove(compiled_path)
def build_all(platform_filter: str, arch_filter: str):
for (platform, archs, ext) in PLATFORMS:
if platform_filter and platform_filter != platform:
continue
archs = [a for a in archs if arch_filter == "" or a == arch_filter]
if len(archs) == 0:
continue
for GUI in [False, True]:
if platform in ["android"] and not GUI:
continue
print(f"Building {platform} gui: {GUI}")
SUB1 = '-gui' if GUI else ''
name = f"{NAME}{SUB1}"
tags = []
if PACK_SUPPORT:
tags.append("packs")
if GUI:
tags.append("gui")
env = {
"GOVCS": "*:off"
}
2023-03-15 15:14:57 +00:00
ldflags = LDFLAGS
if tags.count("gui"):
CmdName = "bedrocktool-gui"
else:
CmdName = "bedrocktool"
ldflags += f" -X github.com/bedrock-tool/bedrocktool/utils.CmdName={CmdName}"
2023-03-15 13:51:31 +00:00
for arch in archs:
compiled_path = f"./tmp/{platform}-{arch}{SUB1}/{name}{ext}"
os.makedirs(os.path.dirname(compiled_path), exist_ok=True)
if GUI and platform != "linux":
2023-03-15 15:14:57 +00:00
build_gui(platform, arch, env, tags, ldflags, compiled_path)
2023-03-15 13:51:31 +00:00
else:
2023-03-15 15:14:57 +00:00
build_cli(platform, arch, env, tags, ldflags, compiled_path)
2023-03-15 13:51:31 +00:00
package(platform, arch, compiled_path, GUI, ext)
def main():
platform_filter = ""
arch_filter = ""
if len(sys.argv) > 1:
platform_filter = sys.argv[1]
if len(sys.argv) > 2:
arch_filter = sys.argv[2]
if platform_filter == "clean":
clean()
return
clean()
make_dirs()
build_all(platform_filter, arch_filter)
main()