Skip to content

Commit

Permalink
Merge pull request #697 from freakboy3742/safe-android-formal-name
Browse files Browse the repository at this point in the history
Fixes #696 - Allow for punctuation in Android formal names
  • Loading branch information
freakboy3742 authored Apr 4, 2022
2 parents 8c4723d + f28ffc3 commit bc3168c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/696.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Android projects that have punctuation in their formal names can now build without error.
30 changes: 30 additions & 0 deletions src/briefcase/platforms/android/gradle.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import subprocess

from briefcase.commands import (
Expand All @@ -13,6 +14,20 @@
from briefcase.integrations.android_sdk import AndroidSDK


def safe_formal_name(name):
"""Converts the name into a safe name on Android.
Certain characters (``/\\:<>"?*|``) can't be used as app names
on Android; ``!`` causes problems with Android build tooling.
Also ensure that trailing, leading, and consecutive whitespace
caused by removing punctuation is collapsed.
:param name: The candidate name
:returns: The safe version of the name.
"""
return re.sub(r'\s+', ' ', re.sub(r'[!/\\:<>"\?\*\|]', "", name)).strip()


class GradleMixin:
output_format = "gradle"
platform = "android"
Expand All @@ -28,6 +43,20 @@ def default_packaging_format(self):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def bundle_path(self, app):
"""
The path to the bundle for the app in the output format.
The bundle is the template-generated source form of the app.
The path will usually be a directory, the existence of which is
indicative that the template has been rolled out for an app.
This overrides the default behavior, using a "safe" formal name
:param app: The app config
"""
return self.platform_path / self.output_format / safe_formal_name(app.formal_name)

def binary_path(self, app):
return (
self.bundle_path(app)
Expand Down Expand Up @@ -88,6 +117,7 @@ def output_format_template_context(self, app: BaseConfig):

return {
'version_code': version_code,
'safe_formal_name': safe_formal_name(app.formal_name),
}


Expand Down
3 changes: 2 additions & 1 deletion tests/platforms/android/gradle/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def test_version_code(create_command, first_app_config, version, build, version_
if build:
first_app_config.build = build
assert create_command.output_format_template_context(first_app_config) == {
'version_code': version_code
'version_code': version_code,
'safe_formal_name': 'First App',
}
# Version code must be less than a 32 bit signed integer MAXINT.
assert int(version_code) < 2147483647
38 changes: 38 additions & 0 deletions tests/platforms/android/gradle/test_safe_formal_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest

from briefcase.platforms.android.gradle import safe_formal_name


@pytest.mark.parametrize(
'formal_name, safe_name',
[
('Hello World', 'Hello World'),
# The invalid list is all stripped
('Hello/World/', 'HelloWorld'),
('Hello\\World', 'HelloWorld'),
('Hello:World', 'HelloWorld'),
('Hello<World', 'HelloWorld'),
('Hello>World', 'HelloWorld'),
('Hello "World"', 'Hello World'),
('Hello World?', 'Hello World'),
('Hello|World', 'HelloWorld'),
('Hello World!', 'Hello World'),
# All invalid punctuation is removed
# Valid punctuation is preserved
('Hello! (World?)', 'Hello (World)'),
# Position of punctuation doesn't matter
('Hello! World', 'Hello World'),
('!Hello World', 'Hello World'),
# If removing punctuation leads to double spaces, reduce the double spaces
('Hello | World', 'Hello World'),
('Hello World |', 'Hello World'),
('| Hello World', 'Hello World'),
]
)
def test_safe_formal_name(formal_name, safe_name):
assert safe_formal_name(formal_name) == safe_name

0 comments on commit bc3168c

Please sign in to comment.