< Summary

Class:Mklinker.Commands.AddLinksCommand
Assembly:Mklinker
File(s):/home/travis/build/rubenchristoffer/Mklinker/Mklinker/Commands/AddLinksCommand.cs
Covered lines:56
Uncovered lines:1
Coverable lines:57
Total lines:101
Line coverage:98.2% (56 of 57)
Covered branches:16
Total branches:16
Branch coverage:100% (16 of 16)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor()100%100%
.ctor(...)10100%100%
Execute(...)20100%100%
CreateLinks(...)100100%100%
TryCreateLink(...)40100%100%

File(s)

/home/travis/build/rubenchristoffer/Mklinker/Mklinker/Commands/AddLinksCommand.cs

#LineLine coverage
 1using System.Linq;
 2using CommandLine;
 3using System.IO.Abstractions;
 4using Mklinker.Abstractions;
 5using System.Text.RegularExpressions;
 6using System.IO;
 7using System.Runtime.CompilerServices;
 8using System;
 9
 10namespace 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)]
 5516    public string sourceDirectoryPath { get; private set; }
 17
 18    [Value (1, HelpText = "The path to the target directory", Required = true)]
 5519    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
 16622    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
 17825    public string regexFilter { get; private set; }
 26
 27    [Option ('a', "absolute-regex", Default = @"[\s\S]*", HelpText = "Additional Regex filter deciding which files / dir
 19028    public string absoluteRegexFilter { get; private set; }
 29
 30    [Option('s', "subdirs", Default = false, HelpText = "Determines if files (not directories) from subdirectories are i
 10831    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
 11134    public bool linkDirectories { get; private set; }
 35
 036    public AddLinksCommand () : base () { }
 37
 5638    public AddLinksCommand (string sourceDirectoryPath, string targetDirectoryPath, ConfigLink.LinkType linkType, string
 2839      this.sourceDirectoryPath = sourceDirectoryPath;
 2840      this.targetDirectoryPath = targetDirectoryPath;
 2841      this.linkType = linkType;
 2842      this.regexFilter = regexFilter;
 2843      this.absoluteRegexFilter = absoluteRegexFilter;
 2844      this.includeSubdirectories = includeSubdirectories;
 2845      this.linkDirectories = linkDirectories;
 2846    }
 47
 2848    internal void Execute (IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver pathRes
 2949      if (!configHandler.DoesConfigExist(path)) {
 150        console.WriteLine($"Config '{ path }' does not exist. Type 'help config' in order to see how you create a config
 151        return;
 52      }
 53
 2754      IConfig config = configHandler.LoadConfig(path);
 2755      CreateLinks (console, configHandler, fileSystem, pathResolver, config, sourceDirectoryPath, targetDirectoryPath);
 2856    }
 57
 8358    internal void CreateLinks (IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver pat
 16359      if (!linkDirectories) {
 70860        foreach (string file in fileSystem.Directory.GetFiles (pathResolver.GetAbsoluteResolvedPath (sourceDirectory, co
 15661          TryCreateLink (console, configHandler, fileSystem, pathResolver, config, file, targetDirectory, true);
 15662        }
 63
 64        // Recursive - Will create links for all files in sub-dirs as well
 15065        if (includeSubdirectories) {
 37866          foreach (string directory in fileSystem.Directory.GetDirectories (pathResolver.GetAbsoluteResolvedPath (source
 5667            CreateLinks (console, configHandler, fileSystem, pathResolver, config, directory, fileSystem.Path.Combine (t
 5668          }
 7069        }
 8370      } else {
 2771        foreach (string directory in fileSystem.Directory.GetDirectories (pathResolver.GetAbsoluteResolvedPath (sourceDi
 672          TryCreateLink (console, configHandler, fileSystem, pathResolver, config, directory, targetDirectory, false);
 673        }
 374      }
 8375    }
 76
 16277    internal void TryCreateLink (IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver p
 16278      try {
 79        // Check absolute path regex filter first
 31280        if (Regex.IsMatch (pathResolver.GetAbsoluteResolvedPath(sourcePath, config.Variables), absoluteRegexFilter)) {
 15081          string fileOrDirectoryName = fileSystem.Path.GetFileName (sourcePath);
 82
 83          // Check file / directory name regex filter second
 28884          if (Regex.IsMatch (fileOrDirectoryName, regexFilter)) {
 13885            AddLinkCommand addLinkCommand = new AddLinkCommand (
 13886            sourcePath,
 13887            fileSystem.Path.Combine(targetBasePath, fileOrDirectoryName),
 13888            linkType,
 13889            path);
 90
 13891            addLinkCommand.Execute (console, configHandler, fileSystem, pathResolver);
 13892          }
 14793        }
 16594      } catch (ArgumentException) {
 395        console.WriteLine ("Regex provided is invalid!", IConsole.ContentType.Negative);
 396      }
 16297    }
 98
 99  }
 100
 101}