Repository: microsoft/terminal
File: src/cascadia/WinRTUtils/inc/WtExeUtils.h (C/C++)

Code before:

#pragma once

constexpr std::wstring_view WtExe{ L"wt.exe" };
constexpr std::wstring_view WtdExe{ L"wtd.exe" };
constexpr std::wstring_view WindowsTerminalExe{ L"WindowsTerminal.exe" };
constexpr std::wstring_view LocalAppDataAppsPath{ L"%LOCALAPPDATA%\\Microsoft\\WindowsApps\\" };
constexpr std::wstring_view ElevateShimExe{ L"elevate-shim.exe" };

// Forward declared from appmodel.h so that we don't need to pull in that header everywhere.
extern "C" {
WINBASEAPI LONG WINAPI GetCurrentPackageId(UINT32* bufferLength, BYTE* buffer);
}

#ifdef WINRT_Windows_ApplicationModel_H
_TIL_INLINEPREFIX bool IsPackaged()
{
    static const auto isPackaged = []() {
        UINT32 bufferLength = 0;
        const auto hr = GetCurrentPackageId(&bufferLength, nullptr);
        return hr != APPMODEL_ERROR_NO_PACKAGE;
    }();
    return isPackaged;
}

_TIL_INLINEPREFIX uint8_t GetBranding()
{
#if defined(WT_BRANDING_RELEASE)
    return 3;
#elif defined(WT_BRANDING_PREVIEW)
    return 2;
#elif defined(WT_BRANDING_CANARY)
    return 1;
#else
    return 0;
#endif
}

// Function Description:
// - This is a helper to determine if we're running as a part of the Dev Build
//   Package or the release package. We'll need to return different text, icons,
//   and use different commandlines depending on which one the user requested.
// - Uses a C++11 "magic static" to make sure this is only computed once.
// - If we can't determine if it's the dev build or not, we'll default to true
// Arguments:
// - <none>
// Return Value:
// - true if we believe this extension is being run in the dev build package.
_TIL_INLINEPREFIX bool IsDevBuild()
{
    // use C++11 magic statics to make sure we only do this once.

Review comment (suggestion):
FWIW, I would much rather we not expose this broadly across the project -- especially with a generic name like this. **Nobody** should be calling this at runtime to make decisions based on branding, because that violates the rule that all branding-specific code should be compiled out when that branding isn't set.

Code after:

#pragma once

constexpr std::wstring_view WtExe{ L"wt.exe" };
constexpr std::wstring_view WtdExe{ L"wtd.exe" };
constexpr std::wstring_view WindowsTerminalExe{ L"WindowsTerminal.exe" };
constexpr std::wstring_view LocalAppDataAppsPath{ L"%LOCALAPPDATA%\\Microsoft\\WindowsApps\\" };
constexpr std::wstring_view ElevateShimExe{ L"elevate-shim.exe" };

// Forward declared from appmodel.h so that we don't need to pull in that header everywhere.
extern "C" {
WINBASEAPI LONG WINAPI GetCurrentPackageId(UINT32* bufferLength, BYTE* buffer);
}

#ifdef WINRT_Windows_ApplicationModel_H
_TIL_INLINEPREFIX bool IsPackaged()
{
    static const auto isPackaged = []() {
        UINT32 bufferLength = 0;
        const auto hr = GetCurrentPackageId(&bufferLength, nullptr);
        return hr != APPMODEL_ERROR_NO_PACKAGE;
    }();
    return isPackaged;
}

// Function Description:
// - This is a helper to determine if we're running as a part of the Dev Build
//   Package or the release package. We'll need to return different text, icons,
//   and use different commandlines depending on which one the user requested.
// - Uses a C++11 "magic static" to make sure this is only computed once.
// - If we can't determine if it's the dev build or not, we'll default to true
// Arguments:
// - <none>
// Return Value:
// - true if we believe this extension is being run in the dev build package.
_TIL_INLINEPREFIX bool IsDevBuild()
{
    // use C++11 magic statics to make sure we only do this once.
    static const auto isDevBuild = []() -> bool {
        if (IsPackaged())
        {
            try
            {
                const auto package = winrt::Windows::ApplicationModel::Package::Current();
                const auto id = package.Id();
                const auto name = id.FullName();
                return til::starts_with(name, L"WindowsTerminalDev");
            }
            CATCH_LOG();
        }