private void InitializeClosureDependencies() { foreach (TypedArgument argument in Arguments) { BlockExpression closure = argument.Expression as BlockExpression; if (closure == null) continue; // ICallableType callableType = closure.ExpressionType as ICallableType; ICallableType callableType = argument.FormalType as ICallableType; if (callableType == null) continue; TypeCollector collector = new TypeCollector(delegate(IType t) { IGenericParameter gp = t as IGenericParameter; return gp != null && InferredTypes.ContainsKey(gp); }); foreach (IType inputType in GetParameterTypes(callableType.GetSignature())) { collector.Visit(inputType); } foreach (IGenericParameter gp in collector.Matches) { RecordClosureDependency(closure, gp); } } }
protected override void RunTest(Program program, string outputFileName) { ExpressionNormalizer aen = new ExpressionNormalizer(program.Architecture.PointerType); aen.Transform(program); EquivalenceClassBuilder eq = new EquivalenceClassBuilder(factory, store); eq.Build(program); #if OLD DataTypeBuilder dtb = new DataTypeBuilder(factory, store, program.Architecture); TraitCollector coll = new TraitCollector(factory, store, dtb, program); coll.CollectProgramTraits(program); dtb.BuildEquivalenceClassDataTypes(); #else TypeCollector coll = new TypeCollector(factory, store, program); coll.CollectTypes(); store.BuildEquivalenceClassDataTypes(factory); #endif TypeVariableReplacer tvr = new TypeVariableReplacer(store); tvr.ReplaceTypeVariables(); TypeTransformer trans = new TypeTransformer(factory, store, program); trans.Transform(); using (FileUnitTester fut = new FileUnitTester(outputFileName)) { foreach (Procedure proc in program.Procedures.Values) { proc.Write(false, fut.TextWriter); fut.TextWriter.WriteLine(); } store.Write(fut.TextWriter); fut.AssertFilesEqual(); } }
protected override void RunTest(Program prog, string outputFile) { aen.Transform(prog); eqb.Build(prog); TypeCollector trco = new TypeCollector(factory, store, prog); trco.CollectTypes(); dtb.BuildEquivalenceClassDataTypes(); Verify(prog, outputFile); }
public void UninstallServices(IEnumerable<String> assemblies, IEnumerable<String> typeNames) { var typeCollector = new TypeCollector(assemblies, typeNames); foreach (var type in typeCollector.CollectConcreteTypes<IWindowsService>()) { Console.WriteLine("Uninstalling {0} from assembly {1}...", type.Name, type.Assembly.FullName); WindowsServiceInstaller.RuntimeUninstall(type.Assembly, type); } }
protected override void RunTest(Program program, string outputFileName) { var listener = new FakeDecompilerEventListener(); ExpressionNormalizer aen = new ExpressionNormalizer(program.Architecture.PointerType); aen.Transform(program); EquivalenceClassBuilder eq = new EquivalenceClassBuilder(factory, store, listener); eq.Build(program); #if OLD DataTypeBuilder dtb = new DataTypeBuilder(factory, store, program.Architecture); TraitCollector coll = new TraitCollector(factory, store, dtb, program); coll.CollectProgramTraits(program); sktore.BuildEquivalenceClassDataTypes(factory); #else TypeCollector coll = new TypeCollector(factory, store, program, listener); coll.CollectTypes(); store.BuildEquivalenceClassDataTypes(factory); #endif TypeVariableReplacer tvr = new TypeVariableReplacer(store); tvr.ReplaceTypeVariables(); Exception theEx = null; try { TypeTransformer trans = new TypeTransformer(factory, store, program); trans.Transform(); } catch (Exception ex) { theEx = ex; } using (FileUnitTester fut = new FileUnitTester(outputFileName)) { if (theEx != null) { fut.TextWriter.WriteLine(theEx.Message); fut.TextWriter.WriteLine(theEx.StackTrace); } foreach (Procedure proc in program.Procedures.Values) { proc.Write(false, fut.TextWriter); fut.TextWriter.WriteLine(); } store.Write(fut.TextWriter); fut.AssertFilesEqual(); } }
public void ShouldReadAssembly() { ITypeCollector coll = new TypeCollector(); coll.Collect(typeof(TestFixture).Assembly); Assert.AreEqual(1, coll.Asses.Count); Assert.IsTrue(coll.Events.Count >= 678); Assert.IsTrue(coll.Fields.Count >= 14761); Assert.IsTrue(coll.Methods.Count >= 29653); Assert.AreEqual(1, coll.Modules.Count); Assert.IsTrue(coll.Properties.Count >= 5255); Assert.IsTrue(coll.Types.Count >= 1732); Assert.IsTrue(coll.GenericMethods.Count >= 172); Assert.IsTrue(coll.GenericTypes.Count >= 109); }
protected void RunStringTest(Program program, string expectedOutput) { var sw = new StringWriter(); sw.WriteLine("// Before ///////"); DumpProgram(program, sw); var eventListener = new FakeDecompilerEventListener(); SetupPreStages(program); aen.Transform(program); eqb.Build(program); var coll = new TypeCollector(program.TypeFactory, program.TypeStore, program, eventListener); coll.CollectTypes(); program.TypeStore.Dump(); program.TypeStore.BuildEquivalenceClassDataTypes(program.TypeFactory); program.TypeStore.Dump(); tvr.ReplaceTypeVariables(); trans.Transform(); ctn.RenameAllTypes(program.TypeStore); var ter = new TypedExpressionRewriter(program, eventListener); try { ter.RewriteProgram(program); } catch (Exception ex) { sw.WriteLine("** Exception **"); sw.WriteLine(ex); throw; } finally { sw.WriteLine("// After ///////"); DumpProgram(program, sw); } var sActual = sw.ToString(); if (expectedOutput != sActual) { Console.WriteLine(sActual); Assert.AreEqual(expectedOutput, sActual); } }
public void ShouldReadEnum() { ITypeCollector coll = new TypeCollector(); coll.Collect <ActionTargets>(); Assert.AreEqual(0, coll.Asses.Count); Assert.AreEqual(0, coll.Events.Count); Assert.AreEqual(4, coll.Fields.Count); Assert.AreEqual(0, coll.Methods.Count); Assert.AreEqual(0, coll.Modules.Count); Assert.AreEqual(0, coll.Properties.Count); Assert.AreEqual(1, coll.Types.Count); Assert.AreEqual(0, coll.GenericMethods.Count); Assert.AreEqual(0, coll.GenericTypes.Count); Assert.IsTrue(typeof(ActionTargets).IsEnum); }
public void ShouldReadClass() { ITypeCollector coll = new TypeCollector(); coll.Collect(typeof(FrameworkPackageSettings)); Assert.AreEqual(0, coll.Asses.Count); Assert.AreEqual(0, coll.Events.Count); Assert.AreEqual(14, coll.Fields.Count); Assert.AreEqual(0, coll.Methods.Count); Assert.AreEqual(0, coll.Modules.Count); Assert.AreEqual(0, coll.Properties.Count); Assert.AreEqual(1, coll.Types.Count); Assert.AreEqual(0, coll.GenericMethods.Count); Assert.AreEqual(0, coll.GenericTypes.Count); Assert.IsTrue(typeof(FrameworkPackageSettings).IsClass); }
public void ShouldReadInterface() { ITypeCollector coll = new TypeCollector(); coll.Collect <ITestListener>(); Assert.AreEqual(0, coll.Asses.Count); Assert.AreEqual(0, coll.Events.Count); Assert.IsTrue(coll.Fields.Count >= 1240); Assert.IsTrue(coll.Methods.Count >= 1798); Assert.AreEqual(0, coll.Modules.Count); Assert.AreEqual(78, coll.Properties.Count); Assert.AreEqual(20, coll.Types.Count); Assert.IsTrue(coll.GenericMethods.Count >= 10); Assert.AreEqual(6, coll.GenericTypes.Count); Assert.IsTrue(typeof(ITestListener).IsInterface); }
public void ShouldReadGenerics() { ITypeCollector coll = new TypeCollector(); coll.Collect <SuperClass>(); Assert.AreEqual(0, coll.Asses.Count); Assert.AreEqual(1, coll.Events.Count); Assert.IsTrue(coll.Fields.Count >= 20); Assert.IsTrue(coll.Methods.Count >= 28); Assert.AreEqual(0, coll.Modules.Count); Assert.IsTrue(coll.Properties.Count >= 9); Assert.AreEqual(4, coll.Types.Count); Assert.IsTrue(coll.GenericMethods.Count >= 1); Assert.IsTrue(coll.GenericTypes.Count >= 11); Assert.IsTrue(typeof(SuperClass).IsClass); }
/// <summary> /// Bootstraps the application by discovering types. It will discover types using the given configuration /// and feed the types to all discovered implementations of <see cref="ICollectTypes"/>. /// </summary> /// <param name="bootstrapConfiguration">The bootstrap configuration to use.</param> /// <returns>An <see cref="IBootstrapContainer"/> which can be used to create simple types /// and finally initialize the real container.</returns> public IBootstrapContainer BootstrapTypes(IBootstrapConfiguration bootstrapConfiguration) { var assembliesConfiguration = bootstrapConfiguration.AssembliesConfiguration; var assemblySpecifiers = new AssemblySpecifiers(assembliesConfiguration); var typeCollector = new TypeCollector(); var assemblyProvider = new AssemblyProvider( bootstrapConfiguration.AssemblyProviders, assemblySpecifiers, new AssemblyFilters(assembliesConfiguration), new TypeFilters(bootstrapConfiguration.TypesConfiguration), typeCollector); typeCollector.RegisterForAutoBind(assemblyProvider); return typeCollector; }
private void IncludeAdditionalAssemblies(List <Assembly> typeRelatedAssemblies) { foreach (string assemblyName in _attribute.IncludeAdditionalAssemblies) { var additionalAssembly = TypeCollector.TryLoadAssembly(assemblyName); if (additionalAssembly == null) { continue; } if (!typeRelatedAssemblies.Contains(additionalAssembly)) { typeRelatedAssemblies.Add(additionalAssembly); } } }
public void ShouldReadDelegate() { ITypeCollector coll = new TypeCollector(); coll.Collect <TestDelegate>(); Assert.AreEqual(0, coll.Asses.Count); Assert.AreEqual(0, coll.Events.Count); Assert.AreEqual(0, coll.Fields.Count); Assert.AreEqual(4, coll.Methods.Count); Assert.AreEqual(0, coll.Modules.Count); Assert.AreEqual(0, coll.Properties.Count); Assert.AreEqual(1, coll.Types.Count); Assert.AreEqual(0, coll.GenericMethods.Count); Assert.AreEqual(0, coll.GenericTypes.Count); Assert.IsTrue(typeof(TestDelegate).IsDelegate()); }
private static IEnumerable <TypeDeclaration> GetTypeDeclarations(NamespaceDeclaration namespaceDeclaration) { var collector = new TypeCollector(); return(namespaceDeclaration.SyntaxNodes .Select(info => { Func <TypeDeclarationSyntax, TypeDeclarationSyntaxInfo> selector = x => new TypeDeclarationSyntaxInfo(info.CodeFile, x.SyntaxTree == null ? x.Identifier.ValueText : x.GetName(x.SyntaxTree.GetRoot()), x); return new { info, selector }; }) .SelectMany(x => collector.GetTypes(x.info.Syntax).Select(x.selector)) .GroupBy(x => x.Name) .Select(x => new TypeDeclaration { Name = x.Key, SyntaxNodes = x })); }
public void select_implement_of_type2() { // Arrange var types = new[] { typeof(NormalClass), typeof(AbstractClass), typeof(DateTime), typeof(ImplementInterface), 2.GetType() }; var selector = TypeCollector.Create(types); // Actions var result = selector.ImplementationOf <IAmInterface>().ToList(); // Assertions. result.Should().NotBeNullOrEmpty(); result.Should().Contain(typeof(ImplementInterface)); result.Should().NotContain(typeof(NormalClass)); result.Should().NotContain(typeof(AbstractClass)); result.Should().NotContain(typeof(DateTime)); }
public void ignore_static_classes() { // Arrange var staticClass = typeof(StaticClass); var abstractClass = typeof(AbstractClass); var normalClass = typeof(AnnotationsValidator); var structType = typeof(DateTime); var collection = new[] { staticClass, abstractClass, normalClass, structType }; // Actions var selector = TypeCollector.Create(collection); var result = selector.IgnoreStaticClasses().ToList(); // Assertions result.Should().NotBeNull(); result.Should().NotContain(staticClass); result.Should().Contain(abstractClass); result.Should().Contain(normalClass); result.Should().Contain(structType); }
protected override void RunTest(Program program, string outputFile) { using (FileUnitTester fut = new FileUnitTester(outputFile)) { fut.TextWriter.WriteLine("// Before ///////"); DumpProgram(program, fut); SetupPreStages(program); aen.Transform(program); eqb.Build(program); #if OLD coll = new TraitCollector(factory, store, dtb, program); coll.CollectProgramTraits(program); #else var coll = new TypeCollector(program.TypeFactory, program.TypeStore, program); coll.CollectTypes(); #endif program.TypeStore.BuildEquivalenceClassDataTypes(program.TypeFactory); program.TypeStore.Dump(); tvr.ReplaceTypeVariables(); trans.Transform(); ctn.RenameAllTypes(program.TypeStore); ter = new TypedExpressionRewriter(program); try { ter.RewriteProgram(program); } catch (Exception ex) { fut.TextWriter.WriteLine("** Exception **"); fut.TextWriter.WriteLine(ex); } finally { fut.TextWriter.WriteLine("// After ///////"); DumpProgAndStore(program, fut); } } }
protected override void RunTest(Program program, string outputFile) { using (FileUnitTester fut = new FileUnitTester(outputFile)) { fut.TextWriter.WriteLine("// Before ///////"); DumpProgram(program, fut); SetupPreStages(program); aen.Transform(program); eqb.Build(program); #if OLD coll = new TraitCollector(factory, store, dtb, program); coll.CollectProgramTraits(program); #else var coll = new TypeCollector(program.TypeFactory, program.TypeStore, program); coll.CollectTypes(); #endif program.TypeStore.BuildEquivalenceClassDataTypes(program.TypeFactory); program.TypeStore.Dump(); tvr.ReplaceTypeVariables(); trans.Transform(); ctn.RenameAllTypes(program.TypeStore); ter = new TypedExpressionRewriter(program); try { ter.RewriteProgram(program); } catch (Exception ex) { fut.TextWriter.WriteLine("** Exception **"); fut.TextWriter.WriteLine(ex); } finally { fut.TextWriter.WriteLine("// After ///////"); DumpProgAndStore(program, fut); } } }
private TypeItem[] GetFilteredTypes() { bool containsMSCorLib = false; var typeRelatedAssemblies = ProjectSettings.UseBuiltInNames ? TypeCollector.GetAssembliesTypeHasAccessTo(_declaringType, out containsMSCorLib) : TypeCollector.GetAssembliesTypeHasAccessTo(_declaringType); if (_attribute.IncludeAdditionalAssemblies != null) { IncludeAdditionalAssemblies(typeRelatedAssemblies); } var filteredTypes = TypeCollector.GetFilteredTypesFromAssemblies(typeRelatedAssemblies, _attribute); bool replaceBuiltInNames = ProjectSettings.UseBuiltInNames && containsMSCorLib; int filteredTypesLength = filteredTypes.Count; var typeItems = new TypeItem[filteredTypesLength]; for (int i = 0; i < filteredTypesLength; i++) { var type = filteredTypes[i]; string fullTypeName = type.FullName; Assert.IsNotNull(fullTypeName); if (replaceBuiltInNames) { fullTypeName = fullTypeName.ReplaceWithBuiltInName(true); } typeItems[i] = new TypeItem(type, fullTypeName, _attribute.Grouping); } Sedgewick.SortInPlace(typeItems); return(typeItems); }
private static IMethod RemapMethod(Node node, GenericMappedMethod gmm, IType[] genParams) { var sourceMethod = gmm.SourceMember; if (sourceMethod.GenericInfo != null) { throw new CompilerError(node, "Mapping generic methods in generators is not implemented yet"); } var baseType = sourceMethod.DeclaringType; var genericInfo = baseType.GenericInfo; if (genericInfo == null) { throw new CompilerError(node, "Mapping generic nested types in generators is not implemented yet"); } var genericArgs = ((IGenericArgumentsProvider)gmm.DeclaringType).GenericArguments; var collector = new TypeCollector(type => type is IGenericParameter); foreach (var arg in genericArgs) { collector.Visit(arg); } var mapper = new GeneratorTypeReplacer(); foreach (var genParam in collector.Matches) { var mappedArg = genParams.SingleOrDefault(gp => gp.Name == genParam.Name); if (mappedArg != null) { mapper.Replace(genParam, mappedArg); } } var newType = (IConstructedTypeInfo) new GenericConstructedType( baseType, genericArgs.Select(mapper.MapType).ToArray()); return((IMethod)newType.Map(sourceMethod)); }
/// <summary> /// Bind a name to this scope, including destructuring assignment. /// </summary> public void Bind(Analyzer analyzer, Exp target, DataType rvalue, BindingKind kind) { switch (target) { case Identifier id: this.Bind(analyzer, id, rvalue, kind); break; case PyTuple tup: this.Bind(analyzer, tup.values, rvalue, kind); break; case PyList list: this.Bind(analyzer, list.elts, rvalue, kind); break; case AttributeAccess attr: DataType targetType = TransformExp(analyzer, attr.Expression, this); setAttr(analyzer, attr, rvalue, targetType); break; case ArrayRef sub: DataType valueType = TransformExp(analyzer, sub.array, this); var xform = new TypeCollector(this, analyzer); TransformExprs(analyzer, sub.subs, this); if (valueType is ListType t) { t.setElementType(UnionType.Union(t.eltType, rvalue)); } break; default: if (target != null) { analyzer.AddProblem(target, "invalid location for assignment"); } break; } }
private void RunTestCore(Program program) { var listener = new FakeDecompilerEventListener(); var aen = new ExpressionNormalizer(program.Architecture.PointerType); aen.Transform(program); var eq = new EquivalenceClassBuilder(factory, store, listener); eq.Build(program); var coll = new TypeCollector(factory, store, program, listener); coll.CollectTypes(); store.BuildEquivalenceClassDataTypes(factory); var tvr = new TypeVariableReplacer(store); tvr.ReplaceTypeVariables(); var trans = new TypeTransformer(factory, store, program); trans.Transform(); }
public void select_all_classes() { // Arrange var normalClass = typeof(NormalClass); var abstractClass = typeof(AbstractClass); var structType = typeof(DateTime); var interaceType = typeof(IAmInterface); var types = new[] { normalClass, abstractClass, structType, interaceType }; var selector = TypeCollector.Create(types); // Actions var result = selector.AllClasses().ToList(); // Assertions. result.Should().NotBeNullOrEmpty(); result.Should().Contain(normalClass); result.Should().Contain(abstractClass); result.Should().NotContain(interaceType); result.Should().NotContain(structType); }
private SortedSet <TypeItem> GetFilteredTypes() { var typeRelatedAssemblies = TypeCollector.GetAssembliesTypeHasAccessTo(_declaringType); if (_attribute.IncludeAdditionalAssemblies != null) { IncludeAdditionalAssemblies(typeRelatedAssemblies); } var filteredTypes = TypeCollector.GetFilteredTypesFromAssemblies( typeRelatedAssemblies, _attribute); bool replaceBuiltInNames = _attribute.UseBuiltInNames && typeRelatedAssemblies .Any(assembly => assembly.GetName().Name == "mscorlib"); var sortedTypes = new SortedSet <TypeItem>(new TypeItemComparer()); for (int i = 0; i < filteredTypes.Count; i++) { var type = filteredTypes[i]; string fullTypeName = type.FullName; if (fullTypeName == null) { continue; } if (replaceBuiltInNames) { TypeNameFormatter.TryReplaceWithBuiltInName(ref fullTypeName); } sortedTypes.Add(new TypeItem(type, fullTypeName, _attribute.Grouping)); } return(sortedTypes); }
protected override void RunTest(Program program, string outputFile) { FileUnitTester fut = null; try { fut = new FileUnitTester(outputFile); var factory = program.TypeFactory; var store = program.TypeStore; var listener = new FakeDecompilerEventListener(); var aen = new ExpressionNormalizer(program.Platform.PointerType); var eqb = new EquivalenceClassBuilder(factory, store, listener); var tyco = new TypeCollector(factory, store, program, listener); aen.Transform(program); eqb.Build(program); tyco.CollectTypes(); if (buildEquivalenceClasses) { store.BuildEquivalenceClassDataTypes(factory); new TypeVariableReplacer(store).ReplaceTypeVariables(); } } catch (Exception ex) { fut.TextWriter.WriteLine(ex.Message); fut.TextWriter.WriteLine(ex.StackTrace); throw; } finally { DumpProgAndStore(program, fut); fut.Dispose(); } }
public void RunServices(IEnumerable<String> assemblies, IEnumerable<String> typeNames) { var typeCollector = new TypeCollector(assemblies, typeNames); var serviceGroup = new WindowsServiceGroup(); foreach (var type in typeCollector.CollectConcreteTypes<IWindowsService>()) { var service = Activator.CreateInstance(type) as IWindowsService; if (service == null) throw new InvalidOperationException(String.Format("Unable to instantiate {0} from assembly {1}", type.Name, type.Assembly.FullName)); serviceGroup.Add(service); Console.WriteLine("[muster] Found {0} in {1}", type.Name, type.Assembly.GetName().Name); } // TODO: Service params? if (Environment.UserInteractive) ConsoleHarness.Run(new String[] { }, serviceGroup); else ServiceBase.Run(new WindowsServiceHarness(serviceGroup)); }
private void Process(string name, IEnumerable <TypeDefinition> types, IMetadataTokenProvider invRef, IDependencyReport report) { var units = report.Units; var nativeTypeName = Capitalize(Path.GetFileNameWithoutExtension(name)); var collot = new TypeCollector(); INamingStrategy nameArgStrategy = null; foreach (var meth in types.SelectMany(t => t.Methods)) { PInvokeInfo pinv; if (!meth.HasPInvokeInfo || invRef != (pinv = meth.PInvokeInfo).Module) { continue; } var nativeMethName = pinv.EntryPoint; var retType = Deobfuscate(meth.ReturnType.ToString()); var parms = Deobfuscate(GetParamStr(meth)); var key = $"{nativeMethName} {retType} {parms}"; IUnit unit; if (!units.TryGetValue(name, out unit)) { units[name] = unit = new AssemblyUnit(nativeTypeName, new Version("0.0.0.0")); } IType ptype; if (!unit.Types.TryGetValue(nativeTypeName, out ptype)) { unit.Types[nativeTypeName] = ptype = new AssemblyType(nativeTypeName, TypeKind.Class); } nameArgStrategy = new NativeArgNameStrategy(ptype); collot.Collect(meth); var newRetType = nameArgStrategy.GetName(meth.ReturnType); var methRetType = Deobfuscate(meth.ReturnType.FullName); if (newRetType != null) { methRetType = newRetType; } IMethod pmethod; if (!ptype.Methods.TryGetValue(key, out pmethod)) { ptype.Methods[key] = pmethod = new AssemblyMethod(nativeMethName, methRetType); } pmethod.Parameters.Clear(); foreach (var parm in meth.Parameters) { var newParmType = nameArgStrategy.GetName(parm.ParameterType); var mparmType = Deobfuscate(parm.ParameterType.FullName); if (newParmType != null) { mparmType = newParmType; } var mparm = new MethodParameter(parm.Name, mparmType); pmethod.Parameters.Add(mparm); } const StringSplitOptions sso = StringSplitOptions.None; var text = $"{meth}".Split(new[] { $"{meth.ReturnType}" }, 2, sso).Last().Trim(); pmethod.Aliases.Add(Deobfuscate(text)); } if (nameArgStrategy == null) { return; } foreach (var type in collot.Types) { CheckAndInclude(report, type, nameArgStrategy); } RewriteTypeNames(collot, nameArgStrategy, report); }
/// <summary> /// Generates the specialized resolver and formatters for the types that require serialization in a given compilation. /// </summary> /// <param name="compilation">The compilation to read types from as an input to code generation.</param> /// <param name="output">The name of the generated source file.</param> /// <param name="resolverName">The resolver name.</param> /// <param name="namespace">The namespace for the generated type to be created in. May be null.</param> /// <param name="useMapMode">A boolean value that indicates whether all formatters should use property maps instead of more compact arrays.</param> /// <param name="multipleIfDirectiveOutputSymbols">A comma-delimited list of symbols that should surround redundant generated files. May be null.</param> /// <param name="externalIgnoreTypeNames"> May be null.</param> /// <returns>A task that indicates when generation has completed.</returns> public async Task GenerateFileAsync( Compilation compilation, string output, string resolverName, string @namespace, bool useMapMode, string multipleIfDirectiveOutputSymbols, string[] externalIgnoreTypeNames) { var namespaceDot = string.IsNullOrWhiteSpace(@namespace) ? string.Empty : @namespace + "."; var multipleOutputSymbols = multipleIfDirectiveOutputSymbols?.Split(',') ?? Array.Empty <string>(); var sw = Stopwatch.StartNew(); foreach (var multioutSymbol in multipleOutputSymbols.Length == 0 ? new[] { string.Empty } : multipleOutputSymbols) { logger("Project Compilation Start:" + compilation.AssemblyName); var collector = new TypeCollector(compilation, true, useMapMode, externalIgnoreTypeNames, x => Console.WriteLine(x)); logger("Project Compilation Complete:" + sw.Elapsed.ToString()); sw.Restart(); logger("Method Collect Start"); var(objectInfo, enumInfo, genericInfo, unionInfo) = collector.Collect(); logger("Method Collect Complete:" + sw.Elapsed.ToString()); logger("Output Generation Start"); sw.Restart(); if (Path.GetExtension(output) == ".cs") { // SingleFile Output var objectFormatterTemplates = objectInfo .GroupBy(x => (x.Namespace, x.IsStringKey)) .Select(x => { var(nameSpace, isStringKey) = x.Key; var objectSerializationInfos = x.ToArray(); var template = isStringKey ? new StringKeyFormatterTemplate() : (IFormatterTemplate) new FormatterTemplate(); template.Namespace = namespaceDot + "Formatters" + (nameSpace is null ? string.Empty : "." + nameSpace); template.ObjectSerializationInfos = objectSerializationInfos; return(template); }) .ToArray(); var enumFormatterTemplates = enumInfo .GroupBy(x => x.Namespace) .Select(x => new EnumTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key), EnumSerializationInfos = x.ToArray(), }) .ToArray(); var unionFormatterTemplates = unionInfo .GroupBy(x => x.Namespace) .Select(x => new UnionTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key), UnionSerializationInfos = x.ToArray(), }) .ToArray(); var resolverTemplate = new ResolverTemplate() { Namespace = namespaceDot + "Resolvers", FormatterNamespace = namespaceDot + "Formatters", ResolverName = resolverName, RegisterInfos = genericInfo.Where(x => !x.IsOpenGenericType).Cast <IResolverRegisterInfo>().Concat(enumInfo).Concat(unionInfo).Concat(objectInfo.Where(x => !x.IsOpenGenericType)).ToArray(), }; var sb = new StringBuilder(); sb.AppendLine(resolverTemplate.TransformText()); sb.AppendLine(); foreach (var item in enumFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } sb.AppendLine(); foreach (var item in unionFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } sb.AppendLine(); foreach (var item in objectFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } if (multioutSymbol == string.Empty) { await OutputAsync(output, sb.ToString(), cancellationToken); } else { var fname = Path.GetFileNameWithoutExtension(output) + "." + MultiSymbolToSafeFilePath(multioutSymbol) + ".cs"; var text = $"#if {multioutSymbol}" + Environment.NewLine + sb.ToString() + Environment.NewLine + "#endif"; await OutputAsync(Path.Combine(Path.GetDirectoryName(output), fname), text, cancellationToken); } } else { // Multiple File output foreach (var x in objectInfo) { var template = x.IsStringKey ? new StringKeyFormatterTemplate() : (IFormatterTemplate) new FormatterTemplate(); template.Namespace = namespaceDot + "Formatters" + (x.Namespace is null ? string.Empty : "." + x.Namespace); template.ObjectSerializationInfos = new[] { x }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken); } foreach (var x in enumInfo) { var template = new EnumTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace), EnumSerializationInfos = new[] { x }, }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken); } foreach (var x in unionInfo) { var template = new UnionTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace), UnionSerializationInfos = new[] { x }, }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken); } var resolverTemplate = new ResolverTemplate() { Namespace = namespaceDot + "Resolvers", FormatterNamespace = namespaceDot + "Formatters", ResolverName = resolverName, RegisterInfos = genericInfo.Where(x => !x.IsOpenGenericType).Cast <IResolverRegisterInfo>().Concat(enumInfo).Concat(unionInfo).Concat(objectInfo.Where(x => !x.IsOpenGenericType)).ToArray(), }; await OutputToDirAsync(output, resolverTemplate.Namespace, resolverTemplate.ResolverName, multioutSymbol, resolverTemplate.TransformText(), cancellationToken); } if (objectInfo.Length == 0 && enumInfo.Length == 0 && genericInfo.Length == 0 && unionInfo.Length == 0) { logger("Generated result is empty, unexpected result?"); } } logger("Output Generation Complete:" + sw.Elapsed.ToString()); }
/// <summary> /// Generates the specialized resolver and formatters for the types that require serialization in a given compilation. /// </summary> /// <param name="compilation">The compilation to read types from as an input to code generation.</param> /// <param name="output">The name of the generated source file.</param> /// <param name="resolverName">The resolver name.</param> /// <param name="namespace">The namespace for the generated type to be created in. May be null.</param> /// <param name="useMapMode">A boolean value that indicates whether all formatters should use property maps instead of more compact arrays.</param> /// <param name="multipleIfDirectiveOutputSymbols">A comma-delimited list of symbols that should surround redundant generated files. May be null.</param> /// <param name="externalIgnoreTypeNames"> May be null.</param> /// <returns>A task that indicates when generation has completed.</returns> public async Task GenerateFileAsync( Compilation compilation, string output, string resolverName, string? @namespace, bool useMapMode, string?multipleIfDirectiveOutputSymbols, string[]?externalIgnoreTypeNames) { var namespaceDot = string.IsNullOrWhiteSpace(@namespace) ? string.Empty : @namespace + "."; var multipleOutputSymbols = multipleIfDirectiveOutputSymbols?.Split(',') ?? Array.Empty <string>(); var sw = Stopwatch.StartNew(); foreach (var multiOutputSymbol in multipleOutputSymbols.Length == 0 ? new[] { string.Empty } : multipleOutputSymbols) { logger("Project Compilation Start:" + compilation.AssemblyName); var collector = new TypeCollector(compilation, true, useMapMode, externalIgnoreTypeNames, Console.WriteLine); logger("Project Compilation Complete:" + sw.Elapsed.ToString()); sw.Restart(); logger("Method Collect Start"); var(objectInfo, enumInfo, genericInfo, unionInfo) = collector.Collect(); logger("Method Collect Complete:" + sw.Elapsed.ToString()); logger("Output Generation Start"); sw.Restart(); if (Path.GetExtension(output) == ".cs") { // SingleFile Output var fullGeneratedProgramText = GenerateSingleFileSync(resolverName, namespaceDot, objectInfo, enumInfo, unionInfo, genericInfo); if (multiOutputSymbol == string.Empty) { await OutputAsync(output, fullGeneratedProgramText); } else { var fname = Path.GetFileNameWithoutExtension(output) + "." + MultiSymbolToSafeFilePath(multiOutputSymbol) + ".cs"; var text = $"#if {multiOutputSymbol}" + Environment.NewLine + fullGeneratedProgramText + Environment.NewLine + "#endif"; await OutputAsync(Path.Combine(Path.GetDirectoryName(output) ?? string.Empty, fname), text); } } else { // Multiple File output await GenerateMultipleFileAsync(output, resolverName, objectInfo, enumInfo, unionInfo, namespaceDot, multiOutputSymbol, genericInfo); } if (objectInfo.Length == 0 && enumInfo.Length == 0 && genericInfo.Length == 0 && unionInfo.Length == 0) { logger("Generated result is empty, unexpected result?"); } } logger("Output Generation Complete:" + sw.Elapsed.ToString()); }
public ActivateMessageHubs(TypeCollector collector, IContainer container, ISubscriber subscriber) { _collector = collector; _container = container; _subscriber = subscriber; }
public static IServiceCollection AddMessagePipe(this IServiceCollection services, Action <MessagePipeOptions> configure) { var options = new MessagePipeOptions(); configure(options); services.AddSingleton(options); // add as singleton instance services.AddSingleton(typeof(MessagePipeDiagnosticsInfo)); services.AddSingleton(typeof(EventFactory)); // filters. // attribute and order is deterministic at compile, so use Singleton lifetime of cache. services.AddSingleton(typeof(AttributeFilterProvider <MessageHandlerFilterAttribute>)); services.AddSingleton(typeof(AttributeFilterProvider <AsyncMessageHandlerFilterAttribute>)); services.AddSingleton(typeof(AttributeFilterProvider <RequestHandlerFilterAttribute>)); services.AddSingleton(typeof(AttributeFilterProvider <AsyncRequestHandlerFilterAttribute>)); services.AddSingleton(typeof(FilterAttachedMessageHandlerFactory)); services.AddSingleton(typeof(FilterAttachedAsyncMessageHandlerFactory)); services.AddSingleton(typeof(FilterAttachedRequestHandlerFactory)); services.AddSingleton(typeof(FilterAttachedAsyncRequestHandlerFactory)); foreach (var item in options.GetGlobalFilterTypes()) { services.TryAddTransient(item); // filter itself is Transient } #if !UNITY_2018_3_OR_NEWER // open generics implemntations(.NET Only) { var lifetime = options.InstanceLifetime; // pubsub lifetime // keyless PubSub services.Add(typeof(MessageBrokerCore <>), lifetime); services.Add(typeof(IPublisher <>), typeof(MessageBroker <>), lifetime); services.Add(typeof(ISubscriber <>), typeof(MessageBroker <>), lifetime); services.Add(typeof(BufferedMessageBrokerCore <>), lifetime); services.Add(typeof(IBufferedPublisher <>), typeof(BufferedMessageBroker <>), lifetime); services.Add(typeof(IBufferedSubscriber <>), typeof(BufferedMessageBroker <>), lifetime); // keyless PubSub async services.Add(typeof(AsyncMessageBrokerCore <>), lifetime); services.Add(typeof(IAsyncPublisher <>), typeof(AsyncMessageBroker <>), lifetime); services.Add(typeof(IAsyncSubscriber <>), typeof(AsyncMessageBroker <>), lifetime); services.Add(typeof(BufferedAsyncMessageBrokerCore <>), lifetime); services.Add(typeof(IBufferedAsyncPublisher <>), typeof(BufferedAsyncMessageBroker <>), lifetime); services.Add(typeof(IBufferedAsyncSubscriber <>), typeof(BufferedAsyncMessageBroker <>), lifetime); // keyed PubSub services.Add(typeof(MessageBrokerCore <,>), lifetime); services.Add(typeof(IPublisher <,>), typeof(MessageBroker <,>), lifetime); services.Add(typeof(ISubscriber <,>), typeof(MessageBroker <,>), lifetime); // keyed PubSub async services.Add(typeof(AsyncMessageBrokerCore <,>), lifetime); services.Add(typeof(IAsyncPublisher <,>), typeof(AsyncMessageBroker <,>), lifetime); services.Add(typeof(IAsyncSubscriber <,>), typeof(AsyncMessageBroker <,>), lifetime); } var lifetime2 = options.RequestHandlerLifetime; // requesthandler lifetime // RequestHandler services.Add(typeof(IRequestHandler <,>), typeof(RequestHandler <,>), lifetime2); services.Add(typeof(IAsyncRequestHandler <,>), typeof(AsyncRequestHandler <,>), lifetime2); // RequestAll services.Add(typeof(IRequestAllHandler <,>), typeof(RequestAllHandler <,>), lifetime2); services.Add(typeof(IAsyncRequestAllHandler <,>), typeof(AsyncRequestAllHandler <,>), lifetime2); // auto registration is .NET only. if (options.EnableAutoRegistration) { // auto register filter and requesthandler // request handler is option's lifetime, filter is transient if (options.autoregistrationAssemblies == null && options.autoregistrationTypes == null) { AddRequestHandlerAndFilterFromTypes(services, lifetime2, TypeCollector.CollectFromCurrentDomain()); } else { var fromAssemblies = (options.autoregistrationAssemblies != null) ? TypeCollector.CollectFromAssemblies(options.autoregistrationAssemblies) : Enumerable.Empty <Type>(); var types = options.autoregistrationTypes ?? Enumerable.Empty <Type>(); AddRequestHandlerAndFilterFromTypes(services, lifetime2, fromAssemblies.Concat(types).Distinct()); } } #endif return(services); }
public async Task GenerateFileAsync( string input, string output, string conditionalSymbol, string resolverName, string @namespace, bool useMapMode, string multipleIfDirectiveOutputSymbols) { var namespaceDot = string.IsNullOrWhiteSpace(@namespace) ? string.Empty : @namespace + "."; var conditionalSymbols = conditionalSymbol?.Split(',') ?? Array.Empty <string>(); var multipleOutputSymbols = multipleIfDirectiveOutputSymbols?.Split(',') ?? Array.Empty <string>(); var sw = Stopwatch.StartNew(); foreach (var multioutSymbol in multipleOutputSymbols.Length == 0 ? new[] { string.Empty } : multipleOutputSymbols) { logger("Project Compilation Start:" + input); var compilation = (Path.GetExtension(input) == ".csproj") ? await MessagePackCompilation.CreateFromProjectAsync(input.Split(','), conditionalSymbols.Concat(new[] { multioutSymbol }).ToArray(), cancellationToken) .ConfigureAwait(false) : await MessagePackCompilation.CreateFromDirectoryAsync(input, conditionalSymbols.Concat(new[] { multioutSymbol }).ToArray(), cancellationToken).ConfigureAwait(false); var collector = new TypeCollector(compilation, true, useMapMode, x => Console.WriteLine(x)); logger("Project Compilation Complete:" + sw.Elapsed.ToString()); sw.Restart(); logger("Method Collect Start"); var(objectInfo, enumInfo, genericInfo, unionInfo) = collector.Collect(); logger("Method Collect Complete:" + sw.Elapsed.ToString()); logger("Output Generation Start"); sw.Restart(); if (Path.GetExtension(output) == ".cs") { // SingleFile Output var objectFormatterTemplates = objectInfo .GroupBy(x => x.Namespace) .Select(x => new FormatterTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key), ObjectSerializationInfos = x.ToArray(), }) .ToArray(); var enumFormatterTemplates = enumInfo .GroupBy(x => x.Namespace) .Select(x => new EnumTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key), EnumSerializationInfos = x.ToArray(), }) .ToArray(); var unionFormatterTemplates = unionInfo .GroupBy(x => x.Namespace) .Select(x => new UnionTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key), UnionSerializationInfos = x.ToArray(), }) .ToArray(); var resolverTemplate = new ResolverTemplate() { Namespace = namespaceDot + "Resolvers", FormatterNamespace = namespaceDot + "Formatters", ResolverName = resolverName, RegisterInfos = genericInfo.Cast <IResolverRegisterInfo>().Concat(enumInfo).Concat(unionInfo).Concat(objectInfo).ToArray(), }; var sb = new StringBuilder(); sb.AppendLine(resolverTemplate.TransformText()); sb.AppendLine(); foreach (var item in enumFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } sb.AppendLine(); foreach (var item in unionFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } sb.AppendLine(); foreach (var item in objectFormatterTemplates) { var text = item.TransformText(); sb.AppendLine(text); } if (multioutSymbol == string.Empty) { await OutputAsync(output, sb.ToString(), cancellationToken).ConfigureAwait(false); } else { var fname = Path.GetFileNameWithoutExtension(output) + "." + MultiSymbolToSafeFilePath(multioutSymbol) + ".cs"; var text = $"#if {multioutSymbol}" + Environment.NewLine + sb.ToString() + Environment.NewLine + "#endif"; await OutputAsync(Path.Combine(Path.GetDirectoryName(output), fname), text, cancellationToken).ConfigureAwait(false); } } else { // Multiple File output foreach (var x in objectInfo) { var template = new FormatterTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace), ObjectSerializationInfos = new[] { x }, }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false); } foreach (var x in enumInfo) { var template = new EnumTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace), EnumSerializationInfos = new[] { x }, }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false); } foreach (var x in unionInfo) { var template = new UnionTemplate() { Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace), UnionSerializationInfos = new[] { x }, }; var text = template.TransformText(); await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false); } var resolverTemplate = new ResolverTemplate() { Namespace = namespaceDot + "Resolvers", FormatterNamespace = namespaceDot + "Formatters", ResolverName = resolverName, RegisterInfos = genericInfo.Cast <IResolverRegisterInfo>().Concat(enumInfo).Concat(unionInfo).Concat(objectInfo).ToArray(), }; await OutputToDirAsync(output, resolverTemplate.Namespace, resolverTemplate.ResolverName, multioutSymbol, resolverTemplate.TransformText(), cancellationToken).ConfigureAwait(false); } } logger("Output Generation Complete:" + sw.Elapsed.ToString()); }
protected DomBinder() { Types = new TypeCollector(this); }
public TypeFinder(TypeCollector collector) { _collector = collector; }