aboutsummaryrefslogtreecommitdiff
path: root/src/WixBuildTools.TestSupport/Builder.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixBuildTools.TestSupport/Builder.cs')
-rw-r--r--src/WixBuildTools.TestSupport/Builder.cs113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/WixBuildTools.TestSupport/Builder.cs b/src/WixBuildTools.TestSupport/Builder.cs
new file mode 100644
index 00000000..62439ff7
--- /dev/null
+++ b/src/WixBuildTools.TestSupport/Builder.cs
@@ -0,0 +1,113 @@
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
3namespace WixBuildTools.TestSupport
4{
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Text;
9 using WixToolset.Dtf.WindowsInstaller;
10
11 public class Builder
12 {
13 public Builder(string sourceFolder, Type extensionType = null, string[] bindPaths = null)
14 {
15 this.SourceFolder = sourceFolder;
16 this.ExtensionType = extensionType;
17 this.BindPaths = bindPaths;
18 }
19
20 public string[] BindPaths { get; }
21
22 public Type ExtensionType { get; }
23
24 public string SourceFolder { get; }
25
26 public string[] BuildAndQuery(Action<string[]> buildFunc, params string[] tables)
27 {
28 var sourceFiles = Directory.GetFiles(this.SourceFolder, "*.wxs");
29 var wxlFiles = Directory.GetFiles(this.SourceFolder, "*.wxl");
30
31 using (var fs = new DisposableFileSystem())
32 {
33 var intermediateFolder = fs.GetFolder();
34 var outputPath = Path.Combine(intermediateFolder, @"bin\test.msi");
35
36 var args = new List<string>
37 {
38 "build",
39 "-o", outputPath,
40 "-intermediateFolder", intermediateFolder,
41 };
42
43 if (this.ExtensionType != null)
44 {
45 args.Add("-ext");
46 args.Add(Path.GetFullPath(new Uri(this.ExtensionType.Assembly.CodeBase).LocalPath));
47 }
48
49 args.AddRange(sourceFiles);
50
51 foreach (var wxlFile in wxlFiles)
52 {
53 args.Add("-loc");
54 args.Add(wxlFile);
55 }
56
57 foreach (var bindPath in this.BindPaths)
58 {
59 args.Add("-bindpath");
60 args.Add(bindPath);
61 }
62
63 buildFunc(args.ToArray());
64
65 return this.Query(outputPath, tables);
66 }
67 }
68
69 private string[] Query(string path, string[] tables)
70 {
71 var results = new List<string>();
72
73 if (tables?.Length > 0)
74 {
75 var sb = new StringBuilder();
76 using (var db = new Database(path))
77 {
78 foreach (var table in tables)
79 {
80 using (var view = db.OpenView($"SELECT * FROM `{table}`"))
81 {
82 view.Execute();
83
84 Record record;
85 while ((record = view.Fetch()) != null)
86 {
87 sb.Clear();
88 sb.AppendFormat("{0}:", table);
89
90 using (record)
91 {
92 for (var i = 0; i < record.FieldCount; ++i)
93 {
94 if (i > 0)
95 {
96 sb.Append("\t");
97 }
98
99 sb.Append(record[i + 1]?.ToString());
100 }
101 }
102
103 results.Add(sb.ToString());
104 }
105 }
106 }
107 }
108 }
109
110 return results.ToArray();
111 }
112 }
113}