Esempio n. 1
0
        private void CompileExample(string path, bool devPath = true)
        {
            var source     = File.ReadAllText(devPath ? Path.Combine("../../Samples/", path) : path);
            var isScript   = Path.GetExtension(path).Equals(".csx");
            var syntaxTree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(kind: isScript ? SourceCodeKind.Script : SourceCodeKind.Regular));
            var references = new[] {
                MetadataReference.CreateFromFile(typeof(int).GetTypeInfo().Assembly.Location),                    // mscorlib
                MetadataReference.CreateFromFile(typeof(Uri).GetTypeInfo().Assembly.Location),                    // System
                MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).GetTypeInfo().Assembly.Location), // System.Core
            };
            var compilation = isScript
                ? CSharpCompilation.CreateScriptCompilation("LinqRewriteExample", syntaxTree, references)
                : CSharpCompilation.Create("LinqRewriteExample", new[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));


            var hasErrs = false;

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

            if (hasErrs)
            {
                return;
            }

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

            hasErrs = false;
            foreach (var item in compilation.GetDiagnostics())
            {
                if (item.Severity == DiagnosticSeverity.Error)
                {
                    hasErrs = true;
                }
                if (item.Severity == DiagnosticSeverity.Warning)
                {
                    continue;
                }
                PrintDiagnostic(item);
            }
            if (hasErrs)
            {
                return;
            }
            Console.WriteLine(rewritten.ToString());
        }
Esempio n. 2
0
        private void CompileExample(string path)
        {
            var workspace = new AdhocWorkspace();
            var proj      = workspace.AddProject("LinqRewriteExample", "C#").WithMetadataReferences(
                new[] {
                MetadataReference.CreateFromFile(typeof(int).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).GetTypeInfo().Assembly.Location),
            }
                ).WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            proj = proj.AddDocument("FastLinqExtensions.cs", File.ReadAllText("../../../Shaman.FastLinq.Sources/FastLinqExtensions.cs")).Project;
            var doc = proj.AddDocument("source.cs", File.ReadAllText(Path.Combine("../../Samples/", path)));

            if (!workspace.TryApplyChanges(doc.Project.Solution))
            {
                throw new Exception();
            }
            proj = doc.Project;
            var comp = proj.GetCompilationAsync().Result;

            var hasErrs = false;

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

            if (hasErrs)
            {
                return;
            }

            var syntaxTree = doc.GetSyntaxTreeAsync().Result;
            var rewriter   = new LinqRewriter(comp.GetSemanticModel(syntaxTree));
            var rewritten  = rewriter.Visit(syntaxTree.GetRoot());

            proj = doc.WithSyntaxRoot(rewritten).Project;

            hasErrs = false;
            foreach (var item in proj.GetCompilationAsync().Result.GetDiagnostics())
            {
                if (item.Severity == DiagnosticSeverity.Error)
                {
                    hasErrs = true;
                }
                if (item.Severity == DiagnosticSeverity.Warning)
                {
                    continue;
                }
                PrintDiagnostic(item);
            }
            if (hasErrs)
            {
                return;
            }
            Console.WriteLine(rewritten.ToString());
        }
Esempio n. 3
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);
            }
        }