예제 #1
0
        /// <summary>
        /// Main scan method: resolve and load dependencies, run roslyn compiler, emit defs and refs
        /// </summary>
        /// <param name="context">Project context.</param>
        internal static async Task <Output> Graph(GraphContext context)
        {
            if (context.Project == null)
            {
                Project project;
                if (!Project.TryGetProject(context.ProjectDirectory, out project))
                {
                    //not a DNX project
                    DirectoryInfo di    = new DirectoryInfo(context.ProjectDirectory);
                    FileInfo[]    fis   = DepresolveConsoleCommand.FindSources(di);
                    string[]      files = new string[fis.Length];
                    for (int i = 0; i < fis.Length; i++)
                    {
                        files[i] = fis[i].FullName;
                    }
                    Microsoft.CodeAnalysis.Text.SourceText[] sources = new Microsoft.CodeAnalysis.Text.SourceText[files.Length];
                    SyntaxTree[] trees = new SyntaxTree[files.Length];
                    Dictionary <SyntaxTree, string> dict = new Dictionary <SyntaxTree, string>();
                    var compilation = CSharpCompilation.Create("name");
                    for (int i = 0; i < files.Length; i++)
                    {
                        try
                        {
                            FileStream source = new FileStream(files[i], FileMode.Open);
                            sources[i] = Microsoft.CodeAnalysis.Text.SourceText.From(source);
                            trees[i]   = CSharpSyntaxTree.ParseText(sources[i]);
                            if (trees[i] != null)
                            {
                                compilation    = compilation.AddSyntaxTrees(trees[i]);
                                dict[trees[i]] = files[i];
                            }
                            source.Dispose();
                        }
                        catch (Exception e)
                        {
                        }
                    }

                    var gr = new GraphRunner();
                    foreach (var st in compilation.SyntaxTrees)
                    {
                        var path = dict[st];
                        if (!string.IsNullOrWhiteSpace(path))
                        {
                            path = Utils.GetRelativePath(path, context.ProjectDirectory);
                            if (!string.IsNullOrWhiteSpace(path) && (path.Substring(0, 3) != ".." + Path.DirectorySeparatorChar) && !path.Equals("file://applyprojectinfo.cs/"))
                            {
                                // this is a source code file we want to grap
                                gr._sm   = compilation.GetSemanticModel(st, false);
                                gr._path = path;
                                var root = await st.GetRootAsync();

                                gr.Visit(root);
                            }
                        }
                    }
                    gr._sm   = null;
                    gr._path = null;
                    gr.RunTokens();
                    return(gr._output);
                }

                context.Project = project;
            }

            if (context.Project.GetTargetFrameworks().Count() == 0)
            {
                return(new Output());
            }
            context.ApplicationHostContext = new ApplicationHostContext
            {
                ProjectDirectory = context.ProjectDirectory,
                Project          = context.Project,
                TargetFramework  = context.Project.GetTargetFrameworks().First().FrameworkName
            };

            context.ApplicationEnvironment = new ApplicationEnvironment(
                context.Project,
                context.ApplicationHostContext.TargetFramework,
                "Debug",
                context.HostEnvironment);

            context.CompilationContext = new CompilationEngineContext(context.ApplicationEnvironment, context.RuntimeEnvironment, context.LoadContextAccessor.Default, new CompilationCache());

            context.CompilationEngine = new CompilationEngine(context.CompilationContext);
            context.LibraryExporter   = context.CompilationEngine.CreateProjectExporter(context.Project, context.ApplicationHostContext.TargetFramework, "Debug");
            context.Export            = context.LibraryExporter.GetExport(context.Project.Name);

            if (!(context.Export.MetadataReferences[0] is IRoslynMetadataReference))
            {
                return(new Output());
            }
            var roslynRef      = (IRoslynMetadataReference)context.Export.MetadataReferences[0];
            var compilationRef = (CompilationReference)roslynRef.MetadataReference;
            var csCompilation  = (CSharpCompilation)compilationRef.Compilation;

            context.Compilation = csCompilation;

            IEnumerable <LibraryDescription>      deps = DepresolveConsoleCommand.DepResolve(context.Project);
            HashSet <PortableExecutableReference> libs = new HashSet <PortableExecutableReference>();

            try
            {
                if (File.Exists(context.ProjectDirectory + "/global.log"))
                {
                    string[] ss = File.ReadAllLines(context.ProjectDirectory + "/global.log");
                    foreach (string s in ss)
                    {
                        libs.Add(MetadataReference.CreateFromFile(s));
                    }
                }
                libs.Add(MetadataReference.CreateFromFile("/opt/DNX_BRANCH/runtimes/dnx-coreclr-linux-x64.1.0.0-rc1-update1/bin/mscorlib.dll"));
            }
            catch (Exception e)
            {
            }
            foreach (LibraryDescription ld in deps)
            {
                PortableExecutableReference r = null;
                try
                {
                    string path = "";
                    if (ld.Path.EndsWith("project.json") && (ld.Path.IndexOf("wrap") != -1))
                    {
                        if (File.Exists(ld.Path))
                        {
                            var content = File.ReadAllText(ld.Path);
                            var spec    = JsonConvert.DeserializeObject <Wrap>(content);
                            path = ld.Path.Substring(0, ld.Path.Length - 12) + spec.frameworks.net451.bin.assembly;
                        }
                    }
                    else
                    {
                        DirectoryInfo di = new DirectoryInfo(ld.Path);
                        path = DepresolveConsoleCommand.FindDll(di, ld.Identity.Name);
                        HandleNuspec(ld.Path, ld.Identity.Name);
                    }
                    r = MetadataReference.CreateFromFile(path);
                }
                catch (Exception e)
                {
                    try
                    {
                        string        name    = ld.Identity.Name;
                        string        path    = ld.Path;
                        string        cd      = path.Substring(0, path.LastIndexOf('/'));
                        DirectoryInfo di      = new DirectoryInfo(cd);
                        string        newpath = DepresolveConsoleCommand.FindDll(di, ld.Identity.Name);
                        HandleNuspec(cd, ld.Identity.Name);
                        r = MetadataReference.CreateFromFile(newpath);
                    }
                    catch (Exception ee)
                    {
                    }
                }
                if (r != null)
                {
                    libs.Add(r);
                }
            }
            context.Compilation = context.Compilation.WithReferences(libs);

            var runner = new GraphRunner();

            foreach (var st in context.Compilation.SyntaxTrees)
            {
                var path = st.FilePath;
                if (!string.IsNullOrWhiteSpace(path))
                {
                    path = Utils.GetRelativePath(path, context.RootPath);
                    if (!string.IsNullOrWhiteSpace(path) && (path.Substring(0, 3) != ".." + Path.DirectorySeparatorChar) && !path.Equals("file://applyprojectinfo.cs/"))
                    {
                        // this is a source code file we want to grap
                        runner._sm   = context.Compilation.GetSemanticModel(st, false);
                        runner._path = path;
                        var root = await st.GetRootAsync();

                        runner.Visit(root);
                    }
                }
            }

            runner._sm   = null;
            runner._path = null;
            runner.RunTokens();
            return(runner._output);
        }
예제 #2
0
        internal static async Task<Output> Graph(GraphContext context)
        {
            if (context.Project == null)
            {
                Project project;
                if (!Project.TryGetProject(context.ProjectDirectory, out project))
                {
                    //not a DNX project
                    DirectoryInfo di = new DirectoryInfo(context.ProjectDirectory);
                    FileInfo[] fis = DepresolveConsoleCommand.FindSources(di);
                    string[] files = new string[fis.Length];
                    for (int i = 0; i < fis.Length; i++)
                    {
                        files[i] = fis[i].FullName;
                    }
                    Microsoft.CodeAnalysis.Text.SourceText[] sources = new Microsoft.CodeAnalysis.Text.SourceText[files.Length];
                    SyntaxTree[] trees = new SyntaxTree[files.Length];
                    Dictionary<SyntaxTree, string> dict = new Dictionary<SyntaxTree, string>();
                    var compilation = CSharpCompilation.Create("name");
                    for (int i = 0; i < files.Length; i++)
                    {
                        try
                        {
                            sources[i] = Microsoft.CodeAnalysis.Text.SourceText.From(new FileStream(files[i], FileMode.Open));
                            trees[i] = CSharpSyntaxTree.ParseText(sources[i]);
                            if (trees[i] != null)
                            {
                                compilation = compilation.AddSyntaxTrees(trees[i]);
                                dict[trees[i]] = files[i];
                            }
                        }
                        catch (Exception e)
                        {
                        }
                    }

                    var gr = new GraphRunner();
                    foreach (var st in compilation.SyntaxTrees)
                    {
                        var path = dict[st];
                        if (!string.IsNullOrWhiteSpace(path))
                        {
                            path = Utils.GetRelativePath(path, context.ProjectDirectory);
                            if (!string.IsNullOrWhiteSpace(path) && (path.Substring(0, 3) != ".." + Path.DirectorySeparatorChar) && !path.Equals("file://applyprojectinfo.cs/"))
                            {
                                // this is a source code file we want to grap
                                gr._sm = compilation.GetSemanticModel(st, false);
                                gr._path = path;
                                var root = await st.GetRootAsync();
                                gr.Visit(root);
                            }
                        }
                    }
                    gr._sm = null;
                    gr._path = null;
                    gr.RunTokens();
                    return gr._output;

                }

                context.Project = project;
            }

            context.ApplicationHostContext = new ApplicationHostContext
            {
                ProjectDirectory = context.ProjectDirectory,
                Project = context.Project,
                TargetFramework = context.Project.GetTargetFrameworks().First().FrameworkName
            };

            context.ApplicationEnvironment = new ApplicationEnvironment(
              context.Project,
              context.ApplicationHostContext.TargetFramework,
              "Debug",
              context.HostEnvironment);

            context.CompilationContext = new CompilationEngineContext(context.ApplicationEnvironment, context.RuntimeEnvironment, context.LoadContextAccessor.Default, new CompilationCache());

            context.CompilationEngine = new CompilationEngine(context.CompilationContext);
            context.LibraryExporter = context.CompilationEngine.CreateProjectExporter(context.Project, context.ApplicationHostContext.TargetFramework, "Debug");
            context.Export = context.LibraryExporter.GetExport(context.Project.Name);

            if (!(context.Export.MetadataReferences[0] is IRoslynMetadataReference))
            {
                return new Output();
            }
            var roslynRef = (IRoslynMetadataReference)context.Export.MetadataReferences[0];
            var compilationRef = (CompilationReference)roslynRef.MetadataReference;
            var csCompilation = (CSharpCompilation)compilationRef.Compilation;
            context.Compilation = csCompilation;

            IEnumerable<LibraryDescription> deps = DepresolveConsoleCommand.DepResolve(context.Project);
            HashSet<PortableExecutableReference> libs = new HashSet<PortableExecutableReference>();
            try
            {
                libs.Add(MetadataReference.CreateFromFile("/opt/DNX_BRANCH/runtimes/dnx-coreclr-linux-x64.1.0.0-rc1-update1/bin/mscorlib.dll"));
            }
            catch (Exception e)
            {
            }
            foreach (LibraryDescription ld in deps)
            {
                PortableExecutableReference r = null;
                try
                {
                    string path = "";
                    if (ld.Path.EndsWith("project.json") && (ld.Path.IndexOf("wrap") != -1))
                    {
                        if (File.Exists(ld.Path))
                        {
                            var content = File.ReadAllText(ld.Path);
                            var spec = JsonConvert.DeserializeObject<Wrap>(content);
                            path = ld.Path.Substring(0, ld.Path.Length - 12) + spec.frameworks.net451.bin.assembly;
                        }
                    }
                    else
                    {
                        DirectoryInfo di = new DirectoryInfo(ld.Path);
                        path = DepresolveConsoleCommand.FindDll(di, ld.Identity.Name);
                        HandleNuspec(ld.Path, ld.Identity.Name);
                    }
                    r = MetadataReference.CreateFromFile(path);
                }
                catch (Exception e)
                {
                    try
                    {
                        string name = ld.Identity.Name;
                        string path = ld.Path;
                        string cd = path.Substring(0, path.LastIndexOf('/'));
                        DirectoryInfo di = new DirectoryInfo(cd);
                        string newpath = DepresolveConsoleCommand.FindDll(di, ld.Identity.Name);
                        HandleNuspec(cd, ld.Identity.Name);
                        r = MetadataReference.CreateFromFile(newpath);
                    }
                    catch (Exception ee)
                    {
                    }
                }
                if (r != null)
                {
                    libs.Add(r);
                }
            }
            context.Compilation = context.Compilation.WithReferences(libs);

            var runner = new GraphRunner();
            foreach (var st in context.Compilation.SyntaxTrees)
            {
                var path = st.FilePath;
                if (!string.IsNullOrWhiteSpace(path))
                {
                    path = Utils.GetRelativePath(path, context.RootPath);
                    if (!string.IsNullOrWhiteSpace(path) && (path.Substring(0, 3) != ".." + Path.DirectorySeparatorChar) && !path.Equals("file://applyprojectinfo.cs/"))
                    {
                        // this is a source code file we want to grap
                        runner._sm = context.Compilation.GetSemanticModel(st, false);
                        runner._path = path;
                        var root = await st.GetRootAsync();
                        runner.Visit(root);
                    }
                }
            }

            runner._sm = null;
            runner._path = null;
            runner.RunTokens();
            return runner._output;
        }
예제 #3
0
    internal static async Task<Output> Graph(GraphContext context)
    {
      if (context.Project == null)
      {
        Project project;
        if (!Project.TryGetProject(context.ProjectDirectory, out project))
          throw new InvalidOperationException("Can't find project");

        context.Project = project;
      }

      context.ApplicationHostContext = new ApplicationHostContext
      {
        ProjectDirectory = context.ProjectDirectory,
        Project = context.Project,
        TargetFramework = context.Project.GetTargetFrameworks().First().FrameworkName
      };

      context.ApplicationEnvironment = new ApplicationEnvironment(
        context.Project,
        context.ApplicationHostContext.TargetFramework,
        "Debug",
        context.HostEnvironment);

      context.CompilationContext = new CompilationEngineContext(context.ApplicationEnvironment, context.RuntimeEnvironment, context.LoadContextAccessor.Default, new CompilationCache());

      context.CompilationEngine = new CompilationEngine(context.CompilationContext);
      context.LibraryExporter = context.CompilationEngine.CreateProjectExporter(context.Project, context.ApplicationHostContext.TargetFramework, "Debug");
      context.Export = context.LibraryExporter.GetExport(context.Project.Name);

      // TODO: If other languages are to be supported, this part needs to change.
      var roslynRef = (IRoslynMetadataReference)context.Export.MetadataReferences[0];
      var compilationRef = (CompilationReference)roslynRef.MetadataReference;
      var csCompilation = (CSharpCompilation)compilationRef.Compilation;
      context.Compilation = csCompilation;

      var runner = new GraphRunner();
      foreach (var st in context.Compilation.SyntaxTrees)
      {
        var path = st.FilePath;
        if (!string.IsNullOrWhiteSpace(path))
        {
          path = Utils.GetRelativePath(path, context.RootPath);
          if (!string.IsNullOrWhiteSpace(path) && path.Substring(0, 3) != ".." + Path.DirectorySeparatorChar)
          {
            // this is a source code file we want to grap
            runner._sm = context.Compilation.GetSemanticModel(st, false);
            runner._path = path;
            var root = await st.GetRootAsync();
            runner.Visit(root);
          }
        }
      }

      runner._sm = null;
      runner._path = null;
      runner.RunTokens();
      return runner._output;
    }
예제 #4
0
        internal static async Task <Output> Graph(GraphContext context)
        {
            if (context.Project == null)
            {
                Project project;
                if (!Project.TryGetProject(context.ProjectDirectory, out project))
                {
                    throw new InvalidOperationException("Can't find project");
                }

                context.Project = project;
            }

            context.ApplicationHostContext = new ApplicationHostContext
            {
                ProjectDirectory = context.ProjectDirectory,
                Project          = context.Project,
                TargetFramework  = context.Project.GetTargetFrameworks().First().FrameworkName
            };

            context.ApplicationEnvironment = new ApplicationEnvironment(
                context.Project,
                context.ApplicationHostContext.TargetFramework,
                "Debug",
                context.HostEnvironment);

            context.CompilationContext = new CompilationEngineContext(context.ApplicationEnvironment, context.RuntimeEnvironment, context.LoadContextAccessor.Default, new CompilationCache());

            context.CompilationEngine = new CompilationEngine(context.CompilationContext);
            context.LibraryExporter   = context.CompilationEngine.CreateProjectExporter(context.Project, context.ApplicationHostContext.TargetFramework, "Debug");
            context.Export            = context.LibraryExporter.GetExport(context.Project.Name);

            // TODO: If other languages are to be supported, this part needs to change.
            var roslynRef      = (IRoslynMetadataReference)context.Export.MetadataReferences[0];
            var compilationRef = (CompilationReference)roslynRef.MetadataReference;
            var csCompilation  = (CSharpCompilation)compilationRef.Compilation;

            context.Compilation = csCompilation;

            var runner = new GraphRunner();

            foreach (var st in context.Compilation.SyntaxTrees)
            {
                var path = st.FilePath;
                if (!string.IsNullOrWhiteSpace(path))
                {
                    path = Utils.GetRelativePath(path, context.RootPath);
                    if (!string.IsNullOrWhiteSpace(path) && path.Substring(0, 3) != ".." + Path.DirectorySeparatorChar)
                    {
                        // this is a source code file we want to grap
                        runner._sm   = context.Compilation.GetSemanticModel(st, false);
                        runner._path = path;
                        var root = await st.GetRootAsync();

                        runner.Visit(root);
                    }
                }
            }

            runner._sm   = null;
            runner._path = null;
            runner.RunTokens();
            return(runner._output);
        }