Skip to content

Commit

Permalink
add feature flags(#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
itn3000 committed Apr 12, 2022
1 parent b8a959b commit 47ecdb6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Cs2Mermaid.Lib/ConvertCsToMermaid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ConvertOptions
public string? LangVersion { get; set; }
public string? ChartOrientation { get; set; }
public bool? AsScript { get; set; }
public IEnumerable<KeyValuePair<string, string>>? Features { get; set; }
}
public static class ConvertCsToMermaid
{
Expand All @@ -33,9 +34,9 @@ static CSharpParseOptions CreateParseOption(ConvertOptions options)
throw new Exception($"failed to parse langveresion: {options.LangVersion}", e);
}
}
if(options.AsScript.HasValue)
if (options.AsScript.HasValue)
{
if(options.AsScript.Value)
if (options.AsScript.Value)
{
ret = ret.WithKind(SourceCodeKind.Script);
}
Expand All @@ -48,6 +49,10 @@ static CSharpParseOptions CreateParseOption(ConvertOptions options)
{
ret = ret.WithPreprocessorSymbols(options.PreprocessorSymbols);
}
if (options.Features != null)
{
ret = ret.WithFeatures(options.Features);
}
return ret;
}
public static void Convert(Stream input, Encoding inputEncoding, TextWriter tw, ConvertOptions convertOptions)
Expand Down Expand Up @@ -128,9 +133,9 @@ static void ConvertInternal(TextWriter tw, string currentNodeName, SyntaxNode no
}
else
{
var tokenString = child.ToString().Aggregate(new StringBuilder(), (sb, c) =>
var tokenString = child.ToString().Aggregate(new StringBuilder(), (sb, c) =>
{
if(char.IsWhiteSpace(c) || c == '"' || char.IsControl(c))
if (char.IsWhiteSpace(c) || c == '"' || char.IsControl(c))
{
sb.Append("\\u" + ((int)c).ToString("x04"));
}
Expand Down
16 changes: 16 additions & 0 deletions Cs2Mermaid.Test/ConvertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.IO;
using System;
using System.Collections.Generic;

namespace Cs2Mermaid.Test;

Expand Down Expand Up @@ -101,4 +102,19 @@ public void LangVersionTest()
_OutputHelper.WriteLine("cs10 = " + cs10_0);
Assert.NotEqual(cs10_0, cs7_3);
}
[Fact]
public void FeaturesTest()
{
var options = new ConvertOptions()
{
Features = new Dictionary<string, string>()
{
["F1"] = "hoge",
["strict"] = "true",
}
};
var result = ConvertCsToMermaid.Convert("1 + 1", options);
Assert.NotNull(result);
_OutputHelper.WriteLine("result = " + result);
}
}
14 changes: 14 additions & 0 deletions Cs2Mermaid/MyCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MyCommandHandler : ICommandHandler
Option<string[]> symbolOption = new Option<string[]>("--pp-symbol", "preprocessor symbol(can be multiple)");
Option<string> orientationOption = new Option<string>(new string[] { "--chart-orientation", "-co" }, "flowchart orientation(default: LR)");
Option<bool> asScriptOption = new Option<bool>("--as-script", "parse as C# script");
Option<string[]> featuresOption = new Option<string[]>("--feature", "add features(key-value must be separated with '=', if separator does not exist, value will be 'true')");
public Option[] GetOptions()
{
return new Option[]
Expand All @@ -30,6 +31,7 @@ public Option[] GetOptions()
symbolOption,
orientationOption,
asScriptOption,
featuresOption,
};
}
public MyCommandHandler()
Expand Down Expand Up @@ -77,14 +79,26 @@ ConvertOptions CreateConvertOptions(InvocationContext context)
var langver = context.ParseResult.GetValueForOption<string>(langVersion);
var orientation = context.ParseResult.GetValueForOption<string>(orientationOption);
var asscript = context.ParseResult.GetValueForOption<bool>(asScriptOption);
var features = GetFeatures(context.ParseResult.GetValueForOption<string[]>(featuresOption));
return new ConvertOptions()
{
LangVersion = langver,
PreprocessorSymbols = presymbols,
ChartOrientation = orientation,
AsScript = asscript,
Features = features,
};
}
Dictionary<string, string>? GetFeatures(string[]? features)
{
if(features == null)
{
return null;
}
return features.Select(x => x.Split('=', 2))
.Where(x => x.Length > 0)
.ToDictionary(x => x[0], x => x.Length >= 2 ? x[1] : "true");
}
void ProcessWithMd(string? output, string? outputEncoding, string? input, string? inputEncoding, ConvertOptions convertOptions)
{
var ie = GetEncoding(inputEncoding);
Expand Down

0 comments on commit 47ecdb6

Please sign in to comment.