From bcb119575165c79e308c9dc79aaaa83800275012 Mon Sep 17 00:00:00 2001 From: Jamie Hodgson Date: Sat, 21 Oct 2023 10:01:51 +0200 Subject: [PATCH] build extra icons pack --- .github/workflows/main.yml | 3 +++ build_extra_icons.py | 21 ++++++++++++++++++++ util/basic_builder.py | 39 +++++++++++++++++++++++++------------- util/image.py | 13 +++++++++++++ 4 files changed, 63 insertions(+), 13 deletions(-) create mode 100755 build_extra_icons.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 10b7201..8e16319 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -38,6 +38,9 @@ jobs: - name: Install oxipng run: which oxipng || cargo install oxipng + - name: Install ImageMagick + run: sudo apt-get install -y imagemagick + - name: Build packs run: | for builder in build_*.py; do diff --git a/build_extra_icons.py b/build_extra_icons.py new file mode 100755 index 0000000..7133936 --- /dev/null +++ b/build_extra_icons.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +from util.basic_builder import BasicBuildSettings, build_basic_pack + + +def main(): + settings = BasicBuildSettings() + settings.repo_url = "https://github.com/jamie-mh/AuthenticatorPro.git" + settings.png_glob = ["extraicons", "*.png"] + settings.output_name = "extra-icons" + settings.pack_name = "Extra Icons" + settings.pack_description = ( + "Icons that don't fit the inclusion criteria or that were removed" + ) + settings.pack_url = "https://github.com/jamie-mh/AuthenticatorPro" + + build_basic_pack(settings) + + +if __name__ == "__main__": + main() diff --git a/util/basic_builder.py b/util/basic_builder.py index 25e3063..e34cb83 100644 --- a/util/basic_builder.py +++ b/util/basic_builder.py @@ -1,17 +1,20 @@ import glob +import shutil import tempfile from os import path +from typing import Optional from util import icon_pack_pb2 from util.git import clone_repo -from util.image import render_svg_as_png, optimise_png +from util.image import render_svg_as_png, optimise_png, resize_png from util.log import LOG from util.pack import add_png_files_to_pack class BasicBuildSettings: repo_url: str - svg_glob: list[str] + svg_glob: Optional[list[str]] = None + png_glob: Optional[list[str]] = None output_name: str pack_name: str pack_description: str @@ -19,18 +22,28 @@ class BasicBuildSettings: def process_icons(settings: BasicBuildSettings, pack_dir: str, build_dir: str): - glob_path = path.join(pack_dir, *settings.svg_glob) - icons = glob.glob(glob_path) + if settings.svg_glob: + glob_path = path.join(pack_dir, *settings.svg_glob) + icons = glob.glob(glob_path) - for icon in icons: - base_name = path.basename(icon) - output_path = path.join(build_dir, base_name.replace("svg", "png")) + for icon in icons: + base_name = path.basename(icon) + output_path = path.join(build_dir, base_name.replace("svg", "png")) - with open(icon, "r") as file: - svg = file.read() - render_svg_as_png(svg, output_path) + with open(icon, "r") as file: + svg = file.read() + render_svg_as_png(svg, output_path) - optimise_png(output_path) + optimise_png(output_path) + elif settings.png_glob: + glob_path = path.join(pack_dir, *settings.png_glob) + icons = glob.glob(glob_path) + + for icon in icons: + base_name = path.basename(icon) + output_path = path.join(build_dir, base_name) + resize_png(icon, output_path) + optimise_png(output_path) def build_pack(settings: BasicBuildSettings, build_dir: str): @@ -52,10 +65,10 @@ def build_basic_pack(settings: BasicBuildSettings): clone_repo(settings.repo_url, pack_dir) with tempfile.TemporaryDirectory() as build_dir: - LOG.info("Converting icons to png") + LOG.info("Processing icons") process_icons(settings, pack_dir, build_dir) LOG.info("Building pack") build_pack(settings, build_dir) - LOG.info("Written to %s", settings.output_name) + LOG.info("Written to %s.iconpack", settings.output_name) diff --git a/util/image.py b/util/image.py index 288bfc8..8363ea6 100644 --- a/util/image.py +++ b/util/image.py @@ -20,6 +20,19 @@ def get_tinted_svg(source: str, colour: str) -> str: return svg.replace("/>", f' fill="#{colour}"/>') +def resize_png(input_path: str, output_path: str): + convert = subprocess.run( + ["convert", "-resize", f"{ICON_SIZE}x{ICON_SIZE}", input_path, output_path], + check=True, + stdout=subprocess.DEVNULL, + stderr=subprocess.PIPE, + ) + + if convert.stdout: + message = convert.stdout.decode("utf-8") + raise RuntimeError("Error calling convert command " + message) + + def optimise_png(path: str): optimisation = subprocess.run( ["oxipng", "-o", "4", "-i", "0", "--strip", "safe", path],