| | | 1 | | using System; |
| | | 2 | | using System.IO.Abstractions; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using CommandLine; |
| | | 5 | | using Mklinker.Abstractions; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace Mklinker.Commands { |
| | | 9 | | |
| | | 10 | | [Verb ("linkall", HelpText = "Generates all links from config")] |
| | | 11 | | class LinkAllCommand : GlobalOptions { |
| | | 12 | | |
| | | 13 | | [Option('v', "verbose", Default = false, HelpText = "Gives more detailed output", Required = false)] |
| | 0 | 14 | | public bool verbose { get; private set; } |
| | | 15 | | |
| | 0 | 16 | | public LinkAllCommand() : base () {} |
| | 6 | 17 | | public LinkAllCommand (string path) : base (path) {} |
| | | 18 | | |
| | 2 | 19 | | internal void Execute (IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, ILinker linker, IPath |
| | 0 | 20 | | if (!configHandler.DoesConfigExist (path)) { |
| | 0 | 21 | | console.WriteLine ($"Config '{ path }' does not exist. Type 'help config' in order to see how you create a confi |
| | 0 | 22 | | return; |
| | | 23 | | } |
| | | 24 | | |
| | 2 | 25 | | IConfig config = configHandler.LoadConfig (path); |
| | | 26 | | |
| | 2 | 27 | | console.WriteLine ("\nCreating links based on config..."); |
| | | 28 | | |
| | 2 | 29 | | int successes = 0; |
| | | 30 | | |
| | | 31 | | // Allow linkers verbose output if flag is set for this command |
| | 2 | 32 | | linker.verbose = verbose; |
| | | 33 | | |
| | 18 | 34 | | foreach (ConfigLink configLink in config.LinkList) { |
| | 4 | 35 | | string resolvedSourcePath = pathResolver.GetAbsoluteResolvedPath (configLink.sourcePath, config.Variables); |
| | 4 | 36 | | string resolvedTargetPath = pathResolver.GetAbsoluteResolvedPath (configLink.targetPath, config.Variables); |
| | | 37 | | |
| | 4 | 38 | | CreateSubDirectories (console, fileSystem, pathResolver, config, resolvedTargetPath); |
| | | 39 | | |
| | 0 | 40 | | if (fileSystem.Directory.Exists(resolvedTargetPath) || fileSystem.File.Exists(resolvedTargetPath)) { |
| | 0 | 41 | | console.Write ($"Path '{configLink.targetPath}' already exists", IConsole.ContentType.Negative); |
| | | 42 | | |
| | 0 | 43 | | if (verbose) { |
| | 0 | 44 | | console.Write ($" (resolved to '{resolvedTargetPath}')", IConsole.ContentType.Negative); |
| | 0 | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | console.WriteLine (); |
| | 0 | 48 | | } else if (linker.CreateLink (resolvedSourcePath, resolvedTargetPath, configLink.linkType)) { |
| | 4 | 49 | | successes++; |
| | 4 | 50 | | } |
| | 4 | 51 | | } |
| | | 52 | | |
| | 2 | 53 | | console.WriteLine ("\n### Finished! Created {0} / {1} links ###", successes, config.LinkList.Count); |
| | 2 | 54 | | } |
| | | 55 | | |
| | 4 | 56 | | internal void CreateSubDirectories (IConsole console, IFileSystem fileSystem, IPathResolver pathResolver, IConfig co |
| | | 57 | | // Create sub-dirs if they do not exist |
| | 4 | 58 | | string path = fileSystem.Path.GetDirectoryName (resolvedTargetPath); |
| | | 59 | | |
| | 6 | 60 | | if (path.Trim().Length != 0 && !fileSystem.Directory.Exists(path)) { |
| | 2 | 61 | | fileSystem.Directory.CreateDirectory (path); |
| | 2 | 62 | | } |
| | 4 | 63 | | } |
| | | 64 | | |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | } |