11using System ;
2+ using System . IO ;
3+ using AppCore . SigningTool . Exceptions ;
24using AppCore . SigningTool . StrongName ;
35using dnlib . DotNet ;
46using McMaster . Extensions . CommandLineUtils ;
@@ -9,9 +11,11 @@ public class SnSignCommand : ICommand<SnCommand>
911 {
1012 private readonly IStrongNameKeyLoader _keyLoader ;
1113 private readonly IStrongNameSigner _signer ;
14+ private CommandOption < bool > _delaySign ;
15+ private CommandOption < bool > _force ;
1216 private CommandArgument _keyFile ;
1317 private CommandArgument _assemblyFile ;
14- private CommandOption < bool > _delaySign ;
18+ private CommandArgument _outAssemblyFile ;
1519
1620 public string Name => "sign" ;
1721
@@ -28,30 +32,44 @@ public void Configure(CommandLineApplication cmd)
2832 _delaySign = cmd . Option < bool > ( "-d|--delay-sign" , "Delay signs the assembly." ,
2933 CommandOptionType . NoValue ) ;
3034
31- _keyFile = cmd . Argument ( "KEYFILE" , "The path of the strong name key file." )
35+ _force = cmd . Option < bool > ( "-f|--force" , "Forces overwrite of an existing assembly." ,
36+ CommandOptionType . NoValue ) ;
37+
38+ _keyFile = cmd . Argument ( "KEYFILE" , "The path of the key file. If the assembly is delay-signed this can be the public key." )
3239 . IsRequired ( false , "The 'KEYFILE' argument is required." ) ;
3340
3441 _assemblyFile = cmd . Argument ( "ASSEMBLY" , "The path of the assembly file to sign." )
3542 . IsRequired ( false , "The 'ASSEMBLY' argument is required." ) ;
43+
44+ _outAssemblyFile = cmd . Argument (
45+ "OUTASSEMBLY" ,
46+ "The path of the signed assembly. If not specified the input assembly is overwritten." ) ;
3647 }
3748
3849 public int Execute ( CommandLineApplication cmd )
3950 {
4051 bool delaySign = _delaySign . HasValue ( ) ;
52+ bool force = _force . HasValue ( ) ;
4153 string keyFile = _keyFile . Value ;
4254 string assemblyFile = _assemblyFile . Value ;
55+ string outAssemblyFile = ! string . IsNullOrEmpty ( _outAssemblyFile . Value )
56+ ? _outAssemblyFile . Value
57+ : null ;
4358
4459 try
4560 {
61+ if ( File . Exists ( outAssemblyFile ) && ! force )
62+ throw new FileAreadyExistsException ( outAssemblyFile ) ;
63+
4664 if ( delaySign )
4765 {
4866 StrongNamePublicKey publicKey = _keyLoader . LoadPublicKey ( keyFile ) ;
49- _signer . DelaySignAssembly ( assemblyFile , publicKey ) ;
67+ _signer . DelaySignAssembly ( assemblyFile , publicKey , outAssemblyFile ) ;
5068 }
5169 else
5270 {
5371 StrongNameKey key = _keyLoader . LoadKey ( keyFile ) ;
54- _signer . SignAssembly ( assemblyFile , key ) ;
72+ _signer . SignAssembly ( assemblyFile , key , outAssemblyFile ) ;
5573 }
5674 }
5775 catch ( Exception error )
@@ -60,6 +78,7 @@ public int Execute(CommandLineApplication cmd)
6078 return ExitCodes . FromException ( error ) ;
6179 }
6280
81+ cmd . Out . WriteLine ( "Assembly '{0}' successfully signed." , outAssemblyFile ) ;
6382 return ExitCodes . Success ;
6483 }
6584 }
0 commit comments