diff --git a/lib/modules/manager/nuget/__fixtures__/sample.csproj b/lib/modules/manager/nuget/__fixtures__/sample.csproj index eae6a29972ccc2e8a59c8c946521d1b398bd1b3a..36f5a469eb9fe77a566a30faf8261b7a72777a7d 100644 --- a/lib/modules/manager/nuget/__fixtures__/sample.csproj +++ b/lib/modules/manager/nuget/__fixtures__/sample.csproj @@ -3,6 +3,7 @@ <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> <Version>0.1.0</Version> + <AutofacVersion>4.5.0</AutofacVersion> </PropertyGroup> <ItemGroup> @@ -11,9 +12,9 @@ </ItemGroup> <ItemGroup> <PackageReference Version="1.2.3" /> - <PackageReference Include="Autofac" Version="4.5.0" /> - <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" /> - <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="1.1.2" /> + <PackageReference Include="Autofac" Version="$(AutofacVersion)" /> + <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="$(AutofacVersion)" /> + <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(UnknownVariable)" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.1.3" /> <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" /> diff --git a/lib/modules/manager/nuget/__snapshots__/extract.spec.ts.snap b/lib/modules/manager/nuget/__snapshots__/extract.spec.ts.snap index 141706ed5a165827459d4507e54a9a02f5689ce5..a8d2ae43fc717d9bc6e884e877306352220934b3 100644 --- a/lib/modules/manager/nuget/__snapshots__/extract.spec.ts.snap +++ b/lib/modules/manager/nuget/__snapshots__/extract.spec.ts.snap @@ -124,22 +124,24 @@ exports[`modules/manager/nuget/extract extractPackageFile() extracts all depende "depType": "nuget", }, { - "currentValue": "1.1.2", "datasource": "nuget", "depName": "Microsoft.AspNetCore.Hosting", "depType": "nuget", + "skipReason": "invalid-version", }, { - "currentValue": "4.1.0", + "currentValue": "4.5.0", "datasource": "nuget", "depName": "Autofac.Extensions.DependencyInjection", "depType": "nuget", + "groupName": "AutofacVersion", }, { "currentValue": "4.5.0", "datasource": "nuget", "depName": "Autofac", "depType": "nuget", + "groupName": "AutofacVersion", }, ] `; diff --git a/lib/modules/manager/nuget/extract.ts b/lib/modules/manager/nuget/extract.ts index 3b405f684335b4d1fbd5e797c01b1c9473da8aa0..56e832df34ec72a6a156265663f444cb337151d4 100644 --- a/lib/modules/manager/nuget/extract.ts +++ b/lib/modules/manager/nuget/extract.ts @@ -1,9 +1,8 @@ import is from '@sindresorhus/is'; -import type { XmlElement, XmlNode } from 'xmldoc'; -import { XmlDocument } from 'xmldoc'; +import type { XmlNode } from 'xmldoc'; +import { XmlDocument, XmlElement } from 'xmldoc'; import { logger } from '../../../logger'; import { getSiblingFileName, localPathExists } from '../../../util/fs'; -import { hasKey } from '../../../util/object'; import { regEx } from '../../../util/regex'; import { NugetDatasource } from '../../datasource/nuget'; import { getDep } from '../dockerfile/extract'; @@ -37,12 +36,13 @@ const elemNames = new Set([ 'GlobalPackageReference', ]); -function isXmlElem(node: XmlNode): boolean { - return hasKey('name', node); +function isXmlElem(node: XmlNode): node is XmlElement { + return node instanceof XmlElement; } function extractDepsFromXml(xmlNode: XmlDocument): NugetPackageDependency[] { const results: NugetPackageDependency[] = []; + const vars = new Map<string, string>(); const todo: XmlElement[] = [xmlNode]; while (todo.length) { const child = todo.pop()!; @@ -58,6 +58,7 @@ function extractDepsFromXml(xmlNode: XmlDocument): NugetPackageDependency[] { if (elemNames.has(name)) { const depName = attr?.Include || attr?.Update; + if (!depName) { continue; } @@ -79,6 +80,23 @@ function extractDepsFromXml(xmlNode: XmlDocument): NugetPackageDependency[] { dep.skipReason = 'invalid-version'; } + let groupName: string | undefined; + + currentValue = currentValue + ?.trim() + ?.replace(/^\$\((\w+)\)$/, (match, key) => { + const val = vars.get(key); + if (val) { + groupName = key; + return val; + } + return match; + }); + + if (groupName) { + dep.groupName = groupName; + } + currentValue = checkVersion .exec(currentValue) ?.groups?.currentValue?.trim(); @@ -116,8 +134,21 @@ function extractDepsFromXml(xmlNode: XmlDocument): NugetPackageDependency[] { }); } } + + const propertyGroup = child.childNamed('PropertyGroup'); + if (propertyGroup) { + for (const propChild of propertyGroup.children) { + if (isXmlElem(propChild)) { + const { name, val } = propChild; + if (!['Version', 'TargetFramework'].includes(name)) { + vars.set(name, val); + } + } + } + } } - todo.push(...(child.children.filter(isXmlElem) as XmlElement[])); + + todo.push(...child.children.filter(isXmlElem)); } } return results;