#tool "nuget:?package=GitVersion.CommandLine"
#addin "Cake.Yaml"

public class ContextInfo
{
    public string NugetVersion { get; set; }
    public string AssemblyVersion { get; set; }
    public GitVersion Git { get; set; }

    public string BuildVersion
    {
        get { return NugetVersion + "-" + Git.Sha; }
    }
}

ContextInfo _versionContext = null;
public ContextInfo VersionContext 
{
    get 
    {
        if(_versionContext == null)
            throw new Exception("The current context has not been read yet. Call ReadContext(FilePath) before accessing the property.");

        return _versionContext;
    }
} 

public ContextInfo ReadContext(FilePath filepath)
{
    _versionContext = DeserializeYamlFromFile<ContextInfo>(filepath);
    _versionContext.Git = GitVersion();
    return _versionContext;
}

public void UpdateAppVeyorBuildVersionNumber()
{
    if(!AppVeyor.IsRunningOnAppVeyor)
    {
        Information("Not running under AppVeyor");
        return;
    }
    
    Information("Running under AppVeyor");
    Information("Updating AppVeyor build version to " + VersionContext.BuildVersion);

    while(true)
    {
        int? increment = null;
        try
        {
            var version = VersionContext.BuildVersion;
            if(increment.HasValue)
                version += "-" + increment.Value;
            AppVeyor.UpdateBuildVersion(version);
            break;
        }
        catch
        {
            increment++;
            if(increment <= 10)
                continue;
        }
    }
}
#tool "nuget:?package=GitVersion.CommandLine"
#addin "Cake.Yaml"

public class ContextInfo
{
    public string NugetVersion { get; set; }
    public string AssemblyVersion { get; set; }
    public GitVersion Git { get; set; }

    public string BuildVersion
    {
        get { return NugetVersion + "-" + Git.Sha; }
    }
}

ContextInfo _versionContext = null;
public ContextInfo VersionContext 
{
    get 
    {
        if(_versionContext == null)
            throw new Exception("The current context has not been read yet. Call ReadContext(FilePath) before accessing the property.");

        return _versionContext;
    }
} 

public ContextInfo ReadContext(FilePath filepath)
{
    _versionContext = DeserializeYamlFromFile<ContextInfo>(filepath);
    _versionContext.Git = GitVersion();
    return _versionContext;
}

public void UpdateAppVeyorBuildVersionNumber()
{
    // if(!AppVeyor.IsRunningOnAppVeyor)
    // {
    //     Information("Not running under AppVeyor");
    //     return;
    // }
    
    Information("Running under AppVeyor");
    Information("Updating AppVeyor build version to " + VersionContext.BuildVersion);

    var increment = 0;
    while(increment < 10)
    {
        try
        {
            var version = VersionContext.BuildVersion;
            if(increment > 0)
                version += "-" + increment;
            
            AppVeyor.UpdateBuildVersion(version);
            break;
        }
        catch
        {
            increment++;
        }
    }
}
Fix infinite loop when retrying a failing build number
Fix infinite loop when retrying a failing build number
