| | 1 | | using System.Linq; |
| | 2 | | using CommandLine; |
| | 3 | | using System.IO.Abstractions; |
| | 4 | | using Mklinker.Abstractions; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using System; |
| | 8 | | using System.Text; |
| | 9 | | using System.Runtime.CompilerServices; |
| | 10 | |
|
| | 11 | | namespace Mklinker.Commands { |
| | 12 | |
|
| | 13 | | [Verb("scan", HelpText = "Detect if circular paths (loops) exist for a given root folder by scanning all directories |
| | 14 | | class ScanCommand { |
| | 15 | |
|
| 0 | 16 | | private List<string> cases = new List<string>(); |
| 0 | 17 | | private bool error = false; |
| | 18 | |
|
| | 19 | | [Value (0, Default = ".", HelpText = "The root folder that should be scanned. Default will scan current working |
| 19 | 20 | | public string rootFolder { get; private set; } |
| | 21 | |
|
| | 22 | | [Option('l', "limit", Default = 30, HelpText = "Maximum amount of subfolders used for detecting loop (recursion |
| 21 | 23 | | public int recursionLimit { get; private set; } |
| | 24 | |
|
| | 25 | | [Option('v', "verbose", Default = false, HelpText = "Will display detailed output including every path that has |
| 5 | 26 | | public bool verbose { get; private set; } |
| | 27 | |
|
| | 28 | | [Option('i', "ignore", Default = false, HelpText = "Will ignore folders Mklinker does not have access to and con |
| 0 | 29 | | public bool ignoreUnauthorizedFolders { get; private set; } |
| | 30 | |
|
| 0 | 31 | | public ScanCommand () {} |
| | 32 | |
|
| 8 | 33 | | public ScanCommand (string rootFolder, int recursionLimit, bool verbose, bool ignoreUnauthorizedFolders) { |
| 4 | 34 | | this.rootFolder = rootFolder; |
| 4 | 35 | | this.recursionLimit = recursionLimit; |
| 4 | 36 | | this.verbose = verbose; |
| 4 | 37 | | this.ignoreUnauthorizedFolders = ignoreUnauthorizedFolders; |
| 4 | 38 | | } |
| | 39 | |
|
| 4 | 40 | | internal void Execute (IConsole console, IFileSystem fileSystem, IPathResolver pathResolver) { |
| 5 | 41 | | if (!fileSystem.Directory.Exists(rootFolder)) { |
| 1 | 42 | | console.WriteLine ($"Root folder '{rootFolder}' does not exist", IConsole.ContentType.Negative); |
| 1 | 43 | | return; |
| | 44 | | } |
| | 45 | |
|
| 3 | 46 | | console.WriteLine ($"### Running recursion limit test (limit = {recursionLimit}) ###", IConsole.ContentType. |
| 3 | 47 | | ScanRecursive (console, fileSystem, pathResolver, rootFolder, rootFolder, 0); |
| | 48 | |
|
| 6 | 49 | | if (!error) { |
| 5 | 50 | | if (cases.Count == 0) { |
| 2 | 51 | | console.WriteLine ("No loops found!", IConsole.ContentType.Positive); |
| 3 | 52 | | } else { |
| 1 | 53 | | console.WriteLine ("Possible loops found!", IConsole.ContentType.Negative); |
| 1 | 54 | | console.WriteLine (); |
| | 55 | |
|
| 1 | 56 | | console.WriteLine ($"### Collecting word count for directory names ###", IConsole.ContentType.Header |
| 1 | 57 | | CountDirectoryWords (console); |
| 1 | 58 | | } |
| 3 | 59 | | } |
| 4 | 60 | | } |
| | 61 | |
|
| 16 | 62 | | internal void ScanRecursive (IConsole console, IFileSystem fileSystem, IPathResolver pathResolver, string rootFo |
| 16 | 63 | | if (error) |
| 0 | 64 | | return; |
| | 65 | |
|
| 16 | 66 | | try { |
| | 67 | | // Try to find loops by using a recursion limit |
| 90 | 68 | | foreach (string directory in fileSystem.Directory.GetDirectories (currentFolder)) { |
| 15 | 69 | | if (recursionLevel >= recursionLimit) { |
| 0 | 70 | | if (verbose) { |
| 0 | 71 | | console.WriteLine (directory); |
| 0 | 72 | | console.WriteLine (); |
| 0 | 73 | | } |
| | 74 | |
|
| 1 | 75 | | cases.Add (directory.Replace ('\\', '/')); |
| | 76 | |
|
| 1 | 77 | | continue; |
| | 78 | | } |
| | 79 | |
|
| 13 | 80 | | ScanRecursive (console, fileSystem, pathResolver, rootFolder, directory, recursionLevel + 1); |
| 13 | 81 | | } |
| 0 | 82 | | } catch(IOException e) { |
| | 83 | | // Most common error is when recursion liimt is too high |
| 0 | 84 | | console.WriteLine ("An error has occured!", IConsole.ContentType.Negative); |
| 0 | 85 | | console.WriteLine ($"Perhaps recursion limit ({recursionLimit}) is set too high?", IConsole.ContentType. |
| 0 | 86 | | console.WriteLine ($"Try setting recursion limit to {recursionLevel - 1} or lower", IConsole.ContentType |
| | 87 | |
|
| 0 | 88 | | if (verbose) { |
| 0 | 89 | | console.WriteLine (); |
| 0 | 90 | | console.WriteLine (e.ToString(), IConsole.ContentType.Negative); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | cases.Clear (); |
| 0 | 94 | | error = true; |
| 0 | 95 | | } catch (UnauthorizedAccessException e) { |
| 0 | 96 | | if (ignoreUnauthorizedFolders) { |
| 0 | 97 | | if (verbose) { |
| 0 | 98 | | console.WriteLine ("Ignoring folder Mklinker does not have access to"); |
| 0 | 99 | | } |
| 0 | 100 | | } else { |
| 0 | 101 | | console.WriteLine ("An error has occured!", IConsole.ContentType.Negative); |
| 0 | 102 | | console.WriteLine ("Mklinker does not have access to a directory", IConsole.ContentType.Negative); |
| 0 | 103 | | console.WriteLine ("Try again with admin privileges or run with --ignore flag", IConsole.ContentType |
| | 104 | |
|
| 0 | 105 | | if (verbose) { |
| 0 | 106 | | console.WriteLine (); |
| 0 | 107 | | console.WriteLine (e.ToString (), IConsole.ContentType.Negative); |
| 0 | 108 | | } |
| | 109 | |
|
| 0 | 110 | | cases.Clear (); |
| 0 | 111 | | error = true; |
| 0 | 112 | | } |
| 0 | 113 | | } |
| 16 | 114 | | } |
| | 115 | |
|
| 1 | 116 | | internal void CountDirectoryWords (IConsole console) { |
| 1 | 117 | | Dictionary<string, int> wordCount = new Dictionary<string, int> (); |
| 1 | 118 | | Dictionary<string, List<string>> wordAndPaths = new Dictionary<string, List<string>> (); |
| | 119 | |
|
| | 120 | | // Count how often directory words occur in the paths |
| | 121 | | // Directory names that appear often have a higher chance |
| | 122 | | // of being the cause of the loop or near the cause of the loop |
| 5 | 123 | | for (int i = 0; i < cases.Count; i++) { |
| 1 | 124 | | string @case = cases[i]; |
| 1 | 125 | | string[] pathDirectories = @case.Split ('/'); |
| | 126 | |
|
| 26 | 127 | | for (int ii = 0; ii < pathDirectories.Length; ii++) { |
| 8 | 128 | | string word = pathDirectories[ii]; |
| | 129 | |
|
| 12 | 130 | | if (wordCount.ContainsKey (word)) { |
| 4 | 131 | | wordCount[word]++; |
| 12 | 132 | | } else if (!word.Equals(rootFolder)) { |
| 4 | 133 | | wordCount.Add (word, 1); |
| 4 | 134 | | } |
| 8 | 135 | | } |
| 1 | 136 | | } |
| | 137 | |
|
| | 138 | | // Write all word counts at the end |
| 1 | 139 | | var wordCollection = wordCount |
| 5 | 140 | | .OrderByDescending(wc => wc.Value) |
| 5 | 141 | | .Select (wc => $"{wc.Key} ({wc.Value})"); |
| | 142 | |
|
| 1 | 143 | | console.WriteLine ($"Directory name(s) ordered by word count:"); |
| 1 | 144 | | console.Write ("| "); |
| | 145 | |
|
| 15 | 146 | | foreach (string word in wordCollection) { |
| 4 | 147 | | console.Write (word, IConsole.ContentType.Negative); |
| 4 | 148 | | console.Write (" | "); |
| 4 | 149 | | } |
| | 150 | |
|
| 1 | 151 | | console.WriteLine (); |
| 1 | 152 | | } |
| | 153 | |
|
| | 154 | | } |
| | 155 | |
|
| | 156 | | } |