import subprocess, re, sys, os, shutil, json, binascii, hashlib, gzip VER_RE = re.compile(r"v(\d\.\d+\.\d+)(?:-(\d+)-(\w))?") NAME = "bedrocktool" APP_ID = "yuv.pink.bedrocktool" 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}" 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") with open("./subcommands/resourcepack-d/resourcepack-d.go", "rb") as f: PACK_SUPPORT = f.read(100).count(b"package ") > 0 print(f"Pack Support: {PACK_SUPPORT}") LDFLAGS = f"-s -w -X github.com/bedrock-tool/bedrocktool/utils.Version={TAG}" PLATFORMS = [ ("windows", ["amd64"], ".exe"), ("linux", ["amd64"], ""), #("darwin", ["amd64", "arm64"], ""), #("android", ["arm64"], ".apk") ] 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 os.path.exists("./tmp"): shutil.rmtree("./tmp") os.mkdir("./tmp") 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 for GUI in [False, True]: if platform_name in ["android"] and not GUI: continue print(f"Building {platform_name} 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" } for arch in archs: compiled_path = f"./tmp/{platform_name}-{arch}{SUB1}/{name}{ext}" os.makedirs(os.path.dirname(compiled_path), exist_ok=True) env.update({ "GOOS": platform_name, "GOARCH": arch, }) args = [ "go", "build", "-ldflags", LDFLAGS, "-trimpath", "-tags", ",".join(tags), "-v", "-o", compiled_path, ] args.append("./cmd/bedrocktool") print(args) env2 = os.environ.copy() env2.update(env) out = subprocess.run(args, env=env2) out.check_returncode() exe_out_path = f"./builds/{NAME}-{platform_name}-{arch}-{TAG}{SUB1}{ext}" 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) 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) os.remove(compiled_path)