Skip to content
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: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
__pycache__/
.DS_Store
dist
docs
whatsapp/
docs-repo/
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ include = ["truffile*", "truffle*"]

[tool.setuptools.package-data]
"*" = ["*.pyi", "py.typed"]
"truffile" = ["assets/*.png"]
Binary file added truffile/assets/Truffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 27 additions & 8 deletions truffile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mimetypes
import os
import re
from importlib import resources as importlib_resources
import select
import signal
import socket
Expand Down Expand Up @@ -78,7 +79,7 @@ class C:
"/exit",
"/quit",
]
SCAFFOLD_ICON_SOURCE_REL = Path("docs") / "Truffle.png"
SCAFFOLD_ICON_RESOURCE_REL = Path("assets") / "Truffle.png"


class Spinner:
Expand Down Expand Up @@ -416,6 +417,25 @@ def _sample_background_py() -> str:
)


def _load_stock_icon_bytes() -> tuple[bytes | None, str]:
try:
resource_file = importlib_resources.files("truffile").joinpath(str(SCAFFOLD_ICON_RESOURCE_REL))
icon_bytes = resource_file.read_bytes()
return icon_bytes, f"truffile/{SCAFFOLD_ICON_RESOURCE_REL.as_posix()}"
except Exception:
pass

local_package_path = Path(__file__).resolve().parent / SCAFFOLD_ICON_RESOURCE_REL
if local_package_path.exists() and local_package_path.is_file():
return local_package_path.read_bytes(), str(local_package_path)

legacy_docs_path = Path(__file__).resolve().parents[1] / "docs" / "Truffle.png"
if legacy_docs_path.exists() and legacy_docs_path.is_file():
return legacy_docs_path.read_bytes(), str(legacy_docs_path)

return None, f"truffile/{SCAFFOLD_ICON_RESOURCE_REL.as_posix()}"


def cmd_create(args) -> int:
app_name = (args.name or "").strip()
if not app_name:
Expand Down Expand Up @@ -451,21 +471,20 @@ def cmd_create(args) -> int:
slug = _safe_app_slug(app_name)
fg_file = f"{slug}_foreground.py"
bg_file = f"{slug}_background.py"
sdk_root = Path(__file__).resolve().parents[1]
stock_icon_path = sdk_root / SCAFFOLD_ICON_SOURCE_REL
if not stock_icon_path.exists() or not stock_icon_path.is_file():
error(f"Stock icon not found: {stock_icon_path}")
stock_icon_bytes, stock_icon_source = _load_stock_icon_bytes()
if stock_icon_bytes is None:
error(f"Stock icon not found: {stock_icon_source}")
return 1
if stock_icon_path.stat().st_size == 0:
error(f"Stock icon is empty: {stock_icon_path}")
if len(stock_icon_bytes) == 0:
error(f"Stock icon is empty: {stock_icon_source}")
return 1

try:
app_dir.mkdir(parents=True, exist_ok=False)
(app_dir / "truffile.yaml").write_text(_sample_truffile_yaml(app_name, slug), encoding="utf-8")
(app_dir / fg_file).write_text(_sample_foreground_py(), encoding="utf-8")
(app_dir / bg_file).write_text(_sample_background_py(), encoding="utf-8")
(app_dir / "icon.png").write_bytes(stock_icon_path.read_bytes())
(app_dir / "icon.png").write_bytes(stock_icon_bytes)
except Exception as exc:
error(f"Failed to scaffold app: {exc}")
return 1
Expand Down
Loading