Example #1
0
        private static ReferenceInfo?BuildReference(
            ProjectAssetsFile projectAssets,
            string referenceName,
            bool treatAsUsed)
        {
            var dependencyNames       = new HashSet <string>();
            var compilationAssemblies = ImmutableArray.CreateBuilder <string>();
            var referenceType         = ReferenceType.Unknown;

            var packagesPath = projectAssets.Project?.Restore?.PackagesPath ?? string.Empty;

            RoslynDebug.AssertNotNull(projectAssets.Targets);
            RoslynDebug.AssertNotNull(projectAssets.Libraries);

            foreach (var target in projectAssets.Targets.Values)
            {
                var key = target.Keys.FirstOrDefault(library => library.Split('/')[0] == referenceName);
                if (key is null ||
                    !projectAssets.Libraries.TryGetValue(key, out var library))
                {
                    continue;
                }

                var targetLibrary = target[key];

                referenceType = targetLibrary.Type switch
                {
                    "package" => ReferenceType.Package,
                    "project" => ReferenceType.Project,
                    _ => ReferenceType.Assembly
                };

                if (targetLibrary.Dependencies != null)
                {
                    dependencyNames.AddRange(targetLibrary.Dependencies.Keys);
                }

                if (targetLibrary.Compile != null)
                {
                    compilationAssemblies.AddRange(targetLibrary.Compile.Keys
                                                   .Where(assemblyPath => !assemblyPath.EndsWith(NuGetEmptyFileName))
                                                   .Select(assemblyPath => Path.GetFullPath(Path.Combine(packagesPath, library.Path, assemblyPath))));
                }
            }

            if (referenceType == ReferenceType.Unknown)
            {
                return(null);
            }

            var dependencies = dependencyNames
                               .Select(dependency => BuildReference(projectAssets, dependency, treatAsUsed: false))
                               .WhereNotNull()
                               .ToImmutableArray();

            return(new ReferenceInfo(referenceType, referenceName, treatAsUsed, compilationAssemblies.ToImmutable(), dependencies));
        }
    }
Example #2
0
        private static ReferenceInfo?BuildReference(
            ProjectAssetsFile projectAssets,
            Dictionary <string, ProjectAssetsTargetLibrary> target,
            string dependency,
            bool treatAsUsed)
        {
            var key = target.Keys.FirstOrDefault(library => library.Split('/')[0] == dependency);

            if (key is null)
            {
                return(null);
            }

            return(BuildReference(projectAssets, target, dependency, treatAsUsed, key, target[key]));
        }
Example #3
0
        private static ReferenceInfo?BuildReference(
            ProjectAssetsFile projectAssets,
            ReferenceInfo referenceInfo,
            ImmutableHashSet <string> autoReferences)
        {
            var referenceName = referenceInfo.ReferenceType == ReferenceType.Project
                ? Path.GetFileNameWithoutExtension(referenceInfo.ItemSpecification)
                : referenceInfo.ItemSpecification;

            if (autoReferences.Contains(referenceName))
            {
                return(null);
            }

            return(BuildReference(projectAssets, referenceName, referenceInfo.TreatAsUsed));
        }
Example #4
0
        private static ReferenceInfo?BuildReference(
            ProjectAssetsFile projectAssets,
            Dictionary <string, ProjectAssetsTargetLibrary> target,
            string referenceName,
            bool treatAsUsed,
            string key,
            ProjectAssetsTargetLibrary targetLibrary)
        {
            if (projectAssets.Libraries is null ||
                !projectAssets.Libraries.TryGetValue(key, out var library))
            {
                return(null);
            }

            var type = targetLibrary.Type switch
            {
                "package" => ReferenceType.Package,
                "project" => ReferenceType.Project,
                _ => ReferenceType.Assembly
            };

            var dependencies = targetLibrary.Dependencies != null
                ? targetLibrary.Dependencies.Keys
                               .Select(dependency => BuildReference(projectAssets, target, dependency, treatAsUsed: false))
                               .WhereNotNull()
                               .ToImmutableArray()
                : ImmutableArray <ReferenceInfo> .Empty;

            var packagesPath          = projectAssets.Project?.Restore?.PackagesPath ?? string.Empty;
            var compilationAssemblies = targetLibrary.Compile != null
                ? targetLibrary.Compile.Keys
                                        .Where(assemblyPath => !assemblyPath.EndsWith(NuGetEmptyFileName))
                                        .Select(assemblyPath => Path.GetFullPath(Path.Combine(packagesPath, library.Path, assemblyPath)))
                                        .ToImmutableArray()
                : ImmutableArray <string> .Empty;

            return(new ReferenceInfo(type, referenceName, treatAsUsed, compilationAssemblies, dependencies));
        }
    }
Example #5
0
        internal static ImmutableArray <ReferenceInfo> ReadReferences(
            ImmutableArray <ReferenceInfo> projectReferences,
            ProjectAssetsFile projectAssets)
        {
            if (projectAssets is null ||
                projectAssets.Version != 3)
            {
                return(ImmutableArray <ReferenceInfo> .Empty);
            }

            if (projectAssets.Targets is null ||
                projectAssets.Targets.Count == 0)
            {
                return(ImmutableArray <ReferenceInfo> .Empty);
            }

            if (projectAssets.Libraries is null ||
                projectAssets.Libraries.Count == 0)
            {
                return(ImmutableArray <ReferenceInfo> .Empty);
            }

            var autoReferences = projectAssets.Project?.Frameworks?.Values
                                 .Where(framework => framework.Dependencies != null)
                                 .SelectMany(framework => framework.Dependencies !.Keys.Where(key => framework.Dependencies[key].AutoReferenced))
                                 .Distinct()
                                 .ToImmutableHashSet();

            autoReferences ??= ImmutableHashSet <string> .Empty;

            var references = projectReferences
                             .Select(projectReference => BuildReference(projectAssets, projectReference, autoReferences))
                             .WhereNotNull()
                             .ToImmutableArray();

            return(references);
        }