Last active
April 20, 2018 09:25
-
-
Save LindaLawton/54c4b471b8447890234f3ec1f9b6f828 to your computer and use it in GitHub Desktop.
Console application for scanning files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
namespace FileScanner | |
{ | |
public class Program | |
{ | |
private const string _locationOfRepoToScan = @"C:\Development\IdentiyServer\IdentityServer4\src\IdentityServer4"; | |
private const string _localDirectoryReplace = @"C:\Development\IdentiyServer\IdentityServer4\"; | |
private const string _gitHubRepoLocationReaplace = "https://github.com/LindaLawton/IdentityServer4/blob/GDPR/"; | |
private const int _gitLineNumberOffset = 1; | |
private static string _mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); | |
private static string _outputFile = _mydocpath + @"\IdentityServerGDPRScann.txt"; | |
private const string _SearchFor = "_logger."; | |
private const string _InFilesOfType = ".cs"; | |
public static void Main(string[] args) | |
{ | |
if (File.Exists(_outputFile)) | |
{ | |
File.Delete(_outputFile); | |
} | |
if (Directory.Exists(_locationOfRepoToScan)) | |
{ | |
ProcessDirectory(_locationOfRepoToScan); | |
} | |
} | |
// Process all files in the directory passed in, recurse on any directories | |
// that are found, and process the files they contain. | |
public static void ProcessDirectory(string targetDirectory) | |
{ | |
// Process the list of files found in the directory. | |
var fileEntries = Directory.GetFiles(targetDirectory).Where(a => Path.GetExtension(a) == _InFilesOfType).ToList(); | |
foreach (var fileName in fileEntries) | |
ProcessFile(fileName); | |
// Recurse into subdirectories of this directory. | |
var subdirectoryEntries = Directory.GetDirectories(targetDirectory); | |
foreach (var subdirectory in subdirectoryEntries) | |
ProcessDirectory(subdirectory); | |
} | |
public static void ProcessFile(string path) | |
{ | |
using (var outputFile = new StreamWriter(_outputFile, true)) | |
{ | |
var lines = File.ReadLines(path) | |
.Select((line, lineNumber) => new { LineNumber = lineNumber, Line = line }) | |
.Where(line => line.Line.Contains(_SearchFor)) | |
.ToList(); | |
if (lines.Count <= 0) return; | |
outputFile.Output("\r\n"); | |
outputFile.Output($"[{Path.GetFileName(path)}]({BuildGitHubLink(path)})"); | |
foreach (var line in lines) | |
{ | |
outputFile.Output($"{line.LineNumber + _gitLineNumberOffset}. `{line.Line.Trim()}` [{Path.GetFileName(path)}]({BuildGitHubLink(path)}#L{line.LineNumber + _gitLineNumberOffset})"); | |
} | |
} | |
} | |
public static string BuildGitHubLink(string path) | |
{ | |
return path.Replace(_localDirectoryReplace, _gitHubRepoLocationReaplace).Replace("\\", "/"); | |
} | |
} | |
public static class StreamWriterExtensions | |
{ | |
public static void Output(this StreamWriter stream, string line) | |
{ | |
Console.WriteLine(line); // I like to watch while debuging. | |
stream.WriteLine(line); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment