<commit_before>#load "./docker.cake"

public static FilePath FindToolInPath(this ICakeContext context, string tool)
{
    var pathEnv = context.EnvironmentVariable("PATH");
    if (string.IsNullOrEmpty(pathEnv) || string.IsNullOrEmpty(tool)) return tool;

    var paths = pathEnv.Split(new []{ context.IsRunningOnUnix() ? ':' : ';'},  StringSplitOptions.RemoveEmptyEntries);
    return paths.Select(path => new DirectoryPath(path).CombineWithFilePath(tool)).FirstOrDefault(filePath => context.FileExists(filePath.FullPath));
}

public static bool IsOnMainRepo(this ICakeContext context)
{
    var buildSystem = context.BuildSystem();
    string repositoryName = null;
    if (buildSystem.IsRunningOnAppVeyor)
    {
        repositoryName = buildSystem.AppVeyor.Environment.Repository.Name;
    }
    else if (buildSystem.IsRunningOnTravisCI)
    {
        repositoryName = buildSystem.TravisCI.Environment.Repository.Slug;
    }
    else if (buildSystem.IsRunningOnAzurePipelines || buildSystem.IsRunningOnAzurePipelinesHosted)
    {
        repositoryName = buildSystem.TFBuild.Environment.Repository.RepoName;
    }

    context.Information("Repository Name: {0}" , repositoryName);

    return !string.IsNullOrWhiteSpace(repositoryName) && StringComparer.OrdinalIgnoreCase.Equals($"{BuildParameters.MainRepoOwner}/{BuildParameters.MainRepoName}", repositoryName);
}

public static bool IsOnMainBranch(this ICakeContext context)
{
    var buildSystem = context.BuildSystem();
    string repositoryBranch = ExecGitCmd(context, "rev-parse --abbrev-ref HEAD").Single();
    if (buildSystem.IsRunningOnAppVeyor)
    {
        repositoryBranch = buildSystem.AppVeyor.Environment.Repository.Branch;
    }
    else if (buildSystem.IsRunningOnTravisCI)
    {
        repositoryBranch = buildSystem.TravisCI.Environment.Build.Branch;
    }
    else if (buildSystem.IsRunningOnAzurePipelines || buildSystem.IsRunningOnAzurePipelinesHosted)
    {
        repositoryBranch = buildSystem.TFBuild.Environment.Repository.Branch;
    }

    context.Information("Repository Branch: {0}" , repositoryBranch);

    return !string.IsNullOrWhiteSpace(repositoryBranch) && StringComparer.OrdinalIgnoreCase.Equals("master", repositoryBranch);
}

public static bool IsBuildTagged(this ICakeContext context)
{
    var sha = ExecGitCmd(context, "rev-parse --verify HEAD").Single();
    var isTagged = ExecGitCmd(context, "tag --points-at " + sha).Any();

    return isTagged;
}


public static bool IsEnabled(this ICakeContext context, string envVar, bool nullOrEmptyAsEnabled = true)
{
    var value = context.EnvironmentVariable(envVar);

    return string.IsNullOrWhiteSpace(value) ? nullOrEmptyAsEnabled : bool.Parse(value);
}

public static List<string> ExecGitCmd(this ICakeContext context, string cmd)
{
    var gitPath = context.Tools.Resolve(context.IsRunningOnWindows() ? "git.exe" : "git");
    context.StartProcess(gitPath, new ProcessSettings { Arguments = cmd, RedirectStandardOutput = true }, out var redirectedOutput);

    return redirectedOutput.ToList();
}


DirectoryPath HomePath()
{
    return IsRunningOnWindows()
        ? new DirectoryPath(EnvironmentVariable("HOMEDRIVE") +  EnvironmentVariable("HOMEPATH"))
        : new DirectoryPath(EnvironmentVariable("HOME"));
}

void ReplaceTextInFile(FilePath filePath, string oldValue, string newValue, bool encrypt = false)
{
    Information("Replacing {0} with {1} in {2}", oldValue, !encrypt ? newValue : "******", filePath);
    var file = filePath.FullPath.ToString();
    System.IO.File.WriteAllText(file, System.IO.File.ReadAllText(file).Replace(oldValue, newValue));
}

void SetRubyGemPushApiKey(string apiKey)
{
    // it's a hack, creating a credentials file to be able to push the gem
    var workDir = "./src/GitVersionRubyGem";
    var gemHomeDir = HomePath().Combine(".gem");
    var credentialFile = new FilePath(workDir + "/credentials");
    EnsureDirectoryExists(gemHomeDir);
    ReplaceTextInFile(credentialFile, "$api_key$", apiKey, true);
    CopyFileToDirectory(credentialFile, gemHomeDir);
}

GitVersion GetVersion(BuildParameters parameters)
{
    var gitversionFilePath = $"artifacts/gitversion.json";
    var gitversionFile = GetFiles(gitversionFilePath).FirstOrDefault();
    GitVersion gitVersion = null;
    if (gitversionFile == null)
    {
        Build(parameters);

        var settings = new GitVersionSettings { OutputType = GitVersionOutput.Json };
        SetGitVersionTool(settings, parameters, "src/GitVersionExe/**");

        gitVersion = GitVersion(settings);
        SerializeJsonToPrettyFile(gitversionFilePath, gitVersion);
    }
    else
    {
        gitVersion = DeserializeJsonFromFile<GitVersion>(gitversionFile);
    }

    return gitVersion;
}

void RunGitVersionOnCI(BuildParameters parameters)
{
    // set the CI build version number with GitVersion
    if (!parameters.IsLocalBuild && !(parameters.IsRunningOnAzurePipeline && parameters.IsPullRequest))
    {
        var settings = new GitVersionSettings
        {
            UpdateAssemblyInfo = true,
            LogFilePath = "console",
            OutputType = GitVersionOutput.BuildServer
        };
        SetGitVersionTool(settings, parameters, "artifacts/**/bin");

        GitVersion(settings);
    }
}

GitVersionSettings SetGitVersionTool(GitVersionSettings settings, BuildParameters parameters, string toolPath)
{
    var gitversionTool = GetFiles($"{toolPath}/{parameters.CoreFxVersion31}/GitVersion.dll").FirstOrDefault();

    settings.ToolPath = Context.FindToolInPath(IsRunningOnUnix() ? "dotnet" : "dotnet.exe");
    settings.ArgumentCustomization = args => gitversionTool + " " + args.Render();

    return settings;
}

void PublishGitVersionToArtifacts(BuildParameters parameters)<commit_msg>(build) set PR build number  for Azure Devops builds<commit_after>#load "./docker.cake"

public static FilePath FindToolInPath(this ICakeContext context, string tool)
{
    var pathEnv = context.EnvironmentVariable("PATH");
    if (string.IsNullOrEmpty(pathEnv) || string.IsNullOrEmpty(tool)) return tool;

    var paths = pathEnv.Split(new []{ context.IsRunningOnUnix() ? ':' : ';'},  StringSplitOptions.RemoveEmptyEntries);
    return paths.Select(path => new DirectoryPath(path).CombineWithFilePath(tool)).FirstOrDefault(filePath => context.FileExists(filePath.FullPath));
}

public static bool IsOnMainRepo(this ICakeContext context)
{
    var buildSystem = context.BuildSystem();
    string repositoryName = null;
    if (buildSystem.IsRunningOnAppVeyor)
    {
        repositoryName = buildSystem.AppVeyor.Environment.Repository.Name;
    }
    else if (buildSystem.IsRunningOnTravisCI)
    {
        repositoryName = buildSystem.TravisCI.Environment.Repository.Slug;
    }
    else if (buildSystem.IsRunningOnAzurePipelines || buildSystem.IsRunningOnAzurePipelinesHosted)
    {
        repositoryName = buildSystem.TFBuild.Environment.Repository.RepoName;
    }

    context.Information("Repository Name: {0}" , repositoryName);

    return !string.IsNullOrWhiteSpace(repositoryName) && StringComparer.OrdinalIgnoreCase.Equals($"{BuildParameters.MainRepoOwner}/{BuildParameters.MainRepoName}", repositoryName);
}

public static bool IsOnMainBranch(this ICakeContext context)
{
    var buildSystem = context.BuildSystem();
    string repositoryBranch = ExecGitCmd(context, "rev-parse --abbrev-ref HEAD").Single();
    if (buildSystem.IsRunningOnAppVeyor)
    {
        repositoryBranch = buildSystem.AppVeyor.Environment.Repository.Branch;
    }
    else if (buildSystem.IsRunningOnTravisCI)
    {
        repositoryBranch = buildSystem.TravisCI.Environment.Build.Branch;
    }
    else if (buildSystem.IsRunningOnAzurePipelines || buildSystem.IsRunningOnAzurePipelinesHosted)
    {
        repositoryBranch = buildSystem.TFBuild.Environment.Repository.Branch;
    }

    context.Information("Repository Branch: {0}" , repositoryBranch);

    return !string.IsNullOrWhiteSpace(repositoryBranch) && StringComparer.OrdinalIgnoreCase.Equals("master", repositoryBranch);
}

public static bool IsBuildTagged(this ICakeContext context)
{
    var sha = ExecGitCmd(context, "rev-parse --verify HEAD").Single();
    var isTagged = ExecGitCmd(context, "tag --points-at " + sha).Any();

    return isTagged;
}


public static bool IsEnabled(this ICakeContext context, string envVar, bool nullOrEmptyAsEnabled = true)
{
    var value = context.EnvironmentVariable(envVar);

    return string.IsNullOrWhiteSpace(value) ? nullOrEmptyAsEnabled : bool.Parse(value);
}

public static List<string> ExecGitCmd(this ICakeContext context, string cmd)
{
    var gitPath = context.Tools.Resolve(context.IsRunningOnWindows() ? "git.exe" : "git");
    context.StartProcess(gitPath, new ProcessSettings { Arguments = cmd, RedirectStandardOutput = true }, out var redirectedOutput);

    return redirectedOutput.ToList();
}


DirectoryPath HomePath()
{
    return IsRunningOnWindows()
        ? new DirectoryPath(EnvironmentVariable("HOMEDRIVE") +  EnvironmentVariable("HOMEPATH"))
        : new DirectoryPath(EnvironmentVariable("HOME"));
}

void ReplaceTextInFile(FilePath filePath, string oldValue, string newValue, bool encrypt = false)
{
    Information("Replacing {0} with {1} in {2}", oldValue, !encrypt ? newValue : "******", filePath);
    var file = filePath.FullPath.ToString();
    System.IO.File.WriteAllText(file, System.IO.File.ReadAllText(file).Replace(oldValue, newValue));
}

void SetRubyGemPushApiKey(string apiKey)
{
    // it's a hack, creating a credentials file to be able to push the gem
    var workDir = "./src/GitVersionRubyGem";
    var gemHomeDir = HomePath().Combine(".gem");
    var credentialFile = new FilePath(workDir + "/credentials");
    EnsureDirectoryExists(gemHomeDir);
    ReplaceTextInFile(credentialFile, "$api_key$", apiKey, true);
    CopyFileToDirectory(credentialFile, gemHomeDir);
}

GitVersion GetVersion(BuildParameters parameters)
{
    var gitversionFilePath = $"artifacts/gitversion.json";
    var gitversionFile = GetFiles(gitversionFilePath).FirstOrDefault();
    GitVersion gitVersion = null;
    if (gitversionFile == null)
    {
        Build(parameters);

        var settings = new GitVersionSettings { OutputType = GitVersionOutput.Json };
        SetGitVersionTool(settings, parameters, "src/GitVersionExe/**");

        gitVersion = GitVersion(settings);
        SerializeJsonToPrettyFile(gitversionFilePath, gitVersion);
    }
    else
    {
        gitVersion = DeserializeJsonFromFile<GitVersion>(gitversionFile);
    }

    return gitVersion;
}

void RunGitVersionOnCI(BuildParameters parameters)
{
    // set the CI build version number with GitVersion
    if (!parameters.IsLocalBuild)
    {
        var settings = new GitVersionSettings
        {
            UpdateAssemblyInfo = true,
            LogFilePath = "console",
            OutputType = GitVersionOutput.BuildServer
        };
        SetGitVersionTool(settings, parameters, "artifacts/**/bin");

        GitVersion(settings);
    }
}

GitVersionSettings SetGitVersionTool(GitVersionSettings settings, BuildParameters parameters, string toolPath)
{
    var gitversionTool = GetFiles($"{toolPath}/{parameters.CoreFxVersion31}/GitVersion.dll").FirstOrDefault();

    settings.ToolPath = Context.FindToolInPath(IsRunningOnUnix() ? "dotnet" : "dotnet.exe");
    settings.ArgumentCustomization = args => gitversionTool + " " + args.Render();

    return settings;
}

void PublishGitVersionToArtifacts(BuildParameters parameters)
{'fraction_non_alphanumeric': 0.10759328520483605, 'fraction_numerical': 0.001217708967556754, 'mean_word_length': 4.449289099526067, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6236410', 'n_tokens_mistral': 3302, 'n_tokens_neox': 3244, 'n_words': 645}