Exemplo n.º 1
0
        protected virtual CodeTypeDeclaration GenerateClass(CodeNamespace @namespace)
        {
            var generator = typeof(ResourceTypeBuilder);
            var version   = generator.Assembly.GetName( ).Version;
            var type      = Declare.Class(settings.ClassName)
                            .Modifiers(settings.AccessModifiers)
                            .IsPartial(CodeDomProvider.Supports(GeneratorSupport.PartialTypes))
                            .AddSummary(ClassSummary)
                            .AddTo(@namespace);

            if (settings.CustomToolType != null)
            {
                type.AddRemarks(ClassRemarksFormat, generator.FullName, settings.CustomToolType.Name);
            }
            else
            {
                type.AddRemarks(ClassRemarksToollessFormat, generator.FullName);
            }

            if (string.Equals(@namespace.Name.Split('.').Last( ), settings.ClassName, StringComparison.OrdinalIgnoreCase))
            {
                type.Attributed(Declare.Attribute <SuppressMessageAttribute> ("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces"));
            }

            return(type.Attributed(Declare.Attribute <GeneratedCodeAttribute> (generator.FullName, version.ToString( )),
                                   Declare.Attribute <DebuggerNonUserCodeAttribute> ( ),
                                   Declare.Attribute <ObfuscationAttribute> ( )
                                   .WithArgument(nameof(ObfuscationAttribute.Exclude), true)
                                   .WithArgument(nameof(ObfuscationAttribute.ApplyToMembers), true)));
        }
Exemplo n.º 2
0
 private static CodeTypeDeclaration WPF(string className, MemberAttributes memberAttributes, IList <ResourceMapping> map)
 {
     return(Build(className,
                  memberAttributes,
                  map,
                  "Linguist.WPF.TypedLocalizeExtension",
                  true,
                  "System.Windows.Data.BindingBase",
                  Declare.Attribute <TypeConverterAttribute> (Code.TypeOf(Code.NestedType("Linguist.WPF.BindingSyntax", "TypeConverter")))));
 }
Exemplo n.º 3
0
 private static CodeTypeDeclaration XamarinForms(string className, MemberAttributes memberAttributes, IList <ResourceMapping> map)
 {
     return(Build(className,
                  memberAttributes,
                  map,
                  "Linguist.Xamarin.Forms.TypedLocalizeExtension",
                  false,
                  "Xamarin.Forms.BindingBase",
                  Declare.Attribute(Code.Type("Xamarin.Forms.TypeConverterAttribute"),
                                    Code.TypeOf(Code.NestedType("Linguist.Xamarin.Forms.BindingSyntax", "TypeConverter")))));
 }
Exemplo n.º 4
0
 public static CodeAttributeDeclaration WithAttribute <TMember, TAttribute> (this TMember member, params CodeAttributeArgument[] arguments) where TMember : CodeTypeMember
 => member.AddAttribute(Declare.Attribute <TAttribute> (arguments));
Exemplo n.º 5
0
 public static CodeAttributeDeclaration WithAttribute <TMember, TAttribute> (this TMember member) where TMember : CodeTypeMember
 => member.AddAttribute(Declare.Attribute <TAttribute> ());
Exemplo n.º 6
0
 public static CodeAttributeDeclaration AddAttribute <T> (this T member, CodeTypeReference attributeType, params CodeAttributeArgument[] arguments) where T : CodeTypeMember
 => member.AddAttribute(Declare.Attribute(attributeType, arguments));
Exemplo n.º 7
0
 public static CodeAttributeDeclaration AddAttribute <T> (this T member, CodeTypeReference attributeType) where T : CodeTypeMember
 => member.AddAttribute(Declare.Attribute(attributeType));
Exemplo n.º 8
0
 public static CodeParameterDeclarationExpression Params(this CodeParameterDeclarationExpression parameter)
 => parameter.WithAttribute(Declare.Attribute(TypeReference.ParamArrayAttribute));
Exemplo n.º 9
0
        protected virtual CodeMemberMethod GenerateFormatMethod(ResourceMapping mapping)
        {
            var resource          = mapping.Resource;
            var numberOfArguments = mapping.NumberOfArguments;

            if (numberOfArguments <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(numberOfArguments), numberOfArguments, "Number of argument must be greater than zero");
            }

            var localizer = (CodeExpression)null;

            if (settings.LocalizerType != null)
            {
                localizer = Code.Static( ).Property(LocalizerPropertyName);
            }

            var format           = Code.Type <string> ( ).Static( ).Method(nameof(string.Format));
            var formatExpression = (CodeExpression)Code.Instance(settings.AccessModifiers).Property(mapping.Property);

            if (localizer != null)
            {
                format           = localizer.Method(nameof(ILocalizer.Format));
                formatExpression = Code.Constant(resource.Name);
            }

            var summary      = Format(FormatMethodSummary, GeneratePreview((string)resource.Value));
            var formatMethod = Declare.Method <string> (mapping.FormatMethod, settings.AccessModifiers)
                               .AddSummary(summary + FormatResourceComment(resource.Comment));

            var objectType = Code.Type <object> ( );
            var start      = localizer != null ? 3 : 2;
            var parameters = new CodeExpression [start + numberOfArguments];

            parameters [0] = Code.Instance(settings.AccessModifiers).Field(CultureInfoFieldName);
            parameters [1] = formatExpression;
            if (localizer != null)
            {
                parameters [2] = parameters [1];
                parameters [1] = parameters [0];
            }

            for (var index = 0; index < numberOfArguments; index++)
            {
                var parameterName = Format(CultureInfo.InvariantCulture, FormatMethodParameterName, index);

                formatMethod.Parameters.Add(objectType.Parameter(parameterName));

                parameters [start + index] = Code.Variable(parameterName);

                if (numberOfArguments > 1)
                {
                    formatMethod.AddParameterComment(parameterName, FormatMultiParameterComment, Ordinals [Math.Min(index, Ordinals.Length - 1)]);
                }
                else
                {
                    formatMethod.AddParameterComment(parameterName, FormatParameterComment, index);
                }
            }

            if (numberOfArguments > 3)
            {
                formatMethod.Attributed(Declare.Attribute <SuppressMessageAttribute> ("Microsoft.Design", "CA1025:ReplaceRepetitiveArgumentsWithParamsArray"));
            }

            return(formatMethod.Define(method => method.Return(format.Invoke(parameters)))
                   .AddReturnComment(FormatReturnComment));
        }
Exemplo n.º 10
0
        protected virtual void GenerateClassMembers(CodeTypeDeclaration @class)
        {
            var editorBrowsable      = Declare.Attribute <EditorBrowsableAttribute> (Code.Constant(EditorBrowsableState.Advanced));
            var notifyCultureChanged = (CodeExpression)null;

            var ctor = Declare.Constructor( )
                       .AddSummary(ConstructorSummaryFormat, settings.ClassName)
                       .Attributed(Declare.Attribute <SuppressMessageAttribute> ("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode"))
                       .AddTo(@class);

            if (settings.AccessModifiers.HasBitMask(MemberAttributes.Static))
            {
                ctor.Private( );
            }
            else
            {
                ctor.Modifiers(settings.AccessModifiers);
            }

            if ((settings.Options & ResourceTypeOptions.CultureChangedEvent) == ResourceTypeOptions.CultureChangedEvent)
            {
                GenerateCultureChangedEvent(@class, out notifyCultureChanged);
            }

            var resourceManager = GenerateSingleton(@class,
                                                    settings.ResourceManagerType,
                                                    ResourceManagerFieldName,
                                                    settings.ResourceManagerInitializer);

            if (settings.LocalizerType != null)
            {
                var localizer = GenerateSingleton(@class,
                                                  settings.LocalizerType,
                                                  LocalizerFieldName,
                                                  settings.LocalizerInitializer);

                Declare.Property(settings.LocalizerType, LocalizerPropertyName).Static( )
                .Modifiers(settings.AccessModifiers)
                .Get(get => get.Return(localizer))
                .AddSummary(LocalizerPropertySummary)
                .AddTo(@class);
            }

            Declare.Property(settings.ResourceManagerType, ResourceManagerPropertyName).Static( )
            .Modifiers(settings.AccessModifiers)
            .Get(get => get.Return(resourceManager))
            .AddSummary(ResourceManagerPropertySummary)
            .Attributed(editorBrowsable)
            .AddTo(@class);

            var cultureField = Declare.Field <CultureInfo> (CultureInfoFieldName)
                               .Modifiers(settings.AccessModifiers & MemberAttributes.Static)
                               .AddTo(@class);

            var field = @class.Instance( ).Field(CultureInfoFieldName);

            Declare.Property <CultureInfo> (CultureInfoPropertyName)
            .Modifiers(settings.AccessModifiers)
            .Get(get => get.Return(field))
            .Set((set, value) =>
            {
                set.Add(Code.If(field.ObjectEquals(value))
                        .Then(Code.Return( )));
                set.Add(field.Assign(value));
                if (notifyCultureChanged != null)
                {
                    set.Add(notifyCultureChanged);
                }
            })
            .AddSummary(CultureInfoPropertySummary)
            .Attributed(editorBrowsable)
            .AddTo(@class);
        }