Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsPublicRelease and IsPrerelease properties to ThisAssembly class #425

Merged
merged 1 commit into from Jan 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs
Expand Up @@ -42,6 +42,8 @@ namespace AssemblyInfo
static member internal AssemblyVersion = ""1.3.0.0""
static member internal AssemblyFileVersion = ""1.3.1.0""
static member internal AssemblyCompany = ""company""
static member internal IsPublicRelease = false
static member internal IsPrerelease = false
static member internal RootNamespace = """"
do()
";
Expand Down Expand Up @@ -80,6 +82,8 @@ internal static partial class ThisAssembly {
internal const string AssemblyVersion = ""1.3.0.0"";
internal const string AssemblyFileVersion = ""1.3.1.0"";
internal const string AssemblyCompany = ""company"";
internal const bool IsPublicRelease = false;
internal const bool IsPrerelease = false;
internal const string RootNamespace = """";
}
";
Expand Down
8 changes: 6 additions & 2 deletions src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs
Expand Up @@ -731,9 +731,9 @@ public async Task PublicRelease_RegEx_SatisfiedByCheckedOutBranch()

[Theory]
[PairwiseData]
public async Task AssemblyInfo(bool isVB, bool includeNonVersionAttributes, bool gitRepo)
public async Task AssemblyInfo(bool isVB, bool includeNonVersionAttributes, bool gitRepo, bool isPrerelease, bool isPublicRelease)
{
this.WriteVersionFile();
this.WriteVersionFile(prerelease: isPrerelease ? "-beta" : string.Empty);
if (gitRepo)
{
this.InitializeSourceControl();
Expand All @@ -749,6 +749,8 @@ public async Task AssemblyInfo(bool isVB, bool includeNonVersionAttributes, bool
this.testProject.AddProperty("NBGV_EmitNonVersionCustomAttributes", "true");
}

this.globalProperties["PublicRelease"] = isPublicRelease ? "true" : "false";

var result = await this.BuildAsync("Build", logVerbosity: LoggerVerbosity.Minimal);
string assemblyPath = result.BuildResult.ProjectStateAfterBuild.GetPropertyValue("TargetPath");
string versionFileContent = File.ReadAllText(Path.Combine(this.projectDirectory, result.BuildResult.ProjectStateAfterBuild.GetPropertyValue("VersionSourceFile")));
Expand Down Expand Up @@ -795,6 +797,8 @@ public async Task AssemblyInfo(bool isVB, bool includeNonVersionAttributes, bool
Assert.Equal(result.AssemblyCompany, thisAssemblyClass.GetField("AssemblyCompany", fieldFlags)?.GetValue(null));
Assert.Equal(result.AssemblyCopyright, thisAssemblyClass.GetField("AssemblyCopyright", fieldFlags)?.GetValue(null));
Assert.Equal(result.GitCommitId, thisAssemblyClass.GetField("GitCommitId", fieldFlags)?.GetValue(null) ?? string.Empty);
Assert.Equal(result.PublicRelease, thisAssemblyClass.GetField("IsPublicRelease", fieldFlags)?.GetValue(null));
Assert.Equal(!string.IsNullOrEmpty(result.PrereleaseVersion), thisAssemblyClass.GetField("IsPrerelease", fieldFlags)?.GetValue(null));

if (gitRepo)
{
Expand Down
49 changes: 47 additions & 2 deletions src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs
Expand Up @@ -77,6 +77,10 @@ public class AssemblyVersionInfo : Task

public string AssemblyConfiguration { get; set; }

public bool PublicRelease { get; set; }

public string PrereleaseVersion { get; set; }

public string GitCommitId { get; set; }

public string GitCommitDateTicks { get; set; }
Expand Down Expand Up @@ -161,6 +165,11 @@ private CodeTypeDeclaration CreateThisAssemblyClass()
{ "AssemblyConfiguration", this.AssemblyConfiguration },
{ "GitCommitId", this.GitCommitId },
}).ToArray());
thisAssembly.Members.AddRange(CreateFields(new Dictionary<string, bool>
{
{ "IsPublicRelease", this.PublicRelease },
{ "IsPrerelease", !string.IsNullOrEmpty(this.PrereleaseVersion) },
}).ToArray());

if (long.TryParse(this.GitCommitDateTicks, out long gitCommitDateTicks))
{
Expand Down Expand Up @@ -222,9 +231,17 @@ private static IEnumerable<CodeMemberField> CreateFields(IReadOnlyDictionary<str
}
}

private static CodeMemberField CreateField(string name, string value)
private static IEnumerable<CodeMemberField> CreateFields<T>(IReadOnlyDictionary<string, T> namesAndValues)
{
return new CodeMemberField(typeof(string), name)
foreach (var item in namesAndValues)
{
yield return CreateField(item.Key, item.Value);
}
}

private static CodeMemberField CreateField<T>(string name, T value)
{
return new CodeMemberField(typeof(T), name)
{
Attributes = MemberAttributes.Const | MemberAttributes.Assembly,
InitExpression = new CodePrimitiveExpression(value),
Expand Down Expand Up @@ -359,6 +376,12 @@ private void GenerateThisAssemblyClass()
{ "AssemblyConfiguration", this.AssemblyConfiguration },
{ "GitCommitId", this.GitCommitId },
};
var boolFields = new Dictionary<string, bool>
{
{ "IsPublicRelease", this.PublicRelease },
{ "IsPrerelease", !string.IsNullOrEmpty(this.PrereleaseVersion) },
};

if (hasKeyInfo)
{
fields.Add("PublicKey", publicKey);
Expand All @@ -373,6 +396,11 @@ private void GenerateThisAssemblyClass()
}
}

foreach (var pair in boolFields)
{
this.generator.AddThisAssemblyMember(pair.Key, pair.Value);
}

if (long.TryParse(this.GitCommitDateTicks, out long gitCommitDateTicks))
{
this.generator.AddCommitDateProperty(gitCommitDateTicks);
Expand Down Expand Up @@ -417,6 +445,8 @@ internal CodeGenerator()
internal abstract void StartThisAssemblyClass();

internal abstract void AddThisAssemblyMember(string name, string value);

internal abstract void AddThisAssemblyMember(string name, bool value);

internal abstract void EndThisAssemblyClass();

Expand Down Expand Up @@ -459,6 +489,11 @@ internal override void AddThisAssemblyMember(string name, string value)
this.codeBuilder.AppendLine($" static member internal {name} = \"{value}\"");
}

internal override void AddThisAssemblyMember(string name, bool value)
{
this.codeBuilder.AppendLine($" static member internal {name} = {(value ? "true" : "false")}");
}

internal override void EmitNamespaceIfRequired(string ns)
{
this.codeBuilder.AppendLine($"namespace {ns}");
Expand Down Expand Up @@ -516,6 +551,11 @@ internal override void AddThisAssemblyMember(string name, string value)
this.codeBuilder.AppendLine($" internal const string {name} = \"{value}\";");
}

internal override void AddThisAssemblyMember(string name, bool value)
{
this.codeBuilder.AppendLine($" internal const bool {name} = {(value ? "true" : "false")};");
}

internal override void AddCommitDateProperty(long ticks)
{
this.codeBuilder.AppendLine($" internal static readonly System.DateTime GitCommitDate = new System.DateTime({ticks}L, System.DateTimeKind.Utc);");
Expand Down Expand Up @@ -556,6 +596,11 @@ internal override void AddThisAssemblyMember(string name, string value)
this.codeBuilder.AppendLine($" Friend Const {name} As String = \"{value}\"");
}

internal override void AddThisAssemblyMember(string name, bool value)
{
this.codeBuilder.AppendLine($" Friend Const {name} As Boolean = {(value ? "True" : "False")}");
}

internal override void AddCommitDateProperty(long ticks)
{
this.codeBuilder.AppendLine($" Friend Shared ReadOnly GitCommitDate As System.DateTime = New System.DateTime({ticks}L, System.DateTimeKind.Utc)");
Expand Down
Expand Up @@ -162,6 +162,8 @@
AssemblyCopyright="$(AssemblyCopyright)"
AssemblyCompany="$(AssemblyCompany)"
AssemblyConfiguration="$(Configuration)"
PublicRelease="$(PublicRelease)"
PrereleaseVersion="$(PrereleaseVersion)"
GitCommitId="$(GitCommitId)"
GitCommitDateTicks="$(GitCommitDateTicks)"
EmitNonVersionCustomAttributes="$(NBGV_EmitNonVersionCustomAttributes)"
Expand Down