aboutsummaryrefslogtreecommitdiff
path: root/src/wixcop/Program.cs
blob: 8e9534f6eb7141ed6016674d0236661f28283e1c (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
// 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 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 = new WixToolsetServiceProvider();
            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(IServiceProvider 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();
                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;
            }
        }
    }
}