Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/VsSetupConfiguration.cpp (C++)

Code before:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "pch.h"
#include "VsSetupConfiguration.h"

using namespace winrt::Microsoft::Terminal::Settings::Model;

VsSetupConfiguration::VsSetupInstance::VsSetupInstance(ComPtrSetupQuery pQuery, ComPtrSetupInstance pInstance) :
    query(pQuery), // Copy and AddRef the query object.
    inst(std::move(pInstance)), // Take ownership of the instance object.
    installDate(GetInstallDate()),
    version(GetInstallationVersion())
{
    ComPtrCatalogPropertyStore catalogProperties = GetCatalogPropertyStore();
    ComPtrCustomPropertyStore customProperties = GetCustomPropertyStore();
    ComPtrPropertyStore instanceProperties = GetInstancePropertyStore();

    const auto nickname = customProperties ? GetNickname(customProperties.get()) : std::wstring{};
    auto channelName = instanceProperties ? GetChannelName(instanceProperties.get()) : std::wstring{};

    if (channelName == L"Release")
    {
        channelName.clear();
    }

    isRelease = channelName.empty();

    if (catalogProperties)
    {
        profileNameSuffix.append(GetProductLineVersion(catalogProperties.get()));

        if (!nickname.empty())
        {
            profileNameSuffix.append(L" (");
            profileNameSuffix.append(nickname);
            profileNameSuffix.append(L")");
        }

        if (!channelName.empty())
        {
            profileNameSuffix.append(L" [");
            profileNameSuffix.append(channelName);
            profileNameSuffix.append(L"]");
        }
    }
    else

Review comment (suggestion):
This is not proper detection for prereleases. It's only an assumption. Use `ISetupInstanceCatalog::IsPrerelease()` instead: https://github.com/microsoft/vswhere/blob/ded0fdd04473772af1dd001d82b6e3c871731788/src/vswhere.lib/InstanceSelector.cpp#L188

Code after:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "pch.h"
#include "VsSetupConfiguration.h"

using namespace winrt::Microsoft::Terminal::Settings::Model;

std::vector<VsSetupConfiguration::VsSetupInstance> VsSetupConfiguration::QueryInstances()
{
    std::vector<VsSetupInstance> instances;

    // SetupConfiguration is only registered if Visual Studio is installed
    ComPtrSetupQuery pQuery{ wil::CoCreateInstanceNoThrow<SetupConfiguration, ISetupConfiguration2>() };
    if (pQuery == nullptr)
    {
        return instances;
    }

    // Enumerate all valid instances of Visual Studio
    wil::com_ptr<IEnumSetupInstances> e;
    THROW_IF_FAILED(pQuery->EnumInstances(&e));

    for (ComPtrSetupInstance rgpInstance; S_OK == THROW_IF_FAILED(e->Next(1, &rgpInstance, nullptr));)
    {
        instances.emplace_back(pQuery, std::move(rgpInstance));
    }

    // Sort instances based on version and install date from latest to oldest.
    std::sort(instances.begin(), instances.end(), [](const VsSetupInstance& a, const VsSetupInstance& b) {
        auto const aVersion = a.GetComparableVersion();
        auto const bVersion = b.GetComparableVersion();

        if (aVersion == bVersion)
        {
            return a.GetComparableInstallDate() > b.GetComparableInstallDate();
        }

        return aVersion > bVersion;
    });

    return instances;
}

/// <summary>
/// Takes a relative path under a Visual Studio installation and returns the absolute path.
/// </summary>