Skip to content

Commit

Permalink
Deduplicate Webpack builds. (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored May 5, 2024
1 parent a3ccfb1 commit 07e353b
Show file tree
Hide file tree
Showing 100 changed files with 1,373 additions and 1,013 deletions.
8 changes: 4 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ updates:
prefix: npm dependencies (development)
include: scope
- package-ecosystem: npm
directory: betty/extension/cotton_candy/assets/betty.extension.npm._Npm/src/
directory: betty/extension/cotton_candy/webpack/
schedule:
interval: weekly
assignees:
Expand All @@ -37,7 +37,7 @@ updates:
prefix: npm dependencies (Cotton Candy extension)
include: scope
- package-ecosystem: npm
directory: betty/extension/http_api_doc/assets/betty.extension.npm._Npm/src/
directory: betty/extension/http_api_doc/webpack/
schedule:
interval: weekly
assignees:
Expand All @@ -46,7 +46,7 @@ updates:
prefix: npm dependencies (HttpApiDoc extension)
include: scope
- package-ecosystem: npm
directory: betty/extension/maps/assets/betty.extension.npm._Npm/src/
directory: betty/extension/maps/webpack/
schedule:
interval: weekly
assignees:
Expand All @@ -55,7 +55,7 @@ updates:
prefix: npm dependencies (Maps extension)
include: scope
- package-ecosystem: npm
directory: betty/extension/trees/assets/betty.extension.npm._Npm/src/
directory: betty/extension/trees/webpack/
schedule:
interval: weekly
assignees:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ __pycache__
/betty.egg-info
/build
/dist
/prebuild
*.pyc
*.pyc.*

Expand Down
2 changes: 1 addition & 1 deletion .stylelintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
betty/**/assets/betty.extension.npm._Npm/build/*
prebuild/**/*
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prune node_modules
81 changes: 81 additions & 0 deletions betty/_npm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
Provide tools to integrate extensions with `npm <https://www.npmjs.com/>`_.
This module is internal.
"""

from __future__ import annotations

import logging
import sys
from asyncio import subprocess as aiosubprocess
from pathlib import Path
from typing import Sequence

from betty.asyncio import wait_to_thread
from betty.error import UserFacingError
from betty.locale import Str, DEFAULT_LOCALIZER
from betty.requirement import Requirement
from betty.subprocess import run_process


_NPM_UNAVAILABLE_MESSAGE = Str._(
"npm (https://www.npmjs.com/) must be available for features that require Node.js packages to be installed. Ensure that the `npm` executable is available in your `PATH`."
)


class NpmUnavailable(UserFacingError, RuntimeError):
def __init__(self):
super().__init__(_NPM_UNAVAILABLE_MESSAGE)


async def npm(
arguments: Sequence[str],
cwd: Path | None = None,
) -> aiosubprocess.Process:
"""
Run an npm command.
"""
try:
return await run_process(
["npm", *arguments],
cwd=cwd,
# Use a shell on Windows so subprocess can find the executables it needs (see
# https://bugs.python.org/issue17023).
shell=sys.platform.startswith("win32"),
)
except FileNotFoundError:
raise NpmUnavailable()


class NpmRequirement(Requirement):
def __init__(self):
super().__init__()
self._met: bool
self._summary: Str
self._details = _NPM_UNAVAILABLE_MESSAGE

def _check(self) -> None:
if hasattr(self, "_met"):
return
try:
wait_to_thread(npm(["--version"]))
except NpmUnavailable:
self._met = False
self._summary = Str._("`npm` is not available")
else:
self._met = True
self._summary = Str._("`npm` is available")
finally:
logging.getLogger(__name__).debug(self._summary.localize(DEFAULT_LOCALIZER))

def is_met(self) -> bool:
self._check()
return self._met

def summary(self) -> Str:
self._check()
return self._summary

def details(self) -> Str:
return self._details
50 changes: 27 additions & 23 deletions betty/_package/pyinstaller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,31 @@

from betty._package.pyinstaller.hooks import HOOKS_DIRECTORY_PATH
from betty.app import App
from betty.app.extension import discover_extension_types, Extension
from betty.asyncio import gather
from betty.extension.npm import _Npm, build_assets, _NpmBuilder
from betty.app.extension import discover_extension_types
from betty.extension.webpack import (
Webpack,
WebpackEntrypointProvider,
)
from betty.fs import ROOT_DIRECTORY_PATH
from betty.project import ExtensionConfiguration
from betty.job import Context


async def _build_assets() -> None:
npm_builder_extension_types: list[type[_NpmBuilder & Extension]] = [
extension_type
for extension_type in discover_extension_types()
if issubclass(extension_type, _NpmBuilder)
]
async def prebuild_webpack_assets() -> None:
"""
Prebuild Webpack assets for inclusion in package builds.
"""
job_context = Context()
async with App.new_temporary() as app, app:
app.project.configuration.extensions.append(ExtensionConfiguration(_Npm))
for extension_type in npm_builder_extension_types:
app.project.configuration.extensions.append(
ExtensionConfiguration(extension_type)
)
await gather(
*(
[
build_assets(app.extensions[extension_type]) # type: ignore[arg-type]
for extension_type in npm_builder_extension_types
]
)
app.project.configuration.extensions.enable(Webpack)
webpack = app.extensions[Webpack]
app.project.configuration.extensions.enable(
*{
extension_type
for extension_type in discover_extension_types()
if issubclass(extension_type, WebpackEntrypointProvider)
}
)
await webpack.prebuild(job_context=job_context)


async def a_pyz_exe_coll() -> tuple[Analysis, PYZ, EXE, COLLECT]:
Expand All @@ -52,12 +50,18 @@ async def a_pyz_exe_coll() -> tuple[Analysis, PYZ, EXE, COLLECT]:
else:
raise RuntimeError(f"Unsupported platform {sys.platform}.")

await _build_assets()
await prebuild_webpack_assets()
block_cipher = None
datas = []
data_file_path_patterns = [
# Assets.
"betty/assets/**",
"betty/extension/*/assets/**",
# Webpack.
".browserslistrc",
"betty/extension/*/webpack/**",
"tsconfig.json",
"prebuild/**",
]
for data_file_path_pattern in data_file_path_patterns:
for data_file_path_str in glob(
Expand Down
17 changes: 4 additions & 13 deletions betty/assets/betty.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Betty VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-03-27 23:37+0000\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -189,16 +189,7 @@ msgstr ""
msgid "Birth"
msgstr ""

msgid "Built the Cotton Candy front-end assets."
msgstr ""

msgid "Built the HTTP API documentation."
msgstr ""

msgid "Built the interactive family trees."
msgstr ""

msgid "Built the interactive maps."
msgid "Built the Webpack front-end assets."
msgstr ""

msgid "Burial"
Expand Down Expand Up @@ -590,10 +581,10 @@ msgstr ""
msgid "Places"
msgstr ""

msgid "Pre-built assets"
msgid "Pre-built Webpack front-end assets are available"
msgstr ""

msgid "Pre-built assets are unavailable for {extension_names}."
msgid "Pre-built Webpack front-end assets are unavailable"
msgstr ""

msgid "Presence"
Expand Down
23 changes: 7 additions & 16 deletions betty/assets/locale/de-DE/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Betty VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-03-27 23:37+0000\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"PO-Revision-Date: 2024-02-08 13:24+0000\n"
"Last-Translator: Bart Feenstra <bart@bartfeenstra.com>\n"
"Language: de\n"
Expand Down Expand Up @@ -279,17 +279,8 @@ msgstr "Betty-Projektkonfiguration ({supported_formats})"
msgid "Birth"
msgstr "Geburt"

msgid "Built the Cotton Candy front-end assets."
msgstr "Cotton Candy front-end assets erstellt."

msgid "Built the HTTP API documentation."
msgstr "Die HTTP-API-Dokumentation wurde erstellt."

msgid "Built the interactive family trees."
msgstr "Die interaktiven Stammbäume sind erstellt."

msgid "Built the interactive maps."
msgstr "Die interaktiven Karten wurden erstellt."
msgid "Built the Webpack front-end assets."
msgstr "Webpack front-end assets erstellt."

msgid "Burial"
msgstr "Beerdigung"
Expand Down Expand Up @@ -760,11 +751,11 @@ msgstr "Ort"
msgid "Places"
msgstr "Orte"

msgid "Pre-built assets"
msgstr "Vorgefertigte Objekte"
msgid "Pre-built Webpack front-end assets are available"
msgstr "Vorgefertigte Webpack front-end assets sind verfügbar"

msgid "Pre-built assets are unavailable for {extension_names}."
msgstr "Vorgefertigte Objekte für {extension_names} sind nicht verfügbar."
msgid "Pre-built Webpack front-end assets are unavailable"
msgstr "Vorgefertigte Webpack front-end assets sind nicht verfügbar"

msgid "Presence"
msgstr "Anwesenheit"
Expand Down
17 changes: 4 additions & 13 deletions betty/assets/locale/fr-FR/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-03-27 23:37+0000\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"PO-Revision-Date: 2024-02-08 13:24+0000\n"
"Last-Translator: Bart Feenstra <bart@bartfeenstra.com>\n"
"Language: fr\n"
Expand Down Expand Up @@ -233,16 +233,7 @@ msgstr ""
msgid "Birth"
msgstr "Naissance"

msgid "Built the Cotton Candy front-end assets."
msgstr ""

msgid "Built the HTTP API documentation."
msgstr ""

msgid "Built the interactive family trees."
msgstr ""

msgid "Built the interactive maps."
msgid "Built the Webpack front-end assets."
msgstr ""

msgid "Burial"
Expand Down Expand Up @@ -674,10 +665,10 @@ msgstr "Lieu"
msgid "Places"
msgstr "Lieux"

msgid "Pre-built assets"
msgid "Pre-built Webpack front-end assets are available"
msgstr ""

msgid "Pre-built assets are unavailable for {extension_names}."
msgid "Pre-built Webpack front-end assets are unavailable"
msgstr ""

msgid "Presence"
Expand Down
23 changes: 7 additions & 16 deletions betty/assets/locale/nl-NL/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-03-27 23:37+0000\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"PO-Revision-Date: 2024-02-11 15:31+0000\n"
"Last-Translator: Bart Feenstra <bart@bartfeenstra.com>\n"
"Language: nl\n"
Expand Down Expand Up @@ -276,17 +276,8 @@ msgstr "Betty-projectconfiguratie ({supported_formats})"
msgid "Birth"
msgstr "Geboorte"

msgid "Built the Cotton Candy front-end assets."
msgstr "De Cotton Candy front-endassets gegenereerd."

msgid "Built the HTTP API documentation."
msgstr "HTTP API-documentatie gegenereerd."

msgid "Built the interactive family trees."
msgstr "De interactieve stambomen gegenereerd."

msgid "Built the interactive maps."
msgstr "De interactieve kaarten gegenereerd."
msgid "Built the Webpack front-end assets."
msgstr "De Webpack front-end assets gebouwd."

msgid "Burial"
msgstr "Begravenis"
Expand Down Expand Up @@ -755,11 +746,11 @@ msgstr "Plaats"
msgid "Places"
msgstr "Plaatsen"

msgid "Pre-built assets"
msgstr "Kant-en-klare assets"
msgid "Pre-built Webpack front-end assets are available"
msgstr "Vooraf gebouwde Webpack front-end assets zijn beschikbaar."

msgid "Pre-built assets are unavailable for {extension_names}."
msgstr "Kant-en-klare assets zijn beschikbaar voor {extension_names}."
msgid "Pre-built Webpack front-end assets are unavailable"
msgstr "Vooraf gebouwde Webpack front-end assets zijn niet beschikbaar."

msgid "Presence"
msgstr "Aanwezigheid"
Expand Down
Loading

0 comments on commit 07e353b

Please sign in to comment.