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

MSBuild Task for Semantic Versioning & Tests #137

Closed
wants to merge 22 commits into from
Closed

MSBuild Task for Semantic Versioning & Tests #137

wants to merge 22 commits into from

Conversation

JohnTortugo
Copy link
Contributor

@JohnTortugo JohnTortugo commented Apr 26, 2018

Implementation of generation of Versioning Strings as described in the plan.

  • Support for SemVer. 2.0
  • Support for SemVer. 1.0
  • Store computed information on a file to prevent unecessary re-computation.
  • Migrate task for extracting date+revision from OfficialBuildId
  • Add basic testing for Targets and Tasks
  • Documentation with usage examples

Suggestions are welcome.

@JohnTortugo JohnTortugo self-assigned this Apr 26, 2018

namespace Microsoft.DotNet.Build.Tasks.SemVer
{
public class SemVer : Task

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}

// So far we know that these first four fields aren't empty
Version = $"{Major}.{Minor}.{Patch}-{Prerelease}.{ShortDate:00000}.{Builds}+{ShortSHA}";

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

{
private Dictionary<string, string> formats = new Dictionary<string, string> {
{"dev", "{major}.{minor}.{patch}-{prerelease}.{shortdate}.{builds}+{shortsha}"},
{"stable", "{major}.{minor}.{patch}-{prerelease}"},

This comment was marked as spam.

@JohnTortugo JohnTortugo changed the title (DRAFT) MSBuild Task for Semantic Versioning & Tests MSBuild Task for Semantic Versioning & Tests May 1, 2018
<ItemGroup Condition="'$(TargetFramework)' == '$(NetFxTfm)'">
<Reference Include="Microsoft.Build.Tasks.v4.0" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

VersionString = VersionString.Replace("{builds}", Builds.ToString());
VersionString = VersionString.Replace("{shortsha}", ShortSHA);

return true;

This comment was marked as spam.

{
public class SemVer : Task
{
private Dictionary<string, string> formats = new Dictionary<string, string> {

This comment was marked as spam.

VersionString = formats[VersionString];
}

if ((VersionString.Contains("{major}") && Major == 0) ||

This comment was marked as spam.

This comment was marked as spam.

{
VersionString = FormatString.ToLower();

if (formats.ContainsKey(VersionString))

This comment was marked as spam.

Copy link
Member

@mmitche mmitche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments.

@weshaggard
Copy link
Member

I'm still very much interested in why this is a task instead of just done in a targets file? All we are doing is appending strings.

@JohnTortugo
Copy link
Contributor Author

JohnTortugo commented May 1, 2018

Talked with @weshaggard and @mmitche ; I am going to work on the changes they suggested.

@JohnTortugo
Copy link
Contributor Author

Hi guys, I've applied the changes. PTAL

===============================

Task package which handles generation of version strings.
The format that we use is described here: Documentation/Versioning.md

This comment was marked as spam.

ShortSha | string | SHA of the repo last commit.
FormatName | string | The name of the format string you want to use. Defaults are "dev", "stable" and "final".
FormatStrings | ItemGroup | Your custom format strings.
ExitCode | integer | **Output** Indicate whether an error happened (1) or not (0).

This comment was marked as spam.

This comment was marked as spam.

<PropertyGroup>
<VersionString Condition="'%(FormatStrings.Identity)'=='$(FormatName)'">%(FormatStrings.Format)</VersionString>

<ExitCode Condition="'$(VersionString)'!=''">0</ExitCode>

This comment was marked as spam.

if (months > 0) //only allow dates after comparedate
{
return string.Format("{0}{1}", months.ToString("D" + (Padding - 2)), seedDate.Day.ToString("D2"));
}

This comment was marked as spam.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Target Name="Versioning">

This comment was marked as spam.

This comment was marked as spam.

@weshaggard
Copy link
Member

I think the option of creating a few TestCase01.xml, TestCase02.xml, etc files and them calling "dotnet msbuild" is interesting, because: 1) it's simple; 2) we have quite some control of the test and the output; 3) Is quite portable across .net core and .net full?! 4) Do we need something more sophisticated?

Are those TestCase01.xml files csproj's? If so then that is similar to what I had in mind. Having some sample test projects that we call msbuild on and capture the output of the version in some way, or asserting it in the projects themselves by having them fail to build if the versions don't match what is expected.

@JohnTortugo
Copy link
Contributor Author

Are those TestCase01.xml files csproj's? If so then that is similar to what I had in mind. Having some sample test projects that we call msbuild on and capture the output of the version in some way, or asserting it in the projects themselves by having them fail to build if the versions don't match what is expected.

Yes, that's exactly it.

@weshaggard
Copy link
Member

@joperezr given your experience doing this in Buildtools could you also review this PR?

@joperezr
Copy link
Member

Sure, I’ll take a look at this later today

@@ -0,0 +1,134 @@
# Microsoft.DotNet.Build.Tasks.Versioning

Task package which handles generation of version strings. The versioning format that we use is described [here](../../Documentation/Versioning). See ["Task Packages"](../../Documentation/TaskPackages.md#usage) for guidance on installing this package.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@JohnTortugo
Copy link
Contributor Author

Hi @mmitche, @weshaggard / all; I believe we have sufficient material here to call this a minimal working version. Please, let me know what else I should work to get an approval for the merge :-)

}

// Leading zeros are only allowed in SemVer 1.0
if (IncludePadding)

This comment was marked as spam.

This comment was marked as spam.

Factoring reference to Buildtools framework.
Some typos / minor fixes here and there.

dateFromBuildId = dateFromBuildId.ToUniversalTime();
GeneratedShortDate = CreateShortDate(dateFromBuildId, ComparisonDate);
GeneratedRevision = match.Groups[2].Value;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@mmitche
Copy link
Member

mmitche commented May 15, 2018 via email

@JohnTortugo
Copy link
Contributor Author

Thanks. Pushed new update.

@@ -60,7 +60,7 @@ public override bool Execute()
// If OfficialBuildId is passed in, then use that to calculate the version and revision.
if (string.IsNullOrEmpty(OfficialBuildId))
{
GeneratedRevision = "0";
GeneratedRevision = (IncludePadding) ? "00" : "0";

This comment was marked as spam.

@@ -123,6 +124,11 @@ private bool SetVersionAndRevisionFromBuildId(string buildId)
GeneratedShortDate = CreateShortDate(dateFromBuildId, ComparisonDate);
GeneratedRevision = match.Groups[2].Value;

if (!IncludePadding)
{
GeneratedRevision = int.Parse(GeneratedRevision).ToString();

This comment was marked as spam.

</Target>


<Target Name="CreateVersioningCacheFile" AfterTargets="GenerateVersions" Condition="'$(ShouldCreateVersionFileDuringBuild)'=='true'">

This comment was marked as spam.

This comment was marked as spam.

<!-- If VersioningCacheFile exists already then import it; If it doesn't we are going to create it. -->
<Import Condition="Exists('$(VersioningCacheFile)')" Project="$(VersioningCacheFile)" />

<Target Name="GenerateVersions">

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@JohnTortugo
Copy link
Contributor Author

Any objection on merging this PR once currently running checks pass?

@@ -0,0 +1,91 @@
<!-- Copyright (c) .NET Foundation. All rights reserved. -->

This comment was marked as spam.

This comment was marked as spam.

@alexperovich
Copy link
Member

alexperovich commented May 16, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants