forked from microsoft/winget-cli
/
UpgradeCommand.cpp
214 lines (198 loc) · 9.32 KB
/
UpgradeCommand.cpp
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "UpgradeCommand.h"
#include "Workflows/CompletionFlow.h"
#include "Workflows/InstallFlow.h"
#include "Workflows/MultiQueryFlow.h"
#include "Workflows/UpdateFlow.h"
#include "Workflows/WorkflowBase.h"
#include "Workflows/DependenciesFlow.h"
#include "Resources.h"
#include <winget/LocIndependent.h>
namespace AppInstaller::CLI
{
using namespace AppInstaller::CLI::Execution;
using namespace AppInstaller::Manifest;
using namespace AppInstaller::CLI::Workflow;
using namespace AppInstaller::Utility::literals;
namespace
{
// Determines whether we should list available upgrades, instead
// of performing an upgrade
bool ShouldListUpgrade(const Execution::Args& args, ArgTypeCategory argCategories = ArgTypeCategory::None)
{
if (argCategories == ArgTypeCategory::None)
{
argCategories = Argument::GetCategoriesPresent(args);
}
// Valid arguments for list are only those related to the sources and which packages to include (e.g. --include-unknown).
// Instead of checking for them, we check that there aren't any other arguments present.
return !args.Contains(Args::Type::All) &&
WI_AreAllFlagsClear(argCategories, ArgTypeCategory::Manifest | ArgTypeCategory::PackageQuery | ArgTypeCategory::InstallerBehavior);
}
}
std::vector<Argument> UpgradeCommand::GetArguments() const
{
return {
Argument::ForType(Args::Type::MultiQuery), // -q
Argument::ForType(Args::Type::Manifest), // -m
Argument::ForType(Args::Type::Id),
Argument::ForType(Args::Type::Name),
Argument::ForType(Args::Type::Moniker),
Argument::ForType(Args::Type::Version), // -v
Argument::ForType(Args::Type::Channel),
Argument::ForType(Args::Type::Source), // -s
Argument::ForType(Args::Type::Exact), // -e
Argument::ForType(Args::Type::Interactive), // -i
Argument::ForType(Args::Type::Silent), // -h
Argument::ForType(Args::Type::Purge),
Argument::ForType(Args::Type::Log), // -o
Argument::ForType(Args::Type::CustomSwitches),
Argument::ForType(Args::Type::Override),
Argument::ForType(Args::Type::InstallLocation), // -l
Argument{ Execution::Args::Type::InstallScope, Resource::String::InstalledScopeArgumentDescription, ArgumentType::Standard, Argument::Visibility::Help },
Argument::ForType(Args::Type::InstallArchitecture), // -a
Argument::ForType(Args::Type::InstallerType),
Argument::ForType(Args::Type::Locale),
Argument::ForType(Args::Type::HashOverride),
Argument::ForType(Args::Type::SkipDependencies),
Argument::ForType(Args::Type::IgnoreLocalArchiveMalwareScan),
Argument::ForType(Args::Type::AcceptPackageAgreements),
Argument::ForType(Args::Type::AcceptSourceAgreements),
Argument::ForType(Execution::Args::Type::CustomHeader),
Argument{ Args::Type::All, Resource::String::UpdateAllArgumentDescription, ArgumentType::Flag },
Argument{ Args::Type::IncludeUnknown, Resource::String::IncludeUnknownArgumentDescription, ArgumentType::Flag },
Argument{ Args::Type::IncludePinned, Resource::String::IncludePinnedArgumentDescription, ArgumentType::Flag},
Argument::ForType(Args::Type::UninstallPrevious),
Argument::ForType(Args::Type::Force),
};
}
Resource::LocString UpgradeCommand::ShortDescription() const
{
return { Resource::String::UpgradeCommandShortDescription };
}
Resource::LocString UpgradeCommand::LongDescription() const
{
return { Resource::String::UpgradeCommandLongDescription };
}
void UpgradeCommand::Complete(Execution::Context& context, Execution::Args::Type valueType) const
{
if (valueType == Execution::Args::Type::Manifest ||
valueType == Execution::Args::Type::Log ||
valueType == Execution::Args::Type::Override ||
valueType == Execution::Args::Type::InstallLocation)
{
// Intentionally output nothing to allow pass through to filesystem.
return;
}
context <<
OpenSource() <<
OpenCompositeSource(Repository::PredefinedSource::Installed);
switch (valueType)
{
case Execution::Args::Type::MultiQuery:
context <<
RequireCompletionWordNonEmpty <<
SearchSourceForManyCompletion <<
CompleteWithMatchedField;
break;
case Execution::Args::Type::Id:
case Execution::Args::Type::Name:
case Execution::Args::Type::Moniker:
case Execution::Args::Type::Version:
case Execution::Args::Type::Channel:
case Execution::Args::Type::Source:
context <<
CompleteWithSingleSemanticsForValueUsingExistingSource(valueType);
break;
case Args::Type::InstallArchitecture:
case Args::Type::Locale:
// May well move to CompleteWithSingleSemanticsForValue,
// but for now output nothing.
context <<
Workflow::CompleteWithEmptySet;
break;
}
}
Utility::LocIndView UpgradeCommand::HelpLink() const
{
return "https://aka.ms/winget-command-upgrade"_liv;
}
void UpgradeCommand::ValidateArgumentsInternal(Execution::Args& execArgs) const
{
const auto argCategories = Argument::GetCategoriesAndValidateCommonArguments(execArgs, /* requirePackageSelectionArg */ false);
if (!ShouldListUpgrade(execArgs, argCategories) &&
WI_IsFlagClear(argCategories, ArgTypeCategory::PackageQuery) &&
WI_IsFlagSet(argCategories, ArgTypeCategory::SingleInstallerBehavior))
{
throw CommandException(Resource::String::InvalidArgumentWithoutQueryError);
}
}
void UpgradeCommand::ExecuteInternal(Execution::Context& context) const
{
context.SetFlags(Execution::ContextFlag::InstallerExecutionUseUpdate);
// Only allow for source failures when doing a list of available upgrades.
// We have to set it now to allow for source open failures to also just warn.
if (ShouldListUpgrade(context.Args))
{
context.SetFlags(Execution::ContextFlag::TreatSourceFailuresAsWarning);
}
context <<
Workflow::ReportExecutionStage(ExecutionStage::Discovery) <<
Workflow::OpenSource() <<
Workflow::OpenCompositeSource(Workflow::DetermineInstalledSource(context));
if (ShouldListUpgrade(context.Args))
{
// Upgrade with no args list packages with updates available
context <<
SearchSourceForMany <<
HandleSearchResultFailures <<
EnsureMatchesFromSearchResult(OperationType::Upgrade) <<
ReportListResult(true);
}
else if (context.Args.Contains(Execution::Args::Type::All))
{
// --all switch updates all packages found
context <<
SearchSourceForMany <<
HandleSearchResultFailures <<
EnsureMatchesFromSearchResult(OperationType::Upgrade) <<
ReportListResult(true) <<
UpdateAllApplicable;
}
else if (context.Args.Contains(Execution::Args::Type::Manifest))
{
// --manifest case where new manifest is provided
context <<
GetManifestFromArg <<
SearchSourceUsingManifest <<
EnsureOneMatchFromSearchResult(OperationType::Upgrade) <<
GetInstalledPackageVersion <<
EnsureUpdateVersionApplicable <<
SelectInstaller <<
EnsureApplicableInstaller <<
InstallSinglePackage;
}
else
{
// The remaining case: search for specific packages to update
if (!context.Args.Contains(Execution::Args::Type::MultiQuery))
{
context << Workflow::InstallOrUpgradeSinglePackage(OperationType::Upgrade);
}
else
{
bool skipDependencies = Settings::User().Get<Settings::Setting::InstallSkipDependencies>() || context.Args.Contains(Execution::Args::Type::SkipDependencies);
context <<
Workflow::GetMultiSearchRequests <<
Workflow::SearchSubContextsForSingle(OperationType::Upgrade) <<
Workflow::ReportExecutionStage(Workflow::ExecutionStage::Execution) <<
Workflow::ProcessMultiplePackages(
Resource::String::PackageRequiresDependencies,
APPINSTALLER_CLI_ERROR_MULTIPLE_INSTALL_FAILED,
{}, true, skipDependencies);
}
}
}
}