Skip to content

Commit

Permalink
Cookiecutter replaces Copier (#2045)
Browse files Browse the repository at this point in the history
* Fix slugify() function

* Use cookiecutter for `flet create` command

* Use `rich` library
  • Loading branch information
FeodorFitsner authored Nov 8, 2023
1 parent b49fe89 commit 6d0fc80
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 320 deletions.
27 changes: 8 additions & 19 deletions sdk/python/packages/flet-core/src/flet_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import math
import random
import re
import string
import sys
import unicodedata
Expand All @@ -22,28 +23,16 @@ def is_coroutine(method):
return inspect.iscoroutinefunction(method)


def slugify(original: str) -> str:
def slugify(value: str) -> str:
"""
Make a string url friendly. Useful for creating routes for navigation.
>>> slugify("What's up?")
'whats-up'
>>> slugify(" Mitä kuuluu? ")
'mitä-kuuluu'
Converts to lowercase, removes non-word characters (alphanumerics and underscores)
and converts spaces to hyphens. Also strips leading and trailing whitespace.
"""
slugified = original.strip()
slugified = " ".join(slugified.split()) # Remove extra spaces between words
slugified = slugified.lower()
# Remove unicode punctuation
slugified = "".join(
character
for character in slugified
if not unicodedata.category(character).startswith("P")
value = (
unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
)
slugified = slugified.replace(" ", "-")

return slugified
value = re.sub(r"[^\w\s-]", "", value).strip().lower()
return re.sub(r"[-\s]+", "-", value).strip("-")


class Vector(complex):
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/packages/flet/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ websocket-client = "^1.5.2"
watchdog = "^3.0.0"
websockets = "^11.0.3"
packaging = "^23.1"
copier = "^8.2.0"
qrcode = "^7.4.2"
cookiecutter = "^2.4.0"

[tool.poetry.group.dev.dependencies]
pre-commit = "^2.6"
Expand Down
47 changes: 22 additions & 25 deletions sdk/python/packages/flet/src/flet/cli/commands/create.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import argparse
from pathlib import Path

from colorama import Fore, Style
from flet.cli.commands.base import BaseCommand
from flet_core.utils import slugify
from rich import print


class Command(BaseCommand):
Expand Down Expand Up @@ -39,36 +39,33 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
)

def handle(self, options: argparse.Namespace) -> None:
from copier.main import Worker
from cookiecutter.main import cookiecutter

template_data = {"template_name": options.template}

out_dir = Path(options.output_directory)
project_name = options.project_name
if project_name is None:
project_slug = slugify(out_dir.name)
if project_slug is not None and project_slug != "":
project_name = project_slug
out_dir = Path(options.output_directory).resolve()
template_data["out_dir"] = out_dir.name

if project_name is not None:
template_data["project_name"] = project_name
project_name = slugify(
options.project_name if options.project_name else out_dir.name
)
template_data["project_name"] = project_name

if options.description is not None:
template_data["description"] = options.description

# print("Template data:", template_data)
with Worker(
src_path="https://github.com/flet-dev/templates.git",
dst_path=out_dir,
vcs_ref="HEAD",
data=template_data,
defaults=True,
) as worker:
worker.run_copy()
print(Fore.LIGHTGREEN_EX + "Done. Now run:")
print(Style.RESET_ALL)
if options.output_directory != ".":
print(Fore.CYAN + "cd", end=" ")
print(Fore.WHITE + project_name, end="\n")
print(Fore.CYAN + "flet run")
print(Style.RESET_ALL)
cookiecutter(
f"gh:flet-dev/flet-app-templates",
directory=options.template,
output_dir=str(out_dir.parent),
no_input=True,
overwrite_if_exists=True,
extra_context=template_data,
)

# print next steps
print("[spring_green3]Done.[/spring_green3] Now run:\n")
if options.output_directory != ".":
print(f"[cyan]cd[/cyan] [white]{out_dir.name}[/white]")
print("[cyan]flet run[/cyan]\n")
Loading

0 comments on commit 6d0fc80

Please sign in to comment.