aboutsummaryrefslogtreecommitdiff
path: root/src/candle/candle.cs
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2017-09-17 15:35:20 -0700
committerRob Mensching <rob@firegiant.com>2017-09-17 16:00:11 -0700
commitd3d3649a68cb1fa589fdd987a6690dbd5d671f0d (patch)
tree44fe37ee352b7e3a355cc1e08b1d7d5988c14cc0 /src/candle/candle.cs
parenta62610d23d6feb98be3b1e529a4e81b19d77d9d8 (diff)
downloadwix-d3d3649a68cb1fa589fdd987a6690dbd5d671f0d.tar.gz
wix-d3d3649a68cb1fa589fdd987a6690dbd5d671f0d.tar.bz2
wix-d3d3649a68cb1fa589fdd987a6690dbd5d671f0d.zip
Initial code commit
Diffstat (limited to 'src/candle/candle.cs')
-rw-r--r--src/candle/candle.cs200
1 files changed, 200 insertions, 0 deletions
diff --git a/src/candle/candle.cs b/src/candle/candle.cs
new file mode 100644
index 00000000..f5c65cb1
--- /dev/null
+++ b/src/candle/candle.cs
@@ -0,0 +1,200 @@
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.Tools
4{
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Linq;
9 using System.Runtime.InteropServices;
10 using System.Xml.Linq;
11 using WixToolset.Data;
12 using WixToolset.Extensibility;
13
14 /// <summary>
15 /// The main entry point for candle.
16 /// </summary>
17 public sealed class Candle
18 {
19 private CandleCommandLine commandLine;
20
21 private IEnumerable<IPreprocessorExtension> preprocessorExtensions;
22 private IEnumerable<ICompilerExtension> compilerExtensions;
23 private IEnumerable<IExtensionData> extensionData;
24
25 /// <summary>
26 /// The main entry point for candle.
27 /// </summary>
28 /// <param name="args">Commandline arguments for the application.</param>
29 /// <returns>Returns the application error code.</returns>
30 [MTAThread]
31 public static int Main(string[] args)
32 {
33 AppCommon.PrepareConsoleForLocalization();
34 Messaging.Instance.InitializeAppName("CNDL", "candle.exe").Display += AppCommon.ConsoleDisplayMessage;
35
36 Candle candle = new Candle();
37 return candle.Execute(args);
38 }
39
40 private int Execute(string[] args)
41 {
42 try
43 {
44 string[] unparsed = this.ParseCommandLineAndLoadExtensions(args);
45
46 if (!Messaging.Instance.EncounteredError)
47 {
48 if (this.commandLine.ShowLogo)
49 {
50 AppCommon.DisplayToolHeader();
51 }
52
53 if (this.commandLine.ShowHelp)
54 {
55 Console.WriteLine(CandleStrings.HelpMessage);
56 AppCommon.DisplayToolFooter();
57 }
58 else
59 {
60 foreach (string arg in unparsed)
61 {
62 Messaging.Instance.OnMessage(WixWarnings.UnsupportedCommandLineArgument(arg));
63 }
64
65 this.Run();
66 }
67 }
68 }
69 catch (WixException we)
70 {
71 Messaging.Instance.OnMessage(we.Error);
72 }
73 catch (Exception e)
74 {
75 Messaging.Instance.OnMessage(WixErrors.UnexpectedException(e.Message, e.GetType().ToString(), e.StackTrace));
76 if (e is NullReferenceException || e is SEHException)
77 {
78 throw;
79 }
80 }
81
82 return Messaging.Instance.LastErrorNumber;
83 }
84
85 private string[] ParseCommandLineAndLoadExtensions(string[] args)
86 {
87 this.commandLine = new CandleCommandLine();
88 string[] unprocessed = commandLine.Parse(args);
89 if (Messaging.Instance.EncounteredError)
90 {
91 return unprocessed;
92 }
93
94 // Load extensions.
95 ExtensionManager extensionManager = new ExtensionManager();
96 foreach (string extension in this.commandLine.Extensions)
97 {
98 extensionManager.Load(extension);
99 }
100
101 // Preprocessor extension command line processing.
102 this.preprocessorExtensions = extensionManager.Create<IPreprocessorExtension>();
103 foreach (IExtensionCommandLine pce in this.preprocessorExtensions.Where(e => e is IExtensionCommandLine).Cast<IExtensionCommandLine>())
104 {
105 pce.MessageHandler = Messaging.Instance;
106 unprocessed = pce.ParseCommandLine(unprocessed);
107 }
108
109 // Compiler extension command line processing.
110 this.compilerExtensions = extensionManager.Create<ICompilerExtension>();
111 foreach (IExtensionCommandLine cce in this.compilerExtensions.Where(e => e is IExtensionCommandLine).Cast<IExtensionCommandLine>())
112 {
113 cce.MessageHandler = Messaging.Instance;
114 unprocessed = cce.ParseCommandLine(unprocessed);
115 }
116
117 // Extension data command line processing.
118 this.extensionData = extensionManager.Create<IExtensionData>();
119 foreach (IExtensionCommandLine dce in this.extensionData.Where(e => e is IExtensionCommandLine).Cast<IExtensionCommandLine>())
120 {
121 dce.MessageHandler = Messaging.Instance;
122 unprocessed = dce.ParseCommandLine(unprocessed);
123 }
124
125 return commandLine.ParsePostExtensions(unprocessed);
126 }
127
128 private void Run()
129 {
130 // Create the preprocessor and compiler
131 Preprocessor preprocessor = new Preprocessor();
132 preprocessor.CurrentPlatform = this.commandLine.Platform;
133
134 foreach (string includePath in this.commandLine.IncludeSearchPaths)
135 {
136 preprocessor.IncludeSearchPaths.Add(includePath);
137 }
138
139 foreach (IPreprocessorExtension pe in this.preprocessorExtensions)
140 {
141 preprocessor.AddExtension(pe);
142 }
143
144 Compiler compiler = new Compiler();
145 compiler.ShowPedanticMessages = this.commandLine.ShowPedanticMessages;
146 compiler.CurrentPlatform = this.commandLine.Platform;
147
148 foreach (IExtensionData ed in this.extensionData)
149 {
150 compiler.AddExtensionData(ed);
151 }
152
153 foreach (ICompilerExtension ce in this.compilerExtensions)
154 {
155 compiler.AddExtension(ce);
156 }
157
158 // Preprocess then compile each source file.
159 foreach (CompileFile file in this.commandLine.Files)
160 {
161 // print friendly message saying what file is being compiled
162 Console.WriteLine(file.SourcePath);
163
164 // preprocess the source
165 XDocument sourceDocument;
166 try
167 {
168 if (!String.IsNullOrEmpty(this.commandLine.PreprocessFile))
169 {
170 preprocessor.PreprocessOut = this.commandLine.PreprocessFile.Equals("con:", StringComparison.OrdinalIgnoreCase) ? Console.Out : new StreamWriter(this.commandLine.PreprocessFile);
171 }
172
173 sourceDocument = preprocessor.Process(file.SourcePath, this.commandLine.PreprocessorVariables);
174 }
175 finally
176 {
177 if (null != preprocessor.PreprocessOut && Console.Out != preprocessor.PreprocessOut)
178 {
179 preprocessor.PreprocessOut.Close();
180 }
181 }
182
183 // If we're not actually going to compile anything, move on to the next file.
184 if (null == sourceDocument || !String.IsNullOrEmpty(this.commandLine.PreprocessFile))
185 {
186 continue;
187 }
188
189 // and now we do what we came here to do...
190 Intermediate intermediate = compiler.Compile(sourceDocument);
191
192 // save the intermediate to disk if no errors were found for this source file
193 if (null != intermediate)
194 {
195 intermediate.Save(file.OutputPath);
196 }
197 }
198 }
199 }
200}