Beispiel #1
0
        public async Task GetLatestContent(IApplicationConfiguration config)
        {
            if (!String.IsNullOrWhiteSpace(config.GitHubToken))
            {
                var syncTasks = config.Sync.remoteFolders.Select(folder => {
                    var localDirectory = new DirectoryInfo(Path.Combine(config.Sync.locaStoragePath, folder));
                    if (!localDirectory.Exists)
                    {
                        localDirectory.Create();
                    }

                    var githubMirror = GitHubMirror.Create(config.Sync, folder, config.GitHubToken);

                    if (folder == "_posts")
                    {
                        return(githubMirror.SynchronizeAsync().ContinueWith(t => {
                            var deletePosts = db.Posts
                                              .Select(p => p.FileLocation)
                                              .Select(f => Path.Combine(siteBasePath, f))
                                              .Where(f => !File.Exists(f))
                                              .ToArray();

                            var postsUpdater = new PostUpdater(db);
                            foreach (var file in deletePosts)
                            {
                                postsUpdater.FileDeleted(file);
                            }

                            foreach (var file in Directory.EnumerateFiles(localDirectory.FullName, "*", SearchOption.AllDirectories))
                            {
                                postsUpdater.FileUpdated(file);
                            }
                        }));
                    }
                    else if (folder == "_pages")
                    {
                        return(githubMirror.SynchronizeAsync().ContinueWith(t => {
                            var deletePages = db.Pages
                                              .Select(p => p.FileLocation)
                                              .Select(f => Path.Combine(siteBasePath, f))
                                              .Where(f => !File.Exists(f))
                                              .ToArray();

                            var pagesUpdater = new PageUpdater(db);
                            foreach (var file in deletePages)
                            {
                                pagesUpdater.FileDeleted(file);
                            }

                            foreach (var file in Directory.EnumerateFiles(localDirectory.FullName, "*", SearchOption.AllDirectories))
                            {
                                pagesUpdater.FileUpdated(file);
                            }
                        }));
                    }

                    return(githubMirror.SynchronizeAsync(false));
                });

                await Task.WhenAll(syncTasks);

                await config.ScanEnvironmentConfigFile();

                var atomFeed = GetAtomFeed(config);
                using (var xmlWriter = XmlWriter.Create(Path.Combine(siteBasePath, "App_Data/Content/atom.xml"))) {
                    atomFeed.SaveAsAtom10(xmlWriter);
                }

                var sitemap = GetSitemap(config);
                File.WriteAllText(Path.Combine(siteBasePath, "App_Data/Content/sitemap.xml"), sitemap.ToString());
            }
        }