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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
// 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
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using WixToolset.Data;
using WixToolset.Extensibility;
internal class BuildCommand : ICommand
{
public BuildCommand(IEnumerable<SourceFile> sources, IDictionary<string, string> preprocessorVariables, IEnumerable<string> locFiles, IEnumerable<string> libraryFiles, string outputPath, OutputType outputType, IEnumerable<string> cultures, bool bindFiles, IEnumerable<BindPath> bindPaths, string intermediateFolder, string contentsFile, string outputsFile, string builtOutputsFile, string wixProjectFile)
{
this.LocFiles = locFiles;
this.LibraryFiles = libraryFiles;
this.PreprocessorVariables = preprocessorVariables;
this.SourceFiles = sources;
this.OutputPath = outputPath;
this.OutputType = outputType;
this.Cultures = cultures;
this.BindFiles = bindFiles;
this.BindPaths = bindPaths;
this.IntermediateFolder = intermediateFolder ?? Path.GetTempPath();
this.ContentsFile = contentsFile;
this.OutputsFile = outputsFile;
this.BuiltOutputsFile = builtOutputsFile;
this.WixProjectFile = wixProjectFile;
}
public IEnumerable<string> LocFiles { get; }
public IEnumerable<string> LibraryFiles { get; }
private IEnumerable<SourceFile> SourceFiles { get; }
private IDictionary<string, string> PreprocessorVariables { get; }
private string OutputPath { get; }
private OutputType OutputType { get; }
public IEnumerable<string> Cultures { get; }
public bool BindFiles { get; }
public IEnumerable<BindPath> BindPaths { get; }
public string IntermediateFolder { get; }
public string ContentsFile { get; }
public string OutputsFile { get; }
public string BuiltOutputsFile { get; }
public string WixProjectFile { get; }
public int Execute()
{
var intermediates = this.CompilePhase();
var tableDefinitions = new TableDefinitionCollection(WindowsInstallerStandard.GetTableDefinitions());
if (this.OutputType == OutputType.Library)
{
this.LibraryPhase(intermediates, tableDefinitions);
}
else
{
var output = this.LinkPhase(intermediates, tableDefinitions);
if (!Messaging.Instance.EncounteredError)
{
this.BindPhase(output, tableDefinitions);
}
}
return Messaging.Instance.LastErrorNumber;
}
private IEnumerable<Intermediate> CompilePhase()
{
var intermediates = new List<Intermediate>();
var preprocessor = new Preprocessor();
var compiler = new Compiler();
foreach (var sourceFile in this.SourceFiles)
{
var document = preprocessor.Process(sourceFile.SourcePath, this.PreprocessorVariables);
var intermediate = compiler.Compile(document);
intermediates.Add(intermediate);
}
return intermediates;
}
private void LibraryPhase(IEnumerable<Intermediate> intermediates, TableDefinitionCollection tableDefinitions)
{
var localizations = this.LoadLocalizationFiles(tableDefinitions).ToList();
// If there was an error adding localization files, then bail.
if (Messaging.Instance.EncounteredError)
{
return;
}
var sections = intermediates.SelectMany(i => i.Sections).ToList();
LibraryBinaryFileResolver resolver = null;
if (this.BindFiles)
{
resolver = new LibraryBinaryFileResolver();
resolver.FileManagers = new List<IBinderFileManager> { new BinderFileManager() }; ;
resolver.VariableResolver = new WixVariableResolver();
BinderFileManagerCore core = new BinderFileManagerCore();
core.AddBindPaths(this.BindPaths, BindStage.Normal);
foreach (var fileManager in resolver.FileManagers)
{
fileManager.Core = core;
}
}
var librarian = new Librarian();
var library = librarian.Combine(sections, localizations, resolver);
library?.Save(this.OutputPath);
}
private Output LinkPhase(IEnumerable<Intermediate> intermediates, TableDefinitionCollection tableDefinitions)
{
var sections = intermediates.SelectMany(i => i.Sections).ToList();
sections.AddRange(SectionsFromLibraries(tableDefinitions));
var linker = new Linker();
var output = linker.Link(sections, this.OutputType);
return output;
}
private IEnumerable<Section> SectionsFromLibraries(TableDefinitionCollection tableDefinitions)
{
var sections = new List<Section>();
if (this.LibraryFiles != null)
{
foreach (var libraryFile in this.LibraryFiles)
{
try
{
var library = Library.Load(libraryFile, tableDefinitions, false);
sections.AddRange(library.Sections);
}
catch (WixCorruptFileException e)
{
Messaging.Instance.OnMessage(e.Error);
}
catch (WixUnexpectedFileFormatException e)
{
Messaging.Instance.OnMessage(e.Error);
}
}
}
return sections;
}
private void BindPhase(Output output, TableDefinitionCollection tableDefinitions)
{
var localizations = this.LoadLocalizationFiles(tableDefinitions).ToList();
var localizer = new Localizer(localizations);
var resolver = new WixVariableResolver(localizer);
var binder = new Binder();
binder.TempFilesLocation = this.IntermediateFolder;
binder.WixVariableResolver = resolver;
binder.SuppressValidation = true;
binder.ContentsFile = this.ContentsFile;
binder.OutputsFile = this.OutputsFile;
binder.BuiltOutputsFile = this.BuiltOutputsFile;
binder.WixprojectFile = this.WixProjectFile;
if (this.BindPaths != null)
{
binder.BindPaths.AddRange(this.BindPaths);
}
binder.AddExtension(new BinderFileManager());
binder.Bind(output, this.OutputPath);
}
private IEnumerable<Localization> LoadLocalizationFiles(TableDefinitionCollection tableDefinitions)
{
foreach (var loc in this.LocFiles)
{
var localization = Localizer.ParseLocalizationFile(loc, tableDefinitions);
yield return localization;
}
}
/// <summary>
/// File resolution mechanism to create binary library.
/// </summary>
private class LibraryBinaryFileResolver : ILibraryBinaryFileResolver
{
public IEnumerable<IBinderFileManager> FileManagers { get; set; }
public WixVariableResolver VariableResolver { get; set; }
public string Resolve(SourceLineNumber sourceLineNumber, string table, string path)
{
string resolvedPath = this.VariableResolver.ResolveVariables(sourceLineNumber, path, false);
foreach (IBinderFileManager fileManager in this.FileManagers)
{
string finalPath = fileManager.ResolveFile(resolvedPath, table, sourceLineNumber, BindStage.Normal);
if (!String.IsNullOrEmpty(finalPath))
{
return finalPath;
}
}
return null;
}
}
}
}
|