""" 고장코드 검색기 앱을 PWA(Progressive Web App)로 변환하여 안드로이드에서 설치 가능한 앱을 만드는 스크립트 """ import os import subprocess import shutil from pathlib import Path import json import time def build_pwa_app(): """ Flet 앱을 웹으로 게시하고 PWA 설정을 추가합니다. """ print("고장코드 검색기 PWA 빌드를 시작합니다...") # 1. 게시 경로 정리 dist_dir = Path("dist") if dist_dir.exists(): try: print(f"dist 디렉토리 정리 중...") # 파일 삭제 시도 for item in dist_dir.glob('*'): if item.is_file(): try: item.unlink() print(f" 삭제됨: {item}") except Exception as e: print(f" 삭제 실패 (무시됨): {item} - {e}") elif item.is_dir(): try: shutil.rmtree(item) print(f" 삭제됨: {item}") except Exception as e: print(f" 삭제 실패 (무시됨): {item} - {e}") # 디렉토리 자체도 삭제 시도 try: if list(dist_dir.glob('*')): print(f"일부 파일이 삭제되지 않아 dist 디렉토리 유지") else: dist_dir.rmdir() print(f"dist 디렉토리를 삭제했습니다.") except Exception as e: print(f"dist 디렉토리 삭제 실패 (무시됨): {e}") except Exception as e: print(f"dist 정리 중 예외 발생 (무시됨): {e}") # 필요한 경우 dist 디렉토리 생성 if not dist_dir.exists(): dist_dir.mkdir() print("dist 디렉토리를 생성했습니다.") # 2. 필요한 파일 준비 icon_path = "assets/app_icon.png" if not os.path.exists(icon_path): print(f"경고: 아이콘 파일이 없습니다. 기본 아이콘이 필요합니다.") # assets 폴더 생성 assets_dir = Path("assets") if not assets_dir.exists(): assets_dir.mkdir() print("assets 디렉토리를 생성했습니다.") # 기본 아이콘 생성 (텍스트 파일로 대체) with open(icon_path, "w") as f: f.write("This is a placeholder for the app icon.") print(f"임시 아이콘 파일을 생성했습니다.") # 3. Flet 앱을 웹으로 게시 app_name = "고장코드검색기" app_desc = "공기압 차량 고장코드 검색 애플리케이션" cmd = [ "flet", "publish", "main.py", "--app-name", app_name, "--app-short-name", app_name, "--app-description", app_desc, "--assets", "assets" ] print("명령어 실행:", " ".join(cmd)) try: subprocess.run(cmd, check=True) print("웹 앱 게시 완료!") # 4. manifest.json 파일 수정하여 PWA 기능 강화 manifest_path = Path("dist/manifest.json") if manifest_path.exists(): try: with open(manifest_path, "r", encoding="utf-8") as f: manifest = json.load(f) # PWA 설정 추가 manifest.update({ "background_color": "#ffffff", "theme_color": "#2196f3", "display": "standalone", "orientation": "any", "prefer_related_applications": False, "icons": [ { "src": "assets/app_icon.png", "sizes": "512x512", "type": "image/png", "purpose": "any maskable" } ], "shortcuts": [ { "name": "고장코드 검색", "url": "/", "description": "고장코드 검색하기" } ], "screenshots": [ { "src": "assets/app_icon.png", "sizes": "512x512", "type": "image/png" } ] }) with open(manifest_path, "w", encoding="utf-8") as f: json.dump(manifest, f, ensure_ascii=False, indent=2) print("PWA 설정이 업데이트되었습니다.") except Exception as e: print(f"manifest.json 파일 수정 중 오류 발생: {e}") else: print("manifest.json 파일을 찾을 수 없습니다.") print("\n안드로이드에서 설치 가능한 PWA가 생성되었습니다!") print("-----------------------------------------------------") print("안드로이드 설치 방법:") print("1. 웹 서버에 dist 폴더 내용을 업로드합니다.") print("2. 안드로이드 크롬에서 웹사이트에 접속합니다.") print("3. 메뉴에서 '홈 화면에 추가' 옵션을 선택합니다.") print("4. 앱이 홈 화면에 설치됩니다.") print("-----------------------------------------------------") # 로컬 테스트를 위한 명령어 표시 print("\n로컬에서 테스트하려면:") print("python -m http.server 8000 --directory dist") print("브라우저에서 http://localhost:8000 접속") except subprocess.CalledProcessError as e: print(f"빌드 중 오류 발생: {e}") except Exception as e: print(f"예상치 못한 오류 발생: {e}") if __name__ == "__main__": build_pwa_app()