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): update msbuild-sdks section in global.json #11707

Merged
merged 12 commits into from Sep 14, 2021
Merged
9 changes: 9 additions & 0 deletions lib/manager/nuget/__fixtures__/msbuild-sdk-files/global.json
@@ -0,0 +1,9 @@
{
"sdk": {
"version": "5.0.302",
"rollForward": "latestMajor"
},
"msbuild-sdks": {
"YoloDev.Sdk": "0.2.0"
}
}
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1 @@
invalid json
viceice marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
{
"type": "not a nuget global.json"
}
viceice marked this conversation as resolved.
Show resolved Hide resolved
36 changes: 36 additions & 0 deletions lib/manager/nuget/extract.spec.ts
Expand Up @@ -113,6 +113,41 @@ describe('manager/nuget/extract', () => {
await extractPackageFile(otherContents, otherPackageFile, config)
).toMatchSnapshot();
});
it('extracts msbuild-sdks from global.json', async () => {
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
const packageFile = 'msbuild-sdk-files/global.json';
const contents = loadFixture(packageFile);
// FIXME: explicit assert condition
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
expect(await extractPackageFile(contents, packageFile, config))
.toMatchInlineSnapshot(`
Object {
"deps": Array [
Object {
"currentValue": "0",
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
"datasource": null,
"depName": "YoloDev.Sdk",
"depType": "nuget",
"skipReason": "unsupported-datasource",
},
],
}
`);
});
it('handles malformed global.json', async () => {
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
const packageFile = 'msbuild-sdk-files/invalid-json/global.json';
const contents = loadFixture(packageFile);
// FIXME: explicit assert condition
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
expect(
await extractPackageFile(contents, packageFile, config)
).toBeNull();
});
it('handles not-a-nuget global.json', async () => {
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
const packageFile = 'msbuild-sdk-files/not-nuget/global.json';
const contents = loadFixture(packageFile);
// FIXME: explicit assert condition
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
expect(
await extractPackageFile(contents, packageFile, config)
).toBeNull();
});

describe('.config/dotnet-tools.json', () => {
const packageFile = '.config/dotnet-tools.json';
Expand All @@ -126,6 +161,7 @@ describe('manager/nuget/extract', () => {
}
}
}`;

it('works', async () => {
// FIXME: explicit assert condition
expect(
Expand Down
39 changes: 38 additions & 1 deletion lib/manager/nuget/extract.ts
Expand Up @@ -2,10 +2,11 @@ import { XmlDocument, XmlElement, XmlNode } from 'xmldoc';
import { getGlobalConfig } from '../../config/global';
import * as datasourceNuget from '../../datasource/nuget';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import { getSiblingFileName, localPathExists } from '../../util/fs';
import { hasKey } from '../../util/object';
import type { ExtractConfig, PackageDependency, PackageFile } from '../types';
import type { DotnetToolsManifest } from './types';
import type { DotnetToolsManifest, MsbuildGlobalManifest } from './types';
import { getConfiguredRegistries } from './util';

/**
Expand Down Expand Up @@ -112,6 +113,42 @@ export async function extractPackageFile(
return { deps };
}

if (packageFile.endsWith('global.json')) {
const deps: PackageDependency[] = [];
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
let manifest: MsbuildGlobalManifest;

try {
manifest = JSON.parse(content);
} catch (err) {
logger.debug({ fileName: packageFile }, 'Invalid JSON');
return null;
}

if (manifest['msbuild-sdks'] === undefined) {
viceice marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(
{ fileName: packageFile },
'This global.json is not a Nuget file'
);
return null;
}
viceice marked this conversation as resolved.
Show resolved Hide resolved
viceice marked this conversation as resolved.
Show resolved Hide resolved

for (const depName of Object.keys(manifest['msbuild-sdks'])) {
const sdk = manifest['msbuild-sdks'][depName];
const currentValue = sdk[0];
const dep: PackageDependency = {
depType: 'nuget',
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
depName,
currentValue,
datasource: null,
skipReason: SkipReason.UnsupportedDatasource,
viceice marked this conversation as resolved.
Show resolved Hide resolved
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
};

deps.push(dep);
olegkrivtsov marked this conversation as resolved.
Show resolved Hide resolved
}

return { deps };
}

let deps: PackageDependency[] = [];
try {
const parsedXml = new XmlDocument(content);
Expand Down
1 change: 1 addition & 0 deletions lib/manager/nuget/index.ts
Expand Up @@ -10,5 +10,6 @@ export const defaultConfig = {
'\\.(?:cs|fs|vb)proj$',
'\\.(?:props|targets)$',
'\\.config\\/dotnet-tools\\.json$',
'(^|//)global\\.json$',
],
};
10 changes: 10 additions & 0 deletions lib/manager/nuget/types.ts
Expand Up @@ -14,3 +14,13 @@ export interface Registry {
readonly url: string;
readonly name?: string;
}

export interface MsbuildGlobalManifest {
readonly sdk: MsbuildSdk;
readonly 'msbuild-sdks': Record<string, string>;
}

export interface MsbuildSdk {
readonly version: string;
readonly rollForward: string;
}
1 change: 1 addition & 0 deletions lib/types/skip-reason.ts
Expand Up @@ -34,6 +34,7 @@ export enum SkipReason {
UnknownVersion = 'unknown-version',
UnknownVolta = 'unknown-volta',
UnsupportedChartType = 'unsupported-chart-type',
UnsupportedDatasource = 'unsupported-datasource',
UnsupportedRemote = 'unsupported-remote',
UnsupportedUrl = 'unsupported-url',
UnsupportedVersion = 'unsupported-version',
Expand Down