/// <summary>
        /// Enumerate directories and files under the project path of |projet|.
        /// </summary>
        private IEnumerable <TraversedDirectoryEntry> TraverseFileSystem(IFileSystemNameFactory fileNameFactory, IProject project, DirectoryName projectPath)
        {
            Debug.Assert(projectPath.IsAbsoluteName);
            var stack = new Stack <DirectoryName>();

            stack.Push(projectPath);
            while (stack.Count > 0)
            {
                var head = stack.Pop();
                if (head.IsAbsoluteName || project.DirectoryFilter.Include(head.RelativePathName.RelativeName))
                {
                    IList <string> childDirectories;
                    IList <string> childFiles;
                    RelativePathNameExtensions.GetFileSystemEntries(project.RootPath, head.RelativePathName, out childDirectories, out childFiles);
                    // Note: Use "for" loop to avoid memory allocations.
                    for (var i = 0; i < childDirectories.Count; i++)
                    {
                        stack.Push(fileNameFactory.CreateDirectoryName(head, childDirectories[i]));
                    }
                    // Note: Use "for" loop to avoid memory allocations.
                    var childFilenames = new FileName[childFiles.Count];
                    for (var i = 0; i < childFiles.Count; i++)
                    {
                        childFilenames[i] = fileNameFactory.CreateFileName(head, childFiles[i]);
                    }
                    yield return(new TraversedDirectoryEntry(head, childFilenames));
                }
            }
        }
        /// <summary>
        /// Return the |FileName| instance corresponding to the full path |path|. Returns |null| if |path|
        /// is invalid or not part of a project.
        /// </summary>
        public static Tuple <IProject, FileName> GetProjectFileName(IFileSystemNameFactory fileSystemNameFactory, IProjectDiscovery projectDiscovery, FullPathName path)
        {
            var project = projectDiscovery.GetProject(path);

            if (project == null)
            {
                return(null);
            }

            var rootPath   = project.RootPath;
            var rootLength = rootPath.FullName.Length + 1;

            if (rootPath.FullName.Last() == Path.DirectorySeparatorChar)
            {
                rootLength--;
            }

            var directoryName = fileSystemNameFactory.CreateAbsoluteDirectoryName(rootPath);
            var relativePath  = path.FullName.Substring(rootLength);
            var items         = relativePath.Split(new char[] {
                Path.DirectorySeparatorChar
            });

            foreach (var item in items)
            {
                if (item == items.Last())
                {
                    return(Tuple.Create(project, fileSystemNameFactory.CreateFileName(directoryName, item)));
                }

                directoryName = fileSystemNameFactory.CreateDirectoryName(directoryName, item);
            }
            return(null);
        }
        /// <summary>
        /// Return the |FileName| instance corresponding to the full path |path|.
        /// Returns |null| if |path| is invalid or not part of a project.
        /// </summary>
        public static ProjectFileName GetProjectFileName(IFileSystemNameFactory fileSystemNameFactory, IProjectDiscovery projectDiscovery, FullPath path)
        {
            var project = projectDiscovery.GetProject(path);

            if (project == null)
            {
                return(default(ProjectFileName));
            }

            var split        = PathHelpers.SplitPrefix(path.Value, project.RootPath.Value);
            var relativePath = split.Suffix;

            var directoryName = fileSystemNameFactory.CreateAbsoluteDirectoryName(project.RootPath);
            var names         = relativePath.Split(Path.DirectorySeparatorChar);

            foreach (var name in names)
            {
                if (name == names.Last())
                {
                    return(new ProjectFileName(project, fileSystemNameFactory.CreateFileName(directoryName, name)));
                }

                directoryName = fileSystemNameFactory.CreateDirectoryName(directoryName, name);
            }
            return(default(ProjectFileName));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Return the <see cref="ProjectFileName"/> instance of the project file <paramref name="relativePath"/>.
        /// Returns the default value if <paramref name="relativePath"/> is invalid or not part of a project.
        /// </summary>
        public static ProjectFileName CreateProjectFileNameFromRelativePath(
            this IFileSystemNameFactory fileSystemNameFactory, IProject project, string relativePath)
        {
            if (project == null)
            {
                throw new ArgumentNullException();
            }
            if (string.IsNullOrEmpty(relativePath))
            {
                return(default(ProjectFileName));
            }

            var directoryName = fileSystemNameFactory.CreateAbsoluteDirectoryName(project.RootPath);
            var names         = PathHelpers.SplitPath(relativePath).ToList();

            foreach (var name in names)
            {
                if (name == names.Last())
                {
                    return(new ProjectFileName(project, fileSystemNameFactory.CreateFileName(directoryName, name)));
                }

                directoryName = fileSystemNameFactory.CreateDirectoryName(directoryName, name);
            }

            Debug.Assert(false, "Unreachable code");
            throw new InvalidOperationException();
        }
Exemplo n.º 5
0
        public FileName CreateFileName(DirectoryName parent, string simpleName)
        {
            var directory = FindDirectory(parent);

            if (directory != null)
            {
                // Note: We found the corresponding parent, we just need to check the "simple" name part of the relative name.
                int index = SortedArray.BinarySearch(directory.Files, simpleName, FileComparer);
                if (index >= 0)
                {
                    return(directory.Files[index]);
                }
            }
            return(_previous.CreateFileName(parent, simpleName));
        }