Skip to content

Commit

Permalink
Add option to use Git CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Aug 24, 2023
1 parent 4120574 commit d1448f9
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 3 deletions.
144 changes: 144 additions & 0 deletions Cake.Wyam.Recipe/Content/gitprovider.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/// <summary>
/// Supported ways to interact with Git Repository.
/// </summary>
public enum GitProviderType
{
/// <summary>
/// Interact with Git though Cake.Git addin.
/// Requires system to be compatible with Cake.Git addin.
/// </summary>
CakeGit,
/// <summary>
/// Interact with Git though Cake.Git using Git CLI.
/// Requires Git CLI to be available in path.
/// </summary>
Cli
}

/// <summary>
/// Description of a provider to work with a Git repository.
/// </summary>
public interface IGitProvider
{
/// <summary>
/// Commits changes in a repository.
/// </summary>
/// <param name="context">The Cake context.</param>
/// <param name="repositoryDirectoryPath">Root directory of the repository.</param>
/// <param name="name">Name of the committer.</param>
/// <param name="email">Email address of the committer.</param>
/// <param name="message">Commit message.</param>
void Commit(ICakeContext context, DirectoryPath repositoryDirectoryPath, string name, string email, string message);
}

/// <summary>
/// Provider to interact with a repository using Git CLI.
/// </summary>
public class CliGitProvider : IGitProvider
{
/// <inheritdoc />
public void Commit(ICakeContext context, DirectoryPath repositoryDirectoryPath, string name, string email, string message)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException(nameof(name));
}

if (string.IsNullOrWhiteSpace(email))
{
throw new ArgumentNullException(nameof(email));
}

if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentNullException(nameof(message));
}

this.GitCommand(
context,
repositoryDirectoryPath,
"commit",
"--author",
$"{name} <{email}>",
"--message",
$"\"{message}\"");
}

private static IEnumerable<string> GitCommand(
ICakeContext context,
DirectoryPath repositoryDirectoryPath,
params string[] arguments)
{
if (!arguments.Any())
{
throw new ArgumentOutOfRangeException(nameof(arguments));
}
var gitArguments = string.Join(" ", arguments);
var exitCode = context.StartProcess(
"git",
new ProcessSettings
{
Arguments = gitArguments,
WorkingDirectory = repositoryDirectoryPath.FullPath,
RedirectStandardOutput = true,
RedirectStandardError = true
},
out var redirectedStandardOutput,
out var redirectedErrorOutput
);
if (exitCode != 0)
{
throw new Exception(
$"Git command failed with arguments {gitArguments}. Exit code: {exitCode}. Error output: {string.Join(System.Environment.NewLine, redirectedErrorOutput)}"
);
}
return redirectedStandardOutput;
}
}

/// <summary>
/// Provider to interact with a repository using Cake.Git addin.
/// </summary>
public class CakeGitProvider : IGitProvider
{
/// <inheritdoc />
public void Commit(ICakeContext context, DirectoryPath repositoryDirectoryPath, string name, string email, string message)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException(nameof(name));
}

if (string.IsNullOrWhiteSpace(email))
{
throw new ArgumentNullException(nameof(email));
}

if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentNullException(nameof(message));
}

context.GitCommit(repositoryDirectoryPath, name, email, message);
}
}
18 changes: 16 additions & 2 deletions Cake.Wyam.Recipe/Content/parameters.cake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static class BuildParameters
public static string MasterBranchName { get; private set; }
public static string DevelopBranchName { get; private set; }
public static bool IsRunningOnDotNetCore { get; private set; }
public static IGitProvider GitProvider { get; private set; }

public static IDictionary<string, object> WyamSettings
{
Expand Down Expand Up @@ -159,7 +160,8 @@ public static class BuildParameters
string webLinkRoot = null,
string webBaseEditUrl = null,
string masterBranchName = "master",
string developBranchName = "develop")
string developBranchName = "develop",
GitProviderType gitProviderType = GitProviderType.CakeGit)
{
if (context == null)
{
Expand Down Expand Up @@ -234,5 +236,17 @@ public static class BuildParameters
IsMainRepository &&
(IsMasterBranch || IsDevelopBranch) &&
shouldPurgeCloudflareCache);
}

switch (gitProviderType)
{
case GitProviderType.CakeGit:
context.Information("Using Cake.Git for interacting with Git repository");
GitProvider = new CakeGitProvider();
case GitProviderType.Cli:
context.Information("Using Git CLI for interacting with Git repository");
GitProvider = new CliGitProvider();
default:
throw new NotImplementedException("Unsupported Git provider");
}
}
}
2 changes: 1 addition & 1 deletion Cake.Wyam.Recipe/Content/wyam.cake
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BuildParameters.Tasks.PublishDocumentationTask = Task("Publish-Documentation")
GitAddAll(publishFolder);

Information("Commit all changes...");
GitCommit(
BuildParameters.GitProvider.Commit(
publishFolder,
sourceCommit.Committer.Name,
sourceCommit.Committer.Email,
Expand Down

0 comments on commit d1448f9

Please sign in to comment.