forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontFlow.cpp
More file actions
340 lines (285 loc) · 14.1 KB
/
Copy pathFontFlow.cpp
File metadata and controls
340 lines (285 loc) · 14.1 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "FontFlow.h"
#include "TableOutput.h"
#include <winget/Fonts.h>
#include <AppInstallerRuntime.h>
using namespace AppInstaller::Utility;
namespace AppInstaller::CLI::Workflow
{
using namespace AppInstaller::CLI::Execution;
using namespace AppInstaller::Fonts;
namespace
{
struct InstalledFontFamiliesTableLine
{
InstalledFontFamiliesTableLine(Utility::LocIndString familyName, int faceCount)
: FamilyName(familyName), FaceCount(faceCount) {
}
Utility::LocIndString FamilyName;
int FaceCount;
};
struct InstalledFontFacesTableLine
{
InstalledFontFacesTableLine(Utility::LocIndString familyName, Utility::LocIndString faceName, Utility::LocIndString faceVersion, std::filesystem::path filePath)
: FamilyName(familyName), FaceName(faceName), FaceVersion(faceVersion), FilePath(filePath) {
}
Utility::LocIndString FamilyName;
Utility::LocIndString FaceName;
Utility::LocIndString FaceVersion;
std::filesystem::path FilePath;
};
struct InstalledFontFilesTableLine
{
InstalledFontFilesTableLine(Utility::LocIndString title, Utility::LocIndString packageName, Resource::LocString fontStatus, std::filesystem::path filePath)
: Title(title), PackageName(packageName), FontStatus(fontStatus), FilePath(filePath) {
}
Utility::LocIndString Title;
Utility::LocIndString PackageName;
Resource::LocString FontStatus;
std::filesystem::path FilePath;
};
void OutputInstalledFontFamiliesTable(Execution::Context& context, const std::vector<InstalledFontFamiliesTableLine>& lines)
{
Execution::TableOutput<2> table(context.Reporter, { Resource::String::FontFamily, Resource::String::FontFaces });
for (auto line : lines)
{
table.OutputLine({ line.FamilyName, std::to_string(line.FaceCount) });
}
table.Complete();
}
void OutputInstalledFontFacesTable(Execution::Context& context, const std::vector<InstalledFontFacesTableLine>& lines)
{
Execution::TableOutput<4> table(context.Reporter, { Resource::String::FontFamily, Resource::String::FontFace, Resource::String::FontVersion, Resource::String::FontFilePaths });
bool anonymizePath = Settings::User().Get<Settings::Setting::AnonymizePathForDisplay>();
for (auto line : lines)
{
if (anonymizePath)
{
AppInstaller::Runtime::ReplaceProfilePathsWithEnvironmentVariable(line.FilePath);
}
table.OutputLine({ line.FamilyName, line.FaceName, line.FaceVersion, line.FilePath.u8string() });
}
table.Complete();
}
void OutputInstalledFontFilesTable(Execution::Context& context, const std::vector<InstalledFontFilesTableLine>& lines)
{
Execution::TableOutput<4> table(context.Reporter, { Resource::String::FontTitle, Resource::String::FontPackage, Resource::String::FontStatus, Resource::String::FontFilePaths });
bool anonymizePath = Settings::User().Get<Settings::Setting::AnonymizePathForDisplay>();
for (auto line : lines)
{
if (anonymizePath)
{
AppInstaller::Runtime::ReplaceProfilePathsWithEnvironmentVariable(line.FilePath);
}
table.OutputLine({ line.Title, line.PackageName, line.FontStatus, line.FilePath.u8string() });
}
table.Complete();
}
}
void ReportInstalledFonts(Execution::Context& context)
{
Fonts::FontCatalog fontCatalog;
if (context.Args.Contains(Args::Type::Family))
{
// TODO: Create custom source and search mechanism for fonts.
const auto& familyNameArg = AppInstaller::Utility::ConvertToUTF16(context.Args.GetArg(Args::Type::Family));
const auto& fontFamilies = fontCatalog.GetInstalledFontFamilies(familyNameArg);
if (fontFamilies.empty())
{
context.Reporter.Info() << Resource::String::NoInstalledFontFound << std::endl;
return;
}
std::vector<InstalledFontFacesTableLine> lines;
for (const auto& fontFamily : fontFamilies)
{
const auto& familyName = Utility::LocIndString(Utility::ConvertToUTF8(fontFamily.Name));
for (const auto& fontFace : fontFamily.Faces)
{
for (const auto& filePath : fontFace.FilePaths)
{
InstalledFontFacesTableLine line(
familyName,
Utility::LocIndString(Utility::ToLower(Utility::ConvertToUTF8(fontFace.Name))),
Utility::LocIndString(fontFace.Version.ToString()),
filePath.u8string());
lines.push_back(std::move(line));
}
}
}
OutputInstalledFontFacesTable(context, lines);
}
else if (context.Args.Contains(Args::Type::Details))
{
const auto& fontFiles = AppInstaller::Fonts::GetInstalledFontFiles();
std::vector<InstalledFontFilesTableLine> lines;
for (const auto& fontFile : fontFiles)
{
Resource::LocString status;
switch (fontFile.Status)
{
case FontStatus::OK:
status = Resource::LocString(Resource::String::FontStatusOK);
break;
case FontStatus::Corrupt:
status = Resource::LocString(Resource::String::FontStatusCorrupt);
break;
default:
status = Resource::LocString(Resource::String::FontStatusUnknown);
break;
}
InstalledFontFilesTableLine line(
Utility::LocIndString(Utility::ConvertToUTF8(fontFile.Title)),
Utility::LocIndString(Utility::ConvertToUTF8(fontFile.PackageIdentifier)),
status,
fontFile.FilePath.u8string());
lines.push_back(std::move(line));
}
OutputInstalledFontFilesTable(context, lines);
}
else
{
const auto& fontFamilies = fontCatalog.GetInstalledFontFamilies();
std::vector<InstalledFontFamiliesTableLine> lines;
for (const auto& fontFamily : fontFamilies)
{
InstalledFontFamiliesTableLine line(
Utility::LocIndString(Utility::ConvertToUTF8(fontFamily.Name)),
static_cast<int>(fontFamily.Faces.size())
);
lines.push_back(std::move(line));
}
OutputInstalledFontFamiliesTable(context, lines);
}
}
void FontInstallImpl(Execution::Context& context)
{
context.Reporter.Info() << Resource::String::InstallFlowStartingPackageInstall << std::endl;
// We will default to User scope.
Manifest::ScopeEnum scope = Manifest::ScopeEnum::User;
if (context.Args.Contains(Execution::Args::Type::InstallScope))
{
scope = Manifest::ConvertToScopeEnum(context.Args.GetArg(Execution::Args::Type::InstallScope));
}
Fonts::FontContext fontContext;
fontContext.InstallerSource = InstallerSource::WinGet;
fontContext.Scope = scope;
auto& manifest = context.Get<Execution::Data::Manifest>();
fontContext.PackageId = ConvertToUTF16(manifest.Id);
fontContext.PackageVersion = ConvertToUTF16(manifest.Version);
if (context.Args.Contains(Execution::Args::Type::Force))
{
fontContext.Force = true;
}
try
{
const auto& installerPath = context.Get<Execution::Data::InstallerPath>();
// InstallerPath will point to a directory if extracted from an archive.
if (std::filesystem::is_directory(installerPath))
{
const std::vector<Manifest::NestedInstallerFile>& nestedInstallerFiles = context.Get<Execution::Data::Installer>()->NestedInstallerFiles;
for (const auto& nestedInstallerFile : nestedInstallerFiles)
{
fontContext.AddPackageFile(installerPath / ConvertToUTF16(nestedInstallerFile.RelativeFilePath));
}
}
else
{
fontContext.AddPackageFile(installerPath);
}
const auto& fontValidationResult = Fonts::ValidateFontPackage(fontContext);
if (fontValidationResult.Result != FontResult::Success)
{
context.Reporter.Error() << Resource::String::FontValidationFailed << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_FONT_VALIDATION_FAILED);
}
if (fontValidationResult.HasUnsupportedFonts)
{
context.Reporter.Error() << Resource::String::FontFileNotSupported << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_FONT_FILE_NOT_SUPPORTED);
}
if (fontValidationResult.Status == FontStatus::OK && !fontContext.Force)
{
context.Reporter.Warn() << Resource::String::FontAlreadyInstalled << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_FONT_ALREADY_INSTALLED);
}
auto installResult = Fonts::InstallFontPackage(fontContext);
if (installResult.Result() != FontResult::Success)
{
context.Reporter.Error() << Resource::String::FontInstallFailed << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_FONT_INSTALL_FAILED);
}
context.Add<Execution::Data::CorrelatedAppsAndFeaturesEntries>({ fontContext.GetAppsAndFeaturesEntry() });
context.Add<Execution::Data::OperationReturnCode>(installResult.HResult);
}
catch (...)
{
context.Add<Execution::Data::OperationReturnCode>(Workflow::HandleException(context, std::current_exception()));
context.Reporter.Warn() << Resource::String::FontInstallFailed << std::endl;
try
{
// The Font Install code handles rollback where appropriate. If we hit an
// unexpected exception, try to uninstall anyway to arrive at a consistent
// absent state. Since we install side-by-side for versions, this should
// only result in an absent state of an installed font if this were a forced
// install of an existing version.
auto uninstallResult = Fonts::UninstallFontPackage(fontContext);
if (uninstallResult.Result() != FontResult::Success)
{
context.Reporter.Warn() << Resource::String::FontRollbackFailed << std::endl;
}
}
CATCH_LOG();
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_FONT_INSTALL_FAILED);
}
}
void FontUninstallImpl(Execution::Context& context)
{
context.Reporter.Info() << Resource::String::UninstallFlowStartingPackageUninstall << std::endl;
try
{
// We will default to installed scope.
auto scope = Manifest::ConvertToScopeEnum(context.Get<Execution::Data::InstalledPackageVersion>()->GetMetadata()[Repository::PackageVersionMetadata::InstalledScope]);
if (context.Args.Contains(Execution::Args::Type::InstallScope))
{
scope = Manifest::ConvertToScopeEnum(context.Args.GetArg(Execution::Args::Type::InstallScope));
}
Fonts::FontContext fontContext;
fontContext.InstallerSource = InstallerSource::WinGet;
if (context.Args.Contains(Execution::Args::Type::Manifest))
{
const auto& manifest = context.Get<Execution::Data::Manifest>();
fontContext.PackageId = ConvertToUTF16(manifest.Id);
fontContext.PackageVersion = ConvertToUTF16(manifest.Version);
fontContext.Scope = scope;
}
else
{
const std::string moniker = context.Get<Execution::Data::InstalledPackageVersion>()->GetProperty(AppInstaller::Repository::PackageVersionProperty::Moniker);
const std::string version = context.Get<Execution::Data::InstalledPackageVersion>()->GetProperty(AppInstaller::Repository::PackageVersionProperty::Version);
fontContext.PackageId = ConvertToUTF16(moniker);
fontContext.PackageVersion = ConvertToUTF16(version);
fontContext.Scope = scope;
}
if (fontContext.Scope == Manifest::ScopeEnum::Machine && !Runtime::IsRunningAsAdmin())
{
context.Reporter.Error() << Resource::String::CommandRequiresAdmin << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_COMMAND_REQUIRES_ADMIN);
}
auto uninstallResult = Fonts::UninstallFontPackage(fontContext);
if (uninstallResult.Result() != FontResult::Success)
{
context.Reporter.Error() << Resource::String::FontUninstallFailed << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_FONT_UNINSTALL_FAILED);
}
context.Add<Execution::Data::OperationReturnCode>(uninstallResult.HResult);
}
catch (...)
{
context.Add<Execution::Data::OperationReturnCode>(Workflow::HandleException(context, std::current_exception()));
context.Reporter.Error() << Resource::String::FontUninstallFailed << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_FONT_UNINSTALL_FAILED);
}
}
}