Exemple #1
0
        public async Task TestDataHandling()
        {
            using (var project = Services.ProjectService.CreateDotNetProject("C#"))
                using (var workspace = await IdeApp.TypeSystemService.CreateEmptyWorkspace()) {
                    var map = new MonoDevelopWorkspace.ProjectDataMap(workspace);

                    var pid  = map.GetOrCreateId(project, null);
                    var data = map.GetData(pid);

                    Assert.IsNull(data);
                    Assert.IsFalse(map.Contains(pid));

                    data = map.CreateData(pid, ImmutableArray <MonoDevelopMetadataReference> .Empty);

                    Assert.IsNotNull(data);
                    Assert.IsTrue(map.Contains(pid));

                    map.RemoveData(pid);

                    data = map.GetData(pid);

                    Assert.IsNull(data);
                    Assert.IsFalse(map.Contains(pid));
                }
        }
Exemple #2
0
        public async Task TestSimpleCreation()
        {
            using (var project = Services.ProjectService.CreateDotNetProject("C#"))
                using (var workspace = await IdeApp.TypeSystemService.CreateEmptyWorkspace()) {
                    var map = new MonoDevelopWorkspace.ProjectDataMap(workspace);

                    var pid = map.GetId(project);
                    Assert.IsNull(pid);

                    pid = map.GetOrCreateId(project, null);
                    Assert.IsNotNull(pid);

                    var projectInMap = map.GetMonoProject(pid);
                    Assert.AreSame(project, projectInMap);

                    var projectRemovedFromMap = map.RemoveProject(pid);
                    Assert.AreSame(projectInMap, projectRemovedFromMap);

                    Assert.IsNull(map.GetId(project));

                    pid = map.GetOrCreateId(project, null);
                    map.RemoveProject(project);

                    Assert.IsNull(map.GetId(project));
                }
        }
Exemple #3
0
        public async Task TestSimpleCreation_MultiTargetFramework()
        {
            var projectFile = Util.GetSampleProject("multi-target", "multi-target.csproj");

            using (var project = (DotNetProject)await Services.ProjectService.ReadSolutionItem(Util.GetMonitor(), projectFile))
                using (var workspace = await IdeApp.TypeSystemService.CreateEmptyWorkspace()) {
                    var map = new MonoDevelopWorkspace.ProjectDataMap(workspace);

                    var pid = map.GetOrCreateId(project, null, "netcoreapp1.1");
                    Assert.IsNotNull(pid);

                    var pid2 = map.GetOrCreateId(project, null, "netstandard1.0");
                    Assert.IsNotNull(pid2);

                    Assert.AreNotEqual(pid, pid2);

                    // Returns first project for first framework
                    var defaultPid = map.GetId(project);
                    Assert.IsNotNull(defaultPid);
                    Assert.AreEqual("multi-target (netcoreapp1.1)", defaultPid.DebugName);
                    Assert.AreEqual(defaultPid, pid);

                    var pids = map.GetIds(project);
                    Assert.AreEqual(2, pids.Length);
                    Assert.That(pids, Contains.Item(pid));
                    Assert.That(pids, Contains.Item(pid2));

                    var projectInMap = map.GetMonoProject(pid);
                    Assert.AreSame(project, projectInMap);

                    projectInMap = map.GetMonoProject(pid2);
                    Assert.AreSame(project, projectInMap);

                    var foundPid = map.GetId(project, "netcoreapp1.1");
                    Assert.AreSame(pid, foundPid);

                    foundPid = map.GetId(project, "netstandard1.0");
                    Assert.AreSame(pid2, foundPid);

                    var projectRemovedFromMap = map.RemoveProject(pid);
                    Assert.AreSame(projectInMap, projectRemovedFromMap);

                    projectRemovedFromMap = map.RemoveProject(pid2);
                    Assert.AreSame(projectInMap, projectRemovedFromMap);

                    Assert.IsNull(map.GetId(project, "netcoreapp1.1"));
                    Assert.IsNull(map.GetId(project, "netstandard1.0"));

                    pid = map.GetOrCreateId(project, null, "netcoreapp1.1");
                    map.RemoveProject(project);

                    Assert.IsNull(map.GetId(project, "netcoreapp1.1"));

                    pid = map.GetOrCreateId(project, null, "netstandard1.0");
                    map.RemoveProject(project);

                    Assert.IsNull(map.GetId(project, "netstandard1.0"));
                }
        }
        public void Update(ProjectConfiguration projConfig, Project proj, MonoDevelopWorkspace.ProjectDataMap projectMap,
                           ImmutableArray <ProjectFile> files,
                           ImmutableArray <FilePath> analyzers,
                           ImmutableArray <MonoDevelopMetadataReference> metadataReferences,
                           ImmutableArray <Microsoft.CodeAnalysis.ProjectReference> projectReferences)
        {
            if (!enabled)
            {
                return;
            }

            var paths   = new string [files.Length];
            var actions = new string [files.Length];

            for (int i = 0; i < files.Length; ++i)
            {
                paths [i]   = files [i].FilePath;
                actions [i] = files [i].BuildAction;
            }

            var projectRefs = new ReferenceItem [projectReferences.Length];

            for (int i = 0; i < projectReferences.Length; ++i)
            {
                var pr        = projectReferences [i];
                var mdProject = projectMap.GetMonoProject(pr.ProjectId);
                projectRefs [i] = new ReferenceItem {
                    FilePath = mdProject.FileName,
                    Aliases  = pr.Aliases.ToArray(),
                };
            }

            var item = new ProjectCache {
                Format             = format,
                Analyzers          = analyzers.Select(x => (string)x).ToArray(),
                Files              = paths,
                BuildActions       = actions,
                TimeStamp          = File.GetLastWriteTimeUtc(proj.FileName),
                MetadataReferences = metadataReferences.Select(x => {
                    var ri = new ReferenceItem {
                        FilePath = x.FilePath,
                        Aliases  = x.Properties.Aliases.ToArray(),
                    };
                    return(ri);
                }).ToArray(),
                ProjectReferences = projectRefs,
            };

            var cacheFile = GetProjectCacheFile(proj, projConfig.Id);

            var serializer = new JsonSerializer();

            using (var fs = File.Open(cacheFile, FileMode.Create))
                using (var sw = new StreamWriter(fs)) {
                    serializer.Serialize(sw, item);
                }
        }
Exemple #5
0
        public async Task TestMigration()
        {
            using (var project = Services.ProjectService.CreateDotNetProject("C#"))
                using (var project2 = Services.ProjectService.CreateDotNetProject("C#"))
                    using (var workspace = await IdeApp.TypeSystemService.CreateEmptyWorkspace()) {
                        var map = new MonoDevelopWorkspace.ProjectDataMap(workspace);

                        var pid = map.GetOrCreateId(project, null);
                        Assert.IsNotNull(pid);

                        var pid2 = map.GetOrCreateId(project2, project);
                        Assert.AreSame(pid, pid2);
                    }
        }
        public void TestMigration()
        {
            using (var project = Services.ProjectService.CreateDotNetProject("C#"))
                using (var project2 = Services.ProjectService.CreateDotNetProject("C#"))
                    using (var workspace = new MonoDevelopWorkspace(null)) {
                        var map = new MonoDevelopWorkspace.ProjectDataMap(workspace);

                        var pid = map.GetOrCreateId(project, null);
                        Assert.IsNotNull(pid);

                        var pid2 = map.GetOrCreateId(project2, project);
                        Assert.AreSame(pid, pid2);
                    }
        }
        public async Task TestDataHandling_MultiTargetFramework()
        {
            var projectFile = Util.GetSampleProject("multi-target", "multi-target.csproj");

            using (var project = (DotNetProject)await Services.ProjectService.ReadSolutionItem(Util.GetMonitor(), projectFile))
                using (var workspace = await IdeApp.TypeSystemService.CreateEmptyWorkspace()) {
                    var map = new MonoDevelopWorkspace.ProjectDataMap(workspace);

                    var pid  = map.GetOrCreateId(project, null, "netcoreapp1.1");
                    var data = map.GetData(pid);

                    Assert.IsNull(data);
                    Assert.IsFalse(map.Contains(pid));

                    var pid2  = map.GetOrCreateId(project, null, "netstandard1.0");
                    var data2 = map.GetData(pid2);

                    Assert.IsNull(data2);
                    Assert.IsFalse(map.Contains(pid2));

                    data = map.ReplaceData(pid, ImmutableArray <MonoDevelopMetadataReference> .Empty, out var oldData);

                    Assert.IsNotNull(data);
                    Assert.IsTrue(map.Contains(pid));
                    Assert.IsNull(oldData);

                    data2 = map.ReplaceData(pid2, ImmutableArray <MonoDevelopMetadataReference> .Empty, out var oldData2);

                    Assert.IsNotNull(data2);
                    Assert.IsTrue(map.Contains(pid2));
                    Assert.IsNull(oldData2);

                    map.RemoveData(pid);

                    data = map.GetData(pid);

                    Assert.IsNull(data);
                    Assert.IsFalse(map.Contains(pid));
                    Assert.IsTrue(map.Contains(pid2));

                    map.RemoveData(pid2);

                    data2 = map.GetData(pid2);

                    Assert.IsNull(data2);
                    Assert.IsFalse(map.Contains(pid2));
                }
        }
        public void TestSimpleCreation()
        {
            using (var project = Services.ProjectService.CreateDotNetProject("C#"))
                using (var workspace = new MonoDevelopWorkspace(null)) {
                    var map = new MonoDevelopWorkspace.ProjectDataMap(workspace);

                    var pid = map.GetId(project);
                    Assert.IsNull(pid);

                    pid = map.GetOrCreateId(project, null);
                    Assert.IsNotNull(pid);

                    var projectInMap = map.GetMonoProject(pid);
                    Assert.AreSame(project, projectInMap);

                    map.RemoveProject(project, pid);
                    Assert.IsNull(map.GetId(project));
                }
        }
Exemple #9
0
        public async Task TestMigration_MultiTargetFramework()
        {
            var projectFile = Util.GetSampleProject("multi-target", "multi-target.csproj");

            using (var project = (DotNetProject)await Services.ProjectService.ReadSolutionItem(Util.GetMonitor(), projectFile))
                using (var project2 = (DotNetProject)await Services.ProjectService.ReadSolutionItem(Util.GetMonitor(), projectFile))
                    using (var workspace = await IdeApp.TypeSystemService.CreateEmptyWorkspace()) {
                        var map = new MonoDevelopWorkspace.ProjectDataMap(workspace);

                        var pid = map.GetOrCreateId(project, null, "netcoreapp1.1");
                        Assert.IsNotNull(pid);

                        var pid2 = map.GetOrCreateId(project, null, "netstandard1.0");
                        Assert.IsNotNull(pid2);

                        var pid3 = map.GetOrCreateId(project2, project, "netcoreapp1.1");
                        Assert.AreSame(pid, pid3);

                        var pid4 = map.GetOrCreateId(project2, project, "netstandard1.0");
                        Assert.AreSame(pid2, pid4);

                        Assert.AreNotEqual(pid, pid2);
                    }
        }
        public bool TryGetCachedItems(Project p, MonoDevelopMetadataReferenceManager provider, MonoDevelopWorkspace.ProjectDataMap projectMap,
                                      out ImmutableArray <ProjectFile> files,
                                      out ImmutableArray <FilePath> analyzers,
                                      out ImmutableArray <MonoDevelopMetadataReference> metadataReferences,
                                      out ImmutableArray <Microsoft.CodeAnalysis.ProjectReference> projectReferences)
        {
            files              = ImmutableArray <ProjectFile> .Empty;
            analyzers          = ImmutableArray <FilePath> .Empty;
            metadataReferences = ImmutableArray <MonoDevelopMetadataReference> .Empty;
            projectReferences  = ImmutableArray <Microsoft.CodeAnalysis.ProjectReference> .Empty;

            ProjectCache cachedData;

            lock (cachedItems) {
                if (!cachedItems.TryGetValue(p.FileName, out cachedData))
                {
                    return(false);
                }
            }

            var filesBuilder = ImmutableArray.CreateBuilder <ProjectFile> (cachedData.Files.Length);

            for (int i = 0; i < cachedData.Files.Length; ++i)
            {
                filesBuilder.Add(new ProjectFile(cachedData.Files [i], cachedData.BuildActions [i])
                {
                    Project = p,
                });
            }

            files = filesBuilder.MoveToImmutable();

            var analyzersBuilder = ImmutableArray.CreateBuilder <FilePath> (cachedData.Analyzers.Length);

            foreach (var analyzer in cachedData.Analyzers)
            {
                analyzersBuilder.Add(analyzer);
            }
            analyzers = analyzersBuilder.MoveToImmutable();

            var mrBuilder = ImmutableArray.CreateBuilder <MonoDevelopMetadataReference> (cachedData.MetadataReferences.Length);

            foreach (var item in cachedData.MetadataReferences)
            {
                var aliases = item.Aliases != null?item.Aliases.ToImmutableArray() : default;

                var reference = provider.GetOrCreateMetadataReference(item.FilePath, new Microsoft.CodeAnalysis.MetadataReferenceProperties(aliases: aliases));
                mrBuilder.Add(reference);
            }
            metadataReferences = mrBuilder.MoveToImmutable();

            var sol         = p.ParentSolution;
            var solConfig   = sol.GetConfiguration(IdeServices.Workspace.ActiveConfiguration);
            var allProjects = sol.GetAllProjects().ToDictionary(x => x.FileName, x => x);

            var prBuilder = ImmutableArray.CreateBuilder <Microsoft.CodeAnalysis.ProjectReference> (cachedData.ProjectReferences.Length);

            foreach (var item in cachedData.ProjectReferences)
            {
                if (!allProjects.TryGetValue(item.FilePath, out var mdProject))
                {
                    return(false);
                }

                var aliases = item.Aliases != null?item.Aliases.ToImmutableArray() : default;

                var pr = new Microsoft.CodeAnalysis.ProjectReference(projectMap.GetOrCreateId(mdProject, null), aliases.ToImmutableArray());
                prBuilder.Add(pr);
            }
            projectReferences = prBuilder.MoveToImmutable();
            return(true);
        }
 public bool TryGetCachedItems(Project p, MonoDevelopMetadataReferenceManager provider, MonoDevelopWorkspace.ProjectDataMap projectMap,
                               out ProjectCacheInfo info)
 {
     info = new ProjectCacheInfo();
     return(TryGetCachedItems(p, provider, projectMap, out info.SourceFiles, out info.AnalyzerFiles, out info.References, out info.ProjectReferences));
 }
 public void Update(ProjectConfiguration projConfig, Project proj, MonoDevelopWorkspace.ProjectDataMap projectMap, ProjectCacheInfo info)
 {
     Update(projConfig, proj, projectMap, info.SourceFiles, info.AnalyzerFiles, info.References, info.ProjectReferences);
 }
