| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import argparse |
| 4 |
|
| 5 |
from briefcase.channels import get_publication_channels |
| 6 |
from briefcase.channels.base import BasePublicationChannel |
| 7 |
from briefcase.config import AppConfig |
| 8 |
from briefcase.exceptions import BriefcaseCommandError |
| 9 |
|
| 10 |
from .base import BaseCommand, full_options |
| 11 |
|
| 12 |
|
| 13 |
class PublishCommand(BaseCommand): |
| 14 |
command = "publish" |
| 15 |
description = "Publish an app to a distribution channel." |
| 16 |
|
| 17 |
@property |
| 18 |
def packaging_formats(self): |
| 19 |
return [self.output_format] |
| 20 |
|
| 21 |
@property |
| 22 |
def default_packaging_format(self): |
| 23 |
return self.output_format |
| 24 |
|
| 25 |
def _get_channels(self) -> dict[str, type[BasePublicationChannel]]: |
| 26 |
"""Discover available publication channels for this platform/format.""" |
| 27 |
return get_publication_channels(self.platform, self.output_format) |
| 28 |
|
| 29 |
def add_options(self, parser: argparse.ArgumentParser) -> None: |
| 30 |
channels = self._get_channels() |
| 31 |
channel_names = sorted(channels.keys()) |
| 32 |
|
| 33 |
parser.add_argument( |
| 34 |
"-a", |
| 35 |
"--app", |
| 36 |
dest="app_name", |
| 37 |
help="Name of the app to publish (if multiple apps exist in the project)", |
| 38 |
default=argparse.SUPPRESS, |
| 39 |
) |
| 40 |
|
| 41 |
parser.add_argument( |
| 42 |
"-u", |
| 43 |
"--update", |
| 44 |
action="store_true", |
| 45 |
help="Update the app before publishing", |
| 46 |
) |
| 47 |
parser.add_argument( |
| 48 |
"-p", |
| 49 |
"--packaging-format", |
| 50 |
dest="packaging_format", |
| 51 |
help="Packaging format to publish", |
| 52 |
default=self.default_packaging_format, |
| 53 |
choices=self.packaging_formats, |
| 54 |
) |
| 55 |
parser.add_argument( |
| 56 |
"-c", |
| 57 |
"--channel", |
| 58 |
choices=channel_names or None, |
| 59 |
default=None, |
| 60 |
help="The channel to publish to", |
| 61 |
) |
| 62 |
|
| 63 |
def _publish_app( |
| 64 |
self, |
| 65 |
app: AppConfig, |
| 66 |
update: bool, |
| 67 |
packaging_format: str, |
| 68 |
channel: BasePublicationChannel, |
| 69 |
**options, |
| 70 |
) -> dict | None: |
| 71 |
"""Internal method to publish a single app. |
| 72 |
|
| 73 |
:param app: The application to publish |
| 74 |
:param update: Should the application be updated (and rebuilt) first? |
| 75 |
:param packaging_format: The format of the packaging artefact to create. |
| 76 |
:param channel: The resolved BasePublicationChannel instance |
| 77 |
""" |
| 78 |
state = None |
| 79 |
|
| 80 |
# Annotate the packaging format onto the app |
| 81 |
app.packaging_format = packaging_format |
| 82 |
|
| 83 |
if update or not self.distribution_path(app).exists(): |
| 84 |
state = self.package_command( |
| 85 |
app, |
| 86 |
update=update, |
| 87 |
packaging_format=packaging_format, |
| 88 |
**options, |
| 89 |
) |
| 90 |
|
| 91 |
self.verify_app(app) |
| 92 |
|
| 93 |
state = channel.publish_app(app, command=self, **full_options(state, options)) |
| 94 |
|
| 95 |
return state |
| 96 |
|
| 97 |
def __call__( |
| 98 |
self, |
| 99 |
app: AppConfig | None = None, |
| 100 |
app_name: str | None = None, |
| 101 |
update: bool | None = False, |
| 102 |
packaging_format: str | None = None, |
| 103 |
channel: str | None = None, |
| 104 |
**options, |
| 105 |
) -> dict | None: |
| 106 |
apps_to_publish = self.resolve_apps(app=app, app_name=app_name) |
| 107 |
|
| 108 |
# Verify that at least one publication channel is available. |
| 109 |
channels = self._get_channels() |
| 110 |
if not channels: |
| 111 |
raise BriefcaseCommandError( |
| 112 |
f"No publication channels are available for " |
| 113 |
f"{self.platform} {self.output_format}.\n\n" |
| 114 |
f"Install a publication channel plugin and try again." |
| 115 |
) |
| 116 |
|
| 117 |
if channel is None: |
| 118 |
if len(channels) == 1: |
| 119 |
resolved_channel = next(iter(channels.values()))() |
| 120 |
else: |
| 121 |
raise BriefcaseCommandError( |
| 122 |
f"Multiple publication channels are available for " |
| 123 |
f"{self.platform} {self.output_format}: " |
| 124 |
f"{', '.join(sorted(channels))}.\n\n" |
| 125 |
f"Specify a channel with --channel." |
| 126 |
) |
| 127 |
else: |
| 128 |
resolved_channel = channels[channel]() |
| 129 |
|
| 130 |
# Confirm host compatibility, that all required tools are available, |
| 131 |
# and that all app configurations are finalized. |
| 132 |
self.finalize(apps=apps_to_publish.values()) |
| 133 |
|
| 134 |
# Publish all apps to the selected channel. |
| 135 |
state = None |
| 136 |
for _, app_obj in sorted(apps_to_publish.items()): |
| 137 |
state = self._publish_app( |
| 138 |
app_obj, |
| 139 |
update=update, |
| 140 |
packaging_format=packaging_format or self.default_packaging_format, |
| 141 |
channel=resolved_channel, |
| 142 |
**full_options(state, options), |
| 143 |
) |
| 144 |
|
| 145 |
return state |