33// SPDX-License-Identifier: Apache-2.0
44// SPDX-License-Identifier: MIT
55
6- use crate :: bundle:: common:: CommandExt ;
6+ use crate :: { bundle:: common:: CommandExt , Settings } ;
77use bitness:: { self , Bitness } ;
88use log:: { debug, info} ;
99use std:: {
@@ -90,18 +90,11 @@ fn locate_signtool() -> crate::Result<PathBuf> {
9090 Err ( crate :: Error :: SignToolNotFound )
9191}
9292
93- pub fn sign < P : AsRef < Path > > ( path : P , params : & SignParams ) -> crate :: Result < ( ) > {
94- // Convert path to string reference, as we need to pass it as a command-line parameter to signtool
95- let path_str = path. as_ref ( ) . to_str ( ) . unwrap ( ) ;
96-
97- info ! ( action = "Signing" ; "{} with identity \" {}\" " , path_str, params. certificate_thumbprint) ;
98-
93+ pub fn sign_command ( path : & str , params : & SignParams ) -> crate :: Result < ( Command , PathBuf ) > {
9994 // Construct SignTool command
10095 let signtool = locate_signtool ( ) ?;
10196
102- debug ! ( "Running signtool {:?}" , signtool) ;
103-
104- let mut cmd = Command :: new ( signtool) ;
97+ let mut cmd = Command :: new ( & signtool) ;
10598 cmd. arg ( "sign" ) ;
10699 cmd. args ( [ "/fd" , & params. digest_algorithm ] ) ;
107100 cmd. args ( [ "/sha1" , & params. certificate_thumbprint ] ) ;
@@ -116,7 +109,18 @@ pub fn sign<P: AsRef<Path>>(path: P, params: &SignParams) -> crate::Result<()> {
116109 }
117110 }
118111
119- cmd. arg ( path_str) ;
112+ cmd. arg ( path) ;
113+
114+ Ok ( ( cmd, signtool) )
115+ }
116+
117+ pub fn sign < P : AsRef < Path > > ( path : P , params : & SignParams ) -> crate :: Result < ( ) > {
118+ let path_str = path. as_ref ( ) . to_str ( ) . unwrap ( ) ;
119+
120+ info ! ( action = "Signing" ; "{} with identity \" {}\" " , path_str, params. certificate_thumbprint) ;
121+
122+ let ( mut cmd, signtool) = sign_command ( path_str, params) ?;
123+ debug ! ( "Running signtool {:?}" , signtool) ;
120124
121125 // Execute SignTool command
122126 let output = cmd. output_ok ( ) ?;
@@ -126,3 +130,39 @@ pub fn sign<P: AsRef<Path>>(path: P, params: &SignParams) -> crate::Result<()> {
126130
127131 Ok ( ( ) )
128132}
133+
134+ impl Settings {
135+ pub ( crate ) fn can_sign ( & self ) -> bool {
136+ self . windows ( ) . certificate_thumbprint . is_some ( )
137+ }
138+ pub ( crate ) fn sign_params ( & self ) -> SignParams {
139+ SignParams {
140+ product_name : self . product_name ( ) . into ( ) ,
141+ digest_algorithm : self
142+ . windows ( )
143+ . digest_algorithm
144+ . as_ref ( )
145+ . map ( |algorithm| algorithm. to_string ( ) )
146+ . unwrap_or_else ( || "sha256" . to_string ( ) ) ,
147+ certificate_thumbprint : self
148+ . windows ( )
149+ . certificate_thumbprint
150+ . clone ( )
151+ . unwrap_or_default ( ) ,
152+ timestamp_url : self
153+ . windows ( )
154+ . timestamp_url
155+ . as_ref ( )
156+ . map ( |url| url. to_string ( ) ) ,
157+ tsp : self . windows ( ) . tsp ,
158+ }
159+ }
160+ }
161+
162+ pub fn try_sign ( file_path : & std:: path:: PathBuf , settings : & Settings ) -> crate :: Result < ( ) > {
163+ if settings. can_sign ( ) {
164+ info ! ( action = "Signing" ; "{}" , tauri_utils:: display_path( file_path) ) ;
165+ sign ( file_path, & settings. sign_params ( ) ) ?;
166+ }
167+ Ok ( ( ) )
168+ }
0 commit comments