Exemple #13
0
        public void Update(ProjectConfiguration projConfig, string framework, Project proj, MonoDevelopWorkspace.ProjectDataMap projectMap,
                           ImmutableArray <ProjectFile> files,
                           ImmutableArray <FilePath> analyzers,
                           ImmutableArray <MonoDevelopMetadataReference> metadataReferences,
                           ImmutableArray <Microsoft.CodeAnalysis.ProjectReference> projectReferences)
        {
            if (!loaded)
            {
                return;
            }

            var paths   = new string [files.Length];
            var actions = new string [files.Length];

            for (int i = 0; i < files.Length; ++i)
            {
                paths [i]   = files [i].FilePath;
                actions [i] = files [i].BuildAction;
            }

            var projectRefs = new ReferenceItem [projectReferences.Length];

            for (int i = 0; i < projectReferences.Length; ++i)
            {
                var pr = projectReferences [i];
                (Project mdProject, string projectReferenceFramework) = projectMap.GetMonoProjectAndFramework(pr.ProjectId);
                projectRefs [i] = new ReferenceItem {
                    FilePath  = mdProject.FileName,
                    Aliases   = pr.Aliases.ToArray(),
                    Framework = projectReferenceFramework
                };
            }

            var item = new ProjectCache {
                Format             = format,
                Analyzers          = analyzers.Select(x => (string)x).ToArray(),
                Files              = paths,
                BuildActions       = actions,
                MetadataReferences = metadataReferences.Select(x => {
                    var ri = new ReferenceItem {
                        FilePath = x.FilePath,
                        Aliases  = x.Properties.Aliases.ToArray(),
                    };
                    return(ri);
                }).ToArray(),
                ProjectReferences = projectRefs,
            };

            string configId  = GetConfigId(projConfig);
            var    cacheFile = GetProjectCacheFile(proj, configId, framework);

            FileLock fileLock = AcquireWriteLock(cacheFile);

            try {
                lock (fileLock) {
                    var serializer = new JsonSerializer();
                    using (var fs = File.Open(cacheFile, FileMode.Create))
                        using (var sw = new StreamWriter(fs)) {
                            serializer.Serialize(sw, item);
                        }
                }
            } finally {
                ReleaseWriteLock(cacheFile, fileLock);
            }
        }
        public bool TryGetCachedItems(Project p, MonoDevelopMetadataReferenceManager provider, MonoDevelopWorkspace.ProjectDataMap projectMap, string framework,
                                      out ProjectCacheInfo info)
        {
            info = new ProjectCacheInfo();

            List <ProjectCache> cachedDataList;

            lock (cachedItems) {
                if (!cachedItems.TryGetValue(p.FileName, out cachedDataList))
                {
                    return(false);
                }
            }

            ProjectCache cachedData = cachedDataList.FirstOrDefault(cache => cache.Framework == framework);

            if (cachedData == null)
            {
                return(false);
            }

            var filesBuilder = ImmutableArray.CreateBuilder <ProjectFile> (cachedData.Files.Length);

            for (int i = 0; i < cachedData.Files.Length; ++i)
            {
                filesBuilder.Add(new ProjectFile(cachedData.Files [i], cachedData.BuildActions [i])
                {
                    Project = p,
                });
            }

            info.SourceFiles = filesBuilder.MoveToImmutable();

            info.AdditionalFiles   = ToImmutableFilePathArray(cachedData.AdditionalFiles);
            info.AnalyzerFiles     = ToImmutableFilePathArray(cachedData.Analyzers);
            info.EditorConfigFiles = ToImmutableFilePathArray(cachedData.EditorConfigFiles);

            var mrBuilder = ImmutableArray.CreateBuilder <MonoDevelopMetadataReference> (cachedData.MetadataReferences.Length);

            foreach (var item in cachedData.MetadataReferences)
            {
                var aliases = item.Aliases != null?item.Aliases.ToImmutableArray() : default;

                var reference = provider.GetOrCreateMetadataReference(item.FilePath, new Microsoft.CodeAnalysis.MetadataReferenceProperties(aliases: aliases));
                mrBuilder.Add(reference);
            }
            info.References = mrBuilder.MoveToImmutable();

            var sol         = p.ParentSolution;
            var solConfig   = sol.GetConfiguration(IdeServices.Workspace.ActiveConfiguration);
            var allProjects = sol.GetAllProjects().ToDictionary(x => x.FileName, x => x);

            var prBuilder = ImmutableArray.CreateBuilder <Microsoft.CodeAnalysis.ProjectReference> (cachedData.ProjectReferences.Length);

            foreach (var item in cachedData.ProjectReferences)
            {
                if (!allProjects.TryGetValue(item.FilePath, out var mdProject))
                {
                    return(false);
                }

                var aliases = item.Aliases != null?item.Aliases.ToImmutableArray() : default;

                var pr = new Microsoft.CodeAnalysis.ProjectReference(projectMap.GetOrCreateId(mdProject, null, item.Framework), aliases.ToImmutableArray());
                prBuilder.Add(pr);
            }
            info.ProjectReferences = prBuilder.MoveToImmutable();
            return(true);
        }
        public void Update(ProjectConfiguration projConfig, string framework, Project proj, MonoDevelopWorkspace.ProjectDataMap projectMap, ProjectCacheInfo info)
        {
            if (!loaded)
            {
                return;
            }

            var paths   = new string [info.SourceFiles.Length];
            var actions = new string [info.SourceFiles.Length];

            for (int i = 0; i < info.SourceFiles.Length; ++i)
            {
                paths [i]   = info.SourceFiles [i].FilePath;
                actions [i] = info.SourceFiles [i].BuildAction;
            }

            var projectRefs = new ReferenceItem [info.ProjectReferences.Length];

            for (int i = 0; i < info.ProjectReferences.Length; ++i)
            {
                var pr = info.ProjectReferences [i];
                (Project mdProject, string projectReferenceFramework) = projectMap.GetMonoProjectAndFramework(pr.ProjectId);
                projectRefs [i] = new ReferenceItem {
                    FilePath  = mdProject.FileName,
                    Aliases   = pr.Aliases.ToArray(),
                    Framework = projectReferenceFramework
                };
            }

            var item = new ProjectCache {
                Format             = format,
                AdditionalFiles    = info.AdditionalFiles.Select(x => (string)x).ToArray(),
                Analyzers          = info.AnalyzerFiles.Select(x => (string)x).ToArray(),
                EditorConfigFiles  = info.EditorConfigFiles.Select(x => (string)x).ToArray(),
                Files              = paths,
                BuildActions       = actions,
                MetadataReferences = info.References.Select(x => {
                    var ri = new ReferenceItem {
                        FilePath = x.FilePath,
                        Aliases  = x.Properties.Aliases.ToArray(),
                    };
                    return(ri);
                }).ToArray(),
                ProjectReferences = projectRefs,
            };

            string configId  = GetConfigId(projConfig);
            var    cacheFile = GetProjectCacheFile(proj, configId, framework);

            WriteCacheFile(item, cacheFile);
        }