Skip to content

Commit

Permalink
Fix non-test hook code (#3789)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcPMS committed Oct 19, 2023
1 parent e781da4 commit b96dbdb
Showing 1 changed file with 55 additions and 53 deletions.
108 changes: 55 additions & 53 deletions src/AppInstallerCLICore/Workflows/ShellExecuteInstallerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,45 +202,6 @@ namespace AppInstaller::CLI::Workflow

return args;
}

std::filesystem::path GetDismExecutablePath()
{
return AppInstaller::Filesystem::GetExpandedPath("%windir%\\system32\\dism.exe");
}

std::optional<DWORD> DoesWindowsFeatureExist(Execution::Context& context, std::string_view featureName)
{
std::string args = "/Online /Get-FeatureInfo /FeatureName:" + std::string{ featureName };
auto dismExecPath = GetDismExecutablePath();

auto getFeatureInfoResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
dismExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));

return getFeatureInfoResult;
}

std::optional<DWORD> EnableWindowsFeature(Execution::Context& context, std::string_view featureName)
{
std::string args = "/Online /Enable-Feature /NoRestart /FeatureName:" + std::string{ featureName };
auto dismExecPath = GetDismExecutablePath();

AICLI_LOG(Core, Info, << "Enabling Windows Feature [" << featureName << "]");

auto enableFeatureResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
dismExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));

return enableFeatureResult;
}
}

void ShellExecuteInstallImpl(Execution::Context& context)
Expand Down Expand Up @@ -370,18 +331,65 @@ namespace AppInstaller::CLI::Workflow
}
#endif

void ShellExecuteEnableWindowsFeature::operator()(Execution::Context& context) const
std::filesystem::path GetDismExecutablePath()
{
Utility::LocIndView locIndFeatureName{ m_featureName };
return AppInstaller::Filesystem::GetExpandedPath("%windir%\\system32\\dism.exe");
}

std::optional<DWORD> DoesWindowsFeatureExist(Execution::Context& context, std::string_view featureName)
{
#ifndef AICLI_DISABLE_TEST_HOOKS
auto doesFeatureExistResult = s_DoesWindowsFeatureExistResult_Override.has_value() ?
s_DoesWindowsFeatureExistResult_Override.value() :
DoesWindowsFeatureExist(context, m_featureName);
#else
auto doesFeatureExistResult = DoesWindowsFeatureExist(context, featureName);
if (s_DoesWindowsFeatureExistResult_Override)
{
return s_DoesWindowsFeatureExistResult_Override;
}
#endif

std::string args = "/Online /Get-FeatureInfo /FeatureName:" + std::string{ featureName };
auto dismExecPath = GetDismExecutablePath();

auto getFeatureInfoResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
dismExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));

return getFeatureInfoResult;
}

std::optional<DWORD> EnableWindowsFeature(Execution::Context& context, std::string_view featureName)
{
#ifndef AICLI_DISABLE_TEST_HOOKS
if (s_EnableWindowsFeatureResult_Override)
{
return s_EnableWindowsFeatureResult_Override;
}
#endif

std::string args = "/Online /Enable-Feature /NoRestart /FeatureName:" + std::string{ featureName };
auto dismExecPath = GetDismExecutablePath();

AICLI_LOG(Core, Info, << "Enabling Windows Feature [" << featureName << "]");

auto enableFeatureResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
dismExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));

return enableFeatureResult;
}

void ShellExecuteEnableWindowsFeature::operator()(Execution::Context& context) const
{
Utility::LocIndView locIndFeatureName{ m_featureName };

std::optional<DWORD> doesFeatureExistResult = DoesWindowsFeatureExist(context, m_featureName);

if (!doesFeatureExistResult)
{
AICLI_TERMINATE_CONTEXT(E_ABORT);
Expand All @@ -394,13 +402,7 @@ namespace AppInstaller::CLI::Workflow

context.Reporter.Info() << Resource::String::EnablingWindowsFeature(locIndFeatureName) << std::endl;

#ifndef AICLI_DISABLE_TEST_HOOKS
auto enableFeatureResult = s_EnableWindowsFeatureResult_Override.has_value() ?
s_EnableWindowsFeatureResult_Override.value() :
EnableWindowsFeature(context, m_featureName);
#else
auto enableFeatureResult = EnableWindowsFeature(context, featureName);
#endif
std::optional<DWORD> enableFeatureResult = EnableWindowsFeature(context, m_featureName);

if (!enableFeatureResult)
{
Expand Down

0 comments on commit b96dbdb

Please sign in to comment.