Skip to content

Commit

Permalink
feat(nuget): Support for variables (#33416)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Jan 10, 2025
1 parent 4aab664 commit 0427edd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
7 changes: 4 additions & 3 deletions lib/modules/manager/nuget/__fixtures__/sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<Version>0.1.0</Version>
<AutofacVersion>4.5.0</AutofacVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -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" />
Expand Down
6 changes: 4 additions & 2 deletions lib/modules/manager/nuget/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
]
`;
Expand Down
43 changes: 37 additions & 6 deletions lib/modules/manager/nuget/extract.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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()!;
Expand All @@ -58,6 +58,7 @@ function extractDepsFromXml(xmlNode: XmlDocument): NugetPackageDependency[] {

if (elemNames.has(name)) {
const depName = attr?.Include || attr?.Update;

if (!depName) {
continue;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0427edd

Please sign in to comment.