-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hatchling to build distribution (#1112)
* refactor(packaging): use hatchling to build package This includes a custom build hook to build our frontend js & css * refactor(ci): use pypa/gh-action-pypi-publish action * refactor(ci): move publish job to the ci workflow This accomplishes two things: - Ensures that tests pass before publishing - On each workflow run, tests that the packaging works * test(ci): upload built wheel and sdist for examination * refactor(packaging): delete build-dist testenv from tox config * refactor(packaging): remove dist and upload targets from Makefile * docs(README): update the development instructions
- Loading branch information
Showing
10 changed files
with
351 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""A hatch build-hook that builds the front-end js/css at PEP 517 build time. | ||
By default, when building the dist, if the built frontend code is not | ||
present in the source directory, it will be built. (Node must be installed, | ||
or this will fail.) | ||
Environment Variables | ||
--------------------- | ||
There are a couple of environment variables that can be used to provide | ||
additional control over the building of the frontend code. | ||
- ``HATCH_BUILD_NO_HOOKS=true``: skip building the frontend code | ||
- ``HATCH_BUILD_CLEAN=true``: force (re)building of the frontend code | ||
""" | ||
import shutil | ||
import subprocess | ||
from pathlib import Path | ||
|
||
from hatchling.builders.hooks.plugin.interface import BuildHookInterface | ||
|
||
# path to frontend source | ||
FRONTEND = "frontend" | ||
|
||
# path to compiled JS entry point | ||
APP_JS = "lektor/admin/static/app.js" | ||
|
||
|
||
class FrontendBuildHook(BuildHookInterface): | ||
"""Hatching build hook to compile our frontend JS.""" | ||
|
||
def clean(self, versions): | ||
"""Called at the beginning of each PEP 517 build, if HATCH_BUILD_CLEAN is set. | ||
This implementation deletes any compiled frontend code that may be present | ||
in the source tree. | ||
""" | ||
app = self.app | ||
root = self.root | ||
|
||
if not Path(root, FRONTEND).is_dir(): | ||
app.display_info( | ||
"frontend source missing, skipping cleaning compiled output" | ||
) | ||
return | ||
|
||
output_path = Path(root, APP_JS).parent | ||
app.display_info(f"cleaning {output_path.relative_to(root)}") | ||
if output_path.is_dir(): | ||
shutil.rmtree(output_path) | ||
|
||
def initialize(self, version, build_data): | ||
"""Hook called before each package build. | ||
This implementation builds the compiled frontend source, but only | ||
if it is not already present in the source tree. | ||
Node (and npm) must be installed or this step will fail. | ||
""" | ||
app = self.app | ||
root = self.root | ||
|
||
if Path(root, APP_JS).is_file(): | ||
app.display_info(f"{APP_JS} exists, skipping frontend build") | ||
return | ||
|
||
npm = shutil.which("npm") | ||
if npm is None: | ||
app.abort("npm is not available. can not build frontend") | ||
|
||
frontend = Path(root, FRONTEND) | ||
if not frontend.is_dir(): | ||
app.abort("frontend source is missing. can not build frontend") | ||
|
||
app.display_info("npm install") | ||
subprocess.run((npm, "install"), cwd=frontend, check=True) | ||
app.display_info("npm run build") | ||
subprocess.run((npm, "run", "build"), cwd=frontend, check=True) | ||
app.display_success("built frontend static files") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.