Debian Codegraph @

src/briefcase/platforms/macOS/app.py

raw
1 from __future__ import annotations
2
3 from pathlib import Path
4
5 from briefcase.commands import (
6 BuildCommand,
7 CreateCommand,
8 DevCommand,
9 OpenCommand,
10 PackageCommand,
11 PublishCommand,
12 RunCommand,
13 UpdateCommand,
14 )
15 from briefcase.config import AppConfig
16 from briefcase.platforms.macOS import (
17 SigningIdentity,
18 macOSCreateMixin,
19 macOSMixin,
20 macOSPackageMixin,
21 macOSRunMixin,
22 macOSSigningMixin,
23 )
24 from briefcase.platforms.macOS.utils import AppPackagesMergeMixin
25
26
27 class macOSAppMixin(macOSMixin):
28 output_format = "app"
29 supports_external_packaging = True
30
31 def project_path(self, app):
32 return self.binary_path(app) / "Contents"
33
34 def binary_path(self, app):
35 return self.bundle_path(app) / f"{app.formal_name}.app"
36
37 def binary_executable_path(self, app) -> Path:
38 # The actual binary in a macOS app is a known path
39 # inside the "binary" app bundle that is executed.
40 return self.binary_path(app) / "Contents/MacOS" / app.formal_name
41
42
43 class macOSAppCreateCommand(macOSAppMixin, macOSCreateMixin, CreateCommand):
44 description = "Create and populate a macOS app."
45
46 def support_path(self, app: AppConfig, runtime=False) -> Path:
47 if runtime:
48 return super().support_path(app)
49 else:
50 return self.bundle_path(app) / "support"
51
52 def runtime_path(self, app: AppConfig) -> Path:
53 try:
54 return self.path_index(app, "runtime_path")
55 except KeyError:
56 return "python-stdlib"
57
58 def install_app_support_package(self, app: AppConfig):
59 """Install the application support package.
60
61 :param app: The config object for the app
62 """
63 super().install_app_support_package(app)
64
65 # Copy the stdlib into its final location
66 with self.console.wait_bar("Copying standard library into app bundle..."):
67 runtime_support_path = self.support_path(app, runtime=True)
68 if runtime_support_path.is_dir():
69 self.tools.shutil.rmtree(runtime_support_path)
70 runtime_support_path.mkdir()
71
72 # The support package will contain a lot more content than is
73 # actually needed at runtime. Only copy the runtime_path into
74 # the support location nominated by the template. Also ensure
75 # that symlinks are preserved.
76 runtime_path = self.runtime_path(app)
77 self.tools.shutil.copytree(
78 self.support_path(app) / runtime_path,
79 runtime_support_path / Path(runtime_path).name,
80 symlinks=True,
81 )
82
83 def install_app_resources(self, app: AppConfig):
84 super().install_app_resources(app)
85
86 # macOS will cache application icons. Touching the .app folder flushes the icon
87 # cache for the app, ensuring the current icon is loaded.
88 self.binary_path(app).touch(exist_ok=True)
89
90
91 class macOSAppUpdateCommand(macOSAppCreateCommand, UpdateCommand):
92 description = "Update an existing macOS app."
93 supports_debugger = True
94
95
96 class macOSAppOpenCommand(macOSAppMixin, OpenCommand):
97 description = "Open the app bundle folder for an existing macOS app."
98
99
100 class macOSAppBuildCommand(
101 macOSAppMixin,
102 macOSSigningMixin,
103 AppPackagesMergeMixin,
104 BuildCommand,
105 ):
106 description = "Build a macOS app."
107 supports_debugger = True
108
109 def build_app(self, app: AppConfig, **kwargs):
110 """Build the macOS app.
111
112 :param app: The application to build
113 """
114 # Confirm the project isn't currently on an iCloud synced drive.
115 self.verify_not_on_icloud(app)
116
117 self.console.info("Building App...", prefix=app.app_name)
118
119 # Move the unbuilt binary in to the final executable location
120 unbuilt_path = self.unbuilt_executable_path(app)
121 if unbuilt_path.exists():
122 with self.console.wait_bar("Renaming stub binary..."):
123 unbuilt_path.rename(self.binary_executable_path(app))
124
125 if not getattr(app, "universal_build", True):
126 with self.console.wait_bar("Ensuring stub binary is thin..."):
127 # The stub binary is universal by default. If we're building a
128 # non-universal app, we can strip the binary to remove the unused slice.
129 self.ensure_thin_binary(
130 self.binary_executable_path(app),
131 arch=self.tools.host_arch,
132 )
133
134 # macOS apps don't have anything to compile, but they do need to be
135 # signed to be able to execute on Apple Silicon hardware - even if it's only an
136 # ad-hoc signing identity. Apply an ad-hoc signing identity to the
137 # app bundle. However, the app only needs to be signed once. If the build is
138 # being done as part of a package command, the signing will happen during
139 # packaging. The two case can be distinguished because "adhoc_sign" will
140 # be absent from kwargs if only doing a build command, but will be present if
141 # the build is being done as part of a package command.
142 if "adhoc_sign" not in kwargs:
143 self.console.info("Ad-hoc signing app...", prefix=app.app_name)
144 self.sign_app(app=app, identity=SigningIdentity())
145
146
147 class macOSAppRunCommand(macOSRunMixin, macOSAppMixin, RunCommand):
148 description = "Run a macOS app."
149 supports_debugger = True
150
151
152 class macOSAppDevCommand(macOSAppMixin, DevCommand):
153 description = "Run a macOS app in development mode."
154
155
156 class macOSAppPackageCommand(macOSPackageMixin, macOSAppMixin, PackageCommand):
157 description = "Package a macOS app for distribution."
158
159
160 class macOSAppPublishCommand(macOSAppMixin, PublishCommand):
161 description = "Publish a macOS app."
162
163
164 # Declare the briefcase command bindings
165 create = macOSAppCreateCommand
166 update = macOSAppUpdateCommand
167 open = macOSAppOpenCommand
168 build = macOSAppBuildCommand
169 run = macOSAppRunCommand
170 package = macOSAppPackageCommand
171 publish = macOSAppPublishCommand
172 dev = macOSAppDevCommand