private IEnumerable <PageTemplateFile> SearchDirectoryForPageTemplateFiles(string directoryPath, string searchText)
        {
            var directoryContents = _resourceLocator.GetDirectory(directoryPath);

            foreach (var file in directoryContents.Where(f => !f.IsDirectory))
            {
                // filename contains the search text and is located in a 'PageTemplates' folder, but not a 'partials' folder and has the extension .cshtml
                if (Contains(file.Name, searchText) &&
                    !Contains(file.Name, "_ViewStart") &&
                    !Contains(file.Name, "_ViewImports") &&
                    file.Name.EndsWith(VIEW_FILE_EXTENSION, StringComparison.OrdinalIgnoreCase))
                {
                    yield return(MapPageTemplateFile(directoryPath, file));
                }
            }

            foreach (var childDirectoryName in directoryContents
                     .Where(f => f.IsDirectory)
                     .Select(f => f.Name)
                     )
            {
                var childDirectoryPath = FilePathHelper.CombineVirtualPath(directoryPath, childDirectoryName);
                foreach (var file in SearchDirectoryForPageTemplateFiles(childDirectoryPath, searchText))
                {
                    yield return(file);
                }
            }
        }
Beispiel #2
0
        private async Task ProcessFiles(
            Dictionary <string, DocumentationNode> resultNodes,
            DocumentationNode parentNode,
            string[] allFilePaths,
            Dictionary <string, string> redirects)
        {
            foreach (var filePath in allFilePaths)
            {
                // slug path
                var fileExtension                = Path.GetExtension(filePath);
                var fileNameWithoutExtension     = Path.GetFileNameWithoutExtension(filePath);
                var sluggedFileName              = SlugFormatter.ToSlug(fileNameWithoutExtension);
                var sluggedFileNameWithExtension = Path.ChangeExtension(sluggedFileName, fileExtension);

                // skip the file if there's already a redirect
                if (redirects.Any(r => SlugFormatter.ToSlug(r.Key) == sluggedFileName))
                {
                    continue;
                }

                if (fileExtension == MARKDOWN_FILE_EXTENSION)
                {
                    var destinationFilePath = FilePathHelper.CombineVirtualPath(parentNode.Url, sluggedFileNameWithExtension);
                    var updateDate          = File.GetLastWriteTimeUtc(filePath);

                    // if we have a custom index file, map this to the container directory
                    if (sluggedFileName == "index")
                    {
                        parentNode.DocumentFilePath = destinationFilePath;
                        parentNode.UpdateDate       = updateDate;
                    }
                    else
                    {
                        var node = new DocumentationNode()
                        {
                            Title            = fileNameWithoutExtension,
                            Url              = FilePathHelper.CombineVirtualPath(parentNode.Url, sluggedFileName),
                            DocumentFilePath = destinationFilePath,
                            UpdateDate       = updateDate
                        };

                        resultNodes.Add(sluggedFileName, node);
                    }

                    await _fileWriterService.CopyFile(filePath, destinationFilePath);
                }
                else if (fileExtension != ".json")
                {
                    // static files are served out of a separate directory
                    var destinationDirectory = FilePathHelper.CombineVirtualPath(STATIC_FOLDER_NAME, parentNode.Url);
                    var destinationPath      = FilePathHelper.CombineVirtualPath(destinationDirectory, sluggedFileNameWithExtension);

                    await _fileWriterService.EnsureDirectoryExistsAsync(destinationDirectory);

                    await _fileWriterService.CopyFile(filePath, destinationPath);
                }

                // json files ignore, they are assumed to be config
            }
        }
Beispiel #3
0
        private async Task ProcessChildDirectories(Dictionary <string, DocumentationNode> resultNodes, DocumentationNode parentNode, string directoryPath)
        {
            foreach (var childDirectoryPath in Directory.GetDirectories(directoryPath))
            {
                var directory = new DirectoryInfo(childDirectoryPath);
                var slug      = SlugFormatter.ToSlug(directory.Name);

                var childNode = new DocumentationNode()
                {
                    Title = directory.Name,
                    Url   = FilePathHelper.CombineVirtualPath(parentNode.Url, slug)
                };

                childNode.UpdateDate = Directory.GetLastWriteTimeUtc(childDirectoryPath);

                await _fileWriterService.EnsureDirectoryExistsAsync(childNode.Url);
                await ProcessDirectory(childDirectoryPath, childNode);

                // Only add the node if it represents something in the document tree
                // i.e. skip static resource directories.
                if (childNode.Children.Any() ||
                    childNode.DocumentFilePath != null ||
                    childNode.RedirectTo != null)
                {
                    resultNodes.Add(slug, childNode);
                }
            }
        }
        private PageBlockTypeFileLocation CreateTemplateFile(string directoryPath, IFileInfo file)
        {
            var templateFile = new PageBlockTypeFileLocation();

            templateFile.Path     = FilePathHelper.CombineVirtualPath(directoryPath, file.Name);
            templateFile.FileName = Path.GetFileNameWithoutExtension(file.Name);

            var templatePath = FilePathHelper.CombineVirtualPath(directoryPath, TEMPLATES_FOLDER_NAME);

            var templateDirectory = _resourceLocator.GetDirectory(templatePath);

            if (templateDirectory != null)
            {
                templateFile.Templates = FilterViewFiles(templateDirectory)
                                         .GroupBy(t => t.Name, (k, v) => v.FirstOrDefault()) // De-dup
                                         .Select(t => new PageBlockTypeTemplateFileLocation()
                {
                    FileName = Path.GetFileNameWithoutExtension(t.Name),
                    Path     = FilePathHelper.CombineVirtualPath(templatePath, t.Name)
                })
                                         .ToDictionary(t => t.FileName);
            }
            else
            {
                templateFile.Templates = new Dictionary <string, PageBlockTypeTemplateFileLocation>();
            }

            return(templateFile);
        }
Beispiel #5
0
 /// <summary>
 /// Constructor to use when you want to provide a custom resource path prefix, for
 /// when your module is located in a non-standard directory (or for all internal
 /// modules where you pass RouteConstants.InternalModuleResourcePathPrefix).
 /// </summary>
 /// <param name="routePrefix">
 /// The unique entity/folder name of your route to use when building
 /// urls e.g. products, users, honeybagders.
 /// </param>
 /// <param name="resourcePathPrefix">The path prefix to your module e.g. '/admin/modules/'</param>
 public ModuleRouteLibrary(
     string routePrefix,
     string resourcePathPrefix
     )
 {
     ModuleFolderName     = TextFormatter.Pascalize(routePrefix);
     ResourcePrefix       = resourcePathPrefix + ModuleFolderName + "/";
     StaticResourcePrefix = FilePathHelper.CombineVirtualPath(ResourcePrefix, CONTENT_FOLDER);
     UrlPrefix            = RouteConstants.AdminAreaPrefix + "/" + routePrefix;
 }
        private PageTemplateFile MapPageTemplateFile(string virtualDirectoryPath, IFileInfo file)
        {
            var fileName     = Path.ChangeExtension(file.Name, null).TrimStart(TEMPLATE_NAME_CHAR_TO_TRIM);
            var virtualPath  = FilePathHelper.CombineVirtualPath(virtualDirectoryPath, file.Name);
            var templateFile = new PageTemplateFile()
            {
                FileName    = fileName,
                VirtualPath = virtualPath
            };

            return(templateFile);
        }
Beispiel #7
0
        private static async Task <Dictionary <string, string> > ProcessRedirects(
            Dictionary <string, DocumentationNode> resultNodes,
            DocumentationNode parentNode,
            string[] allFilePaths
            )
        {
            Dictionary <string, string> redirects = null;
            var      redirectsFile = allFilePaths.SingleOrDefault(f => Path.GetFileName(f) == "redirects.json");
            DateTime updateDate;

            if (!string.IsNullOrEmpty(redirectsFile))
            {
                redirects = await DeserializeJsonFile <Dictionary <string, string> >(redirectsFile);

                updateDate = File.GetLastWriteTimeUtc(redirectsFile);
            }
            else
            {
                redirects = new Dictionary <string, string>();
                return(redirects);
            }

            var directoryRedirectRule = redirects.GetOrDefault("*");

            if (directoryRedirectRule != null)
            {
                // If we have a directory level redirect rule, assign it to the
                // the directory node and return
                parentNode.RedirectTo = directoryRedirectRule;
                parentNode.UpdateDate = updateDate;

                return(redirects);
            }

            foreach (var redirect in redirects)
            {
                var slug       = SlugFormatter.ToSlug(redirect.Key);
                var path       = FilePathHelper.CombineVirtualPath(parentNode.Url, slug);
                var redirectTo = FilePathHelper.CombineVirtualPath(parentNode.Url, redirect.Value);
                var node       = new DocumentationNode()
                {
                    Title      = redirect.Key,
                    Url        = path,
                    RedirectTo = redirectTo,
                    UpdateDate = updateDate
                };

                resultNodes.Add(slug, node);
            }

            return(redirects ?? new Dictionary <string, string>());
        }
Beispiel #8
0
        /// <summary>
        /// Constructor to use when you want to provide a custom resource path prefix, for
        /// when your module is located in a non-standard directory (or for all internal
        /// modules where you pass RouteConstants.InternalModuleResourcePathPrefix).
        /// </summary>
        /// <param name="routePrefix">
        /// The unique entity/folder name of your route to use when building
        /// urls e.g. products, users, honeybagders.
        /// </param>
        /// <param name="resourcePathPrefix">The path prefix to your module e.g. '/admin/modules/'</param>
        public ModuleRouteLibrary(
            AdminSettings adminSettings,
            string routePrefix,
            string resourcePathPrefix
            )
        {
            ModuleFolderName     = TextFormatter.Pascalize(routePrefix);
            ResourcePrefix       = resourcePathPrefix + ModuleFolderName + "/";
            StaticResourcePrefix = FilePathHelper.CombineVirtualPath(ResourcePrefix, CONTENT_FOLDER);
            UrlPrefix            = routePrefix;

            _adminSettings = adminSettings;
        }
        private Dictionary <string, PageBlockTypeFileLocation> GetPageBlockTypeFileLocations()
        {
            var viewDirectoryPaths = _pageBlockTypeViewLocationRegistrations
                                     .SelectMany(r => r.GetPathPrefixes())
                                     .Select(p => FormatViewFolder(p))
            ;

            var templateFiles    = new Dictionary <string, PageBlockTypeFileLocation>();
            var foldersToExclude = new string[] { TEMPLATES_FOLDER_NAME, PARTIALS_FOLDER_NAME };

            foreach (var directoryPath in viewDirectoryPaths)
            {
                var directory = _resourceLocator.GetDirectory(directoryPath);
                if (!directory.Exists)
                {
                    continue;
                }

                foreach (var viewFile in FilterViewFiles(directory))
                {
                    AddTemplateToDictionary(templateFiles, directoryPath, viewFile);
                }

                foreach (var childDirectory in directory
                         .Where(d => d.IsDirectory &&
                                !foldersToExclude.Any(f => d.Name.Equals(f, StringComparison.OrdinalIgnoreCase))))
                {
                    var childDirectoryPath = FilePathHelper.CombineVirtualPath(directoryPath, childDirectory.Name);

                    foreach (var viewFile in FilterChildDirectoryFiles(childDirectoryPath, directory, foldersToExclude))
                    {
                        AddTemplateToDictionary(templateFiles, childDirectoryPath, viewFile);
                    }
                }
            }

            return(templateFiles);
        }
Beispiel #10
0
        public async Task GenerateAsync()
        {
            var version    = _docGeneratorSettings.Version;
            var sourcePath = _docGeneratorSettings.SourcePath;

            var staticFileFolder = FilePathHelper.CombineVirtualPath(STATIC_FOLDER_NAME, version);

            await _fileWriterService.EnsureDirectoryExistsAsync(version);

            await _fileWriterService.EnsureDirectoryExistsAsync(staticFileFolder);

            if (_docGeneratorSettings.CleanDestination)
            {
                await _fileWriterService.ClearDirectoryAsync(version);

                await _fileWriterService.ClearDirectoryAsync(staticFileFolder);
            }

            var rootNode = new DocumentationNode()
            {
                Title      = "Docs",
                Url        = "/" + version,
                UpdateDate = DateTime.UtcNow
            };

            // Copy files/directories recursively
            await ProcessDirectory(sourcePath, rootNode);

            // Write the completed table of contents file
            var serialized = JsonConvert.SerializeObject(rootNode, GetJsonSetting());
            await _fileWriterService.WriteText(serialized, FilePathHelper.CombineVirtualPath(rootNode.Url, "toc.json"));

            // update the version manifest file in the root
            await UpdateVersionsAsync();

            await PublishWebHook();
        }
Beispiel #11
0
 public string GetStaticResourceUrlPath()
 {
     return(FilePathHelper.CombineVirtualPath(_adminSettings.DirectoryName, StaticResourcePrefix));
 }
Beispiel #12
0
 public string CssDirectory()
 {
     return(FilePathHelper.CombineVirtualPath(_adminSettings.DirectoryName, StaticResourcePrefix, "css"));
 }
Beispiel #13
0
 public string StaticResourceFilePath(string fileName)
 {
     return(FilePathHelper.CombineVirtualPath(StaticResourcePrefix, fileName));
 }
Beispiel #14
0
        public void CombineVirtualPath_WhenValid_ReturnsCombined(string path1, string path2, string path3, string expected)
        {
            var result = FilePathHelper.CombineVirtualPath(path1, path2, path3);

            Assert.Equal(expected, result);
        }
Beispiel #15
0
 public string CssFile(string fileName)
 {
     return(FilePathHelper.CombineVirtualPath(StaticResourcePrefix, "css", fileName + ".css"));
 }
Beispiel #16
0
 public string CssDirectory()
 {
     return(FilePathHelper.CombineVirtualPath(StaticResourcePrefix, "css"));
 }
Beispiel #17
0
 public string StaticResource(string fileName)
 {
     return(FilePathHelper.CombineVirtualPath(_adminSettings.DirectoryName, StaticResourcePrefix, fileName));
 }
Beispiel #18
0
        public void CombineVirtualPath_WhenMultipleNullOrWhitespace_ReturnsPath(string s)
        {
            var result = FilePathHelper.CombineVirtualPath(s, s, s);

            Assert.Equal("/", result);
        }