| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using Mklinker.Abstractions; |
| | 4 | | using System.IO.Abstractions; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Text.RegularExpressions; |
| | 7 | |
|
| | 8 | | namespace Mklinker { |
| | 9 | |
|
| | 10 | | class PathResolver : IPathResolver { |
| | 11 | |
|
| | 12 | | public const string delimiter = "?"; |
| 1 | 13 | | public static readonly string regex = $@"{ Regex.Escape(delimiter) }[^?]*{ Regex.Escape(delimiter) }"; |
| | 14 | |
|
| | 15 | | readonly IFileSystem fileSystem; |
| | 16 | |
|
| 10 | 17 | | public PathResolver (IFileSystem fileSystem) { |
| 5 | 18 | | this.fileSystem = fileSystem; |
| 5 | 19 | | } |
| | 20 | |
|
| 5 | 21 | | public string GetAbsoluteResolvedPath(string unresolvedPath, IEnumerable<Variable> variables) { |
| 5 | 22 | | string resolvedPath = unresolvedPath; |
| | 23 | |
|
| | 24 | | Match match; |
| | 25 | |
|
| 17 | 26 | | while ((match = Regex.Match(resolvedPath, regex)).Success) { |
| 7 | 27 | | string name = match.Value; |
| 16 | 28 | | Variable matchedVariables = variables.FirstOrDefault(variable => (delimiter + variable.name + delimiter).Equals( |
| | 29 | |
|
| 7 | 30 | | if (matchedVariables == null) |
| 2 | 31 | | break; |
| | 32 | |
|
| 5 | 33 | | resolvedPath = resolvedPath.Replace(name, matchedVariables.value, StringComparison.OrdinalIgnoreCase); |
| 5 | 34 | | } |
| | 35 | |
|
| 5 | 36 | | return fileSystem.Path.GetFullPath(resolvedPath.Replace(@"\", "/")).Replace(@"\", "/"); |
| 5 | 37 | | } |
| | 38 | |
|
| | 39 | | } |
| | 40 | |
|
| | 41 | | } |