<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using FlubuCore.IO.Wrappers;
using FlubuCore.Scripting.Attributes;

namespace FlubuCore.Scripting.Analysis
{
    public class ProjectFileAnalyzer : IProjectFileAnalyzer
    {
        private static List<string> _csprojLocations = new List<string>()
        {
            "BuildScript.csproj",
            "BuildScript.csproj",
            "Build.csproj",
            "BuildScript/BuildScript.csproj",
            "BuildScripts/BuildScripts.csproj",
            "Build/Build.Csproj",
        };

        private readonly IFileWrapper _file;

        public ProjectFileAnalyzer(IFileWrapper file)
        {
            _file = file;
        }

        public ProjectFileAnalyzerResult Analyze(string location = null, bool disableAnalysis = false)
        {
            if (disableAnalysis)
            {
                return new ProjectFileAnalyzerResult
                {
                    ProjectFileFound = false
                };
            }

            var result = LocateCsproj(location);

            if (!result.ProjectFileFound)
            {
                return result;
            }

            XPathDocument doc = new XPathDocument(result.ProjectFileLocation);
            XPathNavigator nav = doc.CreateNavigator();
            var packageReferences = nav.Select("//PackageReference");
            while (packageReferences.MoveNext())
            {
                NugetPackageReference reference = new NugetPackageReference();
                reference.Id = packageReferences.Current.GetAttribute("Include", string.Empty);
                reference.Version = packageReferences.Current.GetAttribute("Version", string.Empty);
                result.NugetReferences.Add(reference);
            }

            var assemblyReferences = nav.Select("//Reference");
            while (assemblyReferences.MoveNext())
            {
                AssemblyReference reference = new AssemblyReference();
                reference.Name = assemblyReferences.Current.GetAttribute("Include", string.Empty);
                var packageNode = assemblyReferences.Current.Clone();
                if (packageNode.MoveToChild("HintPath", string.Empty))
                {
                    reference.Path = Path.Combine(Path.GetDirectoryName(result.ProjectFileLocation), packageNode.InnerXml);
                }

                result.AssemblyReferences.Add(reference);
            }

            return result;
        }

        private ProjectFileAnalyzerResult LocateCsproj(string location)
        {
            ProjectFileAnalyzerResult result = new ProjectFileAnalyzerResult();
            if (location == null)
            {
                foreach (var item in _csprojLocations)
                {
                    if (_file.Exists(item))
                    {
                        result.ProjectFileFound = true;
                        result.ProjectFileLocation = Path.GetFullPath(item);
                        break;
                    }
                }
            }
            else
            {
                if (_file.Exists(location))
                {
                    result.ProjectFileFound = true;
                    result.ProjectFileLocation = location;
                }
            }

            return result;
        }
    }
}
<commit_msg>Adds some more default build script locations.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using FlubuCore.IO.Wrappers;
using FlubuCore.Scripting.Attributes;

namespace FlubuCore.Scripting.Analysis
{
    public class ProjectFileAnalyzer : IProjectFileAnalyzer
    {
        private static List<string> _csprojLocations = new List<string>()
        {
            "BuildScript.csproj",
            "BuildScript.csproj",
            "Build.csproj",
            "BuildScript/BuildScript.csproj",
            "BuildScripts/BuildScripts.csproj",
            "Build/Build.csproj",
            "_Build/Build.csproj",
            "_BuildScript/BuildScript.csproj",
            "_BuildScripts/BuildScripts.csproj"
        };

        private readonly IFileWrapper _file;

        public ProjectFileAnalyzer(IFileWrapper file)
        {
            _file = file;
        }

        public ProjectFileAnalyzerResult Analyze(string location = null, bool disableAnalysis = false)
        {
            if (disableAnalysis)
            {
                return new ProjectFileAnalyzerResult
                {
                    ProjectFileFound = false
                };
            }

            var result = LocateCsproj(location);

            if (!result.ProjectFileFound)
            {
                return result;
            }

            XPathDocument doc = new XPathDocument(result.ProjectFileLocation);
            XPathNavigator nav = doc.CreateNavigator();
            var packageReferences = nav.Select("//PackageReference");
            while (packageReferences.MoveNext())
            {
                NugetPackageReference reference = new NugetPackageReference();
                reference.Id = packageReferences.Current.GetAttribute("Include", string.Empty);
                reference.Version = packageReferences.Current.GetAttribute("Version", string.Empty);
                result.NugetReferences.Add(reference);
            }

            var assemblyReferences = nav.Select("//Reference");
            while (assemblyReferences.MoveNext())
            {
                AssemblyReference reference = new AssemblyReference();
                reference.Name = assemblyReferences.Current.GetAttribute("Include", string.Empty);
                var packageNode = assemblyReferences.Current.Clone();
                if (packageNode.MoveToChild("HintPath", string.Empty))
                {
                    reference.Path = Path.Combine(Path.GetDirectoryName(result.ProjectFileLocation), packageNode.InnerXml);
                }

                result.AssemblyReferences.Add(reference);
            }

            return result;
        }

        private ProjectFileAnalyzerResult LocateCsproj(string location)
        {
            ProjectFileAnalyzerResult result = new ProjectFileAnalyzerResult();
            if (location == null)
            {
                foreach (var item in _csprojLocations)
                {
                    if (_file.Exists(item))
                    {
                        result.ProjectFileFound = true;
                        result.ProjectFileLocation = Path.GetFullPath(item);
                        break;
                    }
                }
            }
            else
            {
                if (_file.Exists(location))
                {
                    result.ProjectFileFound = true;
                    result.ProjectFileLocation = location;
                }
            }

            return result;
        }
    }
}

{'fraction_non_alphanumeric': 0.08011953892130354, 'fraction_numerical': 0.0, 'mean_word_length': 1.5734163310142806, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2553819', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1573, 'n_words': 297}