<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OmniSharp.Eventing;
using OmniSharp.FileWatching;
using OmniSharp.Models.Events;
using OmniSharp.Models.WorkspaceInformation;
using OmniSharp.MSBuild.Models;
using OmniSharp.MSBuild.Models.Events;
using OmniSharp.MSBuild.ProjectFile;
using OmniSharp.MSBuild.Resolution;
using OmniSharp.MSBuild.SolutionParsing;
using OmniSharp.Options;
using OmniSharp.Services;

namespace OmniSharp.MSBuild
{
    [Export(typeof(IProjectSystem)), Shared]
    public class MSBuildProjectSystem : IProjectSystem
    {
        private readonly IOmniSharpEnvironment _environment;
        private readonly OmniSharpWorkspace _workspace;
        private readonly DotNetCliService _dotNetCli;
        private readonly MetadataFileReferenceCache _metadataFileReferenceCache;
        private readonly IEventEmitter _eventEmitter;
        private readonly IFileSystemWatcher _fileSystemWatcher;
        private readonly ILoggerFactory _loggerFactory;
        private readonly ILogger _logger;
        private readonly PackageDependencyResolver _packageDepedencyResolver;

        private readonly object _gate = new object();
        private readonly Queue<ProjectFileInfo> _projectsToProcess;
        private readonly ProjectFileInfoCollection _projects;

        private MSBuildOptions _options;
        private string _solutionFileOrRootPath;

        public string Key { get; } = "MsBuild";
        public string Language { get; } = LanguageNames.CSharp;
        public IEnumerable<string> Extensions { get; } = new[] { ".cs" };

        [ImportingConstructor]
        public MSBuildProjectSystem(
            IOmniSharpEnvironment environment,
            OmniSharpWorkspace workspace,
            DotNetCliService dotNetCliService,
            MetadataFileReferenceCache metadataFileReferenceCache,
            IEventEmitter eventEmitter,
            IFileSystemWatcher fileSystemWatcher,
            ILoggerFactory loggerFactory)
        {
            _environment = environment;
            _workspace = workspace;
            _dotNetCli = dotNetCliService;
            _metadataFileReferenceCache = metadataFileReferenceCache;
            _eventEmitter = eventEmitter;
            _fileSystemWatcher = fileSystemWatcher;
            _loggerFactory = loggerFactory;

            _projects = new ProjectFileInfoCollection();
            _projectsToProcess = new Queue<ProjectFileInfo>();
            _logger = loggerFactory.CreateLogger<MSBuildProjectSystem>();
            _packageDepedencyResolver = new PackageDependencyResolver(loggerFactory);
        }

        public void Initalize(IConfiguration configuration)
        {
            _options = new MSBuildOptions();
            ConfigurationBinder.Bind(configuration, _options);

            if (!MSBuildEnvironment.IsInitialized)
            {
                MSBuildEnvironment.Initialize(_logger);

                if (MSBuildEnvironment.IsInitialized &&
                    _environment.LogLevel < LogLevel.Information)
                {
                    var buildEnvironmentInfo = MSBuildHelpers.GetBuildEnvironmentInfo();
                    _logger.LogDebug($"MSBuild environment: {Environment.NewLine}{buildEnvironmentInfo}");
                }
            }

            var initialProjectPaths = GetInitialProjectPaths();

            foreach (var projectPath in initialProjectPaths)
            {
                if (!File.Exists(projectPath))
                {
                    _logger.LogWarning($"Found project that doesn't exist on disk: {projectPath}");
                    continue;
                }

                var project = LoadProject(projectPath);
                if (project == null)
                {
                    // Diagnostics reported while loading the project have already been logged.
                    continue;
                }

                _projectsToProcess.Enqueue(project);
            }

            ProcessProjects();
        }

