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; } }
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; } }
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); } } }
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); } }
public void Compile(CompilationTypes compilationType) { lock (this._lock) { foreach (var mappingService in this._mappingServices) { mappingService.Compile(compilationType); } } }
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)); }
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); }); }
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); }
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>(); } } } } }
public static void PrecompileCollection <T, TN>(CompilationTypes compilationType) { Instance.PrecompileCollection <T, TN>(compilationType); }
public static void Compile(CompilationTypes compilationType) { Instance.Compile(compilationType); }
public void CompileTo(CompilationTypes compileType) { this.CompilationTypeMember = compileType; this.CompilationTypeOverride = true; }
private static bool HasAttributeDeclarationNoCompilationErrors(AttributeData attribute, CompilationTypes compilationTypes) { return(attribute.ConstructorArguments.Length == 1 && compilationTypes.ObjectArray.Equals(attribute.ConstructorArguments.FirstOrDefault().Type)); }