-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathFileLocator.cs
More file actions
268 lines (230 loc) · 11.8 KB
/
FileLocator.cs
File metadata and controls
268 lines (230 loc) · 11.8 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (c) Gustave Monce and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnifiedUpdatePlatform.Services.Composition.Database;
using UnifiedUpdatePlatform.Services.Composition.Database.Applications;
#nullable enable
namespace UnifiedUpdatePlatform.Media.Creator
{
internal static class FileLocator
{
internal static (bool, HashSet<string>) VerifyFilesAreAvailableForCompositionDatabases(HashSet<CompDB> CompositionDatabases, string UUPPath)
{
HashSet<string> missingPackages = new();
foreach (CompDB compDB in CompositionDatabases)
{
(bool succeeded, HashSet<string> missingFiles) = Planning.FileLocator.VerifyFilesAreAvailableForCompDB(compDB, UUPPath);
foreach (string? missingFile in missingFiles)
{
if (!missingPackages.Contains(missingFile))
{
_ = missingPackages.Add(missingFile);
}
}
}
return (missingPackages.Count == 0, missingPackages);
}
internal static CompDB? GetEditionCompDBForLanguage(
IEnumerable<CompDB> CompositionDatabases,
string Edition,
string LanguageCode)
{
foreach (CompDB? compDB in CompositionDatabases)
{
//
// Newer style compdbs have a tag attribute, make use of it.
//
if (compDB.Tags != null)
{
if (compDB.Tags.Type.Equals("Edition", StringComparison.InvariantCultureIgnoreCase) &&
compDB.Tags.Tag?.Count == 3 &&
compDB.Tags.Tag.Find(x => x.Name.Equals("UpdateType", StringComparison.InvariantCultureIgnoreCase))?.Value?.Equals("Canonical", StringComparison.InvariantCultureIgnoreCase) == true &&
compDB.Tags.Tag.Find(x => x.Name.Equals("Language", StringComparison.InvariantCultureIgnoreCase))?.Value?.Equals(LanguageCode, StringComparison.InvariantCultureIgnoreCase) == true &&
compDB.Tags.Tag.Find(x => x.Name.Equals("Edition", StringComparison.InvariantCultureIgnoreCase))?.Value?.Equals(Edition, StringComparison.InvariantCultureIgnoreCase) == true &&
compDB.Name?.EndsWith("~Desktop_Apps~~") != true &&
compDB.Name?.EndsWith("~Desktop_Apps_Moment~~") != true)
{
return compDB;
}
}
//
// Older style compdbs have no tag elements, we need to find out if it's an edition compdb using another way
// TODO: Do not do contains
//
else if (compDB.Features?.Feature?.FirstOrDefault(x =>
x.Type?.Contains("DesktopMedia", StringComparison.InvariantCultureIgnoreCase) == true &&
x.FeatureID?.Contains(LanguageCode, StringComparison.InvariantCultureIgnoreCase) == true &&
x.FeatureID?.Contains(Edition, StringComparison.InvariantCultureIgnoreCase) == true) != null &&
compDB.Name?.EndsWith("~Desktop_Apps~~") != true &&
compDB.Name?.EndsWith("~Desktop_Apps_Moment~~") != true)
{
return compDB;
}
}
return null;
}
internal static (bool Succeeded, string BaseESD) LocateFilesForSetupMediaCreation(
string UUPPath,
string LanguageCode,
IEnumerable<CompDB> CompositionDatabases,
ProgressCallback? progressCallback = null,
string edition = null)
{
progressCallback?.Invoke(Common.Messaging.Common.ProcessPhase.ReadingMetadata, true, 0, "Looking up Composition Database in order to find a Base ESD image appropriate for building windows setup files.");
if (edition != null)
{
CompDB? compDB = GetEditionCompDBForLanguage(CompositionDatabases, edition, LanguageCode);
if (compDB != null)
{
foreach (Package feature in compDB.Features.Feature[0].Packages.Package)
{
Package pkg = compDB.Packages.Package.First(x => x.ID == feature.ID);
string file = pkg.GetCommonlyUsedIncorrectFileName();
if (feature.PackageType == "MetadataESD")
{
if (!File.Exists(Path.Combine(UUPPath, file)))
{
file = pkg.Payload.PayloadItem[0].Path.Replace('\\', Path.DirectorySeparatorChar);
if (!File.Exists(Path.Combine(UUPPath, file)))
{
break;
}
}
return (true, Path.Combine(UUPPath, file));
}
}
}
}
HashSet<CompDB> filteredCompositionDatabases = CompositionDatabases.GetEditionCompDBsForLanguage(LanguageCode);
if (filteredCompositionDatabases.Count > 0)
{
foreach (CompDB? currentCompDB in filteredCompositionDatabases)
{
foreach (Package feature in currentCompDB.Features.Feature[0].Packages.Package)
{
Package pkg = currentCompDB.Packages.Package.First(x => x.ID == feature.ID);
string file = pkg.GetCommonlyUsedIncorrectFileName();
if (feature.PackageType == "MetadataESD")
{
if (!File.Exists(Path.Combine(UUPPath, file)))
{
file = pkg.Payload.PayloadItem[0].Path.Replace('\\', Path.DirectorySeparatorChar);
if (!File.Exists(Path.Combine(UUPPath, file)))
{
break;
}
}
return (true, Path.Combine(UUPPath, file));
}
}
}
}
progressCallback?.Invoke(Common.Messaging.Common.ProcessPhase.Error, true, 0, "While looking up the Composition Database, we couldn't find an edition composition for the specified language. This error is fatal.");
return (false, null);
}
internal static bool GenerateAppXLicenseFiles(
string UUPPath,
string LanguageCode,
string EditionID,
IEnumerable<CompDB> CompositionDatabases,
ProgressCallback? progressCallback = null)
{
bool success = true;
progressCallback?.Invoke(Common.Messaging.Common.ProcessPhase.ReadingMetadata, true, 0, "Enumerating files");
CompDB? compDB = GetEditionCompDBForLanguage(CompositionDatabases, EditionID, LanguageCode);
if (compDB == null)
{
progressCallback?.Invoke(Common.Messaging.Common.ProcessPhase.ReadingMetadata, true, 0, "No compDB found");
goto error;
}
if (CompositionDatabases.Any(x => x.Name?.StartsWith("Build~") == true && (x.Name?.EndsWith("~Desktop_Apps~~") == true || x.Name?.EndsWith("~Desktop_Apps_Moment~~") == true)))
{
IEnumerable<CompDB> AppCompDBs = CompositionDatabases.Where(x => x.Name?.StartsWith("Build~") == true && (x.Name?.EndsWith("~Desktop_Apps~~") == true || x.Name?.EndsWith("~Desktop_Apps_Moment~~") == true));
AppxSelectionEngine.GenerateLicenseXmlFiles(compDB, AppCompDBs, UUPPath);
}
goto exit;
error:
success = false;
exit:
return success;
}
internal static (bool Succeeded, string BaseESD, HashSet<string> ReferencePackages, HashSet<string> ReferencePackagesToConvert) LocateFilesForBaseEditionCreation(
string UUPPath,
string LanguageCode,
string EditionID,
IEnumerable<CompDB> CompositionDatabases,
ProgressCallback? progressCallback = null)
{
bool success = true;
HashSet<string> ReferencePackages = new();
HashSet<string> referencePackagesToConvert = new();
string? BaseESD = null;
progressCallback?.Invoke(Common.Messaging.Common.ProcessPhase.ReadingMetadata, true, 0, "Enumerating files");
CompDB? compDB = GetEditionCompDBForLanguage(CompositionDatabases, EditionID, LanguageCode);
if (compDB == null)
{
progressCallback?.Invoke(Common.Messaging.Common.ProcessPhase.ReadingMetadata, true, 0, "No compDB found");
goto error;
}
foreach (Package feature in compDB.Features.Feature[0].Packages.Package)
{
Package pkg = compDB.Packages.Package.First(x => x.ID == feature.ID);
string file = pkg.GetCommonlyUsedIncorrectFileName();
if (!File.Exists(Path.Combine(UUPPath, file)))
{
file = pkg.Payload.PayloadItem[0].Path.Replace('\\', Path.DirectorySeparatorChar);
if (!File.Exists(Path.Combine(UUPPath, file)))
{
progressCallback?.Invoke(Common.Messaging.Common.ProcessPhase.ReadingMetadata, true, 0, $"File {file} is missing");
goto error;
}
}
if (feature.PackageType == "MetadataESD")
{
BaseESD = Path.Combine(UUPPath, file);
continue;
}
if (file.EndsWith(".esd", StringComparison.InvariantCultureIgnoreCase))
{
_ = ReferencePackages.Add(Path.Combine(UUPPath, file));
}
else if (file.EndsWith(".cab", StringComparison.InvariantCultureIgnoreCase))
{
_ = referencePackagesToConvert.Add(Path.Combine(UUPPath, file));
}
}
if (BaseESD == null)
{
progressCallback?.Invoke(Common.Messaging.Common.ProcessPhase.ReadingMetadata, true, 0, "Base ESD not found");
goto error;
}
goto exit;
error:
success = false;
exit:
return (success, BaseESD, ReferencePackages, referencePackagesToConvert);
}
}
}