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

Code before:
            auto path = winrt::to_string(foundFolder.Path());
            path.append(FragmentsSubDirectory);
            const auto jsonFiles = _AccumulateJsonFilesInDirectory(til::u8u16(path));

            // Provide the package name as the source
            _AddOrModifyProfiles(jsonFiles, ext.Package().DisplayName().c_str());
        }
    }
}

// Method Description:
// - Helper function to apply json stubs in the local app data folder and the global program data folder
// Arguments:
// - The directory to find json files in
// - The set of ignored namespaces
void CascadiaSettings::_ApplyJsonStubsHelper(const std::wstring_view directory, const std::unordered_set<std::wstring>& ignoredNamespaces)
{
    const std::filesystem::path root{ wil::ExpandEnvironmentStringsW<std::wstring>(directory.data()) };

    // The json files should be within subdirectories where the subdirectory name is the app name
    for (auto& protoExtFolder : std::filesystem::directory_iterator(root))
    {
        auto folderPath = protoExtFolder.path().generic_string();

        // We only want the parent folder name as the source (not the full path)
        auto source = til::u8u16(folderPath.substr(folderPath.find_last_of("/") + 1));

        // Only apply the stubs if the parent folder name is not in ignored namespaces
        // (also make sure this is a directory for sanity)
        if (std::filesystem::is_directory(protoExtFolder) && ignoredNamespaces.find(source) == ignoredNamespaces.end())
        {
            const auto jsonFiles = _AccumulateJsonFilesInDirectory(protoExtFolder.path().c_str());
            _AddOrModifyProfiles(jsonFiles, winrt::hstring{ source });
        }
    }
}

// Method Description:
// - Finds all the json files within the given directory
// Arguments:
// - directory: the directory to search
// Return Value:
// - A set containing all the found file data
std::unordered_set<std::string> CascadiaSettings::_AccumulateJsonFilesInDirectory(const std::wstring_view directory)
{
    std::unordered_set<std::string> jsonFiles;

    // Expand out environment strings like %LOCALAPPDATA% and %ProgramData%
    const std::filesystem::path root{ wil::ExpandEnvironmentStringsW<std::wstring>(directory.data()) };

    for (auto& protoExt : std::filesystem::directory_iterator(root))

Review comment (suggestion):
I feel like `std::filesystem` should have a provision for finding the pieces of the path without searching it manually as a string. Or at a minimum, it should expose a constant for "/" as the path separator without having to define it again here.

Code after:
    // Search through app extensions
    // Gets the catalog of extensions with the name "com.microsoft.windows.terminal.settings"
    const auto catalog = Windows::ApplicationModel::AppExtensions::AppExtensionCatalog::Open(winrt::to_hstring(AppExtensionHostName));

    auto extensions = _extractValueFromTaskWithoutMainThreadAwait(catalog.FindAllAsync());

    for (const auto& ext : extensions)
    {
        // Only apply the stubs if the package name is not in ignored namespaces
        if (ignoredNamespaces.find(ext.Package().Id().FamilyName().c_str()) == ignoredNamespaces.end())
        {
            // Likewise, getting the public folder from an extension is an async operation
            // So we use another mutex and condition variable
            auto foundFolder = _extractValueFromTaskWithoutMainThreadAwait(ext.GetPublicFolderAsync());

            // the StorageFolder class has its own methods for obtaining the files within the folder
            // however, all those methods are Async methods
            // you may have noticed that we need to resort to clunky implementations for async operations
            // (they are in _extractValueFromTaskWithoutMainThreadAwait)
            // so for now we will just take the folder path and access the files that way
            auto path = winrt::to_string(foundFolder.Path());
            path.append(FragmentsSubDirectory);

            // If the directory exists, use the fragments in it
            if (std::filesystem::exists(path))
            {
                const auto jsonFiles = _AccumulateJsonFilesInDirectory(til::u8u16(path));

                // Provide the package name as the source
                _ParseAndLayerFragmentFiles(jsonFiles, ext.Package().Id().FamilyName().c_str());
            }
        }
    }
}

// Method Description:
// - Helper function to apply json stubs in the local app data folder and the global program data folder
// Arguments:
// - The directory to find json files in
// - The set of ignored namespaces
void CascadiaSettings::_ApplyJsonStubsHelper(const std::wstring_view directory, const std::unordered_set<std::wstring>& ignoredNamespaces)
{
    // The json files should be within subdirectories where the subdirectory name is the app name
    for (const auto& fragmentExtFolder : std::filesystem::directory_iterator(directory))
    {
        // We only want the parent folder name as the source (not the full path)
        const auto source = fragmentExtFolder.path().filename().wstring();

        // Only apply the stubs if the parent folder name is not in ignored namespaces
        // (also make sure this is a directory for sanity)
        if (std::filesystem::is_directory(fragmentExtFolder) && ignoredNamespaces.find(source) == ignoredNamespaces.end())