aboutsummaryrefslogtreecommitdiff
path: root/src/wixcop/Program.cs
blob: 5e72f8ea48cebe3eabc49521ecff3bfbc7e51c16 (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
// 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.Tools.WixCop
{
    using System;
    using System.Diagnostics;
    using WixToolset.Core;
    using WixToolset.Extensibility;
    using WixToolset.Extensibility.Data;
    using WixToolset.Extensibility.Services;
    using WixToolset.Tools.Core;
    using WixToolset.Tools.WixCop.CommandLine;
    using WixToolset.Tools.WixCop.Interfaces;

    /// <summary>
    /// Wix source code style inspector and converter.
    /// </summary>
    public sealed class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">The commandline arguments.</param>
        /// <returns>The number of errors that were found.</returns>
        [STAThread]
        public static int Main(string[] args)
        {
            var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider();
            var listener = new ConsoleMessageListener("WXCP", "wixcop.exe");

            serviceProvider.AddService<IMessageListener>((x, y) => listener);
            serviceProvider.AddService<IWixCopCommandLineParser>((x, y) => new WixCopCommandLineParser(x));

            var program = new Program();
            return program.Run(serviceProvider, args);
        }

        /// <summary>
        /// Run the application with the given arguments.
        /// </summary>
        /// <param name="serviceProvider">Service provider to use throughout this execution.</param>
        /// <param name="args">The commandline arguments.</param>
        /// <returns>The number of errors that were found.</returns>
        public int Run(IWixToolsetServiceProvider serviceProvider, string[] args)
        {
            try
            {
                var listener = serviceProvider.GetService<IMessageListener>();
                var messaging = serviceProvider.GetService<IMessaging>();
                messaging.SetListener(listener);

                var arguments = serviceProvider.GetService<ICommandLineArguments>();
                arguments.Populate(args);

                var commandLine = serviceProvider.GetService<IWixCopCommandLineParser>();
                commandLine.Arguments = arguments;
                var command = commandLine.ParseWixCopCommandLine();

                if (command.ShowLogo)
                {
                    var wixcopAssembly = this.GetType().Assembly;
                    var fv = FileVersionInfo.GetVersionInfo(wixcopAssembly.Location);

                    Console.WriteLine("WiX Cop version {0}", fv.FileVersion);
                    Console.WriteLine("Copyright (C) .NET Foundation and contributors. All rights reserved.");
                    Console.WriteLine();
                }

                return command?.Execute() ?? 1;
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("wixcop.exe : fatal error WXCP0001 : {0}\r\n\n\nStack Trace:\r\n{1}", e.Message, e.StackTrace);

                return 1;
            }
        }
    }
}