From 14467b30cec9456afc94519c11c17386ce8a8225 Mon Sep 17 00:00:00 2001 From: Darrell Tunnell Date: Thu, 17 Aug 2017 20:32:32 +0100 Subject: [PATCH] Closes #1274 - Got rid of system.web (databinder.eval) I have written a new implementation and to replace databinder.eval so that we can drop system.web as a dependency. System.Web should never have been adopted imho! Corrected docs. --- docs/configuration.md | 2 +- src/GitVersionCore/GitVersionCore.csproj | 1 - .../OutputVariables/VariableProvider.cs | 4 +- src/GitVersionCore/StringFormatWith.cs | 190 +++++------------- 4 files changed, 52 insertions(+), 145 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 46c9c77810..5b289a23f5 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -72,7 +72,7 @@ attributes. Set this to any of the available [variables](/more-info/variables) to change the value of the `AssemblyInformationalVersion` attribute. Default set to `{InformationalVersion}`. It also supports string interpolation -(`{MajorMinorPatch}+{Branch}`) +(`{MajorMinorPatch}+{BranchName}`) ### mode Sets the `mode` of how GitVersion should create a new version. Read more at diff --git a/src/GitVersionCore/GitVersionCore.csproj b/src/GitVersionCore/GitVersionCore.csproj index fea4f65af9..8131080017 100644 --- a/src/GitVersionCore/GitVersionCore.csproj +++ b/src/GitVersionCore/GitVersionCore.csproj @@ -49,7 +49,6 @@ - diff --git a/src/GitVersionCore/OutputVariables/VariableProvider.cs b/src/GitVersionCore/OutputVariables/VariableProvider.cs index ec71af58ed..ef6acb5c55 100644 --- a/src/GitVersionCore/OutputVariables/VariableProvider.cs +++ b/src/GitVersionCore/OutputVariables/VariableProvider.cs @@ -53,9 +53,9 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, { try { - informationalVersion = config.AssemblyInformationalFormat.FormatWith(semverFormatValues); + informationalVersion = config.AssemblyInformationalFormat.FormatWith(semverFormatValues); } - catch (FormatException formex) + catch (ArgumentException formex) { throw new WarningException(string.Format("Unable to format AssemblyInformationalVersion. Check your format string: {0}", formex.Message)); } diff --git a/src/GitVersionCore/StringFormatWith.cs b/src/GitVersionCore/StringFormatWith.cs index 54eb2d5884..a33c1ded82 100644 --- a/src/GitVersionCore/StringFormatWith.cs +++ b/src/GitVersionCore/StringFormatWith.cs @@ -1,170 +1,78 @@ -#region BSD License - -/* -Copyright (c) 2010, NETFx -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - -* Neither the name of Clarius Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#endregion - -// Originally appeared in http://haacked.com/archive/2009/01/14/named-formats-redux.aspx -// Authored by Henri Wiechers -// Ported to NETFx by Daniel Cazzulino -using System; -using System.IO; -using System.Text; -using System.Web; -using System.Web.UI; +using System; +using System.Linq.Expressions; +using System.Reflection; +using System.Text.RegularExpressions; namespace GitVersion { - /// - /// Requires a reference to System.Web. - /// + static class StringFormatWithExtension { + private static readonly Regex TokensRegex = new Regex(@"{\w+}", RegexOptions.Compiled); + /// - /// Formats the string with the given source object. + /// Formats a string template with the given source object. /// Expression like {Id} are replaced with the corresponding - /// property value in the . Supports - /// all DataBinder.Eval expressions formats - /// for property access. - /// - /// - /// The string to format + /// property value in the . + /// Supports property access expressions. + /// + /// The template to be replaced with values from the source object. The template can contain expressions wrapped in curly braces, that point to properties or fields on the source object to be used as a substitute, e.g '{Foo.Bar.CurrencySymbol} foo {Foo.Bar.Price}'. /// The source object to apply to format - public static string FormatWith(this string format, object source) + public static string FormatWith(this string template, T source) { - if (format == null) + if (template == null) { - throw new ArgumentNullException("format"); + throw new ArgumentNullException("template"); } - var result = new StringBuilder(format.Length*2); - - using (var reader = new StringReader(format)) + // {MajorMinorPatch}+{Branch} + var objType = source.GetType(); + foreach (Match match in TokensRegex.Matches(template)) { - var expression = new StringBuilder(); - - var state = State.OutsideExpression; - do - { - int @char; - switch (state) - { - case State.OutsideExpression: - @char = reader.Read(); - switch (@char) - { - case -1: - state = State.End; - break; - case '{': - state = State.OnOpenBracket; - break; - case '}': - state = State.OnCloseBracket; - break; - default: - result.Append((char) @char); - break; - } - break; - case State.OnOpenBracket: - @char = reader.Read(); - switch (@char) - { - case -1: - throw new FormatException(); - case '{': - result.Append('{'); - state = State.OutsideExpression; - break; - default: - expression.Append((char) @char); - state = State.InsideExpression; - break; - } - break; - case State.InsideExpression: - @char = reader.Read(); - switch (@char) - { - case -1: - throw new FormatException(); - case '}': - result.Append(OutExpression(source, expression.ToString())); - expression.Length = 0; - state = State.OutsideExpression; - break; - default: - expression.Append((char) @char); - break; - } - break; - case State.OnCloseBracket: - @char = reader.Read(); - switch (@char) - { - case '}': - result.Append('}'); - state = State.OutsideExpression; - break; - default: - throw new FormatException(); - } - break; - default: - throw new InvalidOperationException("Invalid state."); - } - } while (state != State.End); + var memberAccessExpression = TrimBraces(match.Value); + Func expression = CompileDataBinder(objType, memberAccessExpression); + string propertyValue = expression(source); + template = template.Replace(match.Value, propertyValue); } - return result.ToString(); + return template; + } - static string OutExpression(object source, string expression) - { - var format = ""; - var colonIndex = expression.IndexOf(':'); - if (colonIndex > 0) + static string TrimBraces(string originalExpression) + { + if (!string.IsNullOrWhiteSpace(originalExpression)) { - format = expression.Substring(colonIndex + 1); - expression = expression.Substring(0, colonIndex); + return originalExpression.TrimStart('{').TrimEnd('}'); } + return originalExpression; + } - try + static Func CompileDataBinder(Type type, string expr) + { + var param = Expression.Parameter(typeof(object)); + Expression body = Expression.Convert(param, type); + var members = expr.Split('.'); + for (int i = 0; i < members.Length; i++) { - if (string.IsNullOrEmpty(format)) - { - return (DataBinder.Eval(source, expression) ?? "").ToString(); - } - return DataBinder.Eval(source, expression, "{0:" + format + "}"); + body = Expression.PropertyOrField(body, members[i]); } - catch (HttpException) + var method = typeof(Convert).GetMethod("ToString", BindingFlags.Static | BindingFlags.Public, + null, new Type[] { body.Type }, null); + if (method == null) { - throw new FormatException("Failed to format '" + expression + "'."); + method = typeof(Convert).GetMethod("ToString", BindingFlags.Static | BindingFlags.Public, + null, new Type[] { typeof(object) }, null); + body = Expression.Call(method, Expression.Convert(body, typeof(object))); + } + else + { + body = Expression.Call(method, body); } - } - enum State - { - OutsideExpression, - OnOpenBracket, - InsideExpression, - OnCloseBracket, - End + return Expression.Lambda>(body, param).Compile(); } + } } \ No newline at end of file