Exemplo n.º 1
0
        public virtual ActionResult Generate(string language, string version, string key, bool all)
        {
            if (DebugHelper.IsDebug() == false)
            {
                return(RedirectToAction(MVC.Docs.ActionNames.Index, MVC.Docs.Name));
            }

            var versionsToParse = new List <string>();

            if (all == false)
            {
                versionsToParse.Add(CurrentVersion);
            }

            var parser =
                new DocumentationParser(
                    new ParserOptions
            {
                PathToDocumentationDirectory = GetDocumentationDirectory(),
                VersionsToParse = versionsToParse,
                RootUrl         = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~")),
                ImagesUrl       = GetImagesUrl(HttpContext, DocumentStore, ArticleType.Documentation)
            }, new NoOpGitFileInformationProvider());

            DocumentSession.Delete(AttachmentsController.AttachmentsForDocumentationPrefix);

            var query = DocumentSession
                        .Advanced
                        .DocumentQuery <DocumentationPage>()
                        .GetIndexQuery();

            DocumentStore
            .Operations
            .Send(new DeleteByQueryOperation(query))
            .WaitForCompletion();

            query = DocumentSession
                    .Advanced
                    .DocumentQuery <TableOfContents>()
                    .GetIndexQuery();

            DocumentStore
            .Operations
            .Send(new DeleteByQueryOperation(query))
            .WaitForCompletion();

            DocumentSession.SaveChanges();

            var toDispose   = new List <IDisposable>();
            var attachments = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            try
            {
                DocumentSession.Store(new Attachment(), AttachmentsController.AttachmentsForDocumentationPrefix);

                foreach (var page in parser.Parse())
                {
                    DocumentSession.Store(page);

                    foreach (var image in page.Images)
                    {
                        var fileInfo = new FileInfo(image.ImagePath);
                        if (fileInfo.Exists == false)
                        {
                            continue;
                        }

                        var file = fileInfo.OpenRead();
                        toDispose.Add(file);

                        if (attachments.Add(image.ImageKey))
                        {
                            DocumentSession.Advanced.Attachments.Store(AttachmentsController.AttachmentsForDocumentationPrefix, image.ImageKey, file, AttachmentsController.FileExtensionToContentTypeMapping[fileInfo.Extension]);
                        }
                    }
                }

                foreach (var toc in parser.GenerateTableOfContents())
                {
                    DocumentSession.Store(toc);
                }

                DocumentSession.SaveChanges();
            }
            finally
            {
                foreach (var disposable in toDispose)
                {
                    disposable?.Dispose();
                }
            }

            if (string.IsNullOrEmpty(key))
            {
                return(RedirectToAction(MVC.Docs.ActionNames.ArticlePage, MVC.Docs.Name, new { language = language, version = version }));
            }

            while (true)
            {
                var stats = DocumentStore.Maintenance.Send(new GetStatisticsOperation());
                if (stats.StaleIndexes.Any() == false)
                {
                    break;
                }

                Thread.Sleep(500);
            }

            return(RedirectToAction(
                       MVC.Docs.ActionNames.ArticlePage,
                       MVC.Docs.Name,
                       new { language = CurrentLanguage, version = CurrentVersion, key = key }));
        }
Exemplo n.º 2
0
        public virtual ActionResult Generate(string language, string version, string key, bool all)
        {
            if (DebugHelper.IsDebug() == false)
            {
                return(RedirectToAction(MVC.Docs.ActionNames.Index, MVC.Docs.Name));
            }

            var versionsToParse = new List <string>();

            if (all == false)
            {
                versionsToParse.Add(CurrentVersion);
            }

            var parser =
                new DocumentationParser(
                    new ParserOptions
            {
                PathToDocumentationDirectory = GetDocumentationDirectory(),
                VersionsToParse = versionsToParse,
                RootUrl         = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~")),
                ImagesUrl       = GetImagesUrl(DocumentStore)
            }, new NoOpGitFileInformationProvider());


            foreach (var attachment in DocumentStore.DatabaseCommands.GetAttachments(0, Etag.Empty, 1024))
            {
                DocumentStore.DatabaseCommands.DeleteAttachment(attachment.Key, null);
            }

            DocumentStore
            .DatabaseCommands
            .DeleteByIndex("Raven/DocumentsByEntityName", new IndexQuery {
                Query = "Tag:DocumentationPages OR Tag:TableOfContents"
            })
            .WaitForCompletion();

            foreach (var page in parser.Parse())
            {
                DocumentSession.Store(page);

                Parallel.ForEach(
                    page.Images,
                    image =>
                {
                    if (System.IO.File.Exists(image.ImagePath) == false)
                    {
                        return;
                    }

                    using (var file = System.IO.File.OpenRead(image.ImagePath))
                    {
                        DocumentStore.DatabaseCommands.PutAttachment(image.ImageKey, null, file, new RavenJObject());
                    }
                });
            }

            foreach (var toc in parser.GenerateTableOfContents())
            {
                DocumentSession.Store(toc);
            }

            DocumentSession.SaveChanges();

            if (string.IsNullOrEmpty(key))
            {
                return(RedirectToAction(MVC.Docs.ActionNames.Welcome, MVC.Docs.Name, new { language = language, version = version }));
            }

            while (true)
            {
                var stats = DocumentStore.DatabaseCommands.GetStatistics();
                if (stats.StaleIndexes.Any() == false)
                {
                    break;
                }

                Thread.Sleep(500);
            }

            return(RedirectToAction(
                       MVC.Docs.ActionNames.Articles,
                       MVC.Docs.Name,
                       new { language = CurrentLanguage, version = CurrentVersion, key = key }));
        }