Example #1
0
 /// <summary>
 /// Get the relative path to another directory.
 /// </summary>
 /// <param name="to">The target directory path.</param>
 /// <returns>A <see cref="DirectoryPath"/>.</returns>
 public DirectoryPath GetRelativePath(DirectoryPath to)
 {
     return(GetDirectory().GetRelativePath(to));
 }
Example #2
0
        private List <Path> Walk(DirectoryPath rootPath, Stack <Node> segments, Func <IFileSystemInfo, bool> predicate)
        {
            var results = new List <Path>();
            var segment = segments.Pop();

            var expression          = new Regex("^" + segment.Render() + "$", _options);
            var isDirectoryWildcard = false;

            if (segment is WildcardSegmentNode)
            {
                segments.Push(segment);
                isDirectoryWildcard = true;
            }

            // Get all files and folders.
            var root = _fileSystem.GetDirectory(rootPath);

            if (!root.Exists)
            {
                return(results);
            }
            var rootFullPath = PathCollapser.Collapse(rootPath);

            foreach (var directory in root.GetDirectories("*", SearchScope.Current, predicate))
            {
                var part     = directory.Path.FullPath.Substring(rootFullPath.Length + 1);
                var pathTest = expression.IsMatch(part);

                var subWalkCount = 0;

                if (isDirectoryWildcard)
                {
                    // Walk recursivly down the segment.
                    var nextSegments  = new Stack <Node>(segments.Reverse());
                    var subwalkResult = Walk(directory.Path, nextSegments, predicate);
                    if (subwalkResult.Count > 0)
                    {
                        results.AddRange(subwalkResult);
                    }

                    subWalkCount++;
                }

                // Check without directory wildcard.
                if (segments.Count > subWalkCount && (subWalkCount == 1 || pathTest))
                {
                    // Walk the next segment in the list.
                    var nextSegments  = new Stack <Node>(segments.Skip(subWalkCount).Reverse());
                    var subwalkResult = Walk(directory.Path, nextSegments, predicate);
                    if (subwalkResult.Count > 0)
                    {
                        results.AddRange(subwalkResult);
                    }
                }

                // Got a match?
                if (pathTest && segments.Count == 0)
                {
                    results.Add(directory.Path);
                }
            }

            foreach (var file in root.GetFiles("*", SearchScope.Current, predicate))
            {
                var part     = file.Path.FullPath.Substring(rootFullPath.Length + 1);
                var pathTest = expression.IsMatch(part);

                // Got a match?
                if (pathTest && segments.Count == 0)
                {
                    results.Add(file.Path);
                }
                else if (pathTest)
                {
                    /////////////////////////////////////////////////////////////B
                    // We got a match, but we still have segments left.
                    // Is the next part a directory wild card?
                    /////////////////////////////////////////////////////////////

                    var nextNode = segments.Peek();
                    if (nextNode is WildcardSegmentNode)
                    {
                        var nextSegments  = new Stack <Node>(segments.Skip(1).Reverse());
                        var subwalkResult = Walk(root.Path, nextSegments, predicate);
                        if (subwalkResult.Count > 0)
                        {
                            results.AddRange(subwalkResult);
                        }
                    }
                }
            }
            return(results);
        }
Example #3
0
 public Directory(DirectoryPath path)
 {
     Path       = path;
     _directory = new DirectoryInfo(Path.FullPath);
 }
Example #4
0
 /// <summary>
 /// Get the relative path to another directory.
 /// </summary>
 /// <param name="to">The target directory path.</param>
 /// <returns>A <see cref="DirectoryPath"/>.</returns>
 public DirectoryPath GetRelativePath(DirectoryPath to)
 {
     return(RelativePathResolver.Resolve(this, to));
 }