diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index dded42a39..0a563c586 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -29,7 +29,7 @@ jobs: python -m pip install pip --upgrade python -m pip install -r requirements.txt - name: Build - run: python -m tools.build_www + run: python -m tools.cli --cleanup --font-formats woff2 --html - name: Setup Pages uses: actions/configure-pages@v5 - name: Upload artifact diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 22e0cd8f7..4b73ab6d5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,6 +8,12 @@ jobs: release: permissions: contents: write + strategy: + matrix: + font-size: [10, 12, 16] + width-mode: [monospaced, proportional] + font-format: [otf, woff2, ttf, bdf, pcf, otc, ttc] + fail-fast: false runs-on: ubuntu-latest steps: - name: Checkout @@ -21,7 +27,7 @@ jobs: python -m pip install pip --upgrade python -m pip install -r requirements.txt - name: Build - run: python -m tools.build + run: python -m tools.cli --cleanup --font-sizes ${{ matrix.font-size }} --width-modes ${{ matrix.width-mode }} --font-formats ${{ matrix.font-format }} --release - name: Release uses: softprops/action-gh-release@v2 with: diff --git a/README.md b/README.md index d577d19e3..71344f743 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,7 @@ brew install font-ark-pixel-16px-proportional - [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/) - [Jinja](https://github.com/pallets/jinja) - [Loguru](https://github.com/Delgan/loguru) +- [Cyclopts](https://github.com/BrianPugh/cyclopts) ## 字形依赖 diff --git a/requirements.txt b/requirements.txt index 2e046ddc0..4695184df 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ pillow==10.4.0 beautifulsoup4==4.12.3 Jinja2==3.1.4 loguru==0.7.2 +cyclopts==2.9.3 diff --git a/tools/build.py b/tools/build.py index bddd8cc8d..0f0ff1fc5 100644 --- a/tools/build.py +++ b/tools/build.py @@ -1,41 +1,12 @@ -import itertools -import shutil - -from tools import configs -from tools.configs import path_define -from tools.configs.font import FontConfig -from tools.services import publish_service, info_service, template_service, image_service -from tools.services.font_service import DesignContext +from tools import cli def main(): - if path_define.build_dir.exists(): - shutil.rmtree(path_define.build_dir) - - font_configs = {} - design_contexts = {} - - for font_size in configs.font_sizes: - font_config = FontConfig.load(font_size) - font_configs[font_size] = font_config - design_context = DesignContext.load(font_config) - design_contexts[font_size] = design_context - for width_mode in configs.width_modes: - for font_format in itertools.chain(configs.font_formats, configs.font_collection_formats): - design_context.make_fonts(width_mode, font_format) - publish_service.make_release_zip(font_size, width_mode, font_format) - info_service.make_font_info(design_context, width_mode) - template_service.make_alphabet_html(design_context, width_mode) - template_service.make_demo_html(design_context) - image_service.make_preview_image(font_config) - template_service.make_index_html(font_configs) - template_service.make_playground_html(font_configs) - image_service.make_readme_banner(design_contexts) - image_service.make_github_banner(design_contexts) - image_service.make_itch_io_banner(design_contexts) - image_service.make_itch_io_background(design_contexts) - image_service.make_itch_io_cover(font_configs) - image_service.make_afdian_cover(font_configs) + cli.main( + cleanup=True, + release=True, + all_attachments=True, + ) if __name__ == '__main__': diff --git a/tools/build_www.py b/tools/build_www.py deleted file mode 100644 index a70167cdb..000000000 --- a/tools/build_www.py +++ /dev/null @@ -1,25 +0,0 @@ -from tools import configs -from tools.configs.font import FontConfig -from tools.services import template_service -from tools.services.font_service import DesignContext - - -def main(): - font_configs = {} - design_contexts = {} - - for font_size in configs.font_sizes: - font_config = FontConfig.load(font_size) - font_configs[font_size] = font_config - design_context = DesignContext.load(font_config) - design_contexts[font_size] = design_context - for width_mode in configs.width_modes: - design_context.make_fonts(width_mode, 'woff2') - template_service.make_alphabet_html(design_context, width_mode) - template_service.make_demo_html(design_context) - template_service.make_index_html(font_configs) - template_service.make_playground_html(font_configs) - - -if __name__ == '__main__': - main() diff --git a/tools/cli.py b/tools/cli.py new file mode 100644 index 000000000..e5d9f0958 --- /dev/null +++ b/tools/cli.py @@ -0,0 +1,98 @@ +import shutil + +from cyclopts import App +from loguru import logger + +from tools import configs +from tools.configs import FontSize, WidthMode, FontFormat, FontCollectionFormat, path_define +from tools.configs.font import FontConfig +from tools.services import publish_service, info_service, template_service, image_service +from tools.services.font_service import DesignContext + +app = App(version=configs.version) + + +@app.default +def main( + cleanup: bool = False, + font_sizes: list[FontSize] | None = None, + width_modes: list[WidthMode] | None = None, + font_formats: list[FontFormat | FontCollectionFormat] | None = None, + release: bool = False, + all_attachments: bool = False, + font_info: bool = False, + html: bool = False, + image: bool = False, +): + if font_sizes is None: + font_sizes = configs.font_sizes + if width_modes is None: + width_modes = configs.width_modes + if font_formats is None: + font_formats = configs.font_formats + configs.font_collection_formats + if all_attachments: + font_info = True + html = True + image = True + + print() + print(f'cleanup = {cleanup}') + print(f'font_sizes = {repr(font_sizes)}') + print(f'width_modes = {repr(width_modes)}') + print(f'font_formats = {repr(font_formats)}') + print(f'release = {release}') + print(f'font_info = {font_info}') + print(f'html = {html}') + print(f'image = {image}') + print() + + if cleanup and path_define.build_dir.exists(): + shutil.rmtree(path_define.build_dir) + logger.info("Delete dir: '{}'", path_define.build_dir) + + font_configs = {} + design_contexts = {} + for font_size in font_sizes: + font_config = FontConfig.load(font_size) + font_configs[font_size] = font_config + design_context = DesignContext.load(font_config) + design_contexts[font_size] = design_context + for width_mode in width_modes: + for font_format in font_formats: + design_context.make_fonts(width_mode, font_format) + if release: + publish_service.make_release_zip(font_size, width_mode, font_format) + + all_font_configs = set(font_sizes) == set(configs.font_sizes) + + if font_info: + for font_size in font_sizes: + design_context = design_contexts[font_size] + for width_mode in width_modes: + info_service.make_font_info(design_context, width_mode) + + if html: + for font_size in font_sizes: + design_context = design_contexts[font_size] + for width_mode in width_modes: + template_service.make_alphabet_html(design_context, width_mode) + template_service.make_demo_html(design_context) + if all_font_configs: + template_service.make_index_html(font_configs) + template_service.make_playground_html(font_configs) + + if image: + for font_size in font_sizes: + font_config = font_configs[font_size] + image_service.make_preview_image(font_config) + if all_font_configs: + image_service.make_readme_banner(design_contexts) + image_service.make_github_banner(design_contexts) + image_service.make_itch_io_banner(design_contexts) + image_service.make_itch_io_background(design_contexts) + image_service.make_itch_io_cover(font_configs) + image_service.make_afdian_cover(font_configs) + + +if __name__ == '__main__': + app()