Skip to content

Commit

Permalink
Merge pull request #24 from snyk/feat/project-json-frameworks
Browse files Browse the repository at this point in the history
feat: Extract targetFrameworks from project.json
  • Loading branch information
lili2311 authored Feb 1, 2019
2 parents 8421710 + 7e3c0c4 commit 7db2ad3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
npm-debug.log
obj/
.npmrc
# output
dist
Expand Down
17 changes: 16 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {PkgTree, DepType, parseManifestFile,
getDependencyTreeFromPackagesConfig, getDependencyTreeFromProjectJson,
getDependencyTreeFromProjectFile, ProjectJsonManifest,
getTargetFrameworksFromProjectFile,
getTargetFrameworksFromProjectConfig} from './parsers';
getTargetFrameworksFromProjectConfig,
getTargetFrameworksFromProjectJson} from './parsers';

const PROJ_FILE_EXTENSIONS = [
'.csproj',
Expand All @@ -24,6 +25,7 @@ export {
extractTargetFrameworksFromProjectFile,
extractTargetFrameworksFromProjectConfig,
containsPackageReference,
extractTargetFrameworksFromProjectJson,
PkgTree,
DepType,
};
Expand Down Expand Up @@ -96,6 +98,8 @@ function extractTargetFrameworksFromFiles(
return extractTargetFrameworksFromProjectFile(manifestFileContents);
} else if (_.endsWith(manifestFilePath, 'packages.config')) {
return extractTargetFrameworksFromProjectConfig(manifestFileContents);
} else if (_.endsWith(manifestFilePath, 'project.json')) {
return extractTargetFrameworksFromProjectJson(manifestFileContents);
} else {
throw new Error(`Unsupported file ${manifestFilePath}, Please provide ` +
'a project *.csproj, *.vbproj, *.fsproj or packages.config file.');
Expand Down Expand Up @@ -131,3 +135,14 @@ async function containsPackageReference(manifestFileContents: string) {

return referenceIndex !== -1;
}

async function extractTargetFrameworksFromProjectJson(
manifestFileContents: string): Promise<string[]> {
try {
// trimming required to address files with UTF-8 with BOM encoding
const manifestFile = JSON.parse(manifestFileContents.trim());
return getTargetFrameworksFromProjectJson(manifestFile);
} catch (err) {
throw new Error(`Extracting target framework failed with error ${err.message}`);
}
}
5 changes: 5 additions & 0 deletions lib/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,8 @@ export function getTargetFrameworksFromProjectConfig(manifestFile) {

return targetFrameworksResult;
}

export function getTargetFrameworksFromProjectJson(manifestFile) {
const targetFrameworksResult: string[] = [];
return Object.keys(_.get(manifestFile, 'frameworks', {}));
}
7 changes: 6 additions & 1 deletion test/fixtures/dotnet-project-json/standard-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
},
"RouteMagic": "1.3",
"WebActivatorEx": "2.1.0"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": ["dotnet5.6", "portable-net45+win8"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@
},
"RouteMagic": "1.3",
"WebActivatorEx": "2.1.0"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": ["dotnet5.6", "portable-net45+win8"]
},
"net451": {}
}
}
18 changes: 18 additions & 0 deletions test/lib/target-frameworks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,21 @@ test('.Net packages.config dotnet-empty-manifest target framework extracted', as
'packages.config');
t.deepEqual(targetFrameworks, [], 'targetFramework array is as expected');
});

/*
****** project.json ******
*/

test('.Net project.json single target framework extracted as expected', async (t) => {
const targetFrameworks = await extractTargetFrameworksFromFiles(
`${__dirname}/../fixtures/dotnet-project-json`,
'standard-project.json');
t.deepEqual(targetFrameworks, ['netcoreapp1.0'], 'targetFramework array is as expected');
});

test('.Net project.json multiple target frameworks extracted as expected', async (t) => {
const targetFrameworks = await extractTargetFrameworksFromFiles(
`${__dirname}/../fixtures/dotnet-project-json`,
'utf-8-with-bom-project.json');
t.deepEqual(targetFrameworks, ['netcoreapp1.0', 'net451'], 'targetFramework array is as expected');
});

0 comments on commit 7db2ad3

Please sign in to comment.