Skip to content

Commit fcb5fea

Browse files
committed
(build) improving the docs task - support smart and forced publishing
1 parent a258f36 commit fcb5fea

File tree

5 files changed

+60
-31
lines changed

5 files changed

+60
-31
lines changed

build/Directory.Packages.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<PackageReference Include="Cake.Docker" Version="1.0.0" />
88
<PackageReference Include="Cake.Frosting" Version="1.1.0" />
99
<PackageReference Include="Cake.Git" Version="1.1.0" />
10-
<PackageReference Include="Cake.Kudu" Version="1.0.1" />
1110
<PackageReference Include="Cake.Json" Version="6.0.1" />
1211
<PackageReference Include="Cake.Incubator" Version="6.0.0" />
1312
<PackageReference Include="Cake.Wyam" Version="2.2.12" />

build/common/Utilities/Paths.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ namespace Common.Utilities
44
{
55
public class Paths
66
{
7-
public static readonly DirectoryPath ToolsDirectory = "./tools";
8-
public static readonly DirectoryPath Artifacts = "./artifacts";
9-
public static readonly DirectoryPath Src = "./src";
10-
public static readonly DirectoryPath Docs = "./docs";
11-
public static readonly DirectoryPath Build = "./build";
7+
public static readonly DirectoryPath Root = "./";
8+
9+
public static readonly DirectoryPath ToolsDirectory = $"{Root}tools";
10+
public static readonly DirectoryPath Artifacts = $"{Root}artifacts";
11+
public static readonly DirectoryPath Src = $"{Root}src";
12+
public static readonly DirectoryPath Docs = $"{Root}docs";
13+
public static readonly DirectoryPath Build = $"{Root}build";
1214

1315
public static readonly DirectoryPath Nuspec = $"{Build}/nuspec";
1416

build/docs/BuildContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Docs
77
{
88
public class BuildContext : BuildContextBase
99
{
10+
public bool ForcePublish { get; set; }
1011
public Credentials? Credentials { get; set; }
1112

1213
public WyamSettings? WyamSettings { get; set; }

build/docs/BuildLifetime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using Cake.Common;
23
using Cake.Common.IO;
34
using Cake.Wyam;
45
using Common.Utilities;
@@ -13,6 +14,7 @@ public override void Setup(BuildContext context)
1314
base.Setup(context);
1415

1516
context.Credentials = Credentials.GetCredentials(context);
17+
context.ForcePublish = context.HasArgument("force");
1618

1719
context.WyamSettings = new WyamSettings
1820
{

build/docs/Tasks/PublishDocs.cs

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
11
using System;
2+
using System.Linq;
23
using Cake.Common.Diagnostics;
34
using Cake.Common.IO;
45
using Cake.Core;
56
using Cake.Core.IO;
67
using Cake.Frosting;
78
using Cake.Git;
8-
using Cake.Kudu;
9-
using Cake.Kudu.KuduSync;
9+
using Cake.Wyam;
1010
using Common.Utilities;
1111

1212
namespace Docs.Tasks
1313
{
1414
[TaskName(nameof(PublishDocs))]
1515
[TaskDescription("Published the docs changes to docs specific branch")]
16-
[IsDependentOn(typeof(BuildDocs))]
16+
[IsDependentOn(typeof(Clean))]
1717
public sealed class PublishDocs : FrostingTask<BuildContext>
1818
{
1919
public override bool ShouldRun(BuildContext context)
2020
{
2121
var shouldRun = true;
2222
shouldRun &= context.ShouldRun(context.DirectoryExists(Paths.Docs), "Wyam documentation directory is missing");
23+
shouldRun &= context.ShouldRun(context.IsStableRelease || context.IsPreRelease, $"{nameof(PublishDocs)} works only for releases.");
2324

2425
return shouldRun;
2526
}
2627

2728
public override void Run(BuildContext context)
2829
{
29-
PublishDocumentation(context);
30+
if (context.ForcePublish is false)
31+
{
32+
if (AnyDocsChanged(context))
33+
{
34+
context.Information("Performing a docs publish. Detected docs changes");
35+
PublishDocumentation(context);
36+
}
37+
else
38+
{
39+
context.Information("No docs have changed, so no need to generate documentation");
40+
}
41+
}
42+
else
43+
{
44+
context.Information("Performing a forced docs publish...");
45+
PublishDocumentation(context);
46+
}
47+
}
48+
private static bool AnyDocsChanged(ICakeContext context)
49+
{
50+
var sourceCommit = context.GitLogTip(Paths.Root);
51+
var filesChanged = context.GitDiff(Paths.Root, sourceCommit.Sha);
52+
53+
const string path = "docs/";
54+
var docFileChanged = filesChanged.Any(file => file.OldPath.StartsWith(path) || file.Path.StartsWith(path) || file.Path.Contains("config.wyam"));
55+
return docFileChanged;
3056
}
3157

3258
private static void PublishDocumentation(BuildContext context)
@@ -42,32 +68,31 @@ private static void PublishDocumentation(BuildContext context)
4268
BranchName = publishBranchName
4369
});
4470

45-
context.Information("Sync output files...");
46-
context.Kudu().Sync(context.MakeAbsolute(Paths.ArtifactsDocs.Combine("preview")), publishFolder, new KuduSyncSettings
71+
if (context.WyamSettings is not null)
4772
{
48-
ArgumentCustomization = args => args.Append("--ignore").AppendQuoted(".git;CNAME;_git2")
49-
});
73+
context.WyamSettings.OutputPath = publishFolder;
74+
context.WyamSettings.NoClean = true;
75+
context.Wyam(context.WyamSettings);
76+
}
5077

51-
if (context.GitHasUncommitedChanges(publishFolder))
52-
{
53-
context.Information("Stage all changes...");
54-
context.GitAddAll(publishFolder);
78+
if (!context.GitHasUncommitedChanges(publishFolder)) return;
5579

56-
if (context.GitHasStagedChanges(publishFolder))
57-
{
58-
context.Information("Commit all changes...");
59-
context.GitCommit(
60-
publishFolder,
61-
sourceCommit.Committer.Name,
62-
sourceCommit.Committer.Email,
63-
$"Continuous Integration Publish: {sourceCommit.Sha}\r\n{sourceCommit.Message}"
64-
);
80+
context.Information("Stage all changes...");
81+
context.GitAddAll(publishFolder);
6582

66-
PublishToGitHub(context, publishFolder, publishBranchName);
67-
}
68-
}
83+
if (!context.GitHasStagedChanges(publishFolder)) return;
84+
85+
context.Information("Commit all changes...");
86+
context.GitCommit(
87+
publishFolder,
88+
sourceCommit.Committer.Name,
89+
sourceCommit.Committer.Email,
90+
$"Continuous Integration Publish: {sourceCommit.Sha}\r\n{sourceCommit.Message}"
91+
);
92+
93+
PublishToGitHub(context, publishFolder, publishBranchName);
6994
}
70-
95+
7196
private static void PublishToGitHub(BuildContext context, DirectoryPath publishFolder, string publishBranchName)
7297
{
7398
var username = context.Credentials?.GitHub?.UserName;

0 commit comments

Comments
 (0)