public static List<ValidatorResult> Validate(MethodDeclarationSyntax methodSyntax, IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet<ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol)
		{
			var validator = new AsyncMethodValidator(log, extensionMethodLookup, semanticModel, excludeTypes, cancellationTokenSymbol, methodSyntax);

			validator.Visit(methodSyntax);

			return validator.results;
		}
		protected MethodInvocationInspector(IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup,  SemanticModel semanticModel, HashSet<ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol, MethodDeclarationSyntax methodSyntax)
		{
			this.log = log;
			this.extensionMethodLookup = extensionMethodLookup;
			this.semanticModel = semanticModel;
			this.cancellationTokenSymbol = cancellationTokenSymbol;
			this.methodSyntax = methodSyntax;
			this.excludeTypes = excludeTypes;
		}
 protected MethodInvocationInspector(IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet <ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol, MethodDeclarationSyntax methodSyntax)
 {
     this.log = log;
     this.extensionMethodLookup   = extensionMethodLookup;
     this.semanticModel           = semanticModel;
     this.cancellationTokenSymbol = cancellationTokenSymbol;
     this.methodSyntax            = methodSyntax;
     this.excludeTypes            = excludeTypes;
 }
 public static MethodDeclarationSyntax Rewrite(IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet <ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol, MethodDeclarationSyntax methodSyntax)
 {
     return((MethodDeclarationSyntax) new GeneratedAsyncMethodSubstitutor(log, extensionMethodLookup, semanticModel, excludeTypes, cancellationTokenSymbol, methodSyntax).Visit(methodSyntax));
 }
 public GeneratedAsyncMethodSubstitutor(IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet <ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol, MethodDeclarationSyntax methodSyntax)
     : base(log, extensionMethodLookup, semanticModel, excludeTypes, cancellationTokenSymbol, methodSyntax)
 {
 }
Exemple #6
0
        public string RewriteAndMerge(string[] paths, string[] additionalAssemblyNames = null, string[] excludeTypes = null)
        {
            var syntaxTrees = paths
                              .Select(p => SyntaxFactory.ParseSyntaxTree(File.ReadAllText(p)).WithFilePath(p))
                              .Select(c => c.WithRootAndOptions(UpdateUsings(c.GetCompilationUnitRoot()), c.Options))
                              .ToArray();

            var references = new List <MetadataReference>()
            {
                MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Queryable).GetTypeInfo().Assembly.Location)
            };

            if (additionalAssemblyNames != null)
            {
                var assemblyPath = Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location);

                log.LogMessage("FrameworkPath: " + assemblyPath);

                if (assemblyPath == null)
                {
                    throw new InvalidOperationException();
                }

                var facadesLoaded = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

                var loadedAssemblies = new List <string>();

                references.AddRange(additionalAssemblyNames.SelectMany(n =>
                {
                    var results = new List <MetadataReference>();

                    if (File.Exists(n))
                    {
                        var facadesPath = Path.Combine(Path.GetDirectoryName(n) ?? "", "Facades");

                        results.Add(MetadataReference.CreateFromFile(n));

                        loadedAssemblies.Add(n);

                        if (Directory.Exists(facadesPath) && !facadesLoaded.Contains(facadesPath))
                        {
                            facadesLoaded.Add(facadesPath);

                            results.AddRange(Directory.GetFiles(facadesPath).Select(facadeDll => MetadataReference.CreateFromFile(facadeDll)));
                        }

                        return(results);
                    }

                    if (File.Exists(Path.Combine(assemblyPath, n)))
                    {
                        results.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, n)));

                        return(results);
                    }

                    log.LogError($"Could not find ResolveProjectReferencesResolveProjectReferencesreferenced assembly: {n}");

                    return(results);
                }).Where(c => c != null));

                log.LogMessage($"Loaded assemblies: {string.Join(",", loadedAssemblies)}");
            }

            var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);

            this.compilation = CSharpCompilation
                               .Create("Temp", syntaxTrees, references, options);

            this.lookup = new CompilationLookup(this.compilation);

            var resultTree = this.RewriteAndMerge(syntaxTrees, compilation, excludeTypes);
            var retval     = resultTree.ToString();

#if OUTPUT_COMPILER_ERRORS
            var correctedTrees = this.CorrectAsyncCalls(this.compilation, syntaxTrees, excludeTypes);
            var newCompilation = this.compilation = CSharpCompilation
                                                    .Create("Temp", correctedTrees.Concat(new [] { resultTree }).ToArray(), references, options);

            var emitResult = newCompilation.Emit(Stream.Null);

            if (emitResult.Diagnostics.Any())
            {
                log.LogMessage("Compiler errors:");

                foreach (var diagnostic in emitResult.Diagnostics)
                {
                    var message = diagnostic.GetMessage();

                    if (diagnostic.Severity == DiagnosticSeverity.Info)
                    {
                        log.LogMessage(message);
                    }
                    else
                    {
                        log.LogError(message);
                    }
                }
            }
#endif

            return(retval);
        }
		public static MethodDeclarationSyntax Rewrite(IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet<ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol, MethodDeclarationSyntax methodSyntax)
		{
			return (MethodDeclarationSyntax)new MethodInvocationAsyncRewriter(log, extensionMethodLookup, semanticModel, excludeTypes, cancellationTokenSymbol, methodSyntax).Visit(methodSyntax);
		}
		protected MethodInvocationAsyncRewriter(IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet<ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol, MethodDeclarationSyntax methodSyntax)
			: base(log, extensionMethodLookup, semanticModel, excludeTypes, cancellationTokenSymbol, methodSyntax)
		{
		}
Exemple #9
0
 protected MethodInvocationAsyncRewriter(IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet <ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol, MethodDeclarationSyntax methodSyntax)
     : base(log, extensionMethodLookup, semanticModel, excludeTypes, cancellationTokenSymbol, methodSyntax)
 {
 }
Exemple #10
0
        public static List <ValidatorResult> Validate(MethodDeclarationSyntax methodSyntax, IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet <ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol)
        {
            var validator = new AsyncMethodValidator(log, extensionMethodLookup, semanticModel, excludeTypes, cancellationTokenSymbol, methodSyntax);

            validator.Visit(methodSyntax);

            return(validator.results);
        }
		public GeneratedAsyncMethodSubstitutor(IAsyncRewriterLogger log, CompilationLookup extensionMethodLookup, SemanticModel semanticModel, HashSet<ITypeSymbol> excludeTypes, ITypeSymbol cancellationTokenSymbol, MethodDeclarationSyntax methodSyntax)
			: base(log, extensionMethodLookup, semanticModel, excludeTypes, cancellationTokenSymbol, methodSyntax)
		{
		}
Exemple #12
0
		public string RewriteAndMerge(string[] paths, string[] additionalAssemblyNames = null, string[] excludeTypes = null)
		{
			var syntaxTrees = paths
				.Select(p => SyntaxFactory.ParseSyntaxTree(File.ReadAllText(p)).WithFilePath(p))
				.Select(c => c.WithRootAndOptions(UpdateUsings(c.GetCompilationUnitRoot()), c.Options))
				.ToArray();

			var references = new List<MetadataReference>()
			{
				MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
				MetadataReference.CreateFromFile(typeof(Queryable).GetTypeInfo().Assembly.Location)
			};
			
			if (additionalAssemblyNames != null)
			{
				var assemblyPath = Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location);

				log.LogMessage("FrameworkPath: " + assemblyPath);

				if (assemblyPath == null)
				{
					throw new InvalidOperationException();
				}

				var facadesLoaded = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);

				var loadedAssemblies = new List<string>();

				references.AddRange(additionalAssemblyNames.SelectMany(n =>
				{
					var results = new List<MetadataReference>();
					
					if (File.Exists(n))
					{
					   var facadesPath = Path.Combine(Path.GetDirectoryName(n) ?? "", "Facades");

						results.Add(MetadataReference.CreateFromFile(n));
						
						loadedAssemblies.Add(n);

						if (Directory.Exists(facadesPath) && !facadesLoaded.Contains(facadesPath))
						{
							facadesLoaded.Add(facadesPath);

							results.AddRange(Directory.GetFiles(facadesPath).Select(facadeDll => MetadataReference.CreateFromFile(facadeDll)));
						}

						return results;
					}

					if (File.Exists(Path.Combine(assemblyPath, n)))
					{
						results.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, n)));

						return results;
					}

					log.LogError($"Could not find ResolveProjectReferencesResolveProjectReferencesreferenced assembly: {n}");

					return results;
				}).Where(c => c != null));

				log.LogMessage($"Loaded assemblies: {string.Join(",", loadedAssemblies)}");
			}

			var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);

			this.compilation = CSharpCompilation
				.Create("Temp", syntaxTrees, references, options);

			this.lookup = new CompilationLookup(this.compilation);

			var resultTree = this.RewriteAndMerge(syntaxTrees, compilation, excludeTypes);
			var retval = resultTree.ToString();

#if OUTPUT_COMPILER_ERRORS
			
			var correctedTrees = this.CorrectAsyncCalls(this.compilation, syntaxTrees, excludeTypes);
			var newCompilation = this.compilation = CSharpCompilation
				.Create("Temp", correctedTrees.Concat(new [] { resultTree }).ToArray(), references, options);

			var emitResult = newCompilation.Emit(Stream.Null);

			if (emitResult.Diagnostics.Any())
			{
				log.LogMessage("Compiler errors:");
			
				foreach (var diagnostic in emitResult.Diagnostics)
				{
					var message = diagnostic.GetMessage();

					if (diagnostic.Severity == DiagnosticSeverity.Info)
					{
						log.LogMessage(message);
					}
					else
					{
						log.LogError(message);
					}
				}
			}
#endif

			return retval;
		}