/// <summary>
        /// Get file path with fallback
        /// </summary>
        /// <param name="relativePath">original relative path in markdown.</param>
        /// <param name="context">markdown context</param>
        /// <returns>item1: acutal file path. item: true if it hit fallback file. Otherwise false</returns>
        public static Tuple <string, bool> GetFilePathWithFallback(string relativePath, IMarkdownContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (relativePath == null)
            {
                throw new ArgumentNullException(nameof(relativePath));
                throw new FileNotFoundException($"Couldn't resolve path {relativePath}.");
            }

            // var currentFileFolder = Path.Combine(context.GetBaseFolder(), Path.Combine(context.GetFilePathStack().Select(path => Path.GetDirectoryName(path)).ToArray()));
            // var originalFilePath = Path.Combine(context.GetBaseFolder(),  orginalRelativePath);
            var    filePathToDocset            = relativePath;
            string parentFileDirectoryToDocset = context.GetBaseFolder();
            var    parents = context.GetFilePathStack();

            if (parents != null)
            {
                var parent = parents.Peek();
                filePathToDocset            = (RelativePath)parent + (RelativePath)filePathToDocset;
                parentFileDirectoryToDocset = Path.GetDirectoryName(Path.Combine(context.GetBaseFolder(), parent));
            }

            var  originalFilePath = Path.Combine(context.GetBaseFolder(), filePathToDocset);
            var  actualFilePath   = originalFilePath;
            bool hitFallback      = false;

            if (!File.Exists(originalFilePath))
            {
                var fallbackFolders = context.GetFallbackFolders();
                foreach (var folder in fallbackFolders)
                {
                    var fallbackFilePath         = Path.Combine(folder, filePathToDocset);
                    var fallbackFileRelativePath = PathUtility.MakeRelativePath(parentFileDirectoryToDocset, fallbackFilePath);
                    context.ReportDependency(fallbackFileRelativePath); // All the high priority fallback files should be reported to the dependency.
                    if (File.Exists(fallbackFilePath))
                    {
                        actualFilePath = fallbackFilePath;
                        hitFallback    = true;
                        break;
                    }
                }

                if (!hitFallback)
                {
                    if (fallbackFolders.Count > 0)
                    {
                        throw new FileNotFoundException($"Couldn't find file {filePathToDocset}. Fallback folders: {string.Join(",", fallbackFolders)}", filePathToDocset);
                    }
                    throw new FileNotFoundException($"Couldn't find file {filePathToDocset}.", originalFilePath);
                }
            }

            return(Tuple.Create(actualFilePath, hitFallback));
        }
Exemple #2
0
        /// <summary>
        /// Get file path with fallback
        /// </summary>
        /// <param name="relativePath">original relative path in markdown.</param>
        /// <param name="context">markdown context</param>
        /// <returns>item1: acutal file path. item: true if it hit fallback file. Otherwise false</returns>
        public static Tuple<string, bool> GetFilePathWithFallback(string relativePath, IMarkdownContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (relativePath == null)
            {
                throw new ArgumentNullException(nameof(relativePath));
                throw new FileNotFoundException($"Couldn't resolve path {relativePath}.");
            }

            // var currentFileFolder = Path.Combine(context.GetBaseFolder(), Path.Combine(context.GetFilePathStack().Select(path => Path.GetDirectoryName(path)).ToArray()));
            // var originalFilePath = Path.Combine(context.GetBaseFolder(),  orginalRelativePath);
            var filePathToDocset = relativePath;
            string parentFileDirectoryToDocset = context.GetBaseFolder();
            var parents = context.GetFilePathStack();
            if(parents != null)
            {
                var parent = parents.Peek();
                filePathToDocset = (RelativePath)parent + (RelativePath)filePathToDocset;
                parentFileDirectoryToDocset = Path.GetDirectoryName(Path.Combine(context.GetBaseFolder(), parent));
            }

            var originalFilePath = Path.Combine(context.GetBaseFolder(), filePathToDocset);
            var actualFilePath = originalFilePath;
            bool hitFallback = false;
            if (!File.Exists(originalFilePath))
            {
                var fallbackFolders = context.GetFallbackFolders();
                foreach (var folder in fallbackFolders)
                {
                    var fallbackFilePath = Path.Combine(folder, filePathToDocset);
                    var fallbackFileRelativePath = PathUtility.MakeRelativePath(parentFileDirectoryToDocset, fallbackFilePath);
                    context.ReportDependency(fallbackFileRelativePath); // All the high priority fallback files should be reported to the dependency.
                    if (File.Exists(fallbackFilePath))
                    {
                        actualFilePath = fallbackFilePath;
                        hitFallback = true;
                        break;
                    }
                }

                if (!hitFallback)
                {
                    if (fallbackFolders.Count > 0)
                    {
                        throw new FileNotFoundException($"Couldn't find file {filePathToDocset}. Fallback folders: {string.Join(",", fallbackFolders)}", filePathToDocset);
                    }
                    throw new FileNotFoundException($"Couldn't find file {filePathToDocset}.", originalFilePath);
                }
            }

            return Tuple.Create(actualFilePath, hitFallback);
        }
Exemple #3
0
        private static Tuple <string, bool> FindInFallbackFolders(IMarkdownContext context, RelativePath filePathToDocset, string parentFileDirectoryToDocset, string originalFullPath)
        {
            var fallbackFolders = context.GetFallbackFolders();

            foreach (var folder in fallbackFolders)
            {
                var fallbackFilePath         = Path.Combine(folder, filePathToDocset);
                var fallbackFileRelativePath = PathUtility.MakeRelativePath(parentFileDirectoryToDocset, fallbackFilePath);
                context.ReportDependency(fallbackFileRelativePath); // All the high priority fallback files should be reported to the dependency.
                if (EnvironmentContext.FileAbstractLayer.Exists(fallbackFilePath))
                {
                    return(Tuple.Create(fallbackFilePath, true));
                }
            }
            if (fallbackFolders.Count > 0)
            {
                throw new FileNotFoundException($"Couldn't find file {filePathToDocset}. Fallback folders: {string.Join(",", fallbackFolders)}", filePathToDocset);
            }
            throw new FileNotFoundException($"Couldn't find file {filePathToDocset}.", originalFullPath);
        }