Example #1
0
        /// <summary>
        /// Resolves relative path and returns absolute path.
        /// The method depends only on values of its parameters and their implementation (for fileExists).
        /// It doesn't itself depend on the state of the current process (namely on the current drive directories) or
        /// the state of file system.
        /// </summary>
        /// <param name="path">
        /// Path to resolve.
        /// </param>
        /// <param name="basePath">
        /// Base file path to resolve CWD-relative paths against. Null if not available.
        /// </param>
        /// <param name="baseDirectory">
        /// Base directory to resolve CWD-relative paths against if <paramref name="basePath"/> isn't specified.
        /// Must be absolute path.
        /// Null if not available.
        /// </param>
        /// <param name="searchPaths">
        /// Sequence of paths used to search for unqualified relative paths.
        /// </param>
        /// <param name="fileExists">
        /// Method that tests existence of a file.
        /// </param>
        /// <returns>
        /// The resolved path or null if the path can't be resolved.
        /// </returns>
        internal static string ResolveRelativePath(
            string path,
            string basePath,
            string baseDirectory,
            IEnumerable <string> searchPaths,
            Func <string, bool> fileExists)
        {
            Debug.Assert(baseDirectory == null || searchPaths != null || PathUtilities.IsAbsolute(baseDirectory));
            Debug.Assert(searchPaths != null);
            Debug.Assert(fileExists != null);

            var kind = PathUtilities.GetPathKind(path);

            if (kind == PathKind.Relative)
            {
                // first, look in the base directory:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory != null)
                {
                    string combinedPath = PathUtilities.CombinePathsUnchecked(baseDirectory, path);
                    Debug.Assert(PathUtilities.IsAbsolute(combinedPath));

                    if (fileExists == null || fileExists(combinedPath))
                    {
                        return(combinedPath);
                    }
                }

                // try search paths:
                foreach (var searchPath in searchPaths)
                {
                    string combinedPath = PathUtilities.CombinePathsUnchecked(searchPath, path);

                    Debug.Assert(PathUtilities.IsAbsolute(combinedPath));
                    if (fileExists == null || fileExists(combinedPath))
                    {
                        return(combinedPath);
                    }
                }

                return(null);
            }

            return(ResolveRelativePath(kind, path, basePath, baseDirectory));
        }
Example #2
0
        private static string?ResolveRelativePath(
            PathKind kind,
            string?path,
            string?basePath,
            string?baseDirectory
            )
        {
            Debug.Assert(PathUtilities.GetPathKind(path) == kind);

            switch (kind)
            {
            case PathKind.Empty:
                return(null);

            case PathKind.Relative:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory == null)
                {
                    return(null);
                }

                // with no search paths relative paths are relative to the base directory:
                return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));

            case PathKind.RelativeToCurrentDirectory:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory == null)
                {
                    return(null);
                }

                if (path !.Length == 1)
                {
                    // "."
                    return(baseDirectory);
                }
                else
                {
                    // ".\path"
                    return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));
                }
Example #3
0
 internal static string ResolveRelativePath(string path, string basePath, string baseDirectory)
 {
     Debug.Assert(baseDirectory == null || PathUtilities.IsAbsolute(baseDirectory));
     return(ResolveRelativePath(PathUtilities.GetPathKind(path), path, basePath, baseDirectory));
 }