Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(manager:nuget): support central version management #15698

Merged
merged 5 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Serilog" Version="2.10.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"dependencies": {
".NETCoreApp,Version=v5.0": {
"Serilog": {
"type": "Direct",
"requested": "[2.10.0, )",
"resolved": "2.10.0",
"contentHash": "+QX0hmf37a0/OZLxM3wL7V6/ADvC1XihXN4Kq/p6d8lCPfgkRdiuhbWlMaFjR9Av0dy5F0+MBeDmDdRZN/YwQA=="
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": 1,
"dependencies": {
".NETCoreApp,Version=v5.0": {
"Serilog": {
"type": "Direct",
"requested": "[2.10.0, )",
"resolved": "2.10.0",
"contentHash": "+QX0hmf37a0/OZLxM3wL7V6/ADvC1XihXN4Kq/p6d8lCPfgkRdiuhbWlMaFjR9Av0dy5F0+MBeDmDdRZN/YwQA=="
},
"one": {
"type": "Project",
"dependencies": {
"Serilog": "2.10.0"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../one/one.csproj" />
</ItemGroup>

</Project>
19 changes: 15 additions & 4 deletions lib/modules/manager/nuget/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
UpdateArtifactsConfig,
UpdateArtifactsResult,
} from '../types';
import { getDependentPackageFiles } from './package-tree';
import { CENTRAL_FILE, getDependentPackageFiles } from './package-tree';
import {
getConfiguredRegistries,
getDefaultRegistries,
Expand Down Expand Up @@ -116,7 +116,15 @@ export async function updateArtifacts({
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`nuget.updateArtifacts(${packageFileName})`);

if (!regEx(/(?:cs|vb|fs)proj$/i).test(packageFileName)) {
// https://github.com/NuGet/Home/wiki/Centrally-managing-NuGet-package-versions
const isCentralManament =
packageFileName === CENTRAL_FILE ||
packageFileName.endsWith(`/${CENTRAL_FILE}`);

if (
!isCentralManament &&
!regEx(/(?:cs|vb|fs)proj$/i).test(packageFileName)
) {
// This could be implemented in the future if necessary.
// It's not that easy though because the questions which
// project file to restore how to determine which lock files
Expand All @@ -129,10 +137,13 @@ export async function updateArtifacts({
}

const packageFiles = [
...(await getDependentPackageFiles(packageFileName)),
packageFileName,
...(await getDependentPackageFiles(packageFileName, isCentralManament)),
];

if (!isCentralManament) {
packageFiles.push(packageFileName);
}

logger.trace(
{ packageFiles },
`Found ${packageFiles.length} dependent package files`
Expand Down
19 changes: 19 additions & 0 deletions lib/modules/manager/nuget/package-tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ describe('modules/manager/nuget/package-tree', () => {
]);
});

it('returns projects for two projects with one reference and central versions', async () => {
git.getFileList.mockResolvedValue(['one/one.csproj', 'two/two.csproj']);
Fixtures.mock({
'/tmp/repo/one/one.csproj': Fixtures.get(
'two-one-reference-with-central-versions/one/one.csproj'
),
'/tmp/repo/two/two.csproj': Fixtures.get(
'two-one-reference-with-central-versions/two/two.csproj'
),
'/tmp/repo/Directory.Packages.props': Fixtures.get(
'two-one-reference-with-central-versions/Directory.Packages.props'
),
});

expect(
await getDependentPackageFiles('Directory.Packages.props', true)
).toEqual(['one/one.csproj', 'two/two.csproj']);
});

it('returns two projects for three projects with two linear references', async () => {
git.getFileList.mockResolvedValue([
'one/one.csproj',
Expand Down
35 changes: 26 additions & 9 deletions lib/modules/manager/nuget/package-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,39 @@ import { logger } from '../../../logger';
import { readLocalFile } from '../../../util/fs';
import { getFileList } from '../../../util/git';

export const CENTRAL_FILE = 'Directory.Packages.props';
viceice marked this conversation as resolved.
Show resolved Hide resolved
/**
* Get all package files at any level of ancestry that depend on packageFileName
*/
export async function getDependentPackageFiles(
packageFileName: string
packageFileName: string,
isCentralManament = false
): Promise<string[]> {
const packageFiles = await getAllPackageFiles();
const graph: ReturnType<typeof Graph> = Graph();

if (isCentralManament) {
graph.addNode(packageFileName);
}

const parentDir =
packageFileName === CENTRAL_FILE ? '' : upath.dirname(packageFileName);

for (const f of packageFiles) {
graph.addNode(f);

if (isCentralManament && upath.dirname(f).startsWith(parentDir)) {
graph.addEdge(packageFileName, f);
}
}

for (const f of packageFiles) {
const packageFileContent = (await readLocalFile(f, 'utf8')).toString();
const packageFileContent = await readLocalFile(f, 'utf8');

const doc = new xmldoc.XmlDocument(packageFileContent);
const projectReferenceAttributes = (
doc
.childrenNamed('ItemGroup')
.map((ig) => ig.childrenNamed('ProjectReference')) ?? []
)
const projectReferenceAttributes = doc
.childrenNamed('ItemGroup')
.map((ig) => ig.childrenNamed('ProjectReference'))
.flat()
.map((pf) => pf.attr['Include']);

Expand All @@ -47,7 +58,13 @@ export async function getDependentPackageFiles(
}
}

return recursivelyGetDependentPackageFiles(packageFileName, graph);
const dependents = recursivelyGetDependentPackageFiles(
packageFileName,
graph
);

// deduplicate
return Array.from(new Set(dependents.reverse())).reverse();
}

/**
Expand All @@ -57,7 +74,7 @@ function recursivelyGetDependentPackageFiles(
packageFileName: string,
graph: ReturnType<typeof Graph>
): string[] {
const dependents: string[] = graph.adjacent(packageFileName);
const dependents = graph.adjacent(packageFileName);

if (dependents.length === 0) {
return [];
Expand Down