| | 1 | | using System.Linq; |
| | 2 | | using CommandLine; |
| | 3 | | using System.IO.Abstractions; |
| | 4 | | using Mklinker.Abstractions; |
| | 5 | | using System.Text.RegularExpressions; |
| | 6 | | using System.IO; |
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using System; |
| | 9 | |
|
| | 10 | | namespace Mklinker.Commands { |
| | 11 | |
|
| | 12 | | [Verb ("addlinks", HelpText = "Adds multiple new links at once to config file with optional filtering")] |
| | 13 | | class AddLinksCommand : GlobalOptions { |
| | 14 | |
|
| | 15 | | [Value (0, HelpText = "The path to the source directory", Required = true)] |
| 55 | 16 | | public string sourceDirectoryPath { get; private set; } |
| | 17 | |
|
| | 18 | | [Value (1, HelpText = "The path to the target directory", Required = true)] |
| 55 | 19 | | public string targetDirectoryPath { get; private set; } |
| | 20 | |
|
| | 21 | | [Value (2, Default = ConfigLink.LinkType.Default, HelpText = "The type of link you want to create. Default is Symbol |
| 166 | 22 | | public ConfigLink.LinkType linkType { get; private set; } |
| | 23 | |
|
| | 24 | | [Option('r', "regex", Default = @"[\s\S]*", HelpText = "Regex filter deciding which files / directories to add links |
| 178 | 25 | | public string regexFilter { get; private set; } |
| | 26 | |
|
| | 27 | | [Option ('a', "absolute-regex", Default = @"[\s\S]*", HelpText = "Additional Regex filter deciding which files / dir |
| 190 | 28 | | public string absoluteRegexFilter { get; private set; } |
| | 29 | |
|
| | 30 | | [Option('s', "subdirs", Default = false, HelpText = "Determines if files (not directories) from subdirectories are i |
| 108 | 31 | | public bool includeSubdirectories { get; private set; } |
| | 32 | |
|
| | 33 | | [Option('d', "dirs", Default = false, HelpText = "Determines if directory links should be created instead of file li |
| 111 | 34 | | public bool linkDirectories { get; private set; } |
| | 35 | |
|
| 0 | 36 | | public AddLinksCommand () : base () { } |
| | 37 | |
|
| 56 | 38 | | public AddLinksCommand (string sourceDirectoryPath, string targetDirectoryPath, ConfigLink.LinkType linkType, string |
| 28 | 39 | | this.sourceDirectoryPath = sourceDirectoryPath; |
| 28 | 40 | | this.targetDirectoryPath = targetDirectoryPath; |
| 28 | 41 | | this.linkType = linkType; |
| 28 | 42 | | this.regexFilter = regexFilter; |
| 28 | 43 | | this.absoluteRegexFilter = absoluteRegexFilter; |
| 28 | 44 | | this.includeSubdirectories = includeSubdirectories; |
| 28 | 45 | | this.linkDirectories = linkDirectories; |
| 28 | 46 | | } |
| | 47 | |
|
| 28 | 48 | | internal void Execute (IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver pathRes |
| 29 | 49 | | if (!configHandler.DoesConfigExist(path)) { |
| 1 | 50 | | console.WriteLine($"Config '{ path }' does not exist. Type 'help config' in order to see how you create a config |
| 1 | 51 | | return; |
| | 52 | | } |
| | 53 | |
|
| 27 | 54 | | IConfig config = configHandler.LoadConfig(path); |
| 27 | 55 | | CreateLinks (console, configHandler, fileSystem, pathResolver, config, sourceDirectoryPath, targetDirectoryPath); |
| 28 | 56 | | } |
| | 57 | |
|
| 83 | 58 | | internal void CreateLinks (IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver pat |
| 163 | 59 | | if (!linkDirectories) { |
| 708 | 60 | | foreach (string file in fileSystem.Directory.GetFiles (pathResolver.GetAbsoluteResolvedPath (sourceDirectory, co |
| 156 | 61 | | TryCreateLink (console, configHandler, fileSystem, pathResolver, config, file, targetDirectory, true); |
| 156 | 62 | | } |
| | 63 | |
|
| | 64 | | // Recursive - Will create links for all files in sub-dirs as well |
| 150 | 65 | | if (includeSubdirectories) { |
| 378 | 66 | | foreach (string directory in fileSystem.Directory.GetDirectories (pathResolver.GetAbsoluteResolvedPath (source |
| 56 | 67 | | CreateLinks (console, configHandler, fileSystem, pathResolver, config, directory, fileSystem.Path.Combine (t |
| 56 | 68 | | } |
| 70 | 69 | | } |
| 83 | 70 | | } else { |
| 27 | 71 | | foreach (string directory in fileSystem.Directory.GetDirectories (pathResolver.GetAbsoluteResolvedPath (sourceDi |
| 6 | 72 | | TryCreateLink (console, configHandler, fileSystem, pathResolver, config, directory, targetDirectory, false); |
| 6 | 73 | | } |
| 3 | 74 | | } |
| 83 | 75 | | } |
| | 76 | |
|
| 162 | 77 | | internal void TryCreateLink (IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver p |
| 162 | 78 | | try { |
| | 79 | | // Check absolute path regex filter first |
| 312 | 80 | | if (Regex.IsMatch (pathResolver.GetAbsoluteResolvedPath(sourcePath, config.Variables), absoluteRegexFilter)) { |
| 150 | 81 | | string fileOrDirectoryName = fileSystem.Path.GetFileName (sourcePath); |
| | 82 | |
|
| | 83 | | // Check file / directory name regex filter second |
| 288 | 84 | | if (Regex.IsMatch (fileOrDirectoryName, regexFilter)) { |
| 138 | 85 | | AddLinkCommand addLinkCommand = new AddLinkCommand ( |
| 138 | 86 | | sourcePath, |
| 138 | 87 | | fileSystem.Path.Combine(targetBasePath, fileOrDirectoryName), |
| 138 | 88 | | linkType, |
| 138 | 89 | | path); |
| | 90 | |
|
| 138 | 91 | | addLinkCommand.Execute (console, configHandler, fileSystem, pathResolver); |
| 138 | 92 | | } |
| 147 | 93 | | } |
| 165 | 94 | | } catch (ArgumentException) { |
| 3 | 95 | | console.WriteLine ("Regex provided is invalid!", IConsole.ContentType.Negative); |
| 3 | 96 | | } |
| 162 | 97 | | } |
| | 98 | |
|
| | 99 | | } |
| | 100 | |
|
| | 101 | | } |