diff options
Diffstat (limited to 'src/WixToolset.Core/CommandLine/DecompileCommand.cs')
-rw-r--r-- | src/WixToolset.Core/CommandLine/DecompileCommand.cs | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/src/WixToolset.Core/CommandLine/DecompileCommand.cs b/src/WixToolset.Core/CommandLine/DecompileCommand.cs new file mode 100644 index 00000000..87cead80 --- /dev/null +++ b/src/WixToolset.Core/CommandLine/DecompileCommand.cs | |||
@@ -0,0 +1,212 @@ | |||
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.CommandLine | ||
4 | { | ||
5 | using System; | ||
6 | using System.Collections.Generic; | ||
7 | using System.IO; | ||
8 | using WixToolset.Data; | ||
9 | using WixToolset.Extensibility; | ||
10 | using WixToolset.Extensibility.Data; | ||
11 | using WixToolset.Extensibility.Services; | ||
12 | |||
13 | internal class DecompileCommand : ICommandLineCommand | ||
14 | { | ||
15 | private readonly CommandLine commandLine; | ||
16 | |||
17 | public DecompileCommand(IServiceProvider serviceProvider) | ||
18 | { | ||
19 | this.ServiceProvider = serviceProvider; | ||
20 | this.Messaging = serviceProvider.GetService<IMessaging>(); | ||
21 | this.commandLine = new CommandLine(this.Messaging); | ||
22 | } | ||
23 | |||
24 | public bool ShowLogo => this.commandLine.ShowLogo; | ||
25 | |||
26 | public bool StopParsing => this.commandLine.ShowHelp; | ||
27 | |||
28 | private IServiceProvider ServiceProvider { get; } | ||
29 | |||
30 | public IMessaging Messaging { get; } | ||
31 | |||
32 | private IEnumerable<SourceFile> SourceFiles { get; } | ||
33 | |||
34 | private string OutputPath { get; } | ||
35 | |||
36 | public int Execute() | ||
37 | { | ||
38 | if (this.commandLine.ShowHelp) | ||
39 | { | ||
40 | Console.WriteLine("TODO: Show decompile command help"); | ||
41 | return -1; | ||
42 | } | ||
43 | |||
44 | var context = this.ServiceProvider.GetService<IDecompileContext>(); | ||
45 | context.Extensions = this.ServiceProvider.GetService<IExtensionManager>().Create<IDecompilerExtension>(); | ||
46 | context.DecompilePath = this.commandLine.DecompileFilePath; | ||
47 | context.DecompileType = this.commandLine.CalculateDecompileType(); | ||
48 | context.IntermediateFolder = this.commandLine.CalculateIntermedateFolder(); | ||
49 | context.OutputPath = this.commandLine.CalculateOutputPath(); | ||
50 | |||
51 | try | ||
52 | { | ||
53 | var decompiler = this.ServiceProvider.GetService<IDecompiler>(); | ||
54 | var result = decompiler.Decompile(context); | ||
55 | } | ||
56 | catch (WixException e) | ||
57 | { | ||
58 | this.Messaging.Write(e.Error); | ||
59 | } | ||
60 | |||
61 | if (this.Messaging.EncounteredError) | ||
62 | { | ||
63 | return 1; | ||
64 | } | ||
65 | |||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | public bool TryParseArgument(ICommandLineParser parser, string argument) | ||
70 | { | ||
71 | return this.commandLine.TryParseArgument(argument, parser); | ||
72 | } | ||
73 | |||
74 | private class CommandLine | ||
75 | { | ||
76 | public CommandLine(IMessaging messaging) | ||
77 | { | ||
78 | this.Messaging = messaging; | ||
79 | } | ||
80 | |||
81 | private IMessaging Messaging { get; } | ||
82 | |||
83 | public string DecompileFilePath { get; private set; } | ||
84 | |||
85 | public string DecompileType { get; private set; } | ||
86 | |||
87 | public Platform Platform { get; private set; } | ||
88 | |||
89 | public bool ShowLogo { get; private set; } | ||
90 | |||
91 | public bool ShowHelp { get; private set; } | ||
92 | |||
93 | public string IntermediateFolder { get; private set; } | ||
94 | |||
95 | public string OutputFile { get; private set; } | ||
96 | |||
97 | public bool TryParseArgument(string arg, ICommandLineParser parser) | ||
98 | { | ||
99 | if (parser.IsSwitch(arg)) | ||
100 | { | ||
101 | var parameter = arg.Substring(1); | ||
102 | switch (parameter.ToLowerInvariant()) | ||
103 | { | ||
104 | case "?": | ||
105 | case "h": | ||
106 | case "help": | ||
107 | this.ShowHelp = true; | ||
108 | return true; | ||
109 | |||
110 | case "intermediatefolder": | ||
111 | this.IntermediateFolder = parser.GetNextArgumentAsDirectoryOrError(arg); | ||
112 | return true; | ||
113 | |||
114 | case "o": | ||
115 | case "out": | ||
116 | this.OutputFile = parser.GetNextArgumentAsFilePathOrError(arg); | ||
117 | return true; | ||
118 | |||
119 | case "nologo": | ||
120 | this.ShowLogo = false; | ||
121 | return true; | ||
122 | |||
123 | case "v": | ||
124 | case "verbose": | ||
125 | this.Messaging.ShowVerboseMessages = true; | ||
126 | return true; | ||
127 | |||
128 | case "sw": | ||
129 | case "suppresswarning": | ||
130 | var warning = parser.GetNextArgumentOrError(arg); | ||
131 | if (!String.IsNullOrEmpty(warning)) | ||
132 | { | ||
133 | var warningNumber = Convert.ToInt32(warning); | ||
134 | this.Messaging.SuppressWarningMessage(warningNumber); | ||
135 | } | ||
136 | return true; | ||
137 | } | ||
138 | } | ||
139 | else | ||
140 | { | ||
141 | if (String.IsNullOrEmpty(this.DecompileFilePath)) | ||
142 | { | ||
143 | this.DecompileFilePath = parser.GetArgumentAsFilePathOrError(arg, "decompile file"); | ||
144 | return true; | ||
145 | } | ||
146 | else if (String.IsNullOrEmpty(this.OutputFile)) | ||
147 | { | ||
148 | this.OutputFile = parser.GetArgumentAsFilePathOrError(arg, "output file"); | ||
149 | return true; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | return false; | ||
154 | } | ||
155 | |||
156 | public OutputType CalculateDecompileType() | ||
157 | { | ||
158 | if (String.IsNullOrEmpty(this.DecompileType)) | ||
159 | { | ||
160 | this.DecompileType = Path.GetExtension(this.DecompileFilePath); | ||
161 | } | ||
162 | |||
163 | switch (this.DecompileType.ToLowerInvariant()) | ||
164 | { | ||
165 | case "bundle": | ||
166 | case ".exe": | ||
167 | return OutputType.Bundle; | ||
168 | |||
169 | case "library": | ||
170 | case ".wixlib": | ||
171 | return OutputType.Library; | ||
172 | |||
173 | case "module": | ||
174 | case ".msm": | ||
175 | return OutputType.Module; | ||
176 | |||
177 | case "patch": | ||
178 | case ".msp": | ||
179 | return OutputType.Patch; | ||
180 | |||
181 | case ".pcp": | ||
182 | return OutputType.PatchCreation; | ||
183 | |||
184 | case "product": | ||
185 | case "package": | ||
186 | case ".msi": | ||
187 | return OutputType.Product; | ||
188 | |||
189 | case "transform": | ||
190 | case ".mst": | ||
191 | return OutputType.Transform; | ||
192 | |||
193 | case "intermediatepostlink": | ||
194 | case ".wixipl": | ||
195 | return OutputType.IntermediatePostLink; | ||
196 | } | ||
197 | |||
198 | return OutputType.Unknown; | ||
199 | } | ||
200 | |||
201 | public string CalculateIntermedateFolder() | ||
202 | { | ||
203 | return String.IsNullOrEmpty(this.IntermediateFolder) ? Path.GetTempPath() : this.IntermediateFolder; | ||
204 | } | ||
205 | |||
206 | public string CalculateOutputPath() | ||
207 | { | ||
208 | return String.IsNullOrEmpty(this.OutputFile) ? Path.ChangeExtension(this.DecompileFilePath, ".wxs") : this.OutputFile; | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | } | ||