Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/GitVersion.App.Tests/Helpers/ArgumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using GitVersion.Extensions;

namespace GitVersion.App.Tests
{
Expand Down Expand Up @@ -36,27 +37,27 @@ public override string ToString()

arguments.AppendFormat(" /targetpath \"{0}\"", workingDirectory);

if (!string.IsNullOrWhiteSpace(exec))
if (!exec.IsNullOrWhiteSpace())
{
arguments.AppendFormat(" /exec \"{0}\"", exec);
}

if (!string.IsNullOrWhiteSpace(execArgs))
if (!execArgs.IsNullOrWhiteSpace())
{
arguments.AppendFormat(" /execArgs \"{0}\"", execArgs);
}

if (!string.IsNullOrWhiteSpace(projectFile))
if (!projectFile.IsNullOrWhiteSpace())
{
arguments.AppendFormat(" /proj \"{0}\"", projectFile);
}

if (!string.IsNullOrWhiteSpace(projectArgs))
if (!projectArgs.IsNullOrWhiteSpace())
{
arguments.AppendFormat(" /projargs \"{0}\"", projectArgs);
}

if (!string.IsNullOrWhiteSpace(logFile))
if (!logFile.IsNullOrWhiteSpace())
{
arguments.AppendFormat(" /l \"{0}\"", logFile);
}
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersion.App.Tests/Helpers/GitVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using GitVersion.BuildAgents;
using GitVersion.Helpers;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;

namespace GitVersion.App.Tests
{
Expand Down Expand Up @@ -100,7 +101,7 @@ params KeyValuePair<string, string>[] environments
Console.WriteLine();
Console.WriteLine("-------------------------------------------------------");

if (string.IsNullOrWhiteSpace(arguments.LogFile) || !File.Exists(arguments.LogFile))
if (arguments.LogFile.IsNullOrWhiteSpace() || !File.Exists(arguments.LogFile))
{
return new ExecutionResults(exitCode, output.ToString(), null);
}
Expand Down
5 changes: 3 additions & 2 deletions src/GitVersion.App.Tests/Helpers/ProgramFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using GitVersion.OutputVariables;
using GitVersion.Core.Tests.Helpers;
using Microsoft.Extensions.DependencyInjection;
using GitVersion.Extensions;

namespace GitVersion.App.Tests
{
Expand Down Expand Up @@ -61,7 +62,7 @@ public async Task<ProgramFixtureResult> Run(params string[] args)
// Create the application and override registrations.
var program = new Program(builder => Overrides.ForEach(action => action(builder)));

if (!string.IsNullOrWhiteSpace(workingDirectory))
if (!workingDirectory.IsNullOrWhiteSpace())
{
args = new[] { "-targetpath", workingDirectory }.Concat(args).ToArray();
}
Expand All @@ -86,7 +87,7 @@ public VersionVariables OutputVariables
{
get
{
if (string.IsNullOrWhiteSpace(Output)) return null;
if (Output.IsNullOrWhiteSpace()) return null;

var jsonStartIndex = Output.IndexOf("{", StringComparison.Ordinal);
var jsonEndIndex = Output.IndexOf("}", StringComparison.Ordinal);
Expand Down
8 changes: 4 additions & 4 deletions src/GitVersion.App/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ private void ParseSwitchArguments(Arguments arguments, NameValueCollection switc
private void AddAuthentication(Arguments arguments)
{
var username = environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
if (!string.IsNullOrWhiteSpace(username))
if (!username.IsNullOrWhiteSpace())
{
arguments.Authentication.Username = username;
}

var password = environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
if (!string.IsNullOrWhiteSpace(password))
if (!password.IsNullOrWhiteSpace())
{
arguments.Authentication.Password = password;
}
Expand Down Expand Up @@ -362,7 +362,7 @@ private static void ParseShowVariable(Arguments arguments, string value, string
{
string versionVariable = null;

if (!string.IsNullOrWhiteSpace(value))
if (!value.IsNullOrWhiteSpace())
{
versionVariable = VersionVariables.AvailableVariables.SingleOrDefault(av => av.Equals(value.Replace("'", ""), StringComparison.CurrentCultureIgnoreCase));
}
Expand Down Expand Up @@ -563,7 +563,7 @@ private static NameValueCollection CollectSwitchesAndValuesFromArguments(IList<s
else if (currentKey != null)
{
// And if the current switch does not have a value yet and the value is not itself a switch, set its value to this argument.
if (string.IsNullOrEmpty(switchesAndValues[currentKey]))
if (switchesAndValues[currentKey].IsNullOrEmpty())
{
switchesAndValues[currentKey] = arg;
}
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersion.App/QuotedStringHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GitVersion.Extensions;

namespace GitVersion
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public static string[] SplitUnquoted(string input, char splitChar)

splitted.Add(input.Substring(startIndex, input.Length - startIndex));

return splitted.Where(argument => !string.IsNullOrEmpty(argument)).ToArray();
return splitted.Where(argument => !argument.IsNullOrEmpty()).ToArray();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -219,9 +220,9 @@ public void ThrowsExceptionOnCustomYmlFileDoesNotExist()

private string SetupConfigFileContent(string text, string fileName = null, string path = null)
{
if (string.IsNullOrEmpty(fileName)) fileName = configFileLocator.FilePath;
if (fileName.IsNullOrEmpty()) fileName = configFileLocator.FilePath;
var filePath = fileName;
if (!string.IsNullOrEmpty(path))
if (!path.IsNullOrEmpty())
filePath = Path.Combine(path, filePath);
fileSystem.WriteAllText(filePath, text);
return filePath;
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersion.Core/BuildAgents/Abstractions/IBuildAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace GitVersion.BuildAgents
public interface IBuildAgent
{
bool CanApplyToCurrentContext();
void WriteIntegration(Action<string> writer, VersionVariables variables, bool updateBuildNumber = true);
string GetCurrentBranch(bool usingDynamicRepos);
void WriteIntegration(Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true);
string? GetCurrentBranch(bool usingDynamicRepos);
bool PreventFetch();
bool ShouldCleanUpRemotes();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace GitVersion.BuildAgents
{
public interface IBuildAgentResolver
{
ICurrentBuildAgent Resolve();
ICurrentBuildAgent? Resolve();
}
}
5 changes: 3 additions & 2 deletions src/GitVersion.Core/BuildAgents/AppVeyor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GitVersion.Logging;
using GitVersion.OutputVariables;
using System.Text.Json;
using GitVersion.Extensions;

namespace GitVersion.BuildAgents
{
Expand Down Expand Up @@ -66,10 +67,10 @@ private HttpClient GetHttpClient()
}


public override string GetCurrentBranch(bool usingDynamicRepos)
public override string? GetCurrentBranch(bool usingDynamicRepos)
{
var pullRequestBranchName = Environment.GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH");
if (!string.IsNullOrWhiteSpace(pullRequestBranchName))
if (!pullRequestBranchName.IsNullOrWhiteSpace())
{
return pullRequestBranchName;
}
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersion.Core/BuildAgents/AzurePipelines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override string[] GenerateSetParameterMessage(string name, string value)
};
}

public override string GetCurrentBranch(bool usingDynamicRepos)
public override string? GetCurrentBranch(bool usingDynamicRepos)
{
return Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
}
Expand All @@ -38,7 +38,7 @@ public override string GenerateSetVersionMessage(VersionVariables variables)
// For AzurePipelines, we'll get the Build Number and insert GitVersion variables where
// specified
var buildNumberEnv = Environment.GetEnvironmentVariable("BUILD_BUILDNUMBER");
if (string.IsNullOrWhiteSpace(buildNumberEnv))
if (buildNumberEnv.IsNullOrWhiteSpace())
return variables.FullSemVer;

var newBuildNumber = variables.Aggregate(buildNumberEnv, ReplaceVariables);
Expand Down
9 changes: 5 additions & 4 deletions src/GitVersion.Core/BuildAgents/BuildAgentBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;

Expand All @@ -18,17 +19,17 @@ protected BuildAgentBase(IEnvironment environment, ILog log)

protected abstract string EnvironmentVariable { get; }

public abstract string GenerateSetVersionMessage(VersionVariables variables);
public abstract string? GenerateSetVersionMessage(VersionVariables variables);
public abstract string[] GenerateSetParameterMessage(string name, string value);

public virtual bool CanApplyToCurrentContext() => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(EnvironmentVariable));
public virtual bool CanApplyToCurrentContext() => !Environment.GetEnvironmentVariable(EnvironmentVariable).IsNullOrEmpty();

public virtual string GetCurrentBranch(bool usingDynamicRepos) => null;
public virtual string? GetCurrentBranch(bool usingDynamicRepos) => null;

public virtual bool PreventFetch() => true;
public virtual bool ShouldCleanUpRemotes() => false;

public virtual void WriteIntegration(Action<string> writer, VersionVariables variables, bool updateBuildNumber = true)
public virtual void WriteIntegration(Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true)
{
if (writer == null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersion.Core/BuildAgents/BuildAgentResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public BuildAgentResolver(IEnumerable<IBuildAgent> buildAgents, ILog log)
this.buildAgents = buildAgents ?? Array.Empty<IBuildAgent>();
}

public ICurrentBuildAgent Resolve()
public ICurrentBuildAgent? Resolve()
{
ICurrentBuildAgent instance = null;
ICurrentBuildAgent? instance = null;
foreach (var buildAgent in buildAgents)
{
var agentName = buildAgent.GetType().Name;
Expand Down
2 changes: 2 additions & 0 deletions src/GitVersion.Core/BuildAgents/BuildServerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public override void RegisterTypes(IServiceCollection services)
}

services.AddSingleton<IBuildAgentResolver, BuildAgentResolver>();
#pragma warning disable CS8634 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'class' constraint.
services.AddSingleton(sp => sp.GetService<IBuildAgentResolver>()?.Resolve());
#pragma warning restore CS8634 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'class' constraint.
}
}
}
11 changes: 6 additions & 5 deletions src/GitVersion.Core/BuildAgents/CodeBuild.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.IO;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;

namespace GitVersion.BuildAgents
{
public sealed class CodeBuild : BuildAgentBase
{
private string file;
private string? file;
public const string WebHookEnvironmentVariableName = "CODEBUILD_WEBHOOK_HEAD_REF";
public const string SourceVersionEnvironmentVariableName = "CODEBUILD_SOURCE_VERSION";

Expand Down Expand Up @@ -36,20 +37,20 @@ public override string[] GenerateSetParameterMessage(string name, string value)
};
}

public override string GetCurrentBranch(bool usingDynamicRepos)
public override string? GetCurrentBranch(bool usingDynamicRepos)
{

var currentBranch = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);

if (string.IsNullOrEmpty(currentBranch))
if (currentBranch.IsNullOrEmpty())
{
return Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName);
}

return currentBranch;
}

public override void WriteIntegration(Action<string> writer, VersionVariables variables, bool updateBuildNumber = true)
public override void WriteIntegration(Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true)
{
base.WriteIntegration(writer, variables);
writer($"Outputting variables to '{file}' ... ");
Expand All @@ -59,6 +60,6 @@ public override void WriteIntegration(Action<string> writer, VersionVariables va
public override bool PreventFetch() => true;

public override bool CanApplyToCurrentContext()
=> !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName)) || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName));
=> !Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName).IsNullOrEmpty() || !Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName).IsNullOrEmpty();
}
}
9 changes: 5 additions & 4 deletions src/GitVersion.Core/BuildAgents/Drone.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;

Expand Down Expand Up @@ -30,24 +31,24 @@ public override string[] GenerateSetParameterMessage(string name, string value)
};
}

public override string GetCurrentBranch(bool usingDynamicRepos)
public override string? GetCurrentBranch(bool usingDynamicRepos)
{
// In Drone DRONE_BRANCH variable is equal to destination branch in case of pull request
// https://discourse.drone.io/t/getting-the-branch-a-pull-request-is-created-from/670
// Unfortunately, DRONE_REFSPEC isn't populated, however CI_COMMIT_REFSPEC can be used instead of.
var pullRequestNumber = Environment.GetEnvironmentVariable("DRONE_PULL_REQUEST");
if (!string.IsNullOrWhiteSpace(pullRequestNumber))
if (!pullRequestNumber.IsNullOrWhiteSpace())
{
// DRONE_SOURCE_BRANCH is available in Drone 1.x.x version
var sourceBranch = Environment.GetEnvironmentVariable("DRONE_SOURCE_BRANCH");
if (!string.IsNullOrWhiteSpace(sourceBranch))
if (!sourceBranch.IsNullOrWhiteSpace())
return sourceBranch;

// In drone lower than 1.x.x source branch can be parsed from CI_COMMIT_REFSPEC
// CI_COMMIT_REFSPEC - {sourceBranch}:{destinationBranch}
// https://github.com/drone/drone/issues/2222
var ciCommitRefSpec = Environment.GetEnvironmentVariable("CI_COMMIT_REFSPEC");
if (!string.IsNullOrWhiteSpace(ciCommitRefSpec))
if (!ciCommitRefSpec.IsNullOrWhiteSpace())
{
var colonIndex = ciCommitRefSpec.IndexOf(':');
if (colonIndex > 0)
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersion.Core/BuildAgents/EnvRun.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;

Expand All @@ -15,7 +16,7 @@ public EnvRun(IEnvironment environment, ILog log) : base(environment, log)
public override bool CanApplyToCurrentContext()
{
var envRunDatabasePath = Environment.GetEnvironmentVariable(EnvironmentVariableName);
if (!string.IsNullOrEmpty(envRunDatabasePath))
if (!envRunDatabasePath.IsNullOrEmpty())
{
if (!File.Exists(envRunDatabasePath))
{
Expand Down
7 changes: 4 additions & 3 deletions src/GitVersion.Core/BuildAgents/GitHubActions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;
using System.IO;
Expand Down Expand Up @@ -31,7 +32,7 @@ public override string[] GenerateSetParameterMessage(string name, string value)
return new string[0];
}

public override void WriteIntegration(System.Action<string> writer, VersionVariables variables, bool updateBuildNumber = true)
public override void WriteIntegration(System.Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true)
{
base.WriteIntegration(writer, variables, updateBuildNumber);

Expand All @@ -52,7 +53,7 @@ public override void WriteIntegration(System.Action<string> writer, VersionVaria
using var streamWriter = File.AppendText(gitHubSetEnvFilePath);
foreach (var variable in variables)
{
if (!string.IsNullOrEmpty(variable.Value))
if (!variable.Value.IsNullOrEmpty())
{
streamWriter.WriteLine($"GitVersion_{variable.Key}={variable.Value}");
}
Expand All @@ -64,7 +65,7 @@ public override void WriteIntegration(System.Action<string> writer, VersionVaria
}
}

public override string GetCurrentBranch(bool usingDynamicRepos)
public override string? GetCurrentBranch(bool usingDynamicRepos)
{
return Environment.GetEnvironmentVariable("GITHUB_REF");
}
Expand Down
Loading