aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/CommandLine/DecompileCommand.cs
blob: 3aa750c8163038f6359ac5a3bd3f4c179b301379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// 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.

namespace WixToolset.Core.CommandLine
{
    using System;
    using System.IO;
    using System.Xml.Linq;
    using WixToolset.Data;
    using WixToolset.Extensibility;
    using WixToolset.Extensibility.Data;
    using WixToolset.Extensibility.Services;

    internal class DecompileCommand : ICommandLineCommand
    {
        private readonly CommandLine commandLine;

        public DecompileCommand(IServiceProvider serviceProvider)
        {
            this.ServiceProvider = serviceProvider;
            this.Messaging = serviceProvider.GetService<IMessaging>();
            this.commandLine = new CommandLine(this.Messaging);
        }

        public bool ShowLogo => this.commandLine.ShowLogo;

        public bool StopParsing => this.commandLine.ShowHelp;

        private IServiceProvider ServiceProvider { get; }

        public IMessaging Messaging { get; }

        public int Execute()
        {
            if (this.commandLine.ShowHelp)
            {
                Console.WriteLine("TODO: Show decompile command help");
                return -1;
            }

            var context = this.ServiceProvider.GetService<IDecompileContext>();
            context.Extensions = this.ServiceProvider.GetService<IExtensionManager>().Create<IDecompilerExtension>();
            context.DecompilePath = this.commandLine.DecompileFilePath;
            context.DecompileType = this.commandLine.CalculateDecompileType();
            context.IntermediateFolder = this.commandLine.CalculateIntermedateFolder();
            context.OutputPath = this.commandLine.CalculateOutputPath();

            try
            {
                var decompiler = this.ServiceProvider.GetService<IDecompiler>();
                var result = decompiler.Decompile(context);

                if (!this.Messaging.EncounteredError)
                {
                    result.Document.Save(context.OutputPath, SaveOptions.OmitDuplicateNamespaces);
                }
            }
            catch (WixException e)
            {
                this.Messaging.Write(e.Error);
            }

            if (this.Messaging.EncounteredError)
            {
                return 1;
            }

            return 0;
        }

        public bool TryParseArgument(ICommandLineParser parser, string argument)
        {
            return this.commandLine.TryParseArgument(argument, parser);
        }

        private class CommandLine
        {
            public CommandLine(IMessaging messaging)
            {
                this.Messaging = messaging;
            }

            private IMessaging Messaging { get; }

            public string DecompileFilePath { get; private set; }

            public string DecompileType { get; private set; }

            public Platform Platform { get; private set; }

            public bool ShowLogo { get; private set; }

            public bool ShowHelp { get; private set; }

            public string IntermediateFolder { get; private set; }

            public string OutputFile { get; private set; }

            public bool TryParseArgument(string arg, ICommandLineParser parser)
            {
                if (parser.IsSwitch(arg))
                {
                    var parameter = arg.Substring(1);
                    switch (parameter.ToLowerInvariant())
                    {
                    case "?":
                    case "h":
                    case "help":
                        this.ShowHelp = true;
                        return true;

                    case "intermediatefolder":
                        this.IntermediateFolder = parser.GetNextArgumentAsDirectoryOrError(arg);
                        return true;

                    case "o":
                    case "out":
                        this.OutputFile = parser.GetNextArgumentAsFilePathOrError(arg);
                        return true;

                    case "nologo":
                        this.ShowLogo = false;
                        return true;

                    case "v":
                    case "verbose":
                        this.Messaging.ShowVerboseMessages = true;
                        return true;

                    case "sw":
                    case "suppresswarning":
                        var warning = parser.GetNextArgumentOrError(arg);
                        if (!String.IsNullOrEmpty(warning))
                        {
                            var warningNumber = Convert.ToInt32(warning);
                            this.Messaging.SuppressWarningMessage(warningNumber);
                        }
                        return true;
                    }
                }
                else
                {
                    if (String.IsNullOrEmpty(this.DecompileFilePath))
                    {
                        this.DecompileFilePath = parser.GetArgumentAsFilePathOrError(arg, "decompile file");
                        return true;
                    }
                    else if (String.IsNullOrEmpty(this.OutputFile))
                    {
                        this.OutputFile = parser.GetArgumentAsFilePathOrError(arg, "output file");
                        return true;
                    }
                }

                return false;
            }

            public OutputType CalculateDecompileType()
            {
                if (String.IsNullOrEmpty(this.DecompileType))
                {
                    this.DecompileType = Path.GetExtension(this.DecompileFilePath);
                }

                switch (this.DecompileType.ToLowerInvariant())
                {
                case "bundle":
                case ".exe":
                    return OutputType.Bundle;

                case "library":
                case ".wixlib":
                    return OutputType.Library;

                case "module":
                case ".msm":
                    return OutputType.Module;

                case "patch":
                case ".msp":
                    return OutputType.Patch;

                case ".pcp":
                    return OutputType.PatchCreation;

                case "product":
                case "package":
                case ".msi":
                    return OutputType.Product;

                case "transform":
                case ".mst":
                    return OutputType.Transform;

                case "intermediatepostlink":
                case ".wixipl":
                    return OutputType.IntermediatePostLink;
                }

                return OutputType.Unknown;
            }

            public string CalculateIntermedateFolder()
            {
                return String.IsNullOrEmpty(this.IntermediateFolder) ? Path.GetTempPath() : this.IntermediateFolder;
            }

            public string CalculateOutputPath()
            {
                return String.IsNullOrEmpty(this.OutputFile) ? Path.ChangeExtension(this.DecompileFilePath, ".wxs") : this.OutputFile;
            }
        }
    }
}