mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
refactor(updating): use WinRT async types
This commit is contained in:
@@ -57,12 +57,7 @@ std::optional<fs::path> ObtainInstaller(bool& isUpToDate)
|
||||
|
||||
auto state = UpdateState::read();
|
||||
|
||||
#if USE_STD_EXPECTED
|
||||
std::expected<github_version_info, std::wstring> new_version_info;
|
||||
#else
|
||||
nonstd::expected<github_version_info, std::wstring> new_version_info;
|
||||
#endif
|
||||
get_github_version_info_async(false, new_version_info).get();
|
||||
const auto new_version_info = get_github_version_info_async();
|
||||
if (std::holds_alternative<version_up_to_date>(*new_version_info))
|
||||
{
|
||||
isUpToDate = true;
|
||||
@@ -81,8 +76,7 @@ std::optional<fs::path> ObtainInstaller(bool& isUpToDate)
|
||||
// Cleanup old updates before downloading the latest
|
||||
updating::cleanup_updates();
|
||||
|
||||
std::optional<fs::path> downloaded_installer;
|
||||
download_new_version(std::get<new_version_download_info>(*new_version_info), downloaded_installer).get();
|
||||
auto downloaded_installer = download_new_version(std::get<new_version_download_info>(*new_version_info));
|
||||
if (!downloaded_installer)
|
||||
{
|
||||
Logger::error("Couldn't download new installer");
|
||||
|
||||
@@ -83,20 +83,18 @@ namespace updating
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4702)
|
||||
#if USE_STD_EXPECTED
|
||||
winrt::Windows::Foundation::IAsyncAction get_github_version_info_async(const bool prerelease, std::expected<github_version_info, std::wstring>& result)
|
||||
std::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease)
|
||||
#else
|
||||
winrt::Windows::Foundation::IAsyncAction get_github_version_info_async(const bool prerelease, nonstd::expected<github_version_info, std::wstring>& result)
|
||||
nonstd::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease)
|
||||
#endif
|
||||
{
|
||||
// If the current version starts with 0.0.*, it means we're on a local build from a farm and shouldn't check for updates.
|
||||
if constexpr (VERSION_MAJOR == 0 && VERSION_MINOR == 0)
|
||||
{
|
||||
#if USE_STD_EXPECTED
|
||||
result = std::unexpected(LOCAL_BUILD_ERROR);
|
||||
co_return;
|
||||
return std::unexpected(LOCAL_BUILD_ERROR);
|
||||
#else
|
||||
result = nonstd::make_unexpected(LOCAL_BUILD_ERROR);
|
||||
co_return;
|
||||
return nonstd::make_unexpected(LOCAL_BUILD_ERROR);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -109,7 +107,7 @@ namespace updating
|
||||
|
||||
if (prerelease)
|
||||
{
|
||||
const auto body = co_await client.request(Uri{ ALL_RELEASES_ENDPOINT });
|
||||
const auto body = client.request(Uri{ ALL_RELEASES_ENDPOINT }).get();
|
||||
for (const auto& json : json::JsonValue::Parse(body).GetArray())
|
||||
{
|
||||
auto potential_release_object = json.GetObjectW();
|
||||
@@ -127,7 +125,7 @@ namespace updating
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto body = co_await client.request(Uri{ LATEST_RELEASE_ENDPOINT });
|
||||
const auto body = client.request(Uri{ LATEST_RELEASE_ENDPOINT }).get();
|
||||
release_object = json::JsonValue::Parse(body).GetObjectW();
|
||||
if (auto extracted_version = extract_version_from_release_object(release_object))
|
||||
{
|
||||
@@ -137,26 +135,23 @@ namespace updating
|
||||
|
||||
if (github_version <= current_version)
|
||||
{
|
||||
result = version_up_to_date{};
|
||||
co_return;
|
||||
return version_up_to_date{};
|
||||
}
|
||||
|
||||
auto [installer_download_url, installer_filename] = extract_installer_asset_download_info(release_object);
|
||||
result = new_version_download_info{ extract_release_page_url(release_object),
|
||||
return new_version_download_info{ extract_release_page_url(release_object),
|
||||
std::move(github_version),
|
||||
std::move(installer_download_url),
|
||||
std::move(installer_filename) };
|
||||
co_return;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
#if USE_STD_EXPECTED
|
||||
result = std::unexpected(NETWORK_ERROR);
|
||||
return std::unexpected(NETWORK_ERROR);
|
||||
#else
|
||||
result = nonstd::make_unexpected(NETWORK_ERROR);
|
||||
return nonstd::make_unexpected(NETWORK_ERROR);
|
||||
#endif
|
||||
co_return;
|
||||
}
|
||||
#pragma warning(pop)
|
||||
|
||||
@@ -175,13 +170,12 @@ namespace updating
|
||||
return !ec ? std::optional{ installer_download_path } : std::nullopt;
|
||||
}
|
||||
|
||||
winrt::Windows::Foundation::IAsyncAction download_new_version(const new_version_download_info& new_version, std::optional<std::filesystem::path>& result)
|
||||
std::optional<std::filesystem::path> download_new_version(const new_version_download_info& new_version)
|
||||
{
|
||||
auto installer_download_path = create_download_path();
|
||||
if (!installer_download_path)
|
||||
{
|
||||
result = std::nullopt;
|
||||
co_return;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
*installer_download_path /= new_version.installer_filename;
|
||||
@@ -192,7 +186,7 @@ namespace updating
|
||||
try
|
||||
{
|
||||
http::HttpClient client;
|
||||
co_await client.download(new_version.installer_download_url, *installer_download_path);
|
||||
client.download(new_version.installer_download_url, *installer_download_path).get();
|
||||
download_success = true;
|
||||
break;
|
||||
}
|
||||
@@ -201,8 +195,7 @@ namespace updating
|
||||
// reattempt to download or do nothing
|
||||
}
|
||||
}
|
||||
result = download_success ? installer_download_path : std::nullopt;
|
||||
co_return;
|
||||
return download_success ? installer_download_path : std::nullopt;
|
||||
}
|
||||
|
||||
void cleanup_updates()
|
||||
|
||||
@@ -31,12 +31,12 @@ namespace updating
|
||||
};
|
||||
using github_version_info = std::variant<new_version_download_info, version_up_to_date>;
|
||||
|
||||
winrt::Windows::Foundation::IAsyncAction download_new_version(const new_version_download_info& new_version, std::optional<std::filesystem::path>& result);
|
||||
std::optional<std::filesystem::path> download_new_version(const new_version_download_info& new_version);
|
||||
std::filesystem::path get_pending_updates_path();
|
||||
#if USE_STD_EXPECTED
|
||||
winrt::Windows::Foundation::IAsyncAction get_github_version_info_async(const bool prerelease, std::expected<github_version_info, std::wstring>& result);
|
||||
std::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease = false);
|
||||
#else
|
||||
winrt::Windows::Foundation::IAsyncAction get_github_version_info_async(const bool prerelease, nonstd::expected<github_version_info, std::wstring>& result);
|
||||
nonstd::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease = false);
|
||||
#endif
|
||||
void cleanup_updates();
|
||||
|
||||
|
||||
@@ -173,9 +173,7 @@ void ProcessNewVersionInfo(const github_version_info& version_info,
|
||||
// Cleanup old updates before downloading the latest
|
||||
updating::cleanup_updates();
|
||||
|
||||
std::optional<std::filesystem::path> downloaded_installer;
|
||||
download_new_version(new_version_info, downloaded_installer).get();
|
||||
if (downloaded_installer)
|
||||
if (download_new_version(new_version_info))
|
||||
{
|
||||
state.state = UpdateState::readyToInstall;
|
||||
state.downloadedInstallerFilename = new_version_info.installer_filename;
|
||||
@@ -234,12 +232,7 @@ void PeriodicUpdateWorker()
|
||||
bool version_info_obtained = false;
|
||||
try
|
||||
{
|
||||
#if USE_STD_EXPECTED
|
||||
std::expected<github_version_info, std::wstring> new_version_info;
|
||||
#else
|
||||
nonstd::expected<github_version_info, std::wstring> new_version_info;
|
||||
#endif
|
||||
get_github_version_info_async(false, new_version_info).get();
|
||||
const auto new_version_info = get_github_version_info_async();
|
||||
if (new_version_info.has_value())
|
||||
{
|
||||
version_info_obtained = true;
|
||||
@@ -279,12 +272,7 @@ void CheckForUpdatesCallback()
|
||||
auto state = UpdateState::read();
|
||||
try
|
||||
{
|
||||
#if USE_STD_EXPECTED
|
||||
std::expected<github_version_info, std::wstring> new_version_info;
|
||||
#else
|
||||
nonstd::expected<github_version_info, std::wstring> new_version_info;
|
||||
#endif
|
||||
get_github_version_info_async(false, new_version_info).get();
|
||||
auto new_version_info = get_github_version_info_async();
|
||||
if (!new_version_info)
|
||||
{
|
||||
// We couldn't get a new version from github for some reason, log error
|
||||
|
||||
Reference in New Issue
Block a user