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
|
// 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.BuildTasks
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using WixToolset.Dtf.WindowsInstaller;
using Microsoft.Win32;
/// <summary>
/// This task assigns Culture metadata to files based on the value of the Culture attribute on the
/// WixLocalization element inside the file.
/// </summary>
public class GetLooseFileList : Task
{
private ITaskItem database;
private ITaskItem[] looseFileList;
internal const int MsidbFileAttributesNoncompressed = 8192;
internal const int MsidbFileAttributesCompressed = 16384;
/// <summary>
/// The list of database files to find Loose Files in
/// </summary>
[Required]
public ITaskItem Database
{
get { return this.database; }
set { this.database = value; }
}
/// <summary>
/// The total list of Loose Files in this database
/// </summary>
[Output]
public ITaskItem[] LooseFileList
{
get { return this.looseFileList; }
}
/// <summary>
/// Takes the "defaultDir" column
/// </summary>
/// <returns>Returns the corresponding sourceDir.</returns>
public string SourceDirFromDefaultDir(string defaultDir)
{
string sourceDir;
string[] splitted = defaultDir.Split(':');
if (1 == splitted.Length)
{
sourceDir = splitted[0];
}
else
{
sourceDir = splitted[1];
}
splitted = sourceDir.Split('|');
if (1 == splitted.Length)
{
sourceDir = splitted[0];
}
else
{
sourceDir = splitted[1];
}
return sourceDir;
}
/// <summary>
/// Takes the "FileName" column
/// </summary>
/// <returns>Returns the corresponding source file name.</returns>
public string SourceFileFromFileName(string fileName)
{
string sourceFile;
string[] splitted = fileName.Split('|');
if (1 == splitted.Length)
{
sourceFile = splitted[0];
}
else
{
sourceFile = splitted[1];
}
return sourceFile;
}
/// <summary>
/// Gets a complete list of external Loose Files referenced by the given installer database file.
/// </summary>
/// <returns>True upon completion of the task execution.</returns>
public override bool Execute()
{
string databaseFile = this.database.ItemSpec;
Object []emptyArgs = { };
System.Collections.Generic.List<ITaskItem> looseFileNames = new System.Collections.Generic.List<ITaskItem>();
Dictionary<string, string> ComponentFullDirectory = new Dictionary<string, string>();
Dictionary<string, string> DirectoryIdDefaultDir = new Dictionary<string, string>();
Dictionary<string, string> DirectoryIdParent = new Dictionary<string, string>();
Dictionary<string, string> DirectoryIdFullSource = new Dictionary<string, string>();
int i;
string databaseDir = Path.GetDirectoryName(databaseFile);
// If the file doesn't exist, no Loose Files to return, so exit now
if (!File.Exists(databaseFile))
{
return true;
}
using (Database database = new Database(databaseFile))
{
bool compressed = false;
if (2 == (database.SummaryInfo.WordCount & 2))
{
compressed = true;
}
// If the media table doesn't exist, no Loose Files to return, so exit now
if (null == database.Tables["File"])
{
return true;
}
// Only setup all these helpful indexes if the database is marked as uncompressed. If it's marked as compressed, files are stored at the root,
// so none of these indexes will be used
if (!compressed)
{
if (null == database.Tables["Directory"] || null == database.Tables["Component"])
{
return true;
}
System.Collections.IList directoryRecords = database.ExecuteQuery("SELECT `Directory`,`Directory_Parent`,`DefaultDir` FROM `Directory`", emptyArgs);
// First setup a simple index from DirectoryId to DefaultDir
for (i = 0; i < directoryRecords.Count; i += 3)
{
string directoryId = (string)(directoryRecords[i]);
string directoryParent = (string)(directoryRecords[i + 1]);
string defaultDir = (string)(directoryRecords[i + 2]);
string sourceDir = SourceDirFromDefaultDir(defaultDir);
DirectoryIdDefaultDir[directoryId] = sourceDir;
DirectoryIdParent[directoryId] = directoryParent;
}
// Setup an index from directory Id to the full source path
for (i = 0; i < directoryRecords.Count; i += 3)
{
string directoryId = (string)(directoryRecords[i]);
string directoryParent = (string)(directoryRecords[i + 1]);
string defaultDir = (string)(directoryRecords[i + 2]);
string sourceDir = DirectoryIdDefaultDir[directoryId];
// The TARGETDIR case
if (String.IsNullOrEmpty(directoryParent))
{
DirectoryIdFullSource[directoryId] = databaseDir;
}
else
{
string tempDirectoryParent = directoryParent;
while (!String.IsNullOrEmpty(tempDirectoryParent) && !String.IsNullOrEmpty(DirectoryIdParent[tempDirectoryParent]))
{
sourceDir = Path.Combine(DirectoryIdDefaultDir[tempDirectoryParent], sourceDir);
tempDirectoryParent = DirectoryIdParent[tempDirectoryParent];
}
DirectoryIdFullSource[directoryId] = Path.Combine(databaseDir, sourceDir);
}
}
// Setup an index from component Id to full directory path
System.Collections.IList componentRecords = database.ExecuteQuery("SELECT `Component`,`Directory_` FROM `Component`", emptyArgs);
for (i = 0; i < componentRecords.Count; i += 2)
{
string componentId = (string)(componentRecords[i]);
string componentDir = (string)(componentRecords[i + 1]);
ComponentFullDirectory[componentId] = DirectoryIdFullSource[componentDir];
}
}
System.Collections.IList fileRecords = database.ExecuteQuery("SELECT `Component_`,`FileName`,`Attributes` FROM `File`", emptyArgs);
for (i = 0; i < fileRecords.Count; i += 3)
{
string componentId = (string)(fileRecords[i]);
string fileName = SourceFileFromFileName((string)(fileRecords[i + 1]));
int attributes = (int)(fileRecords[i + 2]);
// If the whole database is marked uncompressed, use the directory layout made above
if ((!compressed && MsidbFileAttributesCompressed != (attributes & MsidbFileAttributesCompressed)))
{
looseFileNames.Add(new TaskItem(Path.GetFullPath(Path.Combine(ComponentFullDirectory[componentId], fileName))));
}
// If the database is marked as compressed, put files at the root
else if (compressed && (MsidbFileAttributesNoncompressed == (attributes & MsidbFileAttributesNoncompressed)))
{
looseFileNames.Add(new TaskItem(Path.GetFullPath(Path.Combine(databaseDir, fileName))));
}
}
}
this.looseFileList = looseFileNames.ToArray();
return true;
}
}
}
|