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: [OSM-628] Add support for global.json #82

Merged
merged 4 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import * as fs from 'fs';
import * as path from 'path';

import {
PkgTree,
DepType,
parseXmlFile,
getDependencyTreeFromPackagesConfig,
getDependencyTreeFromProjectJson,
getDependencyTreeFromProjectFile,
ProjectJsonManifest,
getTargetFrameworksFromProjectFile,
getDependencyTreeFromProjectJson,
getPropertiesMap,
getTargetFrameworksFromProjectAssetsJson,
getTargetFrameworksFromProjectConfig,
getTargetFrameworksFromProjectFile,
getTargetFrameworksFromProjectJson,
getTargetFrameworksFromProjectAssetsJson,
getPropertiesMap,
parseXmlFile,
PkgTree,
ProjectJsonManifest,
PropsLookup,
} from './parsers';

Expand All @@ -38,6 +38,7 @@ export {
containsPackageReference,
extractTargetFrameworksFromProjectJson,
extractTargetFrameworksFromProjectAssetsJson,
extractTargetSdkFromGlobalJson,
extractProps,
PkgTree,
DepType,
Expand Down Expand Up @@ -230,6 +231,25 @@ async function extractTargetFrameworksFromProjectAssetsJson(
}
}

function extractTargetSdkFromGlobalJson(
manifestFileContents: string,
): string | undefined {
try {
// Remove /* */ comments from the JSON string (if any)
// It's allowed: https://learn.microsoft.com/en-us/dotnet/core/tools/global-json#comments-in-globaljson
const jsonWithoutComments = manifestFileContents.replace(
/\/\*[\s\S]*?\*\/|\/\/.*$/gm,
'',
);
const globalJsonAsObj = JSON.parse(jsonWithoutComments);
return globalJsonAsObj?.sdk?.version;
} catch (err: any) {
throw new Error(
`Extracting target framework failed with error ${err.message}`,
);
}
}

async function extractProps(propsFileContents: string): Promise<PropsLookup> {
try {
const propsFile: object = await parseXmlFile(propsFileContents);
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/dotnet-core-global-json/global_normal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "6.0.203",
"rollForward": "latestFeature"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
// This is a comment.
"sdk": {
"version": "7.0.100" /* This is comment 2*/
/* This is a
multiline comment.*/
}
}
97 changes: 40 additions & 57 deletions test/lib/conditional-targetframework.spec.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,51 @@
import {
extractTargetFrameworksFromFiles,
extractTargetFrameworksFromProjectFile,
} from '../../lib';
import { extractTargetFrameworksFromProjectFile } from '../../lib';
import * as fs from 'fs';
import * as path from 'path';

describe('for manifest files with conditional target frameworks', () => {
it('should correctly parse TargetFramework with condition', async () => {
const conditionalManifestFileFullPath = path.resolve(
`${__dirname}/../fixtures/dotnet-conditional-targetframework`,
'conditional-frameworks.csproj',
);
const conditionalManifestFileContents = fs.readFileSync(
conditionalManifestFileFullPath,
'utf-8',
);
const conditionalTargetFrameworks =
await extractTargetFrameworksFromProjectFile(
conditionalManifestFileContents,
);

const regularManifestFileContents = fs.readFileSync(
path.resolve(
it.each([
{
conditional: path.resolve(
`${__dirname}/../fixtures/dotnet-conditional-targetframework`,
'conditional-frameworks.csproj',
),
regular: path.resolve(
`${__dirname}/../fixtures/dotnet-conditional-targetframework`,
'regular-frameworks.csproj',
),
'utf-8',
);
const regularTargetFrameworks =
await extractTargetFrameworksFromProjectFile(regularManifestFileContents);

expect(conditionalTargetFrameworks).toBeTruthy();

// we expect the parser to ignore the condition and yield the same output as if the condition is not there
expect(regularTargetFrameworks).toEqual(conditionalTargetFrameworks);
});

it('should correctly parse TargetFrameworks with condition', async () => {
const conditionalManifestFileFullPath = path.resolve(
`${__dirname}/../fixtures/dotnet-conditional-targetframework`,
'conditional-multitargetframeworks.csproj',
);
const conditionalManifestFileContents = fs.readFileSync(
conditionalManifestFileFullPath,
'utf-8',
);
const conditionalTargetFrameworks =
await extractTargetFrameworksFromProjectFile(
conditionalManifestFileContents,
);

const regularManifestFileContents = fs.readFileSync(
path.resolve(
},
{
conditional: path.resolve(
`${__dirname}/../fixtures/dotnet-conditional-targetframework`,
'conditional-multitargetframeworks.csproj',
),
regular: path.resolve(
`${__dirname}/../fixtures/dotnet-conditional-targetframework`,
'regular-multitargetframeworks.csproj',
),
'utf-8',
);
const regularTargetFrameworks =
await extractTargetFrameworksFromProjectFile(regularManifestFileContents);

expect(conditionalTargetFrameworks).toBeTruthy();

// we expect the parser to ignore the condition and yield the same output as if the condition is not there
expect(regularTargetFrameworks).toEqual(conditionalTargetFrameworks);
});
},
])(
'should correctly parse TargetFramework with condition',
async ({ conditional, regular }) => {
const conditionalManifestFileContents = fs.readFileSync(
conditional,
'utf-8',
);
const conditionalTargetFrameworks =
await extractTargetFrameworksFromProjectFile(
conditionalManifestFileContents,
);

const regularManifestFileContents = fs.readFileSync(regular, 'utf-8');
const regularTargetFrameworks =
await extractTargetFrameworksFromProjectFile(
regularManifestFileContents,
);

expect(conditionalTargetFrameworks).toBeTruthy();

// we expect the parser to ignore the condition and yield the same output as if the condition is not there
expect(regularTargetFrameworks).toEqual(conditionalTargetFrameworks);
},
);
});
34 changes: 34 additions & 0 deletions test/lib/global-json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { extractTargetSdkFromGlobalJson } from '../../lib';
import * as fs from 'fs';
import * as path from 'path';

describe('for global.json target SDKs', () => {
it.each([
{
fixturePath: path.resolve(
`${__dirname}/../fixtures/dotnet-core-global-json`,
'global_normal.json',
),
expected: '6.0.203',
},
{
// Making programmers lives hard:
// https://learn.microsoft.com/en-us/dotnet/core/tools/global-json#comments-in-globaljson
fixturePath: path.resolve(
`${__dirname}/../fixtures/dotnet-core-global-json`,
'global_with_comments.json',
),
expected: '7.0.100',
},
])(
'should correctly parse TargetFramework with condition',
async ({ fixturePath, expected }) => {
const globalJson = fs.readFileSync(fixturePath, 'utf-8');

const targetSdk = extractTargetSdkFromGlobalJson(globalJson);

expect(globalJson).toBeTruthy();
expect(targetSdk).toEqual(expected);
},
);
});
3 changes: 1 addition & 2 deletions test/lib/target-frameworks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// tslint:disable:max-line-length
// tslint:disable:object-literal-key-quotes
import * as fs from 'fs';
import { extractTargetFrameworksFromFiles } from '../../lib';

describe('Target framework tests', () => {
Expand Down Expand Up @@ -199,7 +198,7 @@ describe('Target framework tests', () => {
},
);

it.concurrent('.Net project.assest.json is not valid json', async () => {
it.concurrent('.Net project.assets.json is not valid json', async () => {
try {
const targetFrameworks = await extractTargetFrameworksFromFiles(
`${__dirname}/../fixtures/dotnet-invalid-project-assets`,
Expand Down