summaryrefslogtreecommitdiff
path: root/src/wix/WixToolset.Core/CommandLine/CompileCommand.cs
blob: 73e5bcbe9bb9123b73262e72512449d364a9772b (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
// 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.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    using WixToolset.Data;
    using WixToolset.Extensibility;
    using WixToolset.Extensibility.Data;
    using WixToolset.Extensibility.Services;

    internal class CompileCommand : ICommandLineCommand
    {
        public CompileCommand(IServiceProvider serviceProvider)
        {
            this.ServiceProvider = serviceProvider;
            this.Messaging = serviceProvider.GetService<IMessaging>();
            this.ExtensionManager = serviceProvider.GetService<IExtensionManager>();
        }

        public CompileCommand(IServiceProvider serviceProvider, IEnumerable<SourceFile> sources, IDictionary<string, string> preprocessorVariables, Platform platform)
        {
            this.ServiceProvider = serviceProvider;
            this.Messaging = serviceProvider.GetService<IMessaging>();
            this.ExtensionManager = serviceProvider.GetService<IExtensionManager>();
            this.SourceFiles = sources;
            this.PreprocessorVariables = preprocessorVariables;
            this.Platform = platform;
        }

        public bool ShowHelp { get; set; }

        public bool ShowLogo { get; set; }

        public bool StopParsing { get; }

        private IServiceProvider ServiceProvider { get; }

        private IMessaging Messaging { get; }

        private IExtensionManager ExtensionManager { get; }

        private IEnumerable<SourceFile> SourceFiles { get; }

        private IDictionary<string, string> PreprocessorVariables { get; }

        private Platform Platform { get; }

        public IReadOnlyCollection<string> IncludeSearchPaths { get; }

        public bool TryParseArgument(ICommandLineParser parseHelper, string argument)
        {
            throw new NotImplementedException();
        }

        public Task<int> ExecuteAsync(CancellationToken _)
        {
            foreach (var sourceFile in this.SourceFiles)
            {
                var context = this.ServiceProvider.GetService<IPreprocessContext>();
                context.Extensions = this.ExtensionManager.GetServices<IPreprocessorExtension>();
                context.Platform = this.Platform;
                context.IncludeSearchPaths = this.IncludeSearchPaths;
                context.SourcePath = sourceFile.SourcePath;
                context.Variables = this.PreprocessorVariables;

                IPreprocessResult result = null;
                try
                {
                    var preprocessor = this.ServiceProvider.GetService<IPreprocessor>();
                    result = preprocessor.Preprocess(context);
                }
                catch (WixException e)
                {
                    this.Messaging.Write(e.Error);
                }

                if (this.Messaging.EncounteredError)
                {
                    continue;
                }

                var compileContext = this.ServiceProvider.GetService<ICompileContext>();
                compileContext.Extensions = this.ExtensionManager.GetServices<ICompilerExtension>();
                compileContext.Platform = this.Platform;
                compileContext.Source = result?.Document;

                var compiler = this.ServiceProvider.GetService<ICompiler>();
                var intermediate = compiler.Compile(compileContext);

                intermediate.Save(sourceFile.OutputPath);
            }

            return Task.FromResult(0);
        }
    }
}