| 1 | // 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. |
| 2 | |
| 3 | namespace WixInternal.TestSupport |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | |
| 9 | public class Builder |
| 10 | { |
| 11 | public Builder(string sourceFolder, Type extensionType = null, string[] bindPaths = null, string outputFile = null) |
| 12 | { |
| 13 | this.SourceFolder = sourceFolder; |
| 14 | if (extensionType != null) |
| 15 | { |
| 16 | this.ExtensionTypes = new Type[] { extensionType }; |
| 17 | } |
| 18 | else |
| 19 | { |
| 20 | this.ExtensionTypes = new Type[] { }; |
| 21 | } |
| 22 | this.BindPaths = bindPaths; |
| 23 | this.OutputFile = outputFile ?? "test.msi"; |
| 24 | } |
| 25 | |
| 26 | public Builder(string sourceFolder, Type[] extensionTypes, string[] bindPaths = null, string outputFile = null) |
| 27 | { |
| 28 | this.SourceFolder = sourceFolder; |
| 29 | this.ExtensionTypes = extensionTypes; |
| 30 | this.BindPaths = bindPaths; |
| 31 | this.OutputFile = outputFile ?? "test.msi"; |
| 32 | } |
| 33 | |
| 34 | public string[] BindPaths { get; set; } |
| 35 | |
| 36 | public Type[] ExtensionTypes { get; set; } |
| 37 | |
| 38 | public string OutputFile { get; set; } |
| 39 | |
| 40 | public string SourceFolder { get; } |
| 41 | |
| 42 | public string[] BuildAndQuery(Action<string[]> buildFunc, params string[] tables) |
| 43 | { |
| 44 | return this.BuildAndQuery(buildFunc, validate: false, tables); |
| 45 | } |
| 46 | |
| 47 | public string[] BuildAndQuery(Action<string[]> buildFunc, bool validate, params string[] tables) |
| 48 | { |
| 49 | var sourceFiles = Directory.GetFiles(this.SourceFolder, "*.wxs"); |
| 50 | var wxlFiles = Directory.GetFiles(this.SourceFolder, "*.wxl"); |
| 51 | |
| 52 | using (var fs = new DisposableFileSystem()) |
| 53 | { |
| 54 | var intermediateFolder = fs.GetFolder(); |
| 55 | var outputPath = Path.Combine(intermediateFolder, "bin", this.OutputFile); |
| 56 | |
| 57 | var args = new List<string> |
| 58 | { |
| 59 | "build", |
| 60 | "-o", outputPath, |
| 61 | "-intermediateFolder", intermediateFolder, |
| 62 | }; |
| 63 | |
| 64 | foreach (var ext in this.ExtensionTypes) |
| 65 | { |
| 66 | args.Add("-ext"); |
| 67 | args.Add(Path.GetFullPath(ext.Assembly.Location)); |
| 68 | } |
| 69 | |
| 70 | args.AddRange(sourceFiles); |
| 71 | |
| 72 | foreach (var wxlFile in wxlFiles) |
| 73 | { |
| 74 | args.Add("-loc"); |
| 75 | args.Add(wxlFile); |
| 76 | } |
| 77 | |
| 78 | foreach (var bindPath in this.BindPaths) |
| 79 | { |
| 80 | args.Add("-bindpath"); |
| 81 | args.Add(bindPath); |
| 82 | } |
| 83 | |
| 84 | buildFunc(args.ToArray()); |
| 85 | |
| 86 | if (validate) |
| 87 | { |
| 88 | args = new List<string> |
| 89 | { |
| 90 | "msi", |
| 91 | "validate", |
| 92 | "-intermediateFolder", intermediateFolder, |
| 93 | outputPath, |
| 94 | }; |
| 95 | |
| 96 | buildFunc(args.ToArray()); |
| 97 | } |
| 98 | |
| 99 | return Query.QueryDatabase(outputPath, tables); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public void BuildAndDecompileAndBuild(Action<string[]> buildFunc, Action<string[]> decompileFunc, string decompilePath, bool validate = false) |
| 104 | { |
| 105 | var sourceFiles = Directory.GetFiles(this.SourceFolder, "*.wxs"); |
| 106 | var wxlFiles = Directory.GetFiles(this.SourceFolder, "*.wxl"); |
| 107 | |
| 108 | using (var fs = new DisposableFileSystem()) |
| 109 | { |
| 110 | var intermediateFolder = fs.GetFolder(); |
| 111 | var outputFolder = Path.Combine(intermediateFolder, "bin"); |
| 112 | var decompileExtractFolder = Path.Combine(intermediateFolder, "decompiled", "extract"); |
| 113 | var decompileIntermediateFolder = Path.Combine(intermediateFolder, "decompiled", "obj"); |
| 114 | var decompileBuildFolder = Path.Combine(intermediateFolder, "decompiled", "bin"); |
| 115 | var outputPath = Path.Combine(outputFolder, this.OutputFile); |
| 116 | var decompileBuildPath = Path.Combine(decompileBuildFolder, this.OutputFile); |
| 117 | |
| 118 | // First build. |
| 119 | var firstBuildArgs = new List<string> |
| 120 | { |
| 121 | "build", |
| 122 | "-o", outputPath, |
| 123 | "-intermediateFolder", intermediateFolder, |
| 124 | }; |
| 125 | |
| 126 | foreach (var ext in this.ExtensionTypes) |
| 127 | { |
| 128 | firstBuildArgs.Add("-ext"); |
| 129 | firstBuildArgs.Add(Path.GetFullPath(ext.Assembly.Location)); |
| 130 | } |
| 131 | |
| 132 | firstBuildArgs.AddRange(sourceFiles); |
| 133 | |
| 134 | foreach (var wxlFile in wxlFiles) |
| 135 | { |
| 136 | firstBuildArgs.Add("-loc"); |
| 137 | firstBuildArgs.Add(wxlFile); |
| 138 | } |
| 139 | |
| 140 | foreach (var bindPath in this.BindPaths) |
| 141 | { |
| 142 | firstBuildArgs.Add("-bindpath"); |
| 143 | firstBuildArgs.Add(bindPath); |
| 144 | } |
| 145 | |
| 146 | buildFunc(firstBuildArgs.ToArray()); |
| 147 | |
| 148 | if (validate) |
| 149 | { |
| 150 | firstBuildArgs = new List<string> |
| 151 | { |
| 152 | "msi", |
| 153 | "validate", |
| 154 | "-intermediateFolder", intermediateFolder, |
| 155 | outputPath, |
| 156 | }; |
| 157 | |
| 158 | buildFunc(firstBuildArgs.ToArray()); |
| 159 | } |
| 160 | |
| 161 | // Decompile built output. |
| 162 | var decompileArgs = new List<string> |
| 163 | { |
| 164 | "msi", "decompile", |
| 165 | outputPath, |
| 166 | "-intermediateFolder", decompileIntermediateFolder, |
| 167 | "-x", decompileExtractFolder, |
| 168 | "-o", decompilePath |
| 169 | }; |
| 170 | |
| 171 | foreach (var ext in this.ExtensionTypes) |
| 172 | { |
| 173 | decompileArgs.Add("-ext"); |
| 174 | decompileArgs.Add(Path.GetFullPath(ext.Assembly.Location)); |
| 175 | } |
| 176 | |
| 177 | decompileFunc(decompileArgs.ToArray()); |
| 178 | |
| 179 | // Build decompiled output. |
| 180 | var secondBuildArgs = new List<string> |
| 181 | { |
| 182 | "build", |
| 183 | decompilePath, |
| 184 | "-o", decompileBuildPath, |
| 185 | "-intermediateFolder", decompileIntermediateFolder |
| 186 | }; |
| 187 | |
| 188 | foreach (var ext in this.ExtensionTypes) |
| 189 | { |
| 190 | secondBuildArgs.Add("-ext"); |
| 191 | secondBuildArgs.Add(Path.GetFullPath(ext.Assembly.Location)); |
| 192 | } |
| 193 | |
| 194 | secondBuildArgs.Add("-bindpath"); |
| 195 | secondBuildArgs.Add(outputFolder); |
| 196 | |
| 197 | secondBuildArgs.Add("-bindpath"); |
| 198 | secondBuildArgs.Add(decompileExtractFolder); |
| 199 | |
| 200 | buildFunc(secondBuildArgs.ToArray()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |