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

fix: icon type auto-generation and normalization #696

Merged
merged 11 commits into from
Sep 6, 2024
Merged
3 changes: 2 additions & 1 deletion plugins/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Available IconTypes can be generated automatically using icon TypeScript definit
Writes to `icon_types.py`.

```shell
npm install
cd plugins/ui
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved
python make_docs.py
python make_icon_types.py
```
66 changes: 29 additions & 37 deletions plugins/ui/make_icon_types.py
100644 → 100755
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,80 +1,72 @@
import re
from typing import Dict, Any


def camel_to_snake(name: str) -> str:
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()


def update_dict(
dictionary: Dict[str, str], key: str, value: str, overwrite_condition: bool
) -> None:
if key in dictionary:
if overwrite_condition:
dictionary[key] = value
else:
dictionary[key] = value
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved


relative_path = "./src/js/node_modules/@deephaven/icons/dist/index.d.ts"
icon_pattern = r"^export const (\w+): IconDefinition;$"

icons = {}
snakeCase = {}
noPrefix = {}
snakeCaseNoPrefix = {}

with open(relative_path, "r") as file:
lines = file.readlines()
for line in lines:
if "IconDefinition" in line:
for line in file:
match = re.match(icon_pattern, line)
if match:
icon = line.split(" ")[2].strip()[:-1]
if icon != "IconDefinition":
icons[icon] = icon
snakeCase[camel_to_snake(icon)] = icon

isVsIcon = icon.startswith("vs")
noPrefixIcon = icon[2:]
if noPrefixIcon in noPrefix:
if noPrefixIcon.startswith("vs"):
noPrefix[noPrefixIcon] = icon
else:
noPrefix[noPrefixIcon] = icon
update_dict(noPrefix, noPrefixIcon, icon, isVsIcon)

snakeCaseNoPrefixIcon = camel_to_snake(noPrefixIcon)
if snakeCaseNoPrefixIcon in snakeCaseNoPrefix:
if icon.startswith("vs"):
snakeCaseNoPrefix[snakeCaseNoPrefixIcon] = icon
else:
snakeCaseNoPrefix[snakeCaseNoPrefixIcon] = icon
update_dict(snakeCaseNoPrefix, snakeCaseNoPrefixIcon, icon, isVsIcon)

output_file_path = "./src/deephaven/ui/components/types/icon_types.py"

with open(output_file_path, "w") as output_file:
output_file.truncate(0)

output_file.write(
"from __future__ import annotations"
+ "\n"
+ "from typing import Literal"
+ "\n\n"
"from __future__ import annotations\n" + "from typing import Literal\n\n"
)

## IconTypes
output_file.write("IconTypes = Literal[" + "\n")
for key, value in icons.items():
output_file.write(' "' + key + '",' + "\n")

for key, value in noPrefix.items():
output_file.write(' "' + key + '",' + "\n")

for key, value in snakeCase.items():
output_file.write(' "' + key + '",' + "\n")

output_file.write("IconTypes = Literal[\n")
for key, value in snakeCaseNoPrefix.items():
output_file.write(' "' + key + '",' + "\n")
output_file.write("]" + "\n")

## IconMapping
output_file.write("\n")
output_file.write("IconMapping = {" + "\n")
for key, value in icons.items():
output_file.write(' "' + key + '": "' + value + '",' + "\n")

for key, value in noPrefix.items():
output_file.write(' "' + key + '": "' + value + '",' + "\n")
for dict in [icons, noPrefix, snakeCase, snakeCaseNoPrefix]:
for key, value in dict.items():
output_file.write(' "' + key + '": "' + value + '",' + "\n")
output_file.write("}" + "\n")

for key, value in snakeCase.items():
output_file.write(' "' + key + '": "' + value + '",' + "\n")

for key, value in snakeCaseNoPrefix.items():
output_file.write(' "' + key + '": "' + value + '",' + "\n")
output_file.write("}" + "\n")
print(f"Generated file: {output_file_path}")
print(f"Total number of icon types: {len(snakeCaseNoPrefix)}")
print(
f"Total number of icon mappings: {len(icons) + len(noPrefix) + len(snakeCase) + len(snakeCaseNoPrefix)}"
)
Loading
Loading