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
|
// 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.Bind
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using WixToolset.Data;
using WixToolset.Extensibility;
using WixToolset.Extensibility.Data;
internal class FileResolver
{
private const string BindPathOpenString = "!(bindpath.";
private FileResolver(IEnumerable<IBindPath> bindPaths)
{
this.BindPaths = (bindPaths ?? Array.Empty<IBindPath>()).ToLookup(b => b.Stage);
this.RebaseTarget = this.BindPaths[BindStage.Target].Any();
this.RebaseUpdated = this.BindPaths[BindStage.Updated].Any();
}
public FileResolver(IEnumerable<IBindPath> bindPaths, IEnumerable<IResolverExtension> extensions) : this(bindPaths)
{
this.ResolverExtensions = extensions ?? Array.Empty<IResolverExtension>();
}
public FileResolver(IEnumerable<IBindPath> bindPaths, IEnumerable<ILibrarianExtension> extensions) : this(bindPaths)
{
this.LibrarianExtensions = extensions ?? Array.Empty<ILibrarianExtension>();
}
private ILookup<BindStage, IBindPath> BindPaths { get; }
public bool RebaseTarget { get; }
public bool RebaseUpdated { get; }
private IEnumerable<IResolverExtension> ResolverExtensions { get; }
private IEnumerable<ILibrarianExtension> LibrarianExtensions { get; }
public string Resolve(SourceLineNumber sourceLineNumbers, IntermediateSymbolDefinition symbolDefinition, string source)
{
var checkedPaths = new List<string>();
foreach (var extension in this.LibrarianExtensions)
{
var resolved = extension.ResolveFile(sourceLineNumbers, symbolDefinition, source);
if (resolved?.CheckedPaths != null)
{
checkedPaths.AddRange(resolved.CheckedPaths);
}
if (!String.IsNullOrEmpty(resolved?.Path))
{
return resolved?.Path;
}
}
return this.MustResolveUsingBindPaths(source, symbolDefinition, sourceLineNumbers, BindStage.Normal, checkedPaths);
}
/// <summary>
/// Resolves the source path of a file using binder extensions.
/// </summary>
/// <param name="source">Original source value.</param>
/// <param name="type">Optional type of source file being resolved.</param>
/// <param name="sourceLineNumbers">Optional source line of source file being resolved.</param>
/// <param name="bindStage">The binding stage used to determine what collection of bind paths will be used</param>
/// <param name="alreadyCheckedPaths">Optional collection of paths already checked.</param>
/// <returns>Should return a valid path for the stream to be imported.</returns>
public string ResolveFile(string source, IntermediateSymbolDefinition symbolDefinition, SourceLineNumber sourceLineNumbers, BindStage bindStage, IEnumerable<string> alreadyCheckedPaths = null)
{
var checkedPaths = new List<string>();
if (alreadyCheckedPaths != null)
{
checkedPaths.AddRange(alreadyCheckedPaths);
}
foreach (var extension in this.ResolverExtensions)
{
var resolved = extension.ResolveFile(source, symbolDefinition, sourceLineNumbers, bindStage);
if (resolved?.CheckedPaths != null)
{
checkedPaths.AddRange(resolved.CheckedPaths);
}
if (!String.IsNullOrEmpty(resolved?.Path))
{
return resolved?.Path;
}
}
return this.MustResolveUsingBindPaths(source, symbolDefinition, sourceLineNumbers, bindStage, checkedPaths);
}
private string MustResolveUsingBindPaths(string source, IntermediateSymbolDefinition symbolDefinition, SourceLineNumber sourceLineNumbers, BindStage bindStage, List<string> checkedPaths)
{
string resolved = null;
// If the file exists, we're good to go.
checkedPaths.Add(source);
if (CheckFileExists(source))
{
resolved = source;
}
else if (Path.IsPathRooted(source)) // path is rooted so bindpaths won't help, bail since the file apparently doesn't exist.
{
resolved = null;
}
else // not a rooted path so let's try applying all the different source resolution options.
{
var bindName = String.Empty;
var path = source;
var pathWithoutSourceDir = String.Empty;
if (source.StartsWith(BindPathOpenString, StringComparison.Ordinal))
{
var closeParen = source.IndexOf(')', BindPathOpenString.Length);
if (-1 != closeParen)
{
bindName = source.Substring(BindPathOpenString.Length, closeParen - BindPathOpenString.Length);
path = source.Substring(BindPathOpenString.Length + bindName.Length + 1); // +1 for the closing brace.
path = path.TrimStart('\\'); // remove starting '\\' char so the path doesn't look rooted.
}
}
else if (source.StartsWith("SourceDir\\", StringComparison.Ordinal) || source.StartsWith("SourceDir/", StringComparison.Ordinal))
{
pathWithoutSourceDir = path.Substring(10);
}
var bindPaths = this.BindPaths[bindStage];
foreach (var bindPath in bindPaths)
{
if (String.IsNullOrEmpty(bindName))
{
if (String.IsNullOrEmpty(bindPath.Name))
{
if (!String.IsNullOrEmpty(pathWithoutSourceDir))
{
var filePath = Path.Combine(bindPath.Path, pathWithoutSourceDir);
checkedPaths.Add(filePath);
if (CheckFileExists(filePath))
{
resolved = filePath;
}
}
if (String.IsNullOrEmpty(resolved))
{
var filePath = Path.Combine(bindPath.Path, path);
checkedPaths.Add(filePath);
if (CheckFileExists(filePath))
{
resolved = filePath;
}
}
}
}
else if (bindName.Equals(bindPath.Name, StringComparison.OrdinalIgnoreCase))
{
var filePath = Path.Combine(bindPath.Path, path);
checkedPaths.Add(filePath);
if (CheckFileExists(filePath))
{
resolved = filePath;
}
}
if (!String.IsNullOrEmpty(resolved))
{
break;
}
}
}
if (null == resolved)
{
throw new WixException(ErrorMessages.FileNotFound(sourceLineNumbers, source, symbolDefinition.Name, checkedPaths));
}
return resolved;
}
private static bool CheckFileExists(string path)
{
try
{
return File.Exists(path);
}
catch (ArgumentException)
{
throw new WixException(ErrorMessages.IllegalCharactersInPath(path));
}
}
}
}
|