我如何以编程方式检查解决方案中的引用配置正确 [英] c# - How can I programmatically check that a reference is configured correctly in a solution

查看:93
本文介绍了我如何以编程方式检查解决方案中的引用配置正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们遇到一些构建错误,我们需要手动修复引用. 我想以编程方式检查是否定义了项目引用而不是dll引用: 因此,遍历解决方案中的所有引用,并针对每个项目检查引用是否正确定义.这样,相关的依赖项将被正确配置.

有人知道现有的解决方案吗?我想要一个可以在门禁值机之前运行的验证工具.

解决方案

我已经编写了用于处理".csproj"文件的工具.我的工具递归地遍历文件夹层次结构,处理每个".csproj"文件以验证其是否符合我们的标准(例如要求打开代码分析,警告为错误,强名称签名等).

无论如何,最近我重写了很多文件,以使用Linq-to-XML处理文件,结果比七年前最初编写时要容易得多...

CSProj处理的核心是由一个类完成的,该类期望(通过构造函数).csproj"项目文件的完整路径名作为输入.

它使用Linq-to-XML解析文件的各个部分.看看它-希望它可以帮助您入门.

当尝试各种XML查询以获取.csproj文件的各个位时,我还建议使用Linqpad.

此代码有些特定于我们的要求,但它应该为您提供一个起点.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;


namespace CreateMSBuildProject
{
    /// <summary>Encapsulates a ".csproj" file.</summary>

    sealed class CSProj
    {
        /// <summary>Encapsulates information about a particular build configuration.</summary>

        public sealed class BuildConfiguration
        {
            public BuildConfiguration(XElement configuration)
            {
                // ReSharper disable PossibleNullReferenceException

                OutputPath             = configuration.Element(_ns + "OutputPath").Value;
                WarningLevel           = warningLevel(configuration);
                CodeAnalysisRuleset    = configuration.Element(_ns + "CodeAnalysisRuleSet").Value;
                TreatWarningsAsErrors  = isTrue(configuration.Element(_ns + "TreatWarningsAsErrors"));
                IsCodeAnalysisEnabled  = isTrue(configuration.Element(_ns + "RunCodeAnalysis"));
                IsCodeContractsEnabled = isTrue(configuration.Element(_ns + "CodeContractsEnableRuntimeChecking"));

                // ReSharper restore PossibleNullReferenceException
            }

            public bool IsDebug
            {
                get
                {
                    return (string.Compare(OutputPath, "bin\\debug\\", StringComparison.OrdinalIgnoreCase) == 0);
                }
            }

            public bool IsRelease
            {
                get
                {
                    return(string.Compare(OutputPath, "bin\\release\\", StringComparison.OrdinalIgnoreCase) == 0);
                }
            }

            static bool isTrue(XElement element) // Defaults to false if element is null.
            {
                return (element != null) && (string.Compare(element.Value, "true", StringComparison.OrdinalIgnoreCase) == 0);
            }

            static int warningLevel(XElement element) // Defaults to 4 if not set.
            {
                var level = element.Element(_ns + "WarningLevel");

                if (level != null)
                    return int.Parse(level.Value);
                else
                    return 4; // Default warning level is 4.
            }

            public readonly string OutputPath;
            public readonly string CodeAnalysisRuleset;
            public readonly bool   IsCodeAnalysisEnabled;
            public readonly bool   TreatWarningsAsErrors;
            public readonly bool   IsCodeContractsEnabled;
            public readonly int    WarningLevel;
        }

        /// <summary>Encapsulates information about a referenced assembly.</summary>

        public sealed class AssemblyReference
        {
            public AssemblyReference(XElement reference)
            {
                Include = reference.Attribute("Include").Value.Split(',')[0]; // Get rid of stuff after the first ","

                var hintElem = reference.Element(_ns+"HintPath");

                if (hintElem != null)
                    HintPath = hintElem.Value;
                else
                    HintPath = "";
            }

            public readonly string HintPath;
            public readonly string Include;
        }

        /// <summary>Constructor.</summary>

