Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor build script #49

Merged
merged 4 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/

- Now using `nox` for testing.
- Build script has been moved from inside the package to the root of the repository.
- Added link to MIT license in all SVG files and split the license and Refactoring UI Inc. copyright information into separate lines.

### Removed

- Dropped support for Python 3.7.
- Removed all build scripts and related methods from package.

## [0.1.3]

Expand Down
70 changes: 41 additions & 29 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,74 @@
import shutil
from collections.abc import Sequence
from pathlib import Path
from tempfile import TemporaryDirectory

from nodejs import npm

from wagtail_heroicons.icons import Heroicon
import httpx
from bs4 import BeautifulSoup
from bs4 import Comment

HEROICONS_LATEST_VERSION = "1.0.6"

NODE_SRC_DIR = Path(__file__).parent / "node_modules" / "heroicons"
DEST_DIR = (
Path(__file__).parent / "src" / "wagtail_heroicons" / "templates" / "heroicons"
)


def build(argv: Sequence[str] | None = None) -> int:
args = parse_args(argv)

install_heroicons(args.version, args.dest)

for icon in Heroicon.get_icons():
icon._add_id_and_license()

return 0


def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()

parser.add_argument(
"-v",
"--version",
help="choose the version of heroicons to use",
default=HEROICONS_LATEST_VERSION,
)

parser.add_argument(
"--dest",
help="choose the destination directory",
type=Path,
default=DEST_DIR,
)
args = parser.parse_args(argv)

version = args.version
dest = args.dest

response = httpx.get(
f"https://github.com/tailwindlabs/heroicons/archive/v{version}.zip",
follow_redirects=True,
).raise_for_status()

with TemporaryDirectory() as tmpdir:
with open(Path(tmpdir) / "heroicons.zip", "wb") as f:
f.write(response.content)

return parser.parse_args(argv)
shutil.unpack_archive(Path(tmpdir) / "heroicons.zip", tmpdir)

for icon_type_path in (
Path(tmpdir) / f"heroicons-{version}" / "optimized"
).iterdir():
icon_type_folder = Path(dest) / icon_type_path.name

def install_heroicons(version: str, dest: Path) -> None:
npm.run(["install", f"heroicons@{version}", "--silent", "--no-save"])
shutil.rmtree(icon_type_folder, ignore_errors=True)
shutil.copytree(icon_type_path, icon_type_folder)

for icon_type in ["outline", "solid"]:
shutil.rmtree(dest / icon_type, ignore_errors=True)
shutil.copytree(
NODE_SRC_DIR / icon_type,
dest / icon_type,
)
for icon_path in dest.rglob("*.svg"):
with open(icon_path) as f:
soup = BeautifulSoup(f.read(), "html.parser")
for icon in soup.find_all("svg"):
icon_name = icon_path.stem
icon_type = icon_path.parent.name

shutil.rmtree("node_modules")
icon.attrs["id"] = f"icon-heroicons-{icon_name}-{icon_type}"

icon.insert(0, Comment(" MIT License https://mit-license.org/ "))
icon.insert(1, Comment(" Copyright (c) 2020 Refactoring UI Inc. "))

with open(icon_path, "wb") as f:
f.write(soup.prettify("utf-8"))

return 0


if __name__ == "__main__":
raise SystemExit(build())
raise SystemExit(main())
Binary file added heroicons.zip
Binary file not shown.
17 changes: 15 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ dev = [
"beautifulsoup4",
"bumpver",
"coverage[toml]",
"django-stubs",
"environs",
"hatch",
"httpx",
"mypy",
"nodejs-bin",
"nox[uv]",
"pytest",
"pytest-cov",
"pytest-randomly",
"pytest-xdist",
"ruff"
"ruff",
"types-beautifulsoup4"
]
lint = ["pre-commit"]

Expand Down Expand Up @@ -76,13 +79,17 @@ exclude_lines = [
"if TYPE_CHECKING:",
'def __str__\(self\)\s?\-?\>?\s?\w*\:'
]
fail_under = 75
fail_under = 60

[tool.coverage.run]
branch = true
omit = ["tests/*"]
parallel = true

[tool.django-stubs]
django_settings_module = "tests.settings"
strict_settings = false

[tool.hatch.build]
exclude = [".*", "Justfile"]

Expand All @@ -97,10 +104,16 @@ check_untyped_defs = true
exclude = "docs/.*\\.py$"
mypy_path = "src/"
no_implicit_optional = true
plugins = ["mypy_django_plugin.main"]
warn_redundant_casts = true
warn_unused_configs = true
warn_unused_ignores = true

[[tool.mypy.overrides]]
ignore_errors = true
ignore_missing_imports = true
module = ["tests.*", "wagtail.*"]

[tool.pytest.ini_options]
addopts = "-n auto --dist loadfile --doctest-modules"
norecursedirs = ".* bin build dist *.egg htmlcov logs node_modules templates venv"
Expand Down
28 changes: 0 additions & 28 deletions src/wagtail_heroicons/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,3 @@ def get_icons(cls) -> HeroiconList:
)

return icons

def _add_id_and_license(self) -> None:
try:
from bs4 import BeautifulSoup
from bs4 import Comment
except ImportError as e: # pragma: no cover
raise ImportError(
"bs4 is required to add the id attribute to the <svg> tag."
) from e

with self.path.open("r") as f:
soup = BeautifulSoup(f.read(), "html.parser")

for icon in soup.find_all("svg"):
# Wagtail uses the id attribute to identify icons. The name used in Wagtail
# is the id attribute of the <svg> tag, minus the "icon-" prefix.
icon.attrs[
"id"
] = f"icon-heroicons-{self.path.stem}-{self.path.parent.name}"
icon.insert(
0,
Comment(
" This work is licensed under the MIT License and is Copyright (c) 2020 Refactoring UI Inc. "
),
)

with self.path.open("wb") as f:
f.write(soup.prettify("utf-8"))
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading