/// <summary>
        /// 获取 Nuget 包信息列表
        /// </summary>
        /// <returns>Nuget 包信息列表</returns>
        public IEnumerable <NugetInfo> GetNugetInfos()
        {
            if (!IsGoodFormat())
            {
                throw new InvalidOperationException("无法在格式异常的配置文件中读取 Nuget 信息。");
            }

            var nugetInfoList     = new List <NugetInfo>();
            var packageReferences = CsProj.GetPackageReferences(_xDocument);

            foreach (var packageReference in packageReferences)
            {
                var nugetName    = GetNugetName(packageReference);
                var nugetVersion = GetNugetVersion(packageReference);
                if (string.IsNullOrWhiteSpace(nugetName) || string.IsNullOrWhiteSpace(nugetVersion))
                {
                    continue;
                }

                nugetInfoList.Add(new NugetInfo(nugetName, nugetVersion));
            }

            foreach (var nugetInfoReference in CsProj.GetNugetInfoReferences(_xDocument))
            {
                var nugetInfo = CsProj.GetNugetInfoFromNugetInfoReference(nugetInfoReference, _csProjPath);
                nugetInfoList.Add(nugetInfo);
            }

            return(nugetInfoList);
        }
Exemplo n.º 2
0
        protected override bool FixDocumentByStrategy(NugetFixStrategy nugetFixStrategy)
        {
            var packageReferences = CsProj.GetPackageReferences(Document).Where(x =>
                                                                                x.Attribute(CsProj.IncludeAttribute).Value == nugetFixStrategy.NugetName);
            var nugetInfoReferences = CsProj.GetNugetInfoReferences(Document).Where(x =>
                                                                                    CsProj.GetNugetInfoFromNugetInfoReference(x).Name == nugetFixStrategy.NugetName);

            if (!packageReferences.Any() && !nugetInfoReferences.Any())
            {
                return(false);
            }

            if (nugetFixStrategy.NugetVersion == NugetVersion.IgnoreFix)
            {
                Log = StringSplicer.SpliceWithNewLine(Log, $"    - 根据策略,忽略 {nugetFixStrategy.NugetName} 存在的问题");
                return(true);
            }

            if (packageReferences.Any())
            {
                FixPackageReferences(packageReferences, nugetFixStrategy);
                DeleteNugetInfoReferences(nugetInfoReferences);
            }
            else
            {
                FixNugetInfoReferences(nugetInfoReferences, nugetFixStrategy);
            }

            return(true);
        }
        /// <summary>
        /// 是否格式正常
        /// </summary>
        /// <returns>是否格式正常</returns>
        public bool IsGoodFormat()
        {
            if (_isGoodFormat.HasValue)
            {
                return(_isGoodFormat.Value);
            }

            _isGoodFormat = false;
            var root = _xDocument.Root;

            if (root.Name.LocalName != CsProj.RootName)
            {
                ExceptionMessage = $".csproj 文件根节点名称不为 ${CsProj.RootName}";
                return(false);
            }

            foreach (var packageReference in CsProj.GetPackageReferences(_xDocument))
            {
                if (packageReference.Attribute(CsProj.IncludeAttribute) == null &&
                    packageReference.Attribute(CsProj.UpdateAttribute) == null)
                {
                    ExceptionMessage = $"{CsProj.PackageReferenceName} 缺少 {CsProj.IncludeAttribute} 属性。";
                    return(false);
                }

                if (packageReference.Attribute(CsProj.VersionAttribute) == null &&
                    packageReference.Elements().FirstOrDefault(x => x.Name.LocalName == CsProj.VersionElementName) ==
                    null)
                {
                    ExceptionMessage = $"{CsProj.PackageReferenceName} 缺少必要的版本信息。";
                    return(false);
                }
            }

            _isGoodFormat = true;
            return(true);
        }