forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManifest.cpp
More file actions
243 lines (208 loc) · 8.31 KB
/
Copy pathManifest.cpp
File metadata and controls
243 lines (208 loc) · 8.31 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "winget/Manifest.h"
#include "winget/Locale.h"
#include "winget/UserSettings.h"
namespace AppInstaller::Manifest
{
namespace
{
void AddFoldedStringToSetIfNotEmpty(std::set<string_t>& set, const string_t& value)
{
if (!value.empty())
{
set.emplace(Utility::FoldCase(value));
}
}
}
void Manifest::ApplyLocale(const std::string& locale)
{
CurrentLocalization = DefaultLocalization;
// Get target locale from Preferred Languages settings if applicable
std::vector<std::string> targetLocales;
if (locale.empty())
{
targetLocales = Locale::GetUserPreferredLanguages();
}
else
{
targetLocales.emplace_back(locale);
}
for (auto const& targetLocale : targetLocales)
{
const ManifestLocalization* bestLocalization = nullptr;
double bestScore = Locale::GetDistanceOfLanguage(targetLocale, DefaultLocalization.Locale);
for (auto const& localization : Localizations)
{
double score = Locale::GetDistanceOfLanguage(targetLocale, localization.Locale);
if (score > bestScore)
{
bestLocalization = &localization;
bestScore = score;
}
}
// If there's better locale than default And is compatible with target locale, merge and return;
if (bestScore >= Locale::MinimumDistanceScoreAsCompatibleMatch)
{
if (bestLocalization != nullptr)
{
CurrentLocalization.ReplaceOrMergeWith(*bestLocalization);
}
break;
}
}
}
std::vector<string_t> Manifest::GetAggregatedTags() const
{
std::vector<string_t> resultTags = DefaultLocalization.Get<Localization::Tags>();
for (const auto& locale : Localizations)
{
auto tags = locale.Get<Localization::Tags>();
for (const auto& tag : tags)
{
if (std::find(resultTags.begin(), resultTags.end(), tag) == resultTags.end())
{
resultTags.emplace_back(tag);
}
}
}
return resultTags;
}
std::vector<string_t> Manifest::GetAggregatedCommands() const
{
std::vector<string_t> resultCommands;
for (const auto& installer : Installers)
{
for (const auto& command : installer.Commands)
{
if (std::find(resultCommands.begin(), resultCommands.end(), command) == resultCommands.end())
{
resultCommands.emplace_back(command);
}
}
}
return resultCommands;
}
Utility::VersionRange Manifest::GetArpVersionRange() const
{
bool arpVersionFound = false;
Utility::Version minVersion;
Utility::Version maxVersion;
for (auto const& installer : Installers)
{
if (DoesInstallerTypeSupportArpVersionRange(installer.EffectiveInstallerType()))
{
for (auto const& entry : installer.AppsAndFeaturesEntries)
{
if (!entry.DisplayVersion.empty())
{
Utility::Version arpVersion{ entry.DisplayVersion };
if (!arpVersionFound)
{
// This is the first arp version found, populate both min and max version
minVersion = arpVersion;
maxVersion = arpVersion;
arpVersionFound = true;
continue;
}
if (arpVersion < minVersion)
{
minVersion = arpVersion;
}
else if (arpVersion > maxVersion)
{
maxVersion = arpVersion;
}
}
}
}
}
return arpVersionFound ? Utility::VersionRange{ minVersion, maxVersion } : Utility::VersionRange{};
}
std::vector<string_t> Manifest::GetPackageFamilyNames() const
{
return GetSystemReferenceStrings(
[](const ManifestInstaller& i) -> const Utility::NormalizedString& { return i.PackageFamilyName; });
}
std::vector<string_t> Manifest::GetProductCodes() const
{
return GetSystemReferenceStrings(
[](const ManifestInstaller& i) -> const Utility::NormalizedString& { return i.ProductCode; },
[](const AppsAndFeaturesEntry& e) -> const Utility::NormalizedString& { return e.ProductCode; });
}
std::vector<string_t> Manifest::GetUpgradeCodes() const
{
return GetSystemReferenceStrings(
{},
[](const AppsAndFeaturesEntry& e) -> const Utility::NormalizedString& { return e.UpgradeCode; });
}
std::vector<string_t> Manifest::GetPackageNames() const
{
std::set<string_t> set;
AddFoldedStringToSetIfNotEmpty(set, DefaultLocalization.Get<Localization::PackageName>());
for (const auto& loc : Localizations)
{
AddFoldedStringToSetIfNotEmpty(set, loc.Get<Localization::PackageName>());
}
// In addition to the names used for our display, add the display names from the ARP entries
for (const auto& installer : Installers)
{
for (const auto& appsAndFeaturesEntry : installer.AppsAndFeaturesEntries)
{
AddFoldedStringToSetIfNotEmpty(set, appsAndFeaturesEntry.DisplayName);
}
}
std::vector<Utility::NormalizedString> result(
std::make_move_iterator(set.begin()),
std::make_move_iterator(set.end()));
return result;
}
std::vector<string_t> Manifest::GetPublishers() const
{
std::set<string_t> set;
AddFoldedStringToSetIfNotEmpty(set, DefaultLocalization.Get<Localization::Publisher>());
for (const auto& loc : Localizations)
{
AddFoldedStringToSetIfNotEmpty(set, loc.Get<Localization::Publisher>());
}
// In addition to the publishers used for our display, add the publisher from the ARP entries
for (const auto& installer : Installers)
{
for (const auto& appsAndFeaturesEntry : installer.AppsAndFeaturesEntries)
{
AddFoldedStringToSetIfNotEmpty(set, appsAndFeaturesEntry.Publisher);
}
}
std::vector<Utility::NormalizedString> result(
std::make_move_iterator(set.begin()),
std::make_move_iterator(set.end()));
return result;
}
std::vector<string_t> Manifest::GetSystemReferenceStrings(
std::function<const string_t& (const ManifestInstaller&)> extractStringFromInstaller,
std::function<const string_t& (const AppsAndFeaturesEntry&)> extractStringFromAppsAndFeaturesEntry) const
{
std::set<string_t> set;
for (const auto& installer : Installers)
{
if (extractStringFromInstaller)
{
const auto& installerString = extractStringFromInstaller(installer);
AddFoldedStringToSetIfNotEmpty(set, installerString);
}
if (extractStringFromAppsAndFeaturesEntry)
{
for (const auto& entry : installer.AppsAndFeaturesEntries)
{
const auto& entryString = extractStringFromAppsAndFeaturesEntry(entry);
AddFoldedStringToSetIfNotEmpty(set, entryString);
}
}
}
std::vector<Utility::NormalizedString> result(
std::make_move_iterator(set.begin()),
std::make_move_iterator(set.end()));
return result;
}
}