aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.Native/WixNativeExe.cs
blob: fb41b2f2ba63c5f7d88e83bdbb274b869638abeb (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
// 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.Native
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;

    internal class WixNativeExe
    {
        private const string WixNativeExeFileName = "wixnative.exe";
        private static string PathToWixNativeExe;

        private readonly string commandLine;
        private readonly List<string> stdinLines = new List<string>();

        public WixNativeExe(params object[] args)
        {
            this.commandLine = String.Join(" ", QuoteArgumentsAsNecesary(args));
        }

        public void AddStdinLine(string line)
        {
            this.stdinLines.Add(line);
        }

        public void AddStdinLines(IEnumerable<string> lines)
        {
            this.stdinLines.AddRange(lines);
        }

        public IEnumerable<string> Run()
        {
            EnsurePathToWixNativeExeSet();

            var wixNativeInfo = new ProcessStartInfo(PathToWixNativeExe, this.commandLine)
            {
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
                ErrorDialog = false,
                UseShellExecute = false
            };

            var stdoutLines = new List<string>();

            using (var process = Process.Start(wixNativeInfo))
            {
                process.OutputDataReceived += (s, a) => stdoutLines.Add(a.Data);
                process.BeginOutputReadLine();

                if (this.stdinLines.Count > 0)
                {
                    foreach (var line in this.stdinLines)
                    {
                        process.StandardInput.WriteLine(line);
                    }

                    // Trailing blank line indicates stdin complete.
                    process.StandardInput.WriteLine();
                }

                // If the process successfully exits documentation says we need to wait again
                // without a timeout to ensure that all of the redirected output is captured.
                //
                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    throw new Win32Exception(process.ExitCode);
                }
            }

            return stdoutLines;
        }

        private static void EnsurePathToWixNativeExeSet()
        {
            if (String.IsNullOrEmpty(PathToWixNativeExe))
            {
                var result = typeof(WixNativeExe).Assembly.FindFileRelativeToAssembly(WixNativeExeFileName, searchNativeDllDirectories: true);

                if (!result.Found)
                {
                    throw new PlatformNotSupportedException(
                        $"Could not find platform specific '{WixNativeExeFileName}'",
                        new FileNotFoundException($"Could not find internal piece of WiX Toolset from: {result.PossiblePaths}", WixNativeExeFileName));
                }

                PathToWixNativeExe = result.Path;
            }
        }

        private static IEnumerable<string> QuoteArgumentsAsNecesary(object[] args)
        {
            foreach (var arg in args)
            {
                if (arg is string str)
                {
                    if (String.IsNullOrEmpty(str))
                    {
                    }
                    else if (str.Contains(" ") && !str.StartsWith("\""))
                    {
                        yield return $"\"{str}\"";
                    }
                    else
                    {
                        yield return str;
                    }
                }
                else if (arg is int i)
                {
                    yield return i.ToString();
                }
                else
                {
                    throw new ArgumentException(nameof(args));
                }
            }
        }
    }
}