        private IEnumerable<string> GetInitialProjectPaths()
        {
            // If a solution was provided, use it.
            if (!string.IsNullOrEmpty(_environment.SolutionFilePath))
            {
                _solutionFileOrRootPath = _environment.SolutionFilePath;
                return GetProjectPathsFromSolution(_environment.SolutionFilePath);
            }

            // Otherwise, assume that the path provided is a directory and look for a solution there.
            var solutionFilePath = FindSolutionFilePath(_environment.TargetDirectory, _logger);
            if (!string.IsNullOrEmpty(solutionFilePath))
            {
                _solutionFileOrRootPath = solutionFilePath;
                return GetProjectPathsFromSolution(solutionFilePath);
            }

            // Finally, if there isn't a single solution immediately available,
            // Just process all of the projects beneath the root path.
            _solutionFileOrRootPath = _environment.TargetDirectory;
            return Directory.GetFiles(_environment.TargetDirectory, "*.csproj", SearchOption.AllDirectories);
        }

        private IEnumerable<string> GetProjectPathsFromSolution(string solutionFilePath)
        {
            _logger.LogInformation($"Detecting projects in '{solutionFilePath}'.");

            var solutionFile = SolutionFile.Parse(solutionFilePath);
            var processedProjects = new HashSet<string>();
            var result = new List<string>();

            foreach (var project in solutionFile.ProjectBlocks)
            {
                if (project.Kind == ProjectKind.SolutionFolder)
                {
                    continue;
                }

                // Solution files are assumed to contain relative paths to project files with Windows-style slashes.
                var projectFilePath = project.ProjectPath.Replace('\\', Path.DirectorySeparatorChar);
                projectFilePath = Path.Combine(_environment.TargetDirectory, projectFilePath);
                projectFilePath = Path.GetFullPath(projectFilePath);

                // Have we seen this project? If so, move on.
                if (processedProjects.Contains(projectFilePath))
                {
                    continue;
                }

                if (project.Kind == ProjectKind.CSharpProject ||
                    Path.GetExtension(projectFilePath) == ".csproj")
                {
                    result.Add(projectFilePath);
                }

                processedProjects.Add(projectFilePath);
            }

            return result;
        }

        private void ProcessProjects()<commit_msg>Check with '.csproj' extension with case-insensitive match<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OmniSharp.Eventing;
using OmniSharp.FileWatching;
using OmniSharp.Models.Events;
using OmniSharp.Models.WorkspaceInformation;
using OmniSharp.MSBuild.Models;
using OmniSharp.MSBuild.Models.Events;
using OmniSharp.MSBuild.ProjectFile;
using OmniSharp.MSBuild.Resolution;
using OmniSharp.MSBuild.SolutionParsing;
using OmniSharp.Options;
using OmniSharp.Services;

namespace OmniSharp.MSBuild
{
    [Export(typeof(IProjectSystem)), Shared]
    public class MSBuildProjectSystem : IProjectSystem
    {
        private readonly IOmniSharpEnvironment _environment;
        private readonly OmniSharpWorkspace _workspace;
        private readonly DotNetCliService _dotNetCli;
        private readonly MetadataFileReferenceCache _metadataFileReferenceCache;
        private readonly IEventEmitter _eventEmitter;
        private readonly IFileSystemWatcher _fileSystemWatcher;
        private readonly ILoggerFactory _loggerFactory;
        private readonly ILogger _logger;
        private readonly PackageDependencyResolver _packageDepedencyResolver;

        private readonly object _gate = new object();
        private readonly Queue<ProjectFileInfo> _projectsToProcess;
        private readonly ProjectFileInfoCollection _projects;

        private MSBuildOptions _options;
        private string _solutionFileOrRootPath;

        public string Key { get; } = "MsBuild";
        public string Language { get; } = LanguageNames.CSharp;
        public IEnumerable<string> Extensions { get; } = new[] { ".cs" };

        [ImportingConstructor]
        public MSBuildProjectSystem(
            IOmniSharpEnvironment environment,
            OmniSharpWorkspace workspace,
            DotNetCliService dotNetCliService,
            MetadataFileReferenceCache metadataFileReferenceCache,
            IEventEmitter eventEmitter,
            IFileSystemWatcher fileSystemWatcher,
            ILoggerFactory loggerFactory)
        {
            _environment = environment;
            _workspace = workspace;
            _dotNetCli = dotNetCliService;
            _metadataFileReferenceCache = metadataFileReferenceCache;
            _eventEmitter = eventEmitter;
            _fileSystemWatcher = fileSystemWatcher;
            _loggerFactory = loggerFactory;

            _projects = new ProjectFileInfoCollection();
            _projectsToProcess = new Queue<ProjectFileInfo>();
            _logger = loggerFactory.CreateLogger<MSBuildProjectSystem>();
            _packageDepedencyResolver = new PackageDependencyResolver(loggerFactory);
        }

        public void Initalize(IConfiguration configuration)
        {
            _options = new MSBuildOptions();
            ConfigurationBinder.Bind(configuration, _options);

            if (!MSBuildEnvironment.IsInitialized)
            {
                MSBuildEnvironment.Initialize(_logger);

                if (MSBuildEnvironment.IsInitialized &&
                    _environment.LogLevel < LogLevel.Information)
                {
                    var buildEnvironmentInfo = MSBuildHelpers.GetBuildEnvironmentInfo();
                    _logger.LogDebug($"MSBuild environment: {Environment.NewLine}{buildEnvironmentInfo}");
                }
            }

            var initialProjectPaths = GetInitialProjectPaths();

            foreach (var projectPath in initialProjectPaths)
            {
                if (!File.Exists(projectPath))
                {
                    _logger.LogWarning($"Found project that doesn't exist on disk: {projectPath}");
                    continue;
                }

                var project = LoadProject(projectPath);
                if (project == null)
                {
                    // Diagnostics reported while loading the project have already been logged.
                    continue;
                }

                _projectsToProcess.Enqueue(project);
            }

            ProcessProjects();
        }

        private IEnumerable<string> GetInitialProjectPaths()
        {
            // If a solution was provided, use it.
            if (!string.IsNullOrEmpty(_environment.SolutionFilePath))
            {
                _solutionFileOrRootPath = _environment.SolutionFilePath;
                return GetProjectPathsFromSolution(_environment.SolutionFilePath);
            }

            // Otherwise, assume that the path provided is a directory and look for a solution there.
            var solutionFilePath = FindSolutionFilePath(_environment.TargetDirectory, _logger);
            if (!string.IsNullOrEmpty(solutionFilePath))
            {
                _solutionFileOrRootPath = solutionFilePath;
                return GetProjectPathsFromSolution(solutionFilePath);
            }

            // Finally, if there isn't a single solution immediately available,
            // Just process all of the projects beneath the root path.
            _solutionFileOrRootPath = _environment.TargetDirectory;
            return Directory.GetFiles(_environment.TargetDirectory, "*.csproj", SearchOption.AllDirectories);
        }

        private IEnumerable<string> GetProjectPathsFromSolution(string solutionFilePath)
        {
            _logger.LogInformation($"Detecting projects in '{solutionFilePath}'.");

            var solutionFile = SolutionFile.Parse(solutionFilePath);
            var processedProjects = new HashSet<string>();
            var result = new List<string>();

            foreach (var project in solutionFile.ProjectBlocks)
            {
                if (project.Kind == ProjectKind.SolutionFolder)
                {
                    continue;
                }

                // Solution files are assumed to contain relative paths to project files with Windows-style slashes.
                var projectFilePath = project.ProjectPath.Replace('\\', Path.DirectorySeparatorChar);
                projectFilePath = Path.Combine(_environment.TargetDirectory, projectFilePath);
                projectFilePath = Path.GetFullPath(projectFilePath);

                // Have we seen this project? If so, move on.
                if (processedProjects.Contains(projectFilePath))
                {
                    continue;
                }

                if (project.Kind == ProjectKind.CSharpProject ||
                    string.Equals(Path.GetExtension(projectFilePath), ".csproj", StringComparison.OrdinalIgnoreCase))
                {
                    result.Add(projectFilePath);
                }

                processedProjects.Add(projectFilePath);
            }

            return result;
        }

        private void ProcessProjects()
{'fraction_non_alphanumeric': 0.06594982078853047, 'fraction_numerical': 0.0, 'mean_word_length': 2.2451732961153756, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2970670', 'n_tokens_mistral': 3253, 'n_tokens_neox': 3053, 'n_words': 740}