Пример #1
0
    private static void ProcessType(FxRedist redist, MemberDeclarationSyntax syntax, SemanticModel typeModel, string currentNamespace, ReportWriter reportWriter)
    {
        ISymbol symbol = typeModel.GetDeclaredSymbol(syntax);
        string  typeId = symbol.GetDocumentationCommentId();

        var typeName = typeId.Substring(2);

        if (FxdacSyntaxRewriter.s_ShouldRemoveType(typeName))
        {
            reportWriter.WriteListItem("Removed type {0}", symbol.Name);
            return;
        }
        FxAssembly typeAssembly = redist.GetAssemblyForType(typeId);

        if (typeAssembly.HasEmitedType(typeId))
        {
            return;
        }

        // rewrite type
        var rewriter = new FxdacSyntaxRewriter(reportWriter, typeModel, typeAssembly.Name);

        syntax = (MemberDeclarationSyntax)syntax.Accept(rewriter);

        // add type to assembly
        var typeSyntax = new FxType()
        {
            Declaration = syntax, Symbol = (ITypeSymbol)symbol
        };

        typeAssembly.AddType(currentNamespace, typeSyntax);
    }
Пример #2
0
    static bool HasAllDependencies(FxAssembly assembly, List <FxAssembly> allAssemblies)
    {
        foreach (var dependency in assembly.References)
        {
            var d = allAssemblies.Find((compiled) => { return(compiled.Name == Path.GetFileNameWithoutExtension(dependency)); });

            if (d == null || d.FailedToCompile)
            {
                return(false);
            }
        }
        return(true);
    }
Пример #3
0
    private static bool ProcessSpecification(FxRedist redist, Dictionary <string, string> alreadyLoadedSpecification, string assemblySpecificationFile)
    {
        bool success  = true;
        var  assembly = new FxAssembly();

        assembly.Name = Path.GetFileNameWithoutExtension(assemblySpecificationFile);
        redist.Add(assembly.Name, assembly);
        string[] assemblySpecification = File.ReadAllLines(assemblySpecificationFile);

        Dictionary <string, int> unique = new Dictionary <string, int>();

        foreach (var specificationLine in assemblySpecification)
        {
            var line = specificationLine.Trim();
            if (string.IsNullOrWhiteSpace(line))
            {
                continue;
            }
            if (line.StartsWith("//"))
            {
                continue;                        // comment
            }
            if (line.StartsWith("/r:"))
            {
                assembly.References.Add(line.Substring(3));
                continue;
            }

            try {
                unique.Add("T:" + line, 0);

                try {
                    alreadyLoadedSpecification.Add(line, assemblySpecificationFile);
                }
                catch (ArgumentException) {
                    WriteMessage(ConsoleColor.Red, "{0} in files {1} and {2}", line, assemblySpecificationFile, alreadyLoadedSpecification[line]);
                    success = false;
                }
            }
            catch (ArgumentException) {
                WriteMessage(ConsoleColor.Yellow, "Type {1} duplicated in {0}", assemblySpecificationFile, line);
                success = false;
            }
        }

        assembly.Types.AddRange(unique.Keys);
        return(success);
    }
Пример #4
0
    static bool CompileAssembly(FxAssembly assembly)
    {
        if (assembly.Name == s_LefroverTypesAssembly)
        {
            return(true);
        }

        var assemblySyntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(assembly.SourceFile));

        var references = new List <MetadataReference>();

        foreach (var reference in assembly.References)
        {
            var referenceFile = Path.Combine(s_OutputDlls, reference);
            if (!File.Exists(referenceFile))
            {
                WriteMessage(ConsoleColor.Red, "References {1} of {0} missing", assembly.Name, referenceFile);
                return(false);
            }

            references.Add(MetadataReference.CreateFromFile(referenceFile));
        }

        var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithAllowUnsafe(true);

        CSharpCompilation compilation = CSharpCompilation.Create(
            assembly.Name,
            syntaxTrees: new[] { assemblySyntaxTree },
            references: references,
            options: options
            );

        var dllPath = Path.Combine(s_OutputDlls, assembly.Name + ".dll");

        var emitOptions = new EmitOptions(metadataOnly: true, runtimeMetadataVersion: "v12345");

        using (var ms = new FileStream(dllPath, FileMode.Create, FileAccess.Write, FileShare.None)) {
            EmitResult compilationResult = compilation.Emit(ms, options: emitOptions);
            if (!compilationResult.Success)
            {
                Console.WriteLine("COMPILING {0}", assembly.Name);
                ProcessCompilationErrors(compilationResult);
                return(false);
            }
            else
            {
                Console.WriteLine("{0} created.", dllPath);

                var numberOfWarnings = compilationResult.Diagnostics.Count();
                if (numberOfWarnings > 0)
                {
                    WriteMessage(ConsoleColor.Yellow, "{0} warning[s].", numberOfWarnings);

                    foreach (var warning in compilationResult.Diagnostics)
                    {
                        if (!s_logExcludedWarnings)
                        {
                            if (warning.Id == "CS0618" || warning.Id == "CS0809")
                            {
                                continue;                                                   // obsolete message
                            }
                            if ((warning.Id == "CS0108" || warning.Id == "CS0114") && warning.ToString().Contains("TypeInfo."))
                            {
                                continue;                                                                                                 // skip type info newslot warnings. // TODO: fix TypeInfo
                            }
                        }
                        WriteMessage(ConsoleColor.Yellow, "{0}", warning);
                    }
                }
            }
        }

        var loaded     = Assembly.LoadFrom(dllPath);
        var referenced = loaded.GetReferencedAssemblies();

        foreach (var r in referenced)
        {
            references.RemoveAll((mr) => {
                return(Path.GetFileNameWithoutExtension(mr.Display).Equals(r.Name));
            });
        }

        if (references.Count > 0)
        {
            foreach (var r in references)
            {
                WriteMessage(ConsoleColor.Yellow, "Unnecessary reference {0} in {1}", r.Display, assembly.Name);
            }
        }

        return(true);
    }