Skip to content

Commit

Permalink
Conditional processing for windows batch files using "rem" as the com…
Browse files Browse the repository at this point in the history
…ment.

SimpleConfigModel wires up this processing for: .cmd & .bat
  • Loading branch information
seancpeters committed Sep 22, 2016
1 parent f63440d commit 1c0d078
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum ConditionalType
CNoComments,
CLineComments,
CBlockComments,
HashSignLineComment
HashSignLineComment,
RemLineComment
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public static IReadOnlyList<IOperationProvider> ConditionalSetup(ConditionalType
case ConditionalType.HashSignLineComment:
setup = HashSignLineCommentConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
break;
case ConditionalType.RemLineComment:
setup = RemLineCommentConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
break;
default:
throw new Exception($"Unrecognized conditional type {style}");
}
Expand Down Expand Up @@ -172,10 +175,10 @@ public static List<IOperationProvider> CStyleBlockCommentConditionalSetup(string

public static List<IOperationProvider> CStyleLineCommentsConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
string replaceOperationId = "Replacement (C style): (//) -> ()";
string uncommentOperationId = "Uncomment (C style): (////) -> (//)";
IOperationProvider uncomment = new Replacement("////", "//", uncommentOperationId);
IOperationProvider commentReplace = new Replacement("//", string.Empty, replaceOperationId);
string uncommentOperationId = "Uncomment (C style): (//) -> ()";
string reduceCommentsOperationId = "Reduce comment (C style): (////) -> (//)";
IOperationProvider uncomment = new Replacement("//", string.Empty, uncommentOperationId);
IOperationProvider reduceComment = new Replacement("////", "//", reduceCommentsOperationId);

ConditionalTokens tokens = new ConditionalTokens
{
Expand All @@ -186,7 +189,7 @@ public static List<IOperationProvider> CStyleLineCommentsConditionalSetup(string
ActionableIfTokens = new[] { "////#if" },
ActionableElseIfTokens = new[] { "////#elseif" },
ActionableElseTokens = new[] { "////#else" },
ActionableOperations = new[] { replaceOperationId, uncommentOperationId }
ActionableOperations = new[] { uncommentOperationId, reduceCommentsOperationId }
};

ConditionEvaluator evaluator = EvaluatorSelector.Select(evaluatorType);
Expand All @@ -195,8 +198,8 @@ public static List<IOperationProvider> CStyleLineCommentsConditionalSetup(string
return new List<IOperationProvider>()
{
conditional,
uncomment,
commentReplace
reduceComment,
uncomment
};
}

Expand All @@ -219,14 +222,13 @@ public static List<IOperationProvider> CStyleNoCommentsConditionalSetup(string e
};
}

// TODO: test
// this should work for nginx.conf, Perl, bash, etc.
public static List<IOperationProvider> HashSignLineCommentConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
string uncommentOperationId = "Uncomment (hash line): (##) -> (#)";
string replaceOperationId = "Replacement (hash line): (#) -> ()";
IOperationProvider uncomment = new Replacement("##", "#", uncommentOperationId);
IOperationProvider commentReplace = new Replacement("#", "", replaceOperationId);
string uncommentOperationId = "Uncomment (hash line): (#) -> ()";
string reduceCommentOperationId = "Reduce comment (hash line): (##) -> (#)";
IOperationProvider uncomment = new Replacement("#", string.Empty, uncommentOperationId);
IOperationProvider reduceComment = new Replacement("##", "#", reduceCommentOperationId);

ConditionalTokens tokens = new ConditionalTokens
{
Expand All @@ -237,7 +239,37 @@ public static List<IOperationProvider> HashSignLineCommentConditionalSetup(strin
ActionableIfTokens = new[] { "##if" },
ActionableElseIfTokens = new[] { "##elseif" },
ActionableElseTokens = new[] { "##else" },
ActionableOperations = new[] { replaceOperationId, uncommentOperationId }
ActionableOperations = new[] { uncommentOperationId, reduceCommentOperationId }
};

ConditionEvaluator evaluator = EvaluatorSelector.Select(evaluatorType);
IOperationProvider conditional = new Conditional(tokens, wholeLine, trimWhiteSpace, evaluator, id);

return new List<IOperationProvider>()
{
conditional,
reduceComment,
uncomment
};
}

public static List<IOperationProvider> RemLineCommentConditionalSetup(String evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
string uncommentOperationId = "Replacement (bat rem): (rem) -> ()";
string reduceCommentOperationId = "Uncomment (bat rem): (rem rem) -> (rem)";
IOperationProvider uncomment = new Replacement("rem", string.Empty, uncommentOperationId);
IOperationProvider reduceComment = new Replacement("rem rem", "rem", reduceCommentOperationId);

ConditionalTokens tokens = new ConditionalTokens
{
IfTokens = new[] { "rem #if" },
ElseTokens = new[] { "rem #else" },
ElseIfTokens = new[] { "rem #elseif" },
EndIfTokens = new[] { "rem #endif", "rem rem #endif" },
ActionableIfTokens = new[] { "rem rem #if" },
ActionableElseIfTokens = new[] { "rem rem #elseif" },
ActionableElseTokens = new[] { "rem rem #else" },
ActionableOperations = new[] { uncommentOperationId, reduceCommentOperationId }
};

ConditionEvaluator evaluator = EvaluatorSelector.Select(evaluatorType);
Expand All @@ -246,8 +278,8 @@ public static List<IOperationProvider> HashSignLineCommentConditionalSetup(strin
return new List<IOperationProvider>()
{
conditional,
uncomment,
commentReplace
reduceComment,
uncomment
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,56 +62,6 @@ public static bool ToBool(this JToken token, string key = null, bool defaultValu
{
return defaultValue;
}

// 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ IReadOnlyList<KeyValuePair<string, IGlobalRunConfig>> IRunnableProjectConfig.Spe
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.jsp", "<!--", ConditionalType.Xml));
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.asp", "<!--", ConditionalType.Xml));
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.aspx", "<!--", ConditionalType.Xml));
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.bat", "rem --:", ConditionalType.RemLineComment));
defaultSpecials.Add(new SpecialOperationConfigParams("**/*.cmd", "rem --:", ConditionalType.RemLineComment));

List<KeyValuePair<string, IGlobalRunConfig>> specialOperationConfig = new List<KeyValuePair<string, IGlobalRunConfig>>();

Expand Down
Loading

0 comments on commit 1c0d078

Please sign in to comment.