        public CSProj(string csprojFilePath)
        {
            if (!isValidProjFilePath(csprojFilePath))
                throw new ArgumentOutOfRangeException("csprojFilePath", csprojFilePath, "File does not exist, or filename does not end with '.csproj'.");

            // ReSharper disable PossibleMultipleEnumeration

            XDocument doc = XDocument.Load(csprojFilePath);

            var propertyGroups = getPropertyGroups(doc);

            _projectFilePath              = csprojFilePath;
            _outputType                   = getOutputType(propertyGroups);
            _assemblyName                 = getAssemblyName(propertyGroups);
            _targetFrameworkVersion       = getTargetFrameworkVersion(propertyGroups);
            _projectDependencies          = getProjectDependencies(doc);
            _targetFrameworkProfile       = getTargetFrameworkProfile(doc);
            _xmlDocumentationFiles        = getXmlDocumentationFiles(doc);
            _buildConfigurations          = getBuildConfigurations(doc);
            _assemblyReferences           = getAssemblyReferences(doc);
            _anyStaticCodeAnalysisEnabled = getAnyStaticCodeAnalysisEnabled(doc);
            _platformTargets              = getPlatformTargets(doc);

            // ReSharper restore PossibleMultipleEnumeration
        }

        #region Properties

        /// <summary>The project output type, e.g. "WinExe" or "Library".</summary>

        public string OutputType { get { return _outputType; }  }

        /// <summary>The project's output assembly name.</summary>

        public string AssemblyName { get { return _assemblyName; } }

        /// <summary>The target framework version, or 0.0 if not set.</summary>

        public double TargetFrameworkVersion { get { return _targetFrameworkVersion; } }

        /// <summary>The project dependencies - not to be confused with assembly references!</summary>

        public IEnumerable<string> ProjectDependencies { get { return _projectDependencies; } }

        public IEnumerable<string> XmlDocumentationFiles { get { return _xmlDocumentationFiles; } }

        public IEnumerable<BuildConfiguration> BuildConfigurations { get { return _buildConfigurations; } }

        public string TargetFrameworkProfile { get { return _targetFrameworkProfile; } }

        public IEnumerable<AssemblyReference> AssemblyReferences { get { return _assemblyReferences; } }

        public bool AnyStaticCodeAnalysisEnabled { get { return _anyStaticCodeAnalysisEnabled; } }

        public string ProjectFilePath { get { return _projectFilePath; } }

        public string[] PlatformTargets { get { return _platformTargets; } }

        #endregion Properties

        //—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

        static string[] getPlatformTargets(XDocument doc) 
        {
            var result = (from item in doc.Descendants(_ns + "PlatformTarget") select item.Value).ToArray();

            if (result.Length > 0)
                return result;
            else
                return new []{"AnyCPU"}; // If "PlatformTarget" is not specified, it defaults to "AnyCPU".
        }

        static IEnumerable<XElement> getPropertyGroups(XDocument doc)
        {
            // ReSharper disable PossibleNullReferenceException
            return doc.Element(_ns+"Project").Elements(_ns+"PropertyGroup");
            // ReSharper restore PossibleNullReferenceException
        }

        static string getOutputType(IEnumerable<XElement> propertyGroups)
        {
            return propertyGroups.Elements(_ns+"OutputType").First().Value;
        }

        static string getAssemblyName(IEnumerable<XElement> propertyGroups)
        {
            return propertyGroups.Elements(_ns+"AssemblyName").First().Value;
        }

        static double getTargetFrameworkVersion(IEnumerable<XElement> propertyGroups)
        {
            var targetFrameworkVersion = propertyGroups.Elements(_ns+"TargetFrameworkVersion").FirstOrDefault();

            if (targetFrameworkVersion != null)
                return double.Parse(targetFrameworkVersion.Value.Substring(1)); // Skip first character, which is "v"; eg like "v3.5"
            else
                return 0;
        }

        static string[] getProjectDependencies(XDocument doc)
        {
            return 
            (
                from item in doc.Descendants(_ns + "ProjectReference")
                select item.Attribute("Include").Value
            )
            .ToArray();
        }

        static string getTargetFrameworkProfile(XDocument doc)
        {
            var targetFrameworkProfile = doc.Descendants(_ns + "TargetFrameworkProfile").FirstOrDefault();

            if (targetFrameworkProfile != null)
                return targetFrameworkProfile.Value;
            else
                return "";
        }

        static string[] getXmlDocumentationFiles(XDocument doc)
        {
            return
            (
                from item in doc.Descendants(_ns+"DocumentationFile")
                select item.Value
            )
            .ToArray();
        }

        static BuildConfiguration[] getBuildConfigurations(XDocument doc)
        {
            var configGroups = from item in doc.Descendants(_ns+"PropertyGroup")
                               where item.Descendants(_ns+"OutputPath").Any()
                               select new BuildConfiguration(item);

            return configGroups.ToArray();
        }

        static AssemblyReference[] getAssemblyReferences(XDocument doc)
        {
            var references = from item in doc.Descendants(_ns + "Reference")
                             select new AssemblyReference(item);

            return references.ToArray();
        }

        static bool getAnyStaticCodeAnalysisEnabled(XDocument doc)
        {
            return doc.Descendants(_ns + "CodeContractsRunCodeAnalysis")
                .Where(item => item.Attribute("Condition") == null) // Some of these have a "Condition" attribute, which is OK.
                .Any(item => string.Compare(item.Value, "true", StringComparison.OrdinalIgnoreCase) == 0);
        }

        static bool isValidProjFilePath(string projFilePath)
        {
            return 
            (
                !string.IsNullOrWhiteSpace(projFilePath) 
                && (projFilePath.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) || projFilePath.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase))
                && File.Exists(projFilePath)
            );
        }

        static readonly XNamespace _ns = "http://schemas.microsoft.com/developer/msbuild/2003";

        readonly bool _anyStaticCodeAnalysisEnabled;

        readonly string _projectFilePath;
        readonly string _outputType;
        readonly string _assemblyName;
        readonly string _targetFrameworkProfile;
        readonly double _targetFrameworkVersion;

        readonly string[] _projectDependencies;
        readonly string[] _xmlDocumentationFiles;
        readonly string[] _platformTargets;

        readonly BuildConfiguration[]     _buildConfigurations;
        readonly AssemblyReference[] _assemblyReferences;
    }
}

We are having some build errors and we need to manually fix the references. I would like to programmatically check that project references are defined instead of dll references: Thus go over all the references in a solution and for each project, check that the references are defined correctly. This way the relevant dependencies will be configured correctly.

Does anybody know of an existing solution? I would like a validation tool that will run before the gated check-in.

解决方案

I have written such a tool for processing our ".csproj" files. My tool recursively descends through a folder hierachy, processing each ".csproj" file to verify that it conforms with our standards (such as requiring Code Analysis to be turned on, Warnings as Errors, strong-name signed and so on).

Anyway, I recently rewrote a lot of it to use Linq-to-XML to process the files which turned out to be a lot easier than how I did it when originally written seven years ago...

The core of CSProj handling is done by a class which expects as input (via the constructor) the full pathname of a ".csproj" project file.

It uses Linq-to-XML to parse various parts of the file. Have a look at it - hopefully it will get you started.

I also recommend using Linqpad when trying out various XML queries to get at the various bits of a .csproj file.

This code is somewhat specific to our requirements, but it should give you a starting point.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;


namespace CreateMSBuildProject
{
    /// <summary>Encapsulates a ".csproj" file.</summary>

    sealed class CSProj
    {
        /// <summary>Encapsulates information about a particular build configuration.</summary>

        public sealed class BuildConfiguration
        {
            public BuildConfiguration(XElement configuration)
            {
                // ReSharper disable PossibleNullReferenceException

                OutputPath             = configuration.Element(_ns + "OutputPath").Value;
                WarningLevel           = warningLevel(configuration);
                CodeAnalysisRuleset    = configuration.Element(_ns + "CodeAnalysisRuleSet").Value;
                TreatWarningsAsErrors  = isTrue(configuration.Element(_ns + "TreatWarningsAsErrors"));
                IsCodeAnalysisEnabled  = isTrue(configuration.Element(_ns + "RunCodeAnalysis"));
                IsCodeContractsEnabled = isTrue(configuration.Element(_ns + "CodeContractsEnableRuntimeChecking"));

                // ReSharper restore PossibleNullReferenceException
            }

            public bool IsDebug
            {
                get
                {
                    return (string.Compare(OutputPath, "bin\\debug\\", StringComparison.OrdinalIgnoreCase) == 0);
                }
            }

            public bool IsRelease
            {
                get
                {
                    return(string.Compare(OutputPath, "bin\\release\\", StringComparison.OrdinalIgnoreCase) == 0);
                }
            }

            static bool isTrue(XElement element) // Defaults to false if element is null.
            {
                return (element != null) && (string.Compare(element.Value, "true", StringComparison.OrdinalIgnoreCase) == 0);
            }

            static int warningLevel(XElement element) // Defaults to 4 if not set.
            {
                var level = element.Element(_ns + "WarningLevel");

                if (level != null)
                    return int.Parse(level.Value);
                else
                    return 4; // Default warning level is 4.
            }

            public readonly string OutputPath;
            public readonly string CodeAnalysisRuleset;
            public readonly bool   IsCodeAnalysisEnabled;
            public readonly bool   TreatWarningsAsErrors;
            public readonly bool   IsCodeContractsEnabled;
            public readonly int    WarningLevel;
        }

        /// <summary>Encapsulates information about a referenced assembly.</summary>

        public sealed class AssemblyReference
        {
            public AssemblyReference(XElement reference)
            {
                Include = reference.Attribute("Include").Value.Split(',')[0]; // Get rid of stuff after the first ","

                var hintElem = reference.Element(_ns+"HintPath");

                if (hintElem != null)
                    HintPath = hintElem.Value;
                else
                    HintPath = "";
            }

            public readonly string HintPath;
            public readonly string Include;
        }

        /// <summary>Constructor.</summary>

        public CSProj(string csprojFilePath)
        {
            if (!isValidProjFilePath(csprojFilePath))
                throw new ArgumentOutOfRangeException("csprojFilePath", csprojFilePath, "File does not exist, or filename does not end with '.csproj'.");

            // ReSharper disable PossibleMultipleEnumeration

            XDocument doc = XDocument.Load(csprojFilePath);

            var propertyGroups = getPropertyGroups(doc);

            _projectFilePath              = csprojFilePath;
            _outputType                   = getOutputType(propertyGroups);
            _assemblyName                 = getAssemblyName(propertyGroups);
            _targetFrameworkVersion       = getTargetFrameworkVersion(propertyGroups);
            _projectDependencies          = getProjectDependencies(doc);
            _targetFrameworkProfile       = getTargetFrameworkProfile(doc);
            _xmlDocumentationFiles        = getXmlDocumentationFiles(doc);
            _buildConfigurations          = getBuildConfigurations(doc);
            _assemblyReferences           = getAssemblyReferences(doc);
            _anyStaticCodeAnalysisEnabled = getAnyStaticCodeAnalysisEnabled(doc);
            _platformTargets              = getPlatformTargets(doc);

            // ReSharper restore PossibleMultipleEnumeration
        }

        #region Properties

        /// <summary>The project output type, e.g. "WinExe" or "Library".</summary>

        public string OutputType { get { return _outputType; }  }

        /// <summary>The project's output assembly name.</summary>

        public string AssemblyName { get { return _assemblyName; } }

        /// <summary>The target framework version, or 0.0 if not set.</summary>

        public double TargetFrameworkVersion { get { return _targetFrameworkVersion; } }

        /// <summary>The project dependencies - not to be confused with assembly references!</summary>

        public IEnumerable<string> ProjectDependencies { get { return _projectDependencies; } }

        public IEnumerable<string> XmlDocumentationFiles { get { return _xmlDocumentationFiles; } }

        public IEnumerable<BuildConfiguration> BuildConfigurations { get { return _buildConfigurations; } }

        public string TargetFrameworkProfile { get { return _targetFrameworkProfile; } }

        public IEnumerable<AssemblyReference> AssemblyReferences { get { return _assemblyReferences; } }

        public bool AnyStaticCodeAnalysisEnabled { get { return _anyStaticCodeAnalysisEnabled; } }

        public string ProjectFilePath { get { return _projectFilePath; } }

        public string[] PlatformTargets { get { return _platformTargets; } }

        #endregion Properties

        //—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

        static string[] getPlatformTargets(XDocument doc) 
        {
            var result = (from item in doc.Descendants(_ns + "PlatformTarget") select item.Value).ToArray();

            if (result.Length > 0)
                return result;
            else
                return new []{"AnyCPU"}; // If "PlatformTarget" is not specified, it defaults to "AnyCPU".
        }

        static IEnumerable<XElement> getPropertyGroups(XDocument doc)
        {
            // ReSharper disable PossibleNullReferenceException
            return doc.Element(_ns+"Project").Elements(_ns+"PropertyGroup");
            // ReSharper restore PossibleNullReferenceException
        }

        static string getOutputType(IEnumerable<XElement> propertyGroups)
        {
            return propertyGroups.Elements(_ns+"OutputType").First().Value;
        }

        static string getAssemblyName(IEnumerable<XElement> propertyGroups)
        {
            return propertyGroups.Elements(_ns+"AssemblyName").First().Value;
        }

        static double getTargetFrameworkVersion(IEnumerable<XElement> propertyGroups)
        {
            var targetFrameworkVersion = propertyGroups.Elements(_ns+"TargetFrameworkVersion").FirstOrDefault();

            if (targetFrameworkVersion != null)
                return double.Parse(targetFrameworkVersion.Value.Substring(1)); // Skip first character, which is "v"; eg like "v3.5"
            else
                return 0;
        }

        static string[] getProjectDependencies(XDocument doc)
        {
            return 
            (
                from item in doc.Descendants(_ns + "ProjectReference")
                select item.Attribute("Include").Value
            )
            .ToArray();
        }

        static string getTargetFrameworkProfile(XDocument doc)
        {
            var targetFrameworkProfile = doc.Descendants(_ns + "TargetFrameworkProfile").FirstOrDefault();

            if (targetFrameworkProfile != null)
                return targetFrameworkProfile.Value;
            else
                return "";
        }

        static string[] getXmlDocumentationFiles(XDocument doc)
        {
            return
            (
                from item in doc.Descendants(_ns+"DocumentationFile")
                select item.Value
            )
            .ToArray();
        }

        static BuildConfiguration[] getBuildConfigurations(XDocument doc)
        {
            var configGroups = from item in doc.Descendants(_ns+"PropertyGroup")
                               where item.Descendants(_ns+"OutputPath").Any()
                               select new BuildConfiguration(item);

            return configGroups.ToArray();
        }

        static AssemblyReference[] getAssemblyReferences(XDocument doc)
        {
            var references = from item in doc.Descendants(_ns + "Reference")
                             select new AssemblyReference(item);

            return references.ToArray();
        }

        static bool getAnyStaticCodeAnalysisEnabled(XDocument doc)
        {
            return doc.Descendants(_ns + "CodeContractsRunCodeAnalysis")
                .Where(item => item.Attribute("Condition") == null) // Some of these have a "Condition" attribute, which is OK.
                .Any(item => string.Compare(item.Value, "true", StringComparison.OrdinalIgnoreCase) == 0);
        }

        static bool isValidProjFilePath(string projFilePath)
        {
            return 
            (
                !string.IsNullOrWhiteSpace(projFilePath) 
                && (projFilePath.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) || projFilePath.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase))
                && File.Exists(projFilePath)
            );
        }

        static readonly XNamespace _ns = "http://schemas.microsoft.com/developer/msbuild/2003";

        readonly bool _anyStaticCodeAnalysisEnabled;

        readonly string _projectFilePath;
        readonly string _outputType;
        readonly string _assemblyName;
        readonly string _targetFrameworkProfile;
        readonly double _targetFrameworkVersion;

        readonly string[] _projectDependencies;
        readonly string[] _xmlDocumentationFiles;
        readonly string[] _platformTargets;

        readonly BuildConfiguration[]     _buildConfigurations;
        readonly AssemblyReference[] _assemblyReferences;
    }
}

这篇关于我如何以编程方式检查解决方案中的引用配置正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