From 1333e08c0f53724d30afc605791ce503e566e4d2 Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Thu, 19 Dec 2019 17:13:31 +0000 Subject: [PATCH] (GH-1932) First stab at publishing docs This may well break, and we won't be able to test this until merged since it isn't meant to work on PR builds. --- build.cake | 4 ++ build/wyam.cake | 129 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 1 deletion(-) diff --git a/build.cake b/build.cake index 92fdc99d41..5a62c66fbe 100644 --- a/build.cake +++ b/build.cake @@ -7,9 +7,11 @@ #addin "nuget:?package=Cake.Coverlet&version=2.3.4" #addin "nuget:?package=Cake.Docker&version=0.10.1" #addin "nuget:?package=Cake.Gem&version=0.8.1" +#addin "nuget:?package=Cake.Git&version=0.21.0" #addin "nuget:?package=Cake.Gitter&version=0.11.1" #addin "nuget:?package=Cake.Incubator&version=5.1.0" #addin "nuget:?package=Cake.Json&version=4.0.0" +#addin "nuget:?package=Cake.Kudu&version=0.10.1" #addin "nuget:?package=Cake.Npm&version=0.17.0" #addin "nuget:?package=Cake.Tfx&version=0.9.1" #addin "nuget:?package=Cake.Wyam&version=2.2.9" @@ -22,6 +24,7 @@ #tool "nuget:?package=NUnit.ConsoleRunner&version=3.10.0" #tool "nuget:?package=nuget.commandline&version=5.2.0" #tool "nuget:?package=Wyam&version=2.2.9" +#tool "nuget:?package=KuduSync.NET&version=1.5.2" // Install .NET Core Global tools. #tool "dotnet:?package=Codecov.Tool&version=1.7.2" @@ -143,6 +146,7 @@ Task("Publish") .IsDependentOn("Publish-NuGet") .IsDependentOn("Publish-Chocolatey") .IsDependentOn("Publish-Gem") + .IsDependentOn("Publish-Documentation") .Finally(() => { if (publishingError) diff --git a/build/wyam.cake b/build/wyam.cake index 1f5bef2b8d..bcce2ad471 100644 --- a/build/wyam.cake +++ b/build/wyam.cake @@ -12,7 +12,7 @@ Task("Preview-Documentation") { Recipe = "Docs", Theme = "Samson", - OutputPath = MakeAbsolute(MakeAbsolute(Directory("artifacts/Documentation"))), + OutputPath = MakeAbsolute(Directory("artifacts/Documentation")), RootPath = MakeAbsolute(Directory("docs")), Preview = true, Watch = true, @@ -29,3 +29,130 @@ Task("Preview-Documentation") } }); }); + +Task("Force-Publish-Documentation") + .IsDependentOn("Clean-Documentation") + .WithCriteria(() => DirectoryExists(MakeAbsolute(Directory("docs"))), "Wyam documentation directory is missing") + .WithCriteria((context, parameters) => !parameters.IsPullRequest, "Publish-Documentation works only for non-PR commits.") + .Does((parameters) => +{ + Wyam(new WyamSettings + { + Recipe = "Docs", + Theme = "Samson", + OutputPath = MakeAbsolute(Directory("artifacts/Documentation")), + RootPath = MakeAbsolute(Directory("docs")), + ConfigurationFile = MakeAbsolute((FilePath)"config.wyam"), + PreviewVirtualDirectory = "GitVersion", + Settings = new Dictionary + { + { "Host", "gittools.github.io" }, + { "LinkRoot", "GitVersion" }, + { "BaseEditUrl", "https://github.com/gittools/GitVersion/tree/master/docs/input/" }, + { "SourceFiles", MakeAbsolute(parameters.Paths.Directories.Source) + "/**/{!bin,!obj,!packages,!*.Tests,}/**/*.cs" }, + { "Title", "GitVersion" }, + { "IncludeGlobalNamespace", false } + } + }); + + PublishDocumentation(parameters); +}); + +Task("Publish-Documentation") + .IsDependentOn("Clean-Documentation") + .WithCriteria(() => DirectoryExists(MakeAbsolute(Directory("docs"))), "Wyam documentation directory is missing") + .Does((parameters) => +{ + // Check to see if any documentation has changed + var sourceCommit = GitLogTip("./"); + Information("Source Commit Sha: {0}", sourceCommit.Sha); + var filesChanged = GitDiff("./", sourceCommit.Sha); + Information("Number of changed files: {0}", filesChanged.Count); + var docFileChanged = false; + + var wyamDocsFolderDirectoryName = "docs"; + + foreach (var file in filesChanged) + { + var forwardSlash = '/'; + Verbose("Changed File OldPath: {0}, Path: {1}", file.OldPath, file.Path); + if (file.OldPath.Contains(string.Format("{0}{1}", wyamDocsFolderDirectoryName, forwardSlash)) || + file.Path.Contains(string.Format("{0}{1}", wyamDocsFolderDirectoryName, forwardSlash)) || + file.Path.Contains("config.wyam")) + { + docFileChanged = true; + break; + } + } + + if (docFileChanged) + { + Information("Detected that documentation files have changed, so running Wyam..."); + + Wyam(new WyamSettings + { + Recipe = "Docs", + Theme = "Samson", + OutputPath = MakeAbsolute(Directory("artifacts/Documentation")), + RootPath = MakeAbsolute(Directory("docs")), + ConfigurationFile = MakeAbsolute((FilePath)"config.wyam"), + PreviewVirtualDirectory = "GitVersion", + Settings = new Dictionary + { + { "Host", "gittools.github.io" }, + { "LinkRoot", "GitVersion" }, + { "BaseEditUrl", "https://github.com/gittools/GitVersion/tree/master/docs/input/" }, + { "SourceFiles", MakeAbsolute(parameters.Paths.Directories.Source) + "/**/{!bin,!obj,!packages,!*.Tests,}/**/*.cs" }, + { "Title", "GitVersion" }, + { "IncludeGlobalNamespace", false } + } + }); + + PublishDocumentation(parameters); + } + else + { + Information("No documentation has changed, so no need to generate documentation"); + } +}) +.OnError(exception => +{ + Error(exception.Message); + Information("Publish-Documentation Task failed, but continuing with next Task..."); + publishingError = true; +}); + +public void PublishDocumentation(BuildParameters parameters) +{ + var sourceCommit = GitLogTip("./"); + + var publishFolder = MakeAbsolute(Directory("artifacts/temp/_PublishedDocumentation")).Combine(DateTime.Now.ToString("yyyyMMdd_HHmmss")); + Information("Publishing Folder: {0}", publishFolder); + Information("Getting publish branch..."); + GitClone("https://github.com/gittools/GitVersion", publishFolder, new GitCloneSettings{ BranchName = "gh-pages" }); + + Information("Sync output files..."); + Kudu.Sync(MakeAbsolute(Directory("artifacts/Documentation")), publishFolder, new KuduSyncSettings { + ArgumentCustomization = args=>args.Append("--ignore").AppendQuoted(".git;CNAME") + }); + + if (GitHasUncommitedChanges(publishFolder)) + { + Information("Stage all changes..."); + GitAddAll(publishFolder); + + if (GitHasStagedChanges(publishFolder)) + { + Information("Commit all changes..."); + GitCommit( + publishFolder, + sourceCommit.Committer.Name, + sourceCommit.Committer.Email, + string.Format("Continuous Integration Publish: {0}\r\n{1}", sourceCommit.Sha, sourceCommit.Message) + ); + + Information("Pushing all changes..."); + GitPush(publishFolder, parameters.Credentials.GitHub.Token, "x-oauth-basic", "gh-pages"); + } + } +}