Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ImrePyhvel committed Mar 12, 2015
2 parents 5573d1e + dd9b258 commit d256f4b
Show file tree
Hide file tree
Showing 28 changed files with 652 additions and 85 deletions.
4 changes: 2 additions & 2 deletions AssemblyVersioning/AssemblyInfoFileCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public static String GenerateAttributeRow<TAttribute>(String generator, String v
{
if (version == null) { return null; }

const String pattern = @"[assembly: {0}(""{1}"")] // algorithm: {2}
const String pattern = @"[assembly: {0}(@""{1}"")] // algorithm: {2}
";
String attributeName = typeof(TAttribute).Name
.Replace(typeof(Attribute).Name, ""); //without Attribute at end.

return String.Format(pattern,
attributeName,
version,
version.Replace(@"""", @""""""), // support version content with double-quotes.
generator);
}
}
Expand Down
12 changes: 10 additions & 2 deletions AssemblyVersioning/AssemblyVersioning.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@
<ItemGroup>
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />
<Reference Include="Nortal.Utilities.TextTemplating">
<HintPath>..\packages\Nortal.Utilities.TextTemplating.0.10.0\lib\Nortal.Utilities.TextTemplating.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfoFileCreator.cs" />
<Compile Include="GeneratorResolver.cs" />
<Compile Include="Generators\CustomVersionGenerator.cs" />
<Compile Include="Generators\CustomVersionGeneratorModel.cs" />
<Compile Include="Generators\DateToVersionNumberCalculation.cs" />
<Compile Include="Generators\NugetSemanticVersionGenerator.cs" />
<Compile Include="Generators\HumanReadableBuildInfoGenerator.cs" />
<Compile Include="GlobalSuppressions.cs" />
Expand All @@ -83,6 +89,7 @@
<SubType>Designer</SubType>
</None>
<None Include="Properties\Nortal.Utilities.AssemblyVersioning.MsBuildTask.props" />
<None Include="_nugetPackaging\MsBuildTask\build\Nortal.Utilities.AssemblyVersioning.MsBuildTask.props" />
<None Include="_nugetPackaging\PackageAllRelease.bat" />
<None Include="_nugetPackaging\Dll\Nortal.Utilities.AssemblyVersioning.nuspec">
<SubType>Designer</SubType>
Expand All @@ -100,12 +107,13 @@
<None Include="_nugetPackaging\PackageAllDebug.bat" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="CustomDictionary.xml">
<CodeAnalysisDictionary Include="Properties\CustomDictionary.xml">
<SubType>Designer</SubType>
</CodeAnalysisDictionary>
</ItemGroup>
<ItemGroup>
<Content Include="_nugetPackaging\MsBuildTask\Readme.OnInstall.txt" />
<Content Include="LICENSE_Apache2.0.txt" />
<Content Include="_nugetPackaging\MsBuildTask\Readme.txt" />
<None Include="_nugetPackaging\MsBuildTask\tools\install.ps1" />
</ItemGroup>
<ItemGroup />
Expand Down
20 changes: 17 additions & 3 deletions AssemblyVersioning/GenerateExtendedAssemblyInfoTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ limitations under the License.
This file is from project https://github.com/NortalLTD/AssemblyVersioning, Nortal.Utilities.AssemblyVersioning, file 'GenerateExtendedAssemblyInfoTask.cs'.
*/

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Nortal.Utilities.AssemblyVersioning
{
Expand All @@ -43,11 +43,20 @@ public class GenerateExtendedAssemblyInfoTask : Task
public ITaskItem OutputFile { get; set; }

public String BuildConfiguration { get; set; }
public Boolean IsPrerelease { get; set; }

public String GeneratorForFileVersion { get; set; }
public String GeneratorForInformationalVersion { get; set; }
public String GeneratorForConfiguration { get; set; }

public String CustomField1 { get; set; }
public String CustomField2 { get; set; }
public String CustomField3 { get; set; }

/// <summary>
/// The entry-point to task functionality.
/// </summary>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public override bool Execute()
{
Expand All @@ -73,6 +82,10 @@ private VersionGenerationContext InitializeContext()
var context = new VersionGenerationContext();
context.BaseVersion = BaseVersionExtractor.Extract(this.Log, this.BaseVersionFile);
context.BuildConfiguration = this.BuildConfiguration;
context.IsPrerelease = this.IsPrerelease;
context.CustomField1 = this.CustomField1;
context.CustomField2 = this.CustomField2;
context.CustomField3 = this.CustomField3;
return context;
}

Expand All @@ -90,8 +103,9 @@ internal String GenerateAttributeLine<TAttribute>(VersionGenerationContext conte
where TAttribute: Attribute
{
this.Log.LogMessage("Generating content for '{0}' using algorithm '{1}'..", typeof(TAttribute).Name, generatorName);
var generator = GeneratorResolver.ResolveByName(generatorName);
var generator = GeneratorResolver.ResolveWithArgument(generatorName, context);
Debug.Assert(generator != null);

String row = AssemblyInfoFileCreator.GenerateAttributeRow<TAttribute>(generator, context);
return row;
}
Expand Down
40 changes: 36 additions & 4 deletions AssemblyVersioning/GeneratorResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ limitations under the License.
This file is from project https://github.com/NortalLTD/AssemblyVersioning, Nortal.Utilities.AssemblyVersioning, file 'GeneratorResolver.cs'.
*/

using System.Globalization;
using Nortal.Utilities.AssemblyVersioning.Generators;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Nortal.Utilities.AssemblyVersioning
{
/// <summary>
/// Responsible for findin a generator class instance to use based on user configuration input.
/// </summary>
public static class GeneratorResolver
{
/// <summary>
/// Lists all supported generators automatically found in this assembly.
/// </summary>
/// <returns></returns>
public static IEnumerable<Type> FindAllGeneratorTypes()
{
Type searchInterface = typeof(IVersionGenerator);
Expand All @@ -38,10 +46,33 @@ public static IEnumerable<Type> FindAllGeneratorTypes()
}
}

public static IVersionGenerator ResolveByName(String name)
/// <summary>
/// Scans included generators and tries to find a match for provided generator name.
/// </summary>
/// <param name="nameWithArgument">name as provided by MsBuild, may contain argument separated by colon.</param>
/// <param name="context">Context to storage generator argument in.</param>
/// <returns></returns>
public static IVersionGenerator ResolveWithArgument(String nameWithArgument, VersionGenerationContext context)
{
if (String.IsNullOrEmpty(name)) { return new SkipVersionGenerator(); }
if (context == null) { throw new ArgumentNullException("context"); }
if (String.IsNullOrEmpty(nameWithArgument)) { return new SkipVersionGenerator(); }
// argument is separated from name by colon. Name cannot contain special symbols:
Regex regex = new Regex(@"^(?<namePart>[a-zA-Z\d]+)(:(?<argumentPart>.+))?$");
var match = regex.Match(nameWithArgument);
if (match.Success)
{
context.VersionGenerationArgument = match.Groups["argumentPart"].Value;
var namePart = match.Groups["namePart"].Value;
return ResolveByName(namePart);
}

//special case: when input does not match the format, then it is to be considered a format for CustomVersionGenerator:
context.VersionGenerationArgument = nameWithArgument; //everything is context.
return new CustomVersionGenerator();
}

private static IVersionGenerator ResolveByName(String name)
{
foreach (var type in FindAllGeneratorTypes())
{
if (CheckNameMatchesType(name, type))
Expand All @@ -54,14 +85,15 @@ public static IVersionGenerator ResolveByName(String name)

private static bool CheckNameMatchesType(String name, Type type)
{
//try names with a fallback logic.
return CheckNameMatchesTypeNameExactly(name, type)
|| CheckNameMatchesTypeNameExactly(name + "Generator", type) // if suffix was omitted
|| CheckNameMatchesTypeNameExactly(name + "VersionGenerator", type); // if suffix was omitted
}

private static bool CheckNameMatchesTypeNameExactly(String name, Type type)
{
return String.Compare(type.Name, name, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase) == 0;
return String.Compare(type.Name, name, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase) == 0; // be lenient on case - user does not care much about case in configuration.
}
}
}
49 changes: 49 additions & 0 deletions AssemblyVersioning/Generators/CustomVersionGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2013 Imre Pühvel, AS Nortal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file is from project https://github.com/NortalLTD/AssemblyVersioning, Nortal.Utilities.AssemblyVersioning, file 'GenerateExtendedAssemblyInfoTask.cs'.
*/

using Nortal.Utilities.TextTemplating;
using System;

namespace Nortal.Utilities.AssemblyVersioning.Generators
{
/// <summary>
/// Version generator which takes a custom format from context argument and injects the placeholders in format with values from model.
/// </summary>
public sealed class CustomVersionGenerator : IVersionGenerator
{
public string GenerateVersionInfoFrom(VersionGenerationContext context)
{
if (context == null) { throw new ArgumentNullException("context"); }

String customFormat = context.VersionGenerationArgument;
if (String.IsNullOrEmpty(customFormat)) { throw new ArgumentException("Custom format requested but no format was provided in VersionGenerationArgument.", "context"); }

var model = new CustomizedVersionModel(context);

SyntaxSettings settings = new SyntaxSettings()
{
BeginTag = "{",
EndTag = "}",
};

var templateEngine = new TemplateProcessingEngine(settings);
String result = templateEngine.Process(customFormat, model);
return result;
}
}
}
67 changes: 67 additions & 0 deletions AssemblyVersioning/Generators/CustomVersionGeneratorModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2013 Imre Pühvel, AS Nortal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file is from project https://github.com/NortalLTD/AssemblyVersioning, Nortal.Utilities.AssemblyVersioning, file 'GenerateExtendedAssemblyInfoTask.cs'.
*/

using System;

namespace Nortal.Utilities.AssemblyVersioning.Generators
{
public sealed class CustomizedVersionModel
{
public CustomizedVersionModel(VersionGenerationContext context)
{
if (context == null) { throw new ArgumentNullException("context"); }
this.Context = context;
}

// storage for model properties
private VersionGenerationContext Context { get; set; }
private DateTime FixedNow = DateTime.Now; // to ensure it does not change between multiple calls
private DateTime FixedUtcNow = DateTime.UtcNow; // to ensure it does not change between multiple calls

// BaseVersion parts:
public int Major { get { return this.Context.BaseVersion.Major; } }
public int Minor { get { return this.Context.BaseVersion.Minor; } }
public int Build { get { return this.Context.BaseVersion.Build; } }
public int Revision { get { return this.Context.BaseVersion.Revision; } }

// date components:
public DateTime Now { get { return this.FixedNow; } }
public int DateNumber { get { return DateToVersionNumberCalculation.BuildDatePart(this.FixedNow); } }
public int TimeNumber { get { return DateToVersionNumberCalculation.BuildTimePart(this.FixedNow); } }

public DateTime UtcNow { get { return this.FixedUtcNow; } }
public int UtcDateNumber { get { return DateToVersionNumberCalculation.BuildDatePart(this.FixedUtcNow); } }
public int UtcTimeNumber { get { return DateToVersionNumberCalculation.BuildTimePart(this.FixedUtcNow); } }

// user context:
public String BuildConfiguration { get { return this.Context.BuildConfiguration; } }

// Environment
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
public String Domain { get { return Environment.UserDomainName; } }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
public String UserName { get { return Environment.UserName; } }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
public String MachineName { get { return Environment.MachineName; } }

//Custom fields that are filled from MsBuild side
public String CustomField1 { get { return this.Context.CustomField1; } }
public String CustomField2 { get { return this.Context.CustomField2; } }
public String CustomField3 { get { return this.Context.CustomField3; } }
}
}
22 changes: 22 additions & 0 deletions AssemblyVersioning/Generators/DateToVersionNumberCalculation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nortal.Utilities.AssemblyVersioning.Generators
{
internal static class DateToVersionNumberCalculation
{
internal static int BuildDatePart(DateTime forTime)
{
//last digit mod 6 (max value in version component is 65536). Ex: 2016 -> 6; 2017 -> 0
int yearpart = forTime.Year % 10 % 7;
return yearpart * 10000 + 100 * forTime.Month + forTime.Day;
}

internal static int BuildTimePart(DateTime forTime)
{
return forTime.Hour * 100 + forTime.Minute;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override Version GenerateSystemVersion(VersionGenerationContext context)
var baseVersion = context.BaseVersion;

//replace just the revision
int revision = BuildDatePart(now); //reuse the logic already built for HumanReadable2SlotTimestampGenerator
int revision = DateToVersionNumberCalculation.BuildDatePart(now); //reuse the logic already built for HumanReadable2SlotTimestampGenerator

return new Version(baseVersion.Major, baseVersion.Minor, baseVersion.Build, revision);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,13 @@ namespace Nortal.Utilities.AssemblyVersioning.Generators
public class HumanReadable2SlotTimestampGenerator : SystemVersionGeneratorBase
{

internal static int BuildDatePart(DateTime forTime)
{
//last digit mod 6 (max value in version component is 65536). Ex: 2016 -> 6; 2017 -> 0
int yearpart = forTime.Year % 10 % 7;
return yearpart * 10000 + 100 * forTime.Month + forTime.Day;
}

internal static int BuildTimePart(DateTime forTime)
{
return forTime.Hour * 100 + forTime.Minute;
}

public override Version GenerateSystemVersion(VersionGenerationContext context)
{
if (context == null) { throw new ArgumentNullException("context"); }

DateTime buildDate = DateTime.UtcNow;
var build = BuildDatePart(buildDate);
var revision = BuildTimePart(buildDate);
var build = DateToVersionNumberCalculation.BuildDatePart(buildDate);
var revision = DateToVersionNumberCalculation.BuildTimePart(buildDate);

var baseVersion = context.BaseVersion;
return new Version(baseVersion.Major, baseVersion.Minor, build, revision);
Expand Down
Binary file modified AssemblyVersioning/GlobalSuppressions.cs
Binary file not shown.
Loading

0 comments on commit d256f4b

Please sign in to comment.