runner: implement automatic MSIX package uninstallation on startup (#1920)

This commit is contained in:
Andrey Nekrasov 2020-04-03 19:51:28 +03:00 committed by GitHub
parent 38d537bb8a
commit 7cfe74b384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 7 deletions

View File

@ -27,8 +27,3 @@ VersionHelper::VersionHelper(int major, int minor, int revision) :
revision(revision) revision(revision)
{ {
} }
bool VersionHelper::operator>(const VersionHelper& rhs)
{
return std::tie(major, minor, revision) > std::tie(rhs.major, rhs.minor, rhs.revision);
}

View File

@ -1,13 +1,14 @@
#pragma once #pragma once
#include <string> #include <string>
#include <compare>
struct VersionHelper struct VersionHelper
{ {
VersionHelper(std::string str); VersionHelper(std::string str);
VersionHelper(int major, int minor, int revision); VersionHelper(int major, int minor, int revision);
bool operator>(const VersionHelper& rhs); auto operator<=>(const VersionHelper&) const = default;
int major; int major;
int minor; int minor;

View File

@ -14,6 +14,7 @@
#include <winrt/Windows.Web.Http.h> #include <winrt/Windows.Web.Http.h>
#include <winrt/Windows.Web.Http.Headers.h> #include <winrt/Windows.Web.Http.Headers.h>
#include <winrt/Windows.Management.Deployment.h>
#include "VersionHelper.h" #include "VersionHelper.h"
@ -23,6 +24,8 @@ namespace
const wchar_t* DONT_SHOW_AGAIN_RECORD_REGISTRY_PATH = L"delete_previous_powertoys_confirm"; const wchar_t* DONT_SHOW_AGAIN_RECORD_REGISTRY_PATH = L"delete_previous_powertoys_confirm";
const wchar_t* USER_AGENT = L"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; const wchar_t* USER_AGENT = L"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
const wchar_t* LATEST_RELEASE_ENDPOINT = L"https://api.github.com/repos/microsoft/PowerToys/releases/latest"; const wchar_t* LATEST_RELEASE_ENDPOINT = L"https://api.github.com/repos/microsoft/PowerToys/releases/latest";
const wchar_t* MSIX_PACKAGE_NAME = L"Microsoft.PowerToys";
const wchar_t* MSIX_PACKAGE_PUBLISHER = L"CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US";
} }
namespace localized_strings namespace localized_strings
@ -126,3 +129,29 @@ std::future<std::optional<new_version_download_info>> check_for_new_github_relea
co_return std::nullopt; co_return std::nullopt;
} }
} }
std::future<bool> uninstall_previous_msix_version_async()
{
winrt::Windows::Management::Deployment::PackageManager package_manager;
try
{
auto packages = package_manager.FindPackagesForUser({}, MSIX_PACKAGE_NAME, MSIX_PACKAGE_PUBLISHER);
VersionHelper current_version(VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION);
for (auto package : packages)
{
VersionHelper msix_version(package.Id().Version().Major, package.Id().Version().Minor, package.Id().Version().Revision);
if (msix_version < current_version)
{
co_await package_manager.RemovePackageAsync(package.Id().FullName());
co_return true;
}
}
}
catch (...)
{
}
co_return false;
}

View File

@ -10,6 +10,8 @@ std::wstring get_msi_package_path();
bool uninstall_msi_version(const std::wstring& package_path); bool uninstall_msi_version(const std::wstring& package_path);
bool offer_msi_uninstallation(); bool offer_msi_uninstallation();
std::future<bool> uninstall_previous_msix_version_async();
struct new_version_download_info struct new_version_download_info
{ {
winrt::Windows::Foundation::Uri release_page_uri; winrt::Windows::Foundation::Uri release_page_uri;

View File

@ -32,6 +32,8 @@ extern "C" IMAGE_DOS_HEADER __ImageBase;
namespace localized_strings namespace localized_strings
{ {
const wchar_t MSI_VERSION_IS_ALREADY_RUNNING[] = L"An older version of PowerToys is already running."; const wchar_t MSI_VERSION_IS_ALREADY_RUNNING[] = L"An older version of PowerToys is already running.";
const wchar_t OLDER_MSIX_UNINSTALLED[] = L"An older MSIX version of PowerToys was uninstalled.";
const wchar_t GITHUB_NEW_VERSION_AVAILABLE_OFFER_VISIT[] = L"An update to PowerToys is available. Visit our GitHub page to get "; const wchar_t GITHUB_NEW_VERSION_AVAILABLE_OFFER_VISIT[] = L"An update to PowerToys is available. Visit our GitHub page to get ";
const wchar_t GITHUB_NEW_VERSION_AGREE[] = L"Visit"; const wchar_t GITHUB_NEW_VERSION_AGREE[] = L"Visit";
} }
@ -180,6 +182,16 @@ int runner(bool isProcessElevated)
start_msi_uninstallation_sequence(); start_msi_uninstallation_sequence();
} }.detach(); } }.detach();
} }
else
{
std::thread{[] {
if(uninstall_previous_msix_version_async().get())
{
notifications::show_toast(localized_strings::OLDER_MSIX_UNINSTALLED);
}
}}.detach();
}
notifications::register_background_toast_handler(); notifications::register_background_toast_handler();