示例#1
0
        public void Compile(CompilationTypes compilationType, bool forceByDemand = false)
        {
            if (!forceByDemand && ((CompilationTypeOverride && (MapperType & CompilationTypeMember) != MapperType) || (!CompilationTypeOverride && (MapperType & compilationType) != MapperType)))
            {
                return;
            }

            if (_compiling)
            {
                return;
            }

            try
            {
                _compiling = true;
                try
                {
                    CompileInternal();
                }
                catch (Exception ex)
                {
                    throw new ExpressmapperException(
                              string.Format(
                                  "Error error occured trying to compile mapping for: source {0}, destination {1}. See the inner exception for details.",
                                  typeof(T).FullName, typeof(TN).FullName), ex);
                }
            }
            finally
            {
                _compiling = false;
            }
        }
示例#2
0
        public void Compile(CompilationTypes compilationType, bool forceByDemand = false)
        {
            if (!forceByDemand && ((this.CompilationTypeOverride && (this.MapperType & this.CompilationTypeMember) != this.MapperType) ||
                                   (!this.CompilationTypeOverride && (this.MapperType & compilationType) != this.MapperType)))
            {
                return;
            }

            if (this._compiling)
            {
                return;
            }

            try
            {
                this._compiling = true;
                try
                {
                    this.CompileInternal();
                }
                catch (Exception ex)
                {
                    throw new ExpressmapperException(
                              $"Error error occured trying to compile mapping for: source {typeof(T).FullName}, destination {typeof(TN).FullName}. See the inner exception for details.",
                              ex);
                }
            }
            finally
            {
                this._compiling = false;
            }
        }
示例#3
0
        public void Execute(GeneratorExecutionContext context)
        {
#if DEBUG_GENERATOR
            if (!Debugger.IsAttached)
            {
                Debugger.Launch();
            }
#endif

            if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
            {
                return;
            }

            var types = new CompilationTypes(context);

            foreach (var classSymbol in receiver.Classes)
            {
                var sourceText = GetSourceForClass(classSymbol, types);
                if (!(sourceText is null))
                {
                    context.AddSource($"{classSymbol.Name}.g.cs", sourceText);
                }
            }
        }
示例#4
0
        public void PrecompileCollection <T, TN>(CompilationTypes compilationType)
        {
            lock (_lock)
            {
                foreach (var mappingService in _mappingServices)
                {
                    switch (compilationType)
                    {
                    case CompilationTypes.All:
                        mappingService.PrecompileCollection <T, TN>();
                        break;

                    case CompilationTypes.OnlySource:
                    {
                        if (!mappingService.DestinationSupport)
                        {
                            mappingService.PrecompileCollection <T, TN>();
                        }
                        break;
                    }

                    case CompilationTypes.OnlyDestination:
                    {
                        if (mappingService.DestinationSupport)
                        {
                            mappingService.PrecompileCollection <T, TN>();
                        }
                        break;
                    }
                    }
                }
            }
        }
 public IMemberConfiguration <T, TN> CompileTo(CompilationTypes compilationType)
 {
     foreach (var typeMapper in this._typeMappers)
     {
         typeMapper.CompileTo(compilationType);
     }
     return(this);
 }
        public void Compile(CompilationTypes compilationType)
        {
            var typeMappers = new Dictionary <long, ITypeMapper>(TypeMappers);

            foreach (var typeMapper in typeMappers)
            {
                typeMapper.Value.Compile(compilationType);
            }
        }
示例#7
0
 public void Compile(CompilationTypes compilationType)
 {
     lock (this._lock)
     {
         foreach (var mappingService in this._mappingServices)
         {
             mappingService.Compile(compilationType);
         }
     }
 }
示例#8
0
        private SourceText GetSourceForClass(INamedTypeSymbol classSymbol, CompilationTypes types)
        {
            var attribute = classSymbol.GetAttributes().SingleOrDefault((a) => a.AttributeClass.Equals(types.GeneratedCommandAttributeType, SymbolEqualityComparer.Default));

            if (attribute is null)
            {
                return(null);
            }

            var namedArguments = attribute.NamedArguments.ToDictionary((kvp) => kvp.Key, (kvp) => kvp.Value);

            if (namedArguments.TryGetValue("GenerateFactory", out var generateFactory) && (bool)generateFactory.Value == false)
            {
                return(null);
            }

            var command     = attribute.ConstructorArguments[0].Value as string;
            var description = attribute.ConstructorArguments[1].Value as string;

            var properties = classSymbol
                             .GetMembers()
                             .OfType <IFieldSymbol>()
                             .Where((p) =>
            {
                if (!p.IsStatic || !(p.Type is INamedTypeSymbol fieldType) || !fieldType.IsGenericType)
                {
                    return(false);
                }

                var unboundType = fieldType.ConstructedFrom;
                return(unboundType.Equals(types.OptionType, SymbolEqualityComparer.Default) || unboundType.Equals(types.ArgumentType, SymbolEqualityComparer.Default));
            });
            var collectionInitializer = string.Join(",", properties.Select(p => p.Name));

            return(SourceText.From($@"
using Microsoft.Extensions.DependencyInjection;
using System;
using System.CommandLine;

namespace {classSymbol.ContainingNamespace.ToDisplayString()}
{{
    public partial class {classSymbol.Name}
    {{
        public static Command GetCommand(IServiceProvider sp)
        {{
            var command = new Command(""{command}"", ""{description}"") {{ {collectionInitializer} }};
            command.Handler = ActivatorUtilities.CreateInstance<{classSymbol.Name}>(sp);
            return command;
        }}
    }}
}}
", Encoding.UTF8));
        }
示例#9
0
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterCompilationStartAction(compilationStartContext =>
            {
                var compilationTypes = new CompilationTypes(compilationStartContext.Compilation);

                if (!compilationTypes.IsCompilationCapableOfBeingProcessedByThisAnalyzer)
                {
                    return;
                }

                compilationStartContext.RegisterSymbolAction(
                    symbolContext => AnalyzeMethod(symbolContext, compilationTypes), SymbolKind.Method);
            });
        }
示例#10
0
        private static void AnalyzeMethod(SymbolAnalysisContext context, CompilationTypes compilationTypes)
        {
            var method = (IMethodSymbol)context.Symbol;

            var methodAllAttributes = method.GetAttributes();

            if (!methodAllAttributes.ContainsAttributeType(compilationTypes.Theory))
            {
                return;
            }

            var wellFormedInlineDataAttributes = methodAllAttributes
                                                 .Where(a => a.AttributeClass == compilationTypes.InlineData &&
                                                        HasAttributeDeclarationNoCompilationErrors(a, compilationTypes));

            AnalyzeInlineDataAttributesWithinTheory(context, wellFormedInlineDataAttributes);
        }
示例#11
0
 public void PrecompileCollection <T, TN>(CompilationTypes compilationType)
 {
     lock (this._lock)
     {
         foreach (var mappingService in this._mappingServices)
         {
             if ((CompilationTypes.Source & compilationType) == CompilationTypes.Source)
             {
                 if (!mappingService.DestinationSupport)
                 {
                     mappingService.PrecompileCollection <T, TN>();
                 }
             }
             if ((CompilationTypes.Destination & compilationType) == CompilationTypes.Destination)
             {
                 if (mappingService.DestinationSupport)
                 {
                     mappingService.PrecompileCollection <T, TN>();
                 }
             }
         }
     }
 }
示例#12
0
 public static void PrecompileCollection <T, TN>(CompilationTypes compilationType)
 {
     Instance.PrecompileCollection <T, TN>(compilationType);
 }
示例#13
0
 public static void Compile(CompilationTypes compilationType)
 {
     Instance.Compile(compilationType);
 }
示例#14
0
 public void CompileTo(CompilationTypes compileType)
 {
     this.CompilationTypeMember   = compileType;
     this.CompilationTypeOverride = true;
 }
示例#15
0
 private static bool HasAttributeDeclarationNoCompilationErrors(AttributeData attribute,
                                                                CompilationTypes compilationTypes)
 {
     return(attribute.ConstructorArguments.Length == 1 &&
            compilationTypes.ObjectArray.Equals(attribute.ConstructorArguments.FirstOrDefault().Type));
 }