Files
renovate/lib/modules/manager/nuget/extract/single-csharp-file.ts
Philipp Grathwohl 5505b52d75 feat(manager/nuget): Support single file package directives (#40040)
* Add nuget capability to read packages from cs files

This is useful for dotnet source files that make use
of the .NET 10 feature to have single file projects,
where packages and sdks can be defined in a source
file.

* Add tests to validate that the nuget.config file is respected for single csharp files

* Apply PR linter suggestion for prettier code

* Remove unnecessary try-catch to increase code coverage

* Update phrasing in documentation

Co-authored-by: RahulGautamSingh <rahultesnik@gmail.com>

* Use codeblock instead of fixture

* Remove unused import

* Remove unnecessary fixtures and only verify required methods are called

* Fix imports in single-chsarp-file.ts

* Fix import ordering

* Increase code block indent

---------

Co-authored-by: RahulGautamSingh <rahultesnik@gmail.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
2026-02-19 18:06:30 +00:00

39 lines
1.1 KiB
TypeScript

import { regEx } from '../../../../util/regex.ts';
import { NugetDatasource } from '../../../datasource/nuget/index.ts';
import type { PackageDependency, PackageFileContent } from '../../types.ts';
import type { NugetPackageDependency, Registry } from '../types.ts';
import { applyRegistries } from '../util.ts';
// regex for finding
// #:package Name@Version
// or
// #:sdk Name@Version
const packageRegex = regEx(
/^#:(?<type>package|sdk)\s+(?<depName>[A-Za-z0-9_.-]+)@(?<currentValue>[0-9][^\s]*)/,
'gm',
);
export function extractPackagesFromSingleCsharpFile(
content: string,
packageFile: string,
registries: Registry[] | undefined,
): PackageFileContent | null {
const deps: PackageDependency[] = [];
for (const match of content.matchAll(packageRegex)) {
const { type, depName, currentValue } = match.groups!;
const dep: NugetPackageDependency = {
depName,
currentValue,
datasource: NugetDatasource.id,
depType: type === 'package' ? 'nuget' : 'msbuild-sdk',
};
applyRegistries(dep, registries);
deps.push(dep);
}
return { deps };
}