|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import sys |
| 6 | + |
| 7 | +import htmlmin |
| 8 | +import lesscpy |
| 9 | +from jsmin import jsmin |
| 10 | + |
| 11 | + |
| 12 | +def get_parser() -> argparse.ArgumentParser: |
| 13 | + parser = argparse.ArgumentParser( |
| 14 | + description="App Builder", |
| 15 | + ) |
| 16 | + parser.add_argument( |
| 17 | + "-n", |
| 18 | + "--name", |
| 19 | + default=os.path.basename(os.path.dirname(os.path.abspath(__file__))), |
| 20 | + help="App package's base name", |
| 21 | + ) |
| 22 | + parser.add_argument( |
| 23 | + "-d", |
| 24 | + "--debug", |
| 25 | + action="store_true", |
| 26 | + help="Bundle webxdc package with debugging tools included", |
| 27 | + ) |
| 28 | + |
| 29 | + return parser |
| 30 | + |
| 31 | + |
| 32 | +def size_fmt(num: float) -> str: |
| 33 | + suffix = "B" |
| 34 | + for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]: |
| 35 | + if abs(num) < 1024.0: |
| 36 | + return "%3.1f%s%s" % (num, unit, suffix) # noqa |
| 37 | + num /= 1024.0 |
| 38 | + return "%.1f%s%s" % (num, "Yi", suffix) # noqa |
| 39 | + |
| 40 | + |
| 41 | +def minify_js() -> None: |
| 42 | + if os.path.exists("js"): |
| 43 | + os.makedirs("build/js") |
| 44 | + |
| 45 | + bundle = False |
| 46 | + with open("index.html", encoding="utf-8") as src: |
| 47 | + if "js/bundle.js" in src.read(): |
| 48 | + bundle = True |
| 49 | + |
| 50 | + if bundle: |
| 51 | + with open(f"build/js/bundle.js", "w", encoding="utf-8") as dest: |
| 52 | + for filename in sorted(os.listdir("js")): |
| 53 | + if not filename.endswith(".js"): |
| 54 | + continue |
| 55 | + with open(f"js/{filename}", encoding="utf-8") as src: |
| 56 | + if filename.endswith(".min.js"): |
| 57 | + text = src.read() |
| 58 | + else: |
| 59 | + text = jsmin(src.read()) |
| 60 | + dest.write(text) |
| 61 | + if not text.endswith(";"): |
| 62 | + dest.write(";") |
| 63 | + else: |
| 64 | + files.extend( |
| 65 | + [f"js/{name}" for name in os.listdir("js") if name.endswith(".min.js")] |
| 66 | + ) |
| 67 | + for filename in os.listdir("js"): |
| 68 | + if not filename.endswith(".min.js"): |
| 69 | + with open(f"js/{filename}", encoding="utf-8") as src: |
| 70 | + with open( |
| 71 | + f"build/js/{filename}", "w", encoding="utf-8" |
| 72 | + ) as dest: |
| 73 | + dest.write(jsmin(src.read())) |
| 74 | + |
| 75 | + |
| 76 | +def minify_css() -> None: |
| 77 | + if os.path.exists("css"): |
| 78 | + os.makedirs("build/css") |
| 79 | + |
| 80 | + bundle = False |
| 81 | + with open("index.html", encoding="utf-8") as src: |
| 82 | + if "css/bundle.css" in src.read(): |
| 83 | + bundle = True |
| 84 | + |
| 85 | + if bundle: |
| 86 | + with open(f"build/css/bundle.css", "w", encoding="utf-8") as dest: |
| 87 | + for filename in sorted(os.listdir("css")): |
| 88 | + if not filename.endswith(".css"): |
| 89 | + continue |
| 90 | + with open(f"css/{filename}", encoding="utf-8") as src: |
| 91 | + if filename.endswith(".min.css"): |
| 92 | + dest.write(src.read()) |
| 93 | + else: |
| 94 | + dest.write(lesscpy.compile(src, minify=True, xminify=True)) |
| 95 | + else: |
| 96 | + files.extend( |
| 97 | + [ |
| 98 | + f"css/{name}" |
| 99 | + for name in os.listdir("css") |
| 100 | + if name.endswith(".min.css") |
| 101 | + ] |
| 102 | + ) |
| 103 | + for filename in os.listdir("css"): |
| 104 | + if not filename.endswith(".min.css"): |
| 105 | + with open(f"css/{filename}", encoding="utf-8") as src: |
| 106 | + with open( |
| 107 | + f"build/css/{filename}", "w", encoding="utf-8" |
| 108 | + ) as dest: |
| 109 | + dest.write(lesscpy.compile(src, minify=True, xminify=True)) |
| 110 | + |
| 111 | + |
| 112 | +def minify_html() -> None: |
| 113 | + for filename in os.listdir(): |
| 114 | + if filename.endswith(".html"): |
| 115 | + with open(filename, encoding="utf-8") as src: |
| 116 | + with open(f"build/{filename}", "w", encoding="utf-8") as dest: |
| 117 | + dest.write(htmlmin.minify(src.read())) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + args = get_parser().parse_args() |
| 122 | + app_archive = args.name if args.name.endswith(".xdc") else f"{args.name}.xdc" |
| 123 | + files = [] |
| 124 | + |
| 125 | + # CLEAN |
| 126 | + shutil.rmtree("build", ignore_errors=True) |
| 127 | + for name in os.listdir(): |
| 128 | + if os.path.isfile(name) and name.endswith(".xdc"): |
| 129 | + os.remove(name) |
| 130 | + |
| 131 | + if args.debug: |
| 132 | + files.append("eruda.min.js") |
| 133 | + |
| 134 | + if os.path.exists("assets"): |
| 135 | + shutil.copytree("assets", "build/assets") |
| 136 | + |
| 137 | + # TRANSCRYPT |
| 138 | + if os.path.exists("app.py"): |
| 139 | + from transcrypt.__main__ import main as transcrypt |
| 140 | + |
| 141 | + sys.argv = ["transcrypt"] |
| 142 | + if args.debug: |
| 143 | + sys.argv.append("-n") |
| 144 | + sys.argv.append("app.py") |
| 145 | + transcrypt() |
| 146 | + shutil.copytree("__target__", "build/__target__") |
| 147 | + os.remove(f"build/__target__/app.project") |
| 148 | + |
| 149 | + minify_js() |
| 150 | + minify_css() |
| 151 | + minify_html() |
| 152 | + |
| 153 | + # ADD METADATA |
| 154 | + for name in ("manifest.toml", "icon.png", "icon.jpg"): |
| 155 | + if os.path.exists(name): |
| 156 | + files.append(name) |
| 157 | + |
| 158 | + for path in files: |
| 159 | + shutil.copyfile(f"{path}", f"build/{path}") |
| 160 | + project_root = os.path.abspath(".") |
| 161 | + os.chdir("build") |
| 162 | + shutil.make_archive(f"{project_root}/{app_archive}", "zip") |
| 163 | + os.chdir(project_root) |
| 164 | + os.rename(f"{app_archive}.zip", app_archive) |
| 165 | + |
| 166 | + # for testing: |
| 167 | + if os.path.exists("webxdc.js"): |
| 168 | + shutil.copyfile("webxdc.js", "build/webxdc.js") |
| 169 | + |
| 170 | + with open(app_archive, "rb") as file: |
| 171 | + size = len(file.read()) |
| 172 | + print(f"App saved as: {app_archive} ({size_fmt(size)})") |
0 commit comments