Пример #1
0
        private void AddDefaultClrMetadataReferences(HashSet <MetadataReference> commonReferences)
        {
            var assemblies = new[]
            {
                typeof(object).GetTypeInfo().Assembly,
                typeof(Enumerable).GetTypeInfo().Assembly,
                typeof(Stack <>).GetTypeInfo().Assembly,
                typeof(Lazy <,>).GetTypeInfo().Assembly,
                FromName("System.Runtime"),
                FromName("mscorlib")
            };

            var references = assemblies
                             .Where(a => a != null)
                             .Select(a => a.Location)
                             .Distinct()
                             .Select(l => _metadataFileReferenceCache.GetMetadataReference(l));

            foreach (var reference in references)
            {
                commonReferences.Add(reference);
            }

            Assembly FromName(string assemblyName)
            {
                try
                {
                    return(Assembly.Load(new AssemblyName(assemblyName)));
                }
                catch
                {
                    return(null);
                }
            }
        }
Пример #2
0
        private void AddDefaultClrMetadataReferences(ProjectContext projectContext, HashSet <MetadataReference> commonReferences)
        {
            if (projectContext == null || projectContext.TargetFramework?.Framework != ".NETCoreApp")
            {
                var assemblies = new[]
                {
                    typeof(object).GetTypeInfo().Assembly,
                    typeof(Enumerable).GetTypeInfo().Assembly,
                    typeof(Stack <>).GetTypeInfo().Assembly,
                    typeof(Lazy <,>).GetTypeInfo().Assembly,
                    _assemblyLoader.Load("System.Runtime"),
                    _assemblyLoader.Load("mscorlib")
                };

                var references = assemblies
                                 .Where(a => a != null)
                                 .Select(a => a.Location)
                                 .Distinct()
                                 .Select(l => _metadataFileReferenceCache.GetMetadataReference(l));

                foreach (var reference in references)
                {
                    commonReferences.Add(reference);
                }
            }
        }
Пример #3
0
        private void AddDefaultClrMetadataReferences(HashSet <MetadataReference> commonReferences)
        {
            var references = DefaultMetadataReferenceHelper.GetDefaultMetadataReferenceLocations()
                             .Select(l => _metadataFileReferenceCache.GetMetadataReference(l));

            foreach (var reference in references)
            {
                commonReferences.Add(reference);
            }
        }
Пример #4
0
        private void UpdateReferences(Project project, ImmutableArray <string> references)
        {
            var existingReferences = new HashSet <MetadataReference>(project.MetadataReferences);
            var addedReferences    = new HashSet <MetadataReference>();

            foreach (var referencePath in references)
            {
                if (!File.Exists(referencePath))
                {
                    _logger.LogWarning($"Unable to resolve assembly '{referencePath}'");
                }
                else
                {
                    var metadataReference = _metadataFileReferenceCache.GetMetadataReference(referencePath);

                    if (existingReferences.Remove(metadataReference))
                    {
                        continue;
                    }

                    if (!addedReferences.Contains(metadataReference))
                    {
                        _logger.LogDebug($"Adding reference '{referencePath}' to '{project.Name}'.");
                        _workspace.AddMetadataReference(project.Id, metadataReference);
                        addedReferences.Add(metadataReference);
                    }
                }
            }

            foreach (var existingReference in existingReferences)
            {
                _workspace.RemoveMetadataReference(project.Id, existingReference);
            }
        }
Пример #5
0
        private IEnumerable <MetadataReference> GetMetadataReferences(IEnumerable <string> references)
        {
            foreach (var reference in references)
            {
                if (!File.Exists(reference))
                {
                    _logger.LogWarning($"Unable to create MetadataReference. File {reference} does not exist.");
                    continue;
                }

                yield return(_metadataReferenceCache.GetMetadataReference(reference));
            }
        }
        private void UpdateFileReferences(ProjectState state, IEnumerable <string> fileReferences)
        {
            var metadataReferences     = new List <MetadataReference>();
            var fileReferencesToRemove = state.FileMetadataReferences.Keys.ToHashSet();

            foreach (var fileReference in fileReferences)
            {
                if (!File.Exists(fileReference))
                {
                    _logger.LogWarning($"    Could not resolve assembly: {fileReference}");
                    continue;
                }

                if (fileReferencesToRemove.Remove(fileReference))
                {
                    continue;
                }

                var metadataReference = _metadataFileReferenceCache.GetMetadataReference(fileReference);
                state.FileMetadataReferences[fileReference] = metadataReference;
                metadataReferences.Add(metadataReference);
                _logger.LogDebug($"    Add file reference {fileReference}");
            }

            foreach (var reference in metadataReferences)
            {
                _workspace.AddMetadataReference(state.Id, reference);
            }

            foreach (var reference in fileReferencesToRemove)
            {
                var toRemove = state.FileMetadataReferences[reference];
                state.FileMetadataReferences.Remove(reference);
                _workspace.RemoveMetadataReference(state.Id, toRemove);
                _logger.LogDebug($"    Remove file reference {reference}");
            }

            if (metadataReferences.Count != 0 || fileReferencesToRemove.Count != 0)
            {
                _logger.LogInformation($"    Added {metadataReferences.Count} and removed {fileReferencesToRemove.Count} file references");
            }
        }
        private void AddMetadataReference(ISet <MetadataReference> referenceCollection, string fileReference)
        {
            if (!File.Exists(fileReference))
            {
                _logger.LogWarning($"Couldn't add reference to '{fileReference}' because the file was not found.");
                return;
            }

            var metadataReference = _metadataFileReferenceCache.GetMetadataReference(fileReference);

            if (metadataReference == null)
            {
                _logger.LogWarning($"Couldn't add reference to '{fileReference}' because the loaded metadata reference was null.");
                return;
            }

            referenceCollection.Add(metadataReference);
            _assemblyReferences.Add(fileReference);
            _logger.LogDebug($"Added reference to '{fileReference}'");
        }