Skip to content

Commit b81a3be

Browse files
authored
feat!: [OSM-2918] Extract rollforward policy (#94)
BREAKING CHANGE: Removed the `extractTargetSdkFromGlobalJson` method in favour of a new one that also extracts rollForward
1 parent f51e131 commit b81a3be

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

lib/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ export {
3636
buildDepTreeFromFiles,
3737
containsPackageReference,
3838
extractProjectSdkFromProjectFile,
39+
extractSdkAndRollForwardPolicyFromGlobalJson,
3940
extractTargetFrameworksFromFiles,
4041
extractTargetFrameworksFromProjectFile,
4142
extractTargetFrameworksFromProjectConfig,
4243
extractTargetFrameworksFromProjectJson,
4344
extractTargetFrameworksFromProjectAssetsJson,
44-
extractTargetSdkFromGlobalJson,
4545
extractProps,
4646
isSupportedByV2GraphGeneration,
4747
isSupportedByV3GraphGeneration,
@@ -320,14 +320,20 @@ async function extractTargetFrameworksFromProjectAssetsJson(
320320
return getTargetFrameworksFromProjectAssetsJson(manifestFile);
321321
}
322322

323-
function extractTargetSdkFromGlobalJson(
323+
function extractSdkAndRollForwardPolicyFromGlobalJson(
324324
manifestFileContents: string,
325-
): string | undefined {
325+
): {
326+
sdk?: string;
327+
rollForward?: string;
328+
} {
326329
try {
327330
// Use a JSONC parser as that's the format of global.json, which accepts comments,
328331
// see https://learn.microsoft.com/en-us/dotnet/core/tools/global-json#comments-in-globaljson
329332
const globalJsonAsObj = jsonc.parse(manifestFileContents);
330-
return globalJsonAsObj?.sdk?.version;
333+
return {
334+
sdk: globalJsonAsObj?.sdk?.version,
335+
rollForward: globalJsonAsObj?.sdk?.rollForward,
336+
};
331337
} catch (err: any) {
332338
throw new Error(
333339
`Extracting target framework failed with error ${err.message}`,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"rollForward": "latestMajor"
4+
}
5+
}

test/fixtures/dotnet-core-global-json/global_with_comments.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
// This is a comment.
33
"sdk": {
4-
"comment": "This is a comment with a URL https://google.com",
4+
"comment": "This is a JSON string with a URL https://google.com",
55
"version": "7.0.100" /* This is comment 2*/
66
/* This is a
77
multiline comment.*/

test/lib/global-json.spec.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
1-
import { extractTargetSdkFromGlobalJson } from '../../lib';
1+
import { extractSdkAndRollForwardPolicyFromGlobalJson } from '../../lib';
22
import * as fs from 'fs';
33
import * as path from 'path';
44

5-
describe('for global.json target SDKs', () => {
5+
describe('global.json parsing', () => {
66
it.each([
77
{
88
fixturePath: path.resolve(
99
`${__dirname}/../fixtures/dotnet-core-global-json`,
1010
'global_normal.json',
1111
),
12-
expected: '6.0.203',
12+
expected: {
13+
sdk: '6.0.203',
14+
rollForward: 'latestFeature',
15+
},
1316
},
1417
{
15-
// Making programmers lives hard:
16-
// https://learn.microsoft.com/en-us/dotnet/core/tools/global-json#comments-in-globaljson
1718
fixturePath: path.resolve(
1819
`${__dirname}/../fixtures/dotnet-core-global-json`,
1920
'global_with_comments.json',
2021
),
21-
expected: '7.0.100',
22+
expected: {
23+
sdk: '7.0.100',
24+
rollForward: undefined,
25+
},
26+
},
27+
{
28+
fixturePath: path.resolve(
29+
`${__dirname}/../fixtures/dotnet-core-global-json`,
30+
'global_no_version.json',
31+
),
32+
expected: {
33+
sdk: undefined,
34+
rollForward: 'latestMajor',
35+
},
2236
},
2337
])(
24-
'should correctly parse TargetFramework with condition',
38+
'should correctly parse SDK and rollForward',
2539
async ({ fixturePath, expected }) => {
2640
const globalJson = fs.readFileSync(fixturePath, 'utf-8');
2741

28-
const targetSdk = extractTargetSdkFromGlobalJson(globalJson);
42+
const targetSdk =
43+
extractSdkAndRollForwardPolicyFromGlobalJson(globalJson);
2944

3045
expect(globalJson).toBeTruthy();
3146
expect(targetSdk).toEqual(expected);

0 commit comments

Comments
 (0)