Skip to content

Commit

Permalink
Only show latest VS prompts by default
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed Sep 24, 2021
1 parent c070be1 commit 50822aa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
27 changes: 25 additions & 2 deletions src/cascadia/TerminalSettingsModel/BaseVisualStudioGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ void BaseVisualStudioGenerator::GenerateProfiles(std::vector<winrt::com_ptr<impl
{
// There's no point in enumerating valid Visual Studio instances more than once,
// so cache them for use by both Visual Studio profile generators.
static const auto instances = VsSetupConfiguration::QueryInstances();
static auto instances = VsSetupConfiguration::QueryInstances();

for (auto const& instance : instances)
// Sort instances based on version and install date.
std::sort(instances.begin(), instances.end(), BaseVisualStudioGenerator::Compare);

// Iterate through instances from newest to oldest.
bool latest = true;
for (auto it = instances.rbegin(); it != instances.rend(); it++)
{
auto const& instance = *it;

try
{
if (!IsInstanceValid(instance))
Expand All @@ -29,8 +36,24 @@ void BaseVisualStudioGenerator::GenerateProfiles(std::vector<winrt::com_ptr<impl
profile->Commandline(winrt::hstring{ GetProfileCommandLine(instance) });
profile->StartingDirectory(winrt::hstring{ instance.GetInstallationPath() });
profile->Icon(winrt::hstring{ GetProfileIconPath() });
profile->Hidden(!latest);
profiles.emplace_back(std::move(profile));

latest = false;
}
CATCH_LOG();
}
}

bool BaseVisualStudioGenerator::Compare(const VsSetupConfiguration::VsSetupInstance& a, const VsSetupConfiguration::VsSetupInstance& b)
{
auto const aVersion = a.GetComparableVersion();
auto const bVersion = b.GetComparableVersion();

if (aVersion == bVersion)
{
return a.GetComparableInstallDate() < b.GetComparableInstallDate();
}

return aVersion < bVersion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model
virtual std::wstring GetProfileCommandLine(const VsSetupConfiguration::VsSetupInstance& instance) const = 0;
virtual std::wstring GetProfileGuidSeed(const VsSetupConfiguration::VsSetupInstance& instance) const = 0;
virtual std::wstring GetProfileIconPath() const = 0;

private:
static bool Compare(const VsSetupConfiguration::VsSetupInstance& a, const VsSetupConfiguration::VsSetupInstance& b);
};
};
7 changes: 7 additions & 0 deletions src/cascadia/TerminalSettingsModel/VsSetupConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ std::wstring VsSetupConfiguration::GetInstanceId(ISetupInstance* pInst)
return bstrInstanceId.get();
}

unsigned long long VsSetupConfiguration::GetInstallDate(ISetupInstance* pInst)
{
FILETIME ftInstallDate{ 0 };
THROW_IF_FAILED(pInst->GetInstallDate(&ftInstallDate));
return wil::filetime::to_int64(ftInstallDate);
}

std::wstring VsSetupConfiguration::GetStringProperty(ISetupPropertyStore* pProps, std::wstring_view name)
{
if (pProps == nullptr)
Expand Down
41 changes: 39 additions & 2 deletions src/cascadia/TerminalSettingsModel/VsSetupConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model
public:
struct VsSetupInstance
{
VsSetupInstance(VsSetupInstance&&) = default;

std::wstring ResolvePath(std::wstring_view relativePath) const
{
return VsSetupConfiguration::ResolvePath(inst.get(), relativePath);
Expand Down Expand Up @@ -65,7 +67,17 @@ namespace winrt::Microsoft::Terminal::Settings::Model

std::wstring GetVersion() const
{
return GetInstallationVersion(inst.get());
return VsSetupConfiguration::GetInstallationVersion(inst.get());
}

unsigned long long GetComparableInstallDate() const
{
return installDate;
}

unsigned long long GetComparableVersion() const
{
return version;
}

std::wstring GetInstallationPath() const
Expand Down Expand Up @@ -116,13 +128,17 @@ namespace winrt::Microsoft::Terminal::Settings::Model
return profileNameSuffix;
}

VsSetupInstance& operator=(VsSetupInstance&&) = default;

private:
friend class VsSetupConfiguration;

VsSetupInstance(ComPtrSetupQuery pQuery, ComPtrSetupInstance pInstance) :
query(std::move(pQuery)),
inst(std::move(pInstance)),
profileNameSuffix(BuildProfileNameSuffix())
profileNameSuffix(BuildProfileNameSuffix()),
installDate(GetInstallDate()),
version(GetInstallationVersion())
{
}

Expand All @@ -131,6 +147,10 @@ namespace winrt::Microsoft::Terminal::Settings::Model

std::wstring profileNameSuffix;

// Cache oft-accessed properties used in sorting.
unsigned long long installDate;
unsigned long long version;

std::wstring BuildProfileNameSuffix() const
{
ComPtrCatalogPropertyStore catalogProperties = GetCatalogPropertyStore();
Expand Down Expand Up @@ -167,6 +187,22 @@ namespace winrt::Microsoft::Terminal::Settings::Model
return GetVersion();
}

unsigned long long GetInstallDate() const
{
return VsSetupConfiguration::GetInstallDate(inst.get());
}

unsigned long long GetInstallationVersion() const
{
const auto helper = wil::com_query<ISetupHelper>(query);

std::wstring versionString{ GetVersion() };
unsigned long long version{ 0 };

THROW_IF_FAILED(helper->ParseVersion(versionString.data(), &version));
return version;
}

static std::wstring GetChannelNameSuffixTag(ISetupPropertyStore* instanceProperties)
{
std::wstring tag;
Expand Down Expand Up @@ -229,6 +265,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model
static std::wstring GetInstallationVersion(ISetupInstance* pInst);
static std::wstring GetInstallationPath(ISetupInstance* pInst);
static std::wstring GetInstanceId(ISetupInstance* pInst);
static unsigned long long GetInstallDate(ISetupInstance* pInst);
static std::wstring GetStringProperty(ISetupPropertyStore* pProps, std::wstring_view name);
};
};

0 comments on commit 50822aa

Please sign in to comment.