77# just so we can get the root path of our library
88import suap
99
10+ from ...mime_type import MimeType
1011from ...projects import ProjectData
1112from ...formats import make_nsis_installer
1213
@@ -24,6 +25,7 @@ def format_config_and_make_nsis_installer(
2425 temp_folder_path : Path ,
2526 display_name : Optional [str ],
2627 icon_path : Path ,
28+ mime_types : list [MimeType ],
2729 project_data : ProjectData ,
2830):
2931 logger .debug (
@@ -36,9 +38,8 @@ def format_config_and_make_nsis_installer(
3638 temp_folder_path .mkdir (exist_ok = True )
3739 nsis_installer_script_path = temp_folder_path .joinpath ("nsis_installer.nsi" )
3840
39- installer_script_template_path = Path (suap .__file__ ).parent .joinpath (
40- "templates" , "nsis_installer_script.nsi"
41- )
41+ templates_path = Path (suap .__file__ ).parent .joinpath ("templates" )
42+ installer_script_template_path = templates_path .joinpath ("nsis_installer_script.nsi" )
4243
4344 logger .debug (
4445 f"Opening and reading NSIS template installer script at '{ installer_script_template_path } '..."
@@ -50,19 +51,39 @@ def format_config_and_make_nsis_installer(
5051 logger .debug ("Formatting template and creating custom install script..." )
5152
5253 semver = project_data .version
54+ display_name = display_name if display_name is not None else project_data .name
5355
5456 replace_map : dict [str , str ] = {
5557 "suap-binary-name" : binary_name ,
5658 "suap-binary-path" : str (binary_path .absolute ()),
5759 "suap-binary-dist-path" : str (binary_dist_path .absolute ()),
58- "suap-display-name" : display_name if display_name is not None else project_data . name ,
60+ "suap-display-name" : display_name ,
5961 "suap-icon-path" : str (icon_path .absolute ()),
6062 "suap-icon-file-name" : icon_path .name ,
6163
6264 "suap-project-name" : project_data .name ,
6365 "suap-project-version" : f"{ semver .major } .{ semver .minor } .{ semver .patch } " \
6466 f".{ semver .prerelease .split ('.' )[- 1 ] if semver .prerelease is not None else 0 } " ,
6567 "suap-project-description" : project_data .description ,
68+
69+ "suap-app-capabilities-macro" : generate_app_capabilities_macro (
70+ mime_types ,
71+ templates_path ,
72+ binary_name ,
73+ display_name ,
74+ icon_path ,
75+ project_data ,
76+ uninstall = False ,
77+ ),
78+ "suap-app-capabilities-uni-macro" : generate_app_capabilities_macro (
79+ mime_types ,
80+ templates_path ,
81+ binary_name ,
82+ display_name ,
83+ icon_path ,
84+ project_data ,
85+ uninstall = True ,
86+ ),
6687 }
6788
6889 for (suap_key , value ) in replace_map .items ():
@@ -78,4 +99,82 @@ def format_config_and_make_nsis_installer(
7899 if not make_nsis_installer (nsis_installer_script_path ):
79100 raise Exit (1 )
80101
81- logger .info ("Done making NSIS installer, installer executable should be in dist." )
102+ logger .info ("Done making NSIS installer, installer executable should be in dist." )
103+
104+ def generate_app_capabilities_macro (
105+ mime_types : list [MimeType ],
106+ templates_path : Path ,
107+ binary_name : str ,
108+ display_name : str ,
109+ icon_path : Path ,
110+ project_data : ProjectData ,
111+ uninstall : bool ,
112+ ) -> str :
113+ if len (mime_types ) == 0 :
114+ return ""
115+
116+ app_capabilities_template_path = templates_path .joinpath (
117+ "nsis_app_capabilities_uni_macro.nsi" if uninstall else "nsis_app_capabilities_macro.nsi"
118+ )
119+
120+ with open (app_capabilities_template_path , mode = "r" ) as file :
121+ app_capabilities_macro_string = file .read ()
122+
123+ file_associations_and_types_lines = []
124+
125+ for mime_type in mime_types :
126+ file_extension = mime_type .get_file_extension ()
127+
128+ project_name = project_data .name
129+
130+ if file_extension is None :
131+ logger .warning (f"Mime type '{ mime_type .mime_type_string } ' not found, skipping..." )
132+ continue
133+
134+ if not uninstall :
135+ file_associations_and_types_lines .append (
136+ f'WriteRegStr HKCR "Applications\\ { binary_name } .exe\\ SupportedTypes" "{ file_extension } " ""'
137+ )
138+
139+ file_associations_and_types_lines .append (
140+ f'WriteRegStr HKCR "{ file_extension } \\ OpenWithProgIds" "Cloudy.{ project_name } .1" ""'
141+ )
142+
143+ file_associations_and_types_lines .append (
144+ f'WriteRegStr HKLM "SOFTWARE\\ Cloudy\\ { project_name } \\ Capabilities\\ FileAssociations" "{ file_extension } " "Cloudy.{ project_name } .1"'
145+ )
146+
147+ else :
148+ file_associations_and_types_lines .append (
149+ f'DeleteRegValue HKCR "Applications\\ { binary_name } .exe\\ SupportedTypes" "{ file_extension } "'
150+ )
151+
152+ file_associations_and_types_lines .append (
153+ f'DeleteRegValue HKCR "{ file_extension } \\ OpenWithProgIds" "Cloudy.{ project_name } .1"'
154+ )
155+
156+ replace_map : dict [str , str ] = {
157+ "suap-binary-name" : binary_name ,
158+ "suap-display-name" : display_name ,
159+ "suap-icon-file-name" : icon_path .name ,
160+
161+ "suap-project-name" : project_data .name ,
162+ "suap-project-description" : project_data .description ,
163+
164+ "suap-file-associations-and-types-macro" : "\n " .join (file_associations_and_types_lines ),
165+ }
166+
167+ for (suap_key , value ) in replace_map .items ():
168+ app_capabilities_macro_string = app_capabilities_macro_string .replace (
169+ f"{{{ suap_key } }}" , value
170+ )
171+
172+ formatted_macro_string = "" .join ([
173+ f" { line } " for line in app_capabilities_macro_string .splitlines (keepends = True )
174+ ])
175+
176+ logger .debug (
177+ f"Generated and formatted app capabilities macro: \n { formatted_macro_string } "
178+ )
179+
180+ return formatted_macro_string
0 commit comments