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

Add a UUID cookiecutter extension. #1850

Merged
merged 2 commits into from
Jun 2, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1850.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A UUID cookiecutter extension was added to support Windows MSI templating.
16 changes: 16 additions & 0 deletions src/briefcase/integrations/cookiecutter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Jinja2 extensions."""

import uuid

from jinja2.ext import Extension


Expand Down Expand Up @@ -116,3 +118,17 @@ def bool_attr(obj):
return "true" if obj else "false"

environment.filters["bool_attr"] = bool_attr


class UUIDExtension(Extension):
"""Extensions for generating UUIDs."""

def __init__(self, environment):
"""Initialize the extension with the given environment."""
super().__init__(environment)

def dns_uuid5(obj):
"""A DNS-based UUID5 object generated from the provided content."""
return str(uuid.uuid5(uuid.NAMESPACE_DNS, obj))

environment.filters["dns_uuid5"] = dns_uuid5
19 changes: 19 additions & 0 deletions tests/integrations/cookiecutter/test_UUIDExtension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from unittest.mock import MagicMock

import pytest

from briefcase.integrations.cookiecutter import UUIDExtension


@pytest.mark.parametrize(
"value, expected",
[
("example.com", "cfbff0d1-9375-5685-968c-48ce8b15ae17"),
("foobar.example.com", "941bbcd9-03e1-568a-a728-8434055bc338"),
],
)
def test_dns_uuid5_value(value, expected):
env = MagicMock()
env.filters = {}
UUIDExtension(env)
assert env.filters["dns_uuid5"](value) == expected