aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Mutator.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Core/Mutator.cs')
-rw-r--r--src/WixToolset.Core/Mutator.cs115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/WixToolset.Core/Mutator.cs b/src/WixToolset.Core/Mutator.cs
new file mode 100644
index 00000000..d37815f4
--- /dev/null
+++ b/src/WixToolset.Core/Mutator.cs
@@ -0,0 +1,115 @@
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 WixToolset
4{
5 using System;
6 using System.Collections;
7 using WixToolset.Extensibility;
8 using Wix = WixToolset.Data.Serialize;
9
10 /// <summary>
11 /// The WiX Toolset mutator.
12 /// </summary>
13 public sealed class Mutator
14 {
15 private SortedList extensions;
16 private string extensionArgument;
17
18 /// <summary>
19 /// Instantiate a new mutator.
20 /// </summary>
21 public Mutator()
22 {
23 this.extensions = new SortedList();
24 }
25
26 /// <summary>
27 /// Gets or sets the harvester core for the extension.
28 /// </summary>
29 /// <value>The harvester core for the extension.</value>
30 public IHarvesterCore Core { get; set; }
31
32 /// <summary>
33 /// Gets or sets the value of the extension argument passed to heat.
34 /// </summary>
35 /// <value>The extension argument.</value>
36 public string ExtensionArgument
37 {
38 get { return this.extensionArgument; }
39 set { this.extensionArgument = value; }
40 }
41
42 /// <summary>
43 /// Adds a mutator extension.
44 /// </summary>
45 /// <param name="mutatorExtension">The mutator extension to add.</param>
46 public void AddExtension(MutatorExtension mutatorExtension)
47 {
48 this.extensions.Add(mutatorExtension.Sequence, mutatorExtension);
49 }
50
51 /// <summary>
52 /// Mutate a WiX document.
53 /// </summary>
54 /// <param name="wix">The Wix document element.</param>
55 /// <returns>true if mutation was successful</returns>
56 public bool Mutate(Wix.Wix wix)
57 {
58 bool encounteredError = false;
59
60 try
61 {
62 foreach (MutatorExtension mutatorExtension in this.extensions.Values)
63 {
64 if (null == mutatorExtension.Core)
65 {
66 mutatorExtension.Core = this.Core;
67 }
68
69 mutatorExtension.Mutate(wix);
70 }
71 }
72 finally
73 {
74 encounteredError = this.Core.EncounteredError;
75 }
76
77 // return the Wix document element only if mutation completed successfully
78 return !encounteredError;
79 }
80
81 /// <summary>
82 /// Mutate a WiX document.
83 /// </summary>
84 /// <param name="wixString">The Wix document as a string.</param>
85 /// <returns>The mutated Wix document as a string if mutation was successful, else null.</returns>
86 public string Mutate(string wixString)
87 {
88 bool encounteredError = false;
89
90 try
91 {
92 foreach (MutatorExtension mutatorExtension in this.extensions.Values)
93 {
94 if (null == mutatorExtension.Core)
95 {
96 mutatorExtension.Core = this.Core;
97 }
98
99 wixString = mutatorExtension.Mutate(wixString);
100
101 if (String.IsNullOrEmpty(wixString) || this.Core.EncounteredError)
102 {
103 break;
104 }
105 }
106 }
107 finally
108 {
109 encounteredError = this.Core.EncounteredError;
110 }
111
112 return encounteredError ? null : wixString;
113 }
114 }
115}