예제 #1
0
        private static void CompileProject(Microsoft.CodeAnalysis.Project project)
        {
            project = project.WithParseOptions(((CSharpParseOptions)project.ParseOptions).WithPreprocessorSymbols(project.ParseOptions.PreprocessorSymbolNames.Concat(new[] { "LINQREWRITE" })));
            var compilation = project.GetCompilationAsync().Result;

            var hasErrs = false;

            foreach (var item in compilation.GetDiagnostics())
            {
                PrintDiagnostic(item);
                if (item.Severity == DiagnosticSeverity.Error)
                {
                    hasErrs = true;
                }
            }

            if (hasErrs)
            {
                throw new ExitException(1);
            }
            var updatedProject = project;

            if (!NoRewrite)
            {
                foreach (var doc in project.Documents)
                {
                    Console.WriteLine(doc.FilePath);
                    var syntaxTree = doc.GetSyntaxTreeAsync().Result;

                    var rewriter = new LinqRewriter(compilation.GetSemanticModel(syntaxTree));

                    var rewritten = rewriter.Visit(syntaxTree.GetRoot()).NormalizeWhitespace();
                    if (WriteFiles)
                    {
                        var tostring = rewritten.ToFullString();
                        if (syntaxTree.ToString() != tostring)
                        {
                            File.WriteAllText(doc.FilePath, tostring, Encoding.UTF8);
                        }
                    }
                    updatedProject = updatedProject.GetDocument(doc.Id).WithSyntaxRoot(rewritten).Project;
                }
                project     = updatedProject;
                compilation = project.GetCompilationAsync().Result;
                hasErrs     = false;
                foreach (var item in compilation.GetDiagnostics())
                {
                    PrintDiagnostic(item);
                    if (item.Severity == DiagnosticSeverity.Error)
                    {
                        hasErrs = true;
                        if (item.Location != Location.None)
                        {
                            Console.ForegroundColor = ConsoleColor.White;
                            //var lines = item.Location.GetLineSpan();
                            var node = item.Location.SourceTree.GetRoot().FindNode(item.Location.SourceSpan);
                            var k    = node.AncestorsAndSelf().FirstOrDefault(x => x is MethodDeclarationSyntax);
                            if (k != null)
                            {
                                Console.WriteLine(k.ToString());
                            }
                            Console.ResetColor();
                        }
                    }
                }
            }
            string outputPath = project.OutputFilePath.Replace("\\", "/");
            var    objpath    = outputPath.Replace("/bin/", "/obj/");

            if (ForProjBuild)
            {
                outputPath = objpath;
            }

            var ns           = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
            var xml          = XDocument.Load(project.FilePath);
            var hasResources = xml
                               .DescendantNodes()
                               .OfType <XElement>()
                               .Where(x => x.Name == ns + "EmbeddedResource" || x.Name == ns + "Resource")
                               .Any();

            if (hasResources)
            {
                foreach (var resource in Directory.GetFiles(Path.GetDirectoryName(objpath), "*.resources"))
                {
                    File.Delete(resource);
                }

                var args = new object[] { project.FilePath, new Shaman.Runtime.ProcessUtils.RawCommandLineArgument("/p:Configuration=Release") };
                try
                {
                    ProcessUtils.RunPassThrough("msbuild", args);
                }
                catch (Exception ex) when(!(ex is ProcessException))
                {
                    ProcessUtils.RunPassThrough("xbuild", args);
                }
            }
            var resources = hasResources ? Directory.EnumerateFiles(Path.GetDirectoryName(objpath), "*.resources")
                            .Select(x =>
            {
                return(new ResourceDescription(Path.GetFileName(x), () => File.OpenRead(x), true));
            }).ToList() : Enumerable.Empty <ResourceDescription>();

            /*
             * var resources = XDocument.Load(project.FilePath)
             *  .DescendantNodes()
             *  .OfType<XElement>().Where(x => x.Name == ns + "EmbeddedResource")
             *  .Select(x => x.Attribute(ns + "Include"))
             *  .Select(x => Path.Combine(Path.GetDirectoryName(project.FilePath), x.Value))
             *  .Select(x =>
             *  {
             *      var rd = new ResourceDescription();
             *  }).ToList();
             */

            compilation.Emit(outputPath, manifestResources: resources);



            //compilation.Emit(@"C:\temp\roslynrewrite\" + project.AssemblyName + ".dll");
            if (hasErrs)
            {
                throw new ExitException(1);
            }
        }