private void MyStruct_AddWithMethod(CsEnumItem enumItem, bool isFirst) { if (isFirst) { var cw = new CsCodeWriter(); const string maskVariable = "value"; const string modifiedValue = "current"; cw.SingleLineIf("add", $"return {modifiedValue} | {maskVariable};", $"return {modifiedValue} & ~{maskVariable};"); var m = ExtensionsClass .AddMethod(SetOrClearMethod, MyEnumTypeName) .WithBody(cw) .WithStatic(); m.AddAggressiveInlining(ExtensionsClass); m.AddParam(modifiedValue, MyEnumTypeName).UseThis = true; m.AddParam(maskVariable, MyEnumTypeName); m.AddParam("add", "bool"); } { var mask = GetMask(MyEnum, enumItem.EnumName); var cw = CsCodeWriter.Create <SingleTaskEnumsGenerator>(); cw.WriteLine($"{flagsPropertyName} = {flagsPropertyName}.{SetOrClearMethod}({mask}, value);"); cw.WriteLine("return this;"); var m = MyStruct.AddMethod("With" + enumItem.EnumName, OptionsClassName) .WithBody(cw); m.AddParam("value", "bool").ConstValue = "true"; m.Description = enumItem.Description; } }
public void MakeExtensionMethod(CsClass target) { if (EnumOptions is null) { return; } var paramValue = target.GetTypeName(TypeName); var writer = CsCodeWriter.Create <ShellEnumOptionsGenerator>(); writer.Open("switch (value)"); foreach (var i in EnumOptions.Items) { var line = $"case {paramValue}.{i.CsValue}: return {i.LinuxValue.CsEncode()};"; writer.WriteLine(line); } var notSupportedException = target.GetTypeName <NotSupportedException>(); writer.WriteLine($"default: throw new {notSupportedException}();"); writer.Close(); var parameter = new CsMethodParameter("value", paramValue) { UseThis = target.IsStatic }; target.AddMethod(extensionMethodName, "string") .WithBody(writer) .WithStatic() .WithParameter(parameter); }
private void CreateNamedParameters() { foreach (var option in _item.Options.Values) { var p = option.Parameter; if (string.IsNullOrEmpty(p?.Value) || p.Encoder is null) { continue; } var str = MyStruct; var kind = string.IsNullOrEmpty(p.Name) ? ParametrizedOption.OptionValueProcessorKind.SingleValue : ParametrizedOption.OptionValueProcessorKind.Dictionary; if (p.IsCollection) { kind = ParametrizedOption.OptionValueProcessorKind.List; } var cre = new ShellEnumOptionsGenerator(_item.EnumName + option.GetCsName() + "Values", MyNamespace, p.Encoder?.EnumValues); cre.MakeEnumIfNecessary(); cre.MakeExtensionMethod(ExtensionsClass); var propInfo = p.Encoder.GetPropertyTypeName(kind, str, cre.TypeName); var prop = str.AddProperty(option.GetCsName(), propInfo.PropertyType) .WithNoEmitField() .WithMakeAutoImplementIfPossible(); prop.ConstValue = propInfo.PropertyInit; prop.Description = option.FullDescription; { var key = p.Name?.ToLower(); var value = p.Value.Camelise(true).FirstLower(); var setExpression = prop.Name; var methodName = "With" + prop.Name; var cs = CsCodeWriter.Create <SingleTaskEnumsGenerator>(); { switch (kind) { case ParametrizedOption.OptionValueProcessorKind.SingleValue: cs.WriteLine($"{setExpression} = {value};"); break; case ParametrizedOption.OptionValueProcessorKind.Dictionary: cs.WriteLine($"{setExpression}[{key}] = {value};"); break; case ParametrizedOption.OptionValueProcessorKind.List: cs.WriteLine($"if ({value.CodeHasElements("Length")})"); cs.IncIndent(); cs.SingleLineForeach("tmp", value, $"{setExpression}.Add(tmp);"); cs.DecIndent(); methodName = "WithAppend" + prop.Name; break; default: throw new ArgumentOutOfRangeException(); } } cs.WriteLine("return this;"); var fluentMethod = str.AddMethod(methodName, str.Name) .WithBody(cs); var fmParams = FluentMethodParameters.Get(kind, propInfo); if (fmParams.AddKey) { fluentMethod.AddParam <string>(key, str); } var par = fluentMethod.AddParam(value, fmParams.ValueParameterType2); par.Description = p.ValueDescription; fluentMethod.Description = prop.Description; } switch (kind) { case ParametrizedOption.OptionValueProcessorKind.List: { if (cre.HasEnum) { throw new NotSupportedException(); } apps.Add((cc, re) => { cc.WriteDescriptionComment(option); var condition = prop.Name.CodeHasElements(); cc.Open($"if ({condition})"); { var tmp = prop.Name.FirstLower() + "Item"; cc.WriteLine($"yield return {option.AnyWithMinus.CsEncode()};"); var input = new ParametrizedOption.OptionValueProcessorInput(tmp, kind, re); var expression = p.Encoder.Convert(input); cc.SingleLineForeach(tmp, prop.Name, $"yield return {expression};"); } cc.Close(); }); } break; case ParametrizedOption.OptionValueProcessorKind.Dictionary: { if (cre.HasEnum) { throw new NotSupportedException(); } apps.Add((cc, re) => { cc.WriteDescriptionComment(option); cc.Open("foreach(var pair in " + prop.Name + ")"); cc.WriteLine("yield return " + option.AnyWithMinus.CsEncode() + ';'); var input = new ParametrizedOption.OptionValueProcessorInput("pair.Value", kind, re); var expression = p.Encoder.Convert(input); if (p.IsCollection) { throw new NotSupportedException(); } cc.WriteLine($"var value = {expression};"); cc.WriteLine("yield return $\"{pair.Key}={value}\";"); cc.Close(); }); } break; case ParametrizedOption.OptionValueProcessorKind.SingleValue: { apps.Add((cc, resolver) => { cc.WriteDescriptionComment(option); var condition = p.Encoder.GetCondition(prop.Name, kind); cc.Open($"if ({condition})"); { cc.WriteLine("yield return " + option.AnyWithMinus.CsEncode() + ';'); var ex = prop.Name; if (prop.Type.EndsWith("?")) { ex += ".Value"; } var input = new ParametrizedOption.OptionValueProcessorInput(ex, kind, resolver); var expression = p.Encoder.Convert(input); if (p.IsCollection) { cc.SingleLineForeach("tmp", expression, "yield return tmp;"); } else { cc.WriteLine($"yield return {expression};"); } } cc.Close(); }); } break; } } }