| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Core.Burn.CommandLine |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Threading; |
| 8 | using System.Threading.Tasks; |
| 9 | using WixToolset.Core.Burn.Inscribe; |
| 10 | using WixToolset.Data; |
| 11 | using WixToolset.Extensibility.Data; |
| 12 | using WixToolset.Extensibility.Services; |
| 13 | |
| 14 | internal class ReattachSubcommand : BurnSubcommandBase |
| 15 | { |
| 16 | public ReattachSubcommand(IServiceProvider serviceProvider) |
| 17 | { |
| 18 | this.ServiceProvider = serviceProvider; |
| 19 | this.Messaging = serviceProvider.GetService<IMessaging>(); |
| 20 | } |
| 21 | |
| 22 | private IServiceProvider ServiceProvider { get; } |
| 23 | |
| 24 | private IMessaging Messaging { get; } |
| 25 | |
| 26 | private string InputPath { get; set; } |
| 27 | |
| 28 | private string SignedEnginePath { get; set; } |
| 29 | |
| 30 | private string IntermediateFolder { get; set; } |
| 31 | |
| 32 | private string OutputPath { get; set; } |
| 33 | |
| 34 | public override CommandLineHelp GetCommandLineHelp() |
| 35 | { |
| 36 | return new CommandLineHelp("Reattaches a signed burn engine to a bundle.", "burn reattach [options] original.exe -engine signed.exe -o final.exe", new[] |
| 37 | { |
| 38 | new CommandLineHelpSwitch("-engine", "Burn engine extracted using `wix burn detach` and then signed."), |
| 39 | new CommandLineHelpSwitch("-intermediateFolder", "Optional working folder. If not specified %TMP% will be used."), |
| 40 | new CommandLineHelpSwitch("-out", "-o", "Required path to output bundle with reattached signed engine that can then be signed as a whole."), |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | public override Task<int> ExecuteAsync(CancellationToken cancellationToken) |
| 45 | { |
| 46 | if (String.IsNullOrEmpty(this.InputPath)) |
| 47 | { |
| 48 | this.Messaging.Write(ErrorMessages.FilePathRequired("input bundle")); |
| 49 | } |
| 50 | else if (String.IsNullOrEmpty(this.SignedEnginePath)) |
| 51 | { |
| 52 | this.Messaging.Write(ErrorMessages.FilePathRequired("detached signed bundle bundle")); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | if (String.IsNullOrEmpty(this.IntermediateFolder)) |
| 57 | { |
| 58 | this.IntermediateFolder = Path.GetTempPath(); |
| 59 | } |
| 60 | |
| 61 | if (String.IsNullOrEmpty(this.OutputPath)) |
| 62 | { |
| 63 | this.OutputPath = this.InputPath; |
| 64 | } |
| 65 | |
| 66 | var command = new InscribeBundleCommand(this.ServiceProvider, this.InputPath, this.SignedEnginePath, this.OutputPath, this.IntermediateFolder); |
| 67 | var didWork = command.Execute(); |
| 68 | |
| 69 | // If the detach subcommand did not encounter an error but did no work |
| 70 | // then return the special exit code that indicates no work was done (-1000). |
| 71 | var exitCode = this.Messaging.LastErrorNumber; |
| 72 | |
| 73 | if (!didWork && exitCode == 0) |
| 74 | { |
| 75 | exitCode = -1000; |
| 76 | return Task.FromResult(exitCode); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return Task.FromResult(this.Messaging.LastErrorNumber); |
| 81 | } |
| 82 | |
| 83 | public override bool TryParseArgument(ICommandLineParser parser, string argument) |
| 84 | { |
| 85 | if (parser.IsSwitch(argument)) |
| 86 | { |
| 87 | var parameter = argument.Substring(1); |
| 88 | switch (parameter.ToLowerInvariant()) |
| 89 | { |
| 90 | case "engine": |
| 91 | this.SignedEnginePath = parser.GetNextArgumentAsFilePathOrError(argument, "detached signed bundle bundle"); |
| 92 | return true; |
| 93 | |
| 94 | case "intermediatefolder": |
| 95 | this.IntermediateFolder = parser.GetNextArgumentAsDirectoryOrError(argument); |
| 96 | return true; |
| 97 | |
| 98 | case "o": |
| 99 | case "out": |
| 100 | this.OutputPath = parser.GetNextArgumentAsFilePathOrError(argument, "output bundle"); |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | else if (String.IsNullOrEmpty(this.InputPath)) |
| 105 | { |
| 106 | this.InputPath = argument; |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | } |