示例#1
0
        public async Task Setup()
        {
            RootPath = Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "fstest");
            FSStaticBuilder.EnsureDirectoryExists(RootPath);

            PostData        = Enumerable.Range(0, 100).Select(x => Generator.GetPost()).ToArray();
            LayoutData      = Enumerable.Range(0, 100).Select(x => Generator.GetLayout()).ToArray();
            PageData        = Enumerable.Range(0, 100).Select(x => Generator.GetPage()).ToArray();
            FileData        = Enumerable.Range(0, 100).Select(x => Generator.GetFile()).ToArray();
            BlogOptionsData = new BlogOptions();

            BlogBuilder builder = new BlogBuilder(BlogOptionsData, RootPath);
            await builder.Build();

            await builder.BuildPosts(PostData);

            await builder.BuildLayouts(LayoutData);

            await builder.BuildPages(PageData);

            await builder.BuildFiles(FileData);

            BlogService = new FileSystemBlogService(new PhysicalFileProvider(RootPath).AsFileProvider());
        }
示例#2
0
        // full: delete diff post for api remote
        public async Task Push(string name = "", bool full = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                name = Option.CurrentRemote;
            }

            Logger.LogInformation($"Push to remote {name}.");

            if (Option.Remotes.TryGetValue(name, out var remote))
            {
                Logger.LogInformation($"Detect remote {remote.Name} ({Enum.GetName(typeof(RemoteType), remote.Type)}).");
                switch (remote.Type)
                {
                case RemoteType.LocalFS:
                {
                    await toLocalFS(remote);
                }
                break;

                case RemoteType.RemoteFS:
                {
                    throw new NotSupportedException("Not support pushing to remote file system, please push to local file system and sync to remote.");
                }

                case RemoteType.Api:
                {
                    await Connect(name);

                    Logger.LogInformation($"Fetch remote posts.");

                    await Remote.SetOptions(await Local.GetOptions());

                    await SyncRecordRepository(Local.PostService, Remote.PostService, full);

                    await SyncRecordRepository(Local.PageService, Remote.PageService, full);

                    await SyncRecordRepository(Local.LayoutService, Remote.LayoutService, full);
                }
                break;

                case RemoteType.Git:
                {
                    await Connect(name);

                    string tempDist = Path.Join(Environment.CurrentDirectory, "temp/dist");

                    Logger.LogInformation("Generate data.");

                    await toLocalFS(new RemoteOption
                        {
                            Uri  = tempDist,
                            Type = RemoteType.LocalFS,
                            Name = remote.Name
                        });

                    FSExtensions.CopyDirectory(tempDist, GitTempFolder);

                    Logger.LogInformation("Load git config.");

                    string userName = Option.Properties[$"remote.{remote.Name}.git.username"],
                           password = Option.Properties[$"remote.{remote.Name}.git.password"];

                    {
                        if (string.IsNullOrEmpty(userName))
                        {
                            userName = ConsoleExtensions.Input("Input username: "******"Input password: "******"Commit to git.");

                        LibGit2Sharp.Commands.Stage(repo, "*");

                        var signature = new LibGit2Sharp.Signature(
                            new Identity("AcBlog.Tools.Sdk", "tools.sdk@acblog"), DateTimeOffset.Now);
                        repo.Commit(DateTimeOffset.Now.ToString(), signature, signature, new CommitOptions
                            {
                                AllowEmptyCommit = true
                            });

                        Logger.LogInformation($"Push to {repo.Head.RemoteName}.");

                        PushOptions options = new PushOptions
                        {
                            CredentialsProvider = new CredentialsHandler(
                                (url, usernameFromUrl, types) =>
                                new UsernamePasswordCredentials()
                                {
                                    Username = string.IsNullOrEmpty(userName) ? usernameFromUrl : userName,
                                    Password = password
                                })
                        };
                        repo.Network.Push(repo.Head, options);
                    }
                }
                break;
                }
            }
            else
            {
                throw new Exception("No remote");
            }

            async Task toLocalFS(RemoteOption remote)
            {
                FSBuilder fsBuilder = new FSBuilder(remote.Uri);

                fsBuilder.EnsureDirectoryEmpty();

                List <Post> posts = new List <Post>();

                await foreach (var item in Local.PostService.GetAllItems().IgnoreNull())
                {
                    Logger.LogInformation($"Loaded Post {item.Id}: {item.Title}");
                    posts.Add(item);
                }

                List <Layout> layouts = new List <Layout>();

                await foreach (var item in Local.LayoutService.GetAllItems().IgnoreNull())
                {
                    Logger.LogInformation($"Loaded Layout {item.Id}");
                    layouts.Add(item);
                }

                List <Page> pages = new List <Page>();

                await foreach (var item in Local.PageService.GetAllItems().IgnoreNull())
                {
                    Logger.LogInformation($"Loaded Page {item.Id}: {item.Title}");
                    pages.Add(item);
                }

                var baseAddress = Option.Properties[$"remote.{remote.Name}.generator.baseAddress"];

                List <Data.Models.File> files = new List <Data.Models.File>();

                {
                    string path = Path.Join(Environment.CurrentDirectory, AssetsPath);
                    if (Directory.Exists(path))
                    {
                        foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
                        {
                            var id             = Path.GetRelativePath(Environment.CurrentDirectory, file).Replace('\\', '/');
                            Data.Models.File f = new Data.Models.File
                            {
                                Id  = id,
                                Uri = string.IsNullOrWhiteSpace(baseAddress) ? $"/{id}" : $"{baseAddress.TrimEnd('/')}/{id}"
                            };
                            files.Add(f);
                        }
                    }
                }

                Logger.LogInformation("Build data.");
                {
                    BlogOptions options = await Local.GetOptions();

                    BlogBuilder builder = new BlogBuilder(options, Path.Join(remote.Uri));
                    await builder.Build();

                    await builder.BuildPosts(posts);

                    await builder.BuildLayouts(layouts);

                    await builder.BuildPages(pages);

                    await builder.BuildFiles(files);
                }

                {
                    if (!string.IsNullOrEmpty(baseAddress))
                    {
                        Logger.LogInformation("Build sitemap.");
                        var sub = fsBuilder.CreateSubDirectoryBuilder("Site");
                        {
                            var siteMapBuilder = await Local.BuildSitemap(baseAddress);

                            await using var st     = sub.GetFileRewriteStream("sitemap.xml");
                            await using var writer = XmlWriter.Create(st, new XmlWriterSettings { Async = true });
                            siteMapBuilder.Build().WriteTo(writer);
                        }
                        Logger.LogInformation("Build feed.");
                        {
                            var feed = await Local.BuildSyndication(baseAddress);

                            await using (var st = sub.GetFileRewriteStream("atom.xml"))
                            {
                                await using var writer = XmlWriter.Create(st, new XmlWriterSettings { Async = true });
                                feed.GetAtom10Formatter().WriteTo(writer);
                            }
                            await using (var st = sub.GetFileRewriteStream("rss.xml"))
                            {
                                await using var writer = XmlWriter.Create(st, new XmlWriterSettings { Async = true });
                                feed.GetRss20Formatter().WriteTo(writer);
                            }
                        }
                    }
                }
                {
                    string assetsPath = Path.Join(Environment.CurrentDirectory, AssetsPath);
                    if (Directory.Exists(assetsPath))
                    {
                        Logger.LogInformation("Copy assets.");
                        FSExtensions.CopyDirectory(assetsPath, Path.Join(remote.Uri, AssetsPath));
                    }
                }
            }
        }