| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import argparse |
| 4 |
from abc import abstractmethod |
| 5 |
|
| 6 |
from briefcase.config import AppConfig |
| 7 |
|
| 8 |
from .base import BaseCommand, full_options |
| 9 |
|
| 10 |
|
| 11 |
class OpenCommand(BaseCommand): |
| 12 |
command = "open" |
| 13 |
description = "Open an app in the build tool for the target platform." |
| 14 |
|
| 15 |
def add_options(self, parser): |
| 16 |
super().add_options(parser) |
| 17 |
parser.add_argument( |
| 18 |
"-a", |
| 19 |
"--app", |
| 20 |
dest="app_name", |
| 21 |
help="Name of the app to open (if multiple apps exist in the project)", |
| 22 |
default=argparse.SUPPRESS, |
| 23 |
) |
| 24 |
|
| 25 |
@abstractmethod |
| 26 |
def project_path(self, app: AppConfig): |
| 27 |
"""The path in to the project to pass to the shell to open the project.""" |
| 28 |
|
| 29 |
def _open_app(self, app: AppConfig): |
| 30 |
if self.tools.host_os == "Windows": # pragma: no-cover-if-not-windows |
| 31 |
self.tools.os.startfile(self.project_path(app)) |
| 32 |
elif self.tools.host_os == "Darwin": # pragma: no-cover-if-not-macos |
| 33 |
self.tools.subprocess.Popen(["open", self.project_path(app)]) |
| 34 |
else: # pragma: no-cover-if-not-linux |
| 35 |
self.tools.subprocess.Popen(["xdg-open", self.project_path(app)]) |
| 36 |
|
| 37 |
def open_app(self, app: AppConfig, **options): |
| 38 |
"""Open the project for an app. |
| 39 |
|
| 40 |
:param app: The application to open |
| 41 |
""" |
| 42 |
project_path = self.project_path(app) |
| 43 |
if not project_path.exists(): |
| 44 |
state = self.create_command(app, **options) |
| 45 |
else: |
| 46 |
state = None |
| 47 |
|
| 48 |
self.verify_app(app) |
| 49 |
|
| 50 |
self.console.info( |
| 51 |
f"Opening {self.project_path(app).relative_to(self.base_path)}...", |
| 52 |
prefix=app.app_name, |
| 53 |
) |
| 54 |
self._open_app(app) |
| 55 |
|
| 56 |
return state |
| 57 |
|
| 58 |
def __call__( |
| 59 |
self, |
| 60 |
app: AppConfig | None = None, |
| 61 |
app_name: str | None = None, |
| 62 |
**options, |
| 63 |
): |
| 64 |
apps_to_open = self.resolve_apps(app=app, app_name=app_name) |
| 65 |
|
| 66 |
# Confirm host compatibility, that all required tools are available, |
| 67 |
# and that the app configuration is finalized. |
| 68 |
self.finalize(apps=apps_to_open.values()) |
| 69 |
|
| 70 |
state = None |
| 71 |
for _, app_obj in sorted(apps_to_open.items()): |
| 72 |
state = self.open_app(app_obj, **full_options(state, options)) |
| 73 |
|
| 74 |
return state |