Exemplo n.º 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));
        }
Exemplo n.º 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));
                }
Exemplo n.º 3
0
        private static string ResolveRelativePath(PathKind kind, string path, string basePath, string baseDirectory)
        {
            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));
                }

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

                // ".."
                return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));

            case PathKind.RelativeToCurrentRoot:
                string baseRoot;
                if (basePath != null)
                {
                    baseRoot = PathUtilities.GetPathRoot(basePath);
                }
                else if (baseDirectory != null)
                {
                    baseRoot = PathUtilities.GetPathRoot(baseDirectory);
                }
                else
                {
                    return(null);
                }

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

                Debug.Assert(PathUtilities.IsDirectorySeparator(path[0]));
                Debug.Assert(path.Length == 1 || !PathUtilities.IsDirectorySeparator(path[1]));
                Debug.Assert(baseRoot.Length >= 3);
                return(PathUtilities.CombinePathsUnchecked(baseRoot, path.Substring(1)));

            case PathKind.RelativeToDriveDirectory:
                // drive relative paths not supported, can't resolve:
                return(null);

            case PathKind.Absolute:
                return(path);

            default:
                // EDMAURER this is not using ExceptionUtilities.UnexpectedValue() because this file
                // is shared via linking with other code that doesn't have the ExceptionUtilities.
                throw new InvalidOperationException(string.Format("Unexpected PathKind {0}.", kind));
            }
        }
Exemplo n.º 4
0
        private static string ResolveRelativePath(PathKind kind, string path, string basePath, string baseDirectory)
        {
            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));
                }

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

                // ".."
                return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));

            case PathKind.RelativeToCurrentRoot:
                string baseRoot;
                if (basePath != null)
                {
                    baseRoot = PathUtilities.GetPathRoot(basePath);
                }
                else if (baseDirectory != null)
                {
                    baseRoot = PathUtilities.GetPathRoot(baseDirectory);
                }
                else
                {
                    return(null);
                }

                if (string.IsNullOrEmpty(baseRoot))
                {
                    return(null);
                }

                Debug.Assert(PathUtilities.IsDirectorySeparator(path[0]));
                Debug.Assert(path.Length == 1 || !PathUtilities.IsDirectorySeparator(path[1]));
                return(PathUtilities.CombinePathsUnchecked(baseRoot, path.Substring(1)));

            case PathKind.RelativeToDriveDirectory:
                // drive relative paths not supported, can't resolve:
                return(null);

            case PathKind.Absolute:
                return(path);

            default:
                throw ExceptionUtilities.UnexpectedValue(kind);
            }
        }