Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active July 30, 2019 14:30
Show Gist options
  • Save bradoyler/8aaacff4a592919267411c6d09026eac to your computer and use it in GitHub Desktop.
Save bradoyler/8aaacff4a592919267411c6d09026eac to your computer and use it in GitHub Desktop.
scan csproj files for usage of ProjectReference
const glob = require('glob')
const fs = require('fs');
const xml2js = require('xml2js');
const path = require('path');
function parseFile (filePath, parser) {
fs.readFile(filePath, (err, data) => {
parser.parseString(data, (err, result) => {
const itemgroups = result.Project.ItemGroup;
if (itemgroups && itemgroups.length) {
itemgroups.forEach(grp => {
if (grp.ProjectReference) {
const cleanFilePath = filePath.replace(/\.\.\/\.\./, ''); // replace(/\.\.\\\.\.\.\\/, '')
console.log(grp.ProjectReference.length + ' ProjectReferences in ' + cleanFilePath);
grp.ProjectReference.forEach(ref => {
let include = ref.$.Include;
include = include.replace(/\\/g, path.sep)
.replace(/\.\./g, '')
//.replace(/\.\.\//, '');
console.log('--> Project Ref: ', include);
});
}
});
}
});
});
}
let scanPath = '../../**/*.csproj';
const projectFolder = '';
if (projectFolder) {
scanPath = `../../${projectFolder}/**/*.csproj`
}
console.log('Scanning', scanPath);
glob(scanPath, {}, function (err, files) {
if (err) {
console.log('ERR', err);
}
var parser = new xml2js.Parser();
files.forEach(file => {
// console.log(file);
parseFile(file, parser);
});
console.log(files.length + ' .csproj files scanned');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment