From f63440de998a9626da5802bc1dfbf63a4bc8e43a Mon Sep 17 00:00:00 2001 From: seancpeters Date: Thu, 22 Sep 2016 15:13:32 -0700 Subject: [PATCH] Fixed the config for the new hash style line comment conditional. Modified JExtension.ToBool() to also convert strings. --- .../Config/ConditionalConfig.cs | 2 +- .../JExtensions.cs | 79 +++++++++++++++---- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/Config/ConditionalConfig.cs b/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/Config/ConditionalConfig.cs index 12282ecd75..f56a62f3df 100644 --- a/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/Config/ConditionalConfig.cs +++ b/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/Config/ConditionalConfig.cs @@ -233,7 +233,7 @@ public static List HashSignLineCommentConditionalSetup(strin IfTokens = new[] { "#if" }, ElseTokens = new[] { "#else" }, ElseIfTokens = new[] { "#elseif" }, - EndIfTokens = new[] { "#endif" }, + EndIfTokens = new[] { "#endif", "##endif" }, ActionableIfTokens = new[] { "##if" }, ActionableElseIfTokens = new[] { "##elseif" }, ActionableElseTokens = new[] { "##else" }, diff --git a/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/JExtensions.cs b/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/JExtensions.cs index aa0debb887..141af7dbcc 100644 --- a/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/JExtensions.cs +++ b/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/JExtensions.cs @@ -37,30 +37,81 @@ public static string ToString(this JToken token, string key) public static bool ToBool(this JToken token, string key = null, bool defaultValue = false) { - if (key == null) - { - if (token == null || token.Type != JTokenType.Boolean) - { - return defaultValue; - } + JToken checkToken; - return string.Equals(token.ToString(), "true", StringComparison.OrdinalIgnoreCase); + // determine which token to bool-ify + if (token == null) + { + return defaultValue; } - - JObject obj = token as JObject; - - if (obj == null) + else if (key == null) + { + checkToken = token; + } + else if (! ((JObject)token).TryGetValue(key, StringComparison.OrdinalIgnoreCase, out checkToken)) { return defaultValue; } - JToken element; - if (!obj.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out element) || element.Type != JTokenType.Boolean) + // do the conversion on checkToken + if (checkToken.Type == JTokenType.Boolean || checkToken.Type == JTokenType.String) + { + return string.Equals(checkToken.ToString(), "true", StringComparison.OrdinalIgnoreCase); + } + else { return defaultValue; } - return string.Equals(element.ToString(), "true", StringComparison.OrdinalIgnoreCase); + // all old + //if (key == null) + //{ + // if (token != null + // && (token.Type == JTokenType.Boolean || token.Type == JTokenType.String)) + // { + // return string.Equals(token.ToString(), "true", StringComparison.OrdinalIgnoreCase); + // } + // else + // { + // return defaultValue; + // } + + // // old + // //if (token == null || token.Type != JTokenType.Boolean) + // //{ + // // return defaultValue; + // //} + + // //return string.Equals(token.ToString(), "true", StringComparison.OrdinalIgnoreCase); + //} + + //JObject obj = token as JObject; + + //if (obj == null) + //{ + // return defaultValue; + //} + + //JToken element; + + //// new + //if (obj.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out element) + // && (element.Type == JTokenType.Boolean || element.Type == JTokenType.String)) + //{ + // return string.Equals(element.ToString(), "true", StringComparison.OrdinalIgnoreCase); + //} + //else + //{ + // return defaultValue; + //} + + //// old + ////if (!obj.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out element) || element.Type != JTokenType.Boolean) + ////{ + //// return defaultValue; + ////} + + ////return string.Equals(element.ToString(), "true", StringComparison.OrdinalIgnoreCase); } public static int ToInt32(this JToken token, string key = null, int defaultValue = 0)