Beispiel #1
0
        public string GetAssemblyPath(string buildConfiguration)
        {
            var compilationOptions = _project.GetCompilerOptions(_framework, buildConfiguration);
            var outputExtension    = FileNameSuffixes.DotNet.DynamicLib;

            if (_framework.IsDesktop() && compilationOptions.EmitEntryPoint.GetValueOrDefault())
            {
                outputExtension = FileNameSuffixes.DotNet.Exe;
            }

            return(Path.Combine(
                       GetOutputDirectoryPath(buildConfiguration),
                       _project.Name + outputExtension));
        }
Beispiel #2
0
        private static CompilationSettings ToCompilationSettings(CommonCompilerOptions compilerOptions,
                                                                 NuGetFramework targetFramework,
                                                                 string projectDirectory)
        {
            var options = GetCompilationOptions(compilerOptions, projectDirectory);

            options = options.WithSpecificDiagnosticOptions(compilerOptions.SuppressWarnings.ToDictionary(
                                                                suppress => suppress, _ => ReportDiagnostic.Suppress));

            AssemblyIdentityComparer assemblyIdentityComparer =
                targetFramework.IsDesktop() ?
                DesktopAssemblyIdentityComparer.Default :
                null;

            options = options.WithAssemblyIdentityComparer(assemblyIdentityComparer);

            LanguageVersion languageVersion;

            if (!Enum.TryParse <LanguageVersion>(value: compilerOptions.LanguageVersion,
                                                 ignoreCase: true,
                                                 result: out languageVersion))
            {
                languageVersion = LanguageVersion.CSharp6;
            }

            var settings = new CompilationSettings
            {
                LanguageVersion    = languageVersion,
                Defines            = compilerOptions.Defines ?? Enumerable.Empty <string>(),
                CompilationOptions = options
            };

            return(settings);
        }
Beispiel #3
0
        private static FrameworkInformation GetFrameworkInformation(NuGetFramework targetFramework, string referenceAssembliesPath)
        {
            // Check for legacy frameworks
            if (targetFramework.IsDesktop() && targetFramework.Version <= new Version(3, 5))
            {
                return(GetLegacyFrameworkInformation(targetFramework, referenceAssembliesPath));
            }

            var basePath = Path.Combine(referenceAssembliesPath,
                                        targetFramework.Framework,
                                        "v" + GetDisplayVersion(targetFramework));

            if (!string.IsNullOrEmpty(targetFramework.Profile))
            {
                basePath = Path.Combine(basePath, "Profile", targetFramework.Profile);
            }

            var version = new DirectoryInfo(basePath);

            if (!version.Exists)
            {
                return(null);
            }

            return(GetFrameworkInformation(version, targetFramework));
        }
        private IEnumerable <string> GetAssembliesForFramework(LocalPackageInfo package, NuGetFramework framework, IEnumerable <string> files)
        {
            var contentItems = new NuGet.ContentModel.ContentItemCollection();
            HashSet <string> referenceFilter = null;

            contentItems.Load(files);

            // This will throw an appropriate error if the nuspec is missing
            var            nuspec = package.Nuspec;
            IList <string> compileTimeAssemblies = null;
            IList <string> runtimeAssemblies     = null;

            var referenceSet = nuspec.GetReferenceGroups().GetNearest(framework);

            if (referenceSet != null)
            {
                referenceFilter = new HashSet <string>(referenceSet.Items, StringComparer.OrdinalIgnoreCase);
            }

            var conventions     = new ManagedCodeConventions(null);
            var managedCriteria = conventions.Criteria.ForFramework(framework);
            var compileGroup    = contentItems.FindBestItemGroup(managedCriteria, conventions.Patterns.CompileAssemblies, conventions.Patterns.RuntimeAssemblies);

            if (compileGroup != null)
            {
                compileTimeAssemblies = compileGroup.Items.Select(t => t.Path).ToList();
            }

            var runtimeGroup = contentItems.FindBestItemGroup(managedCriteria, conventions.Patterns.RuntimeAssemblies);

            if (runtimeGroup != null)
            {
                runtimeAssemblies = runtimeGroup.Items.Select(p => p.Path).ToList();
            }

            // COMPAT: Support lib/contract so older packages can be consumed
            var contractPath = "lib/contract/" + package.Id + ".dll";
            var hasContract  = files.Any(path => path == contractPath);
            var hasLib       = runtimeAssemblies?.Any();

            if (hasContract &&
                hasLib.HasValue &&
                hasLib.Value &&
                !framework.IsDesktop())
            {
                compileTimeAssemblies.Clear();
                compileTimeAssemblies.Add(contractPath);
            }

            // Apply filters from the <references> node in the nuspec
            if (referenceFilter != null)
            {
                // Remove anything that starts with "lib/" and is NOT specified in the reference filter.
                // runtimes/* is unaffected (it doesn't start with lib/)
                compileTimeAssemblies = compileTimeAssemblies.Where(p => !p.StartsWith("lib/") || referenceFilter.Contains(Path.GetFileName(p))).ToList();
            }

            return(compileTimeAssemblies ?? Enumerable.Empty <string>());
        }
Beispiel #5
0
        private string GetProjectOutputName(NuGetFramework framework)
        {
            var compilationOptions = Project.GetCompilerOptions(framework, Configuration);
            var outputExtension    = ".dll";

            if (framework.IsDesktop() && compilationOptions.EmitEntryPoint.GetValueOrDefault())
            {
                outputExtension = ".exe";
            }

            return(Project.Name + outputExtension);
        }
 private void AutoInjectImplicitProjectJsonAssemblyReferences(NuGetFramework framework,
                                                              IList <ProjectLibraryDependency> packageDependencies)
 {
     if (framework?.IsDesktop() ?? false)
     {
         InjectAssemblyReferenceIfNotPresent("System", packageDependencies);
         if (framework.Version >= new Version(4, 0))
         {
             InjectAssemblyReferenceIfNotPresent("Microsoft.CSharp", packageDependencies);
         }
     }
 }
Beispiel #7
0
        private static string GetProjectOutput(Project project, NuGetFramework framework, string configuration, string outputPath)
        {
            var compilationOptions = project.GetCompilerOptions(framework, configuration);
            var outputExtension    = ".dll";

            if (framework.IsDesktop() && compilationOptions.EmitEntryPoint.GetValueOrDefault())
            {
                outputExtension = ".exe";
            }

            return(Path.Combine(outputPath, project.Name + outputExtension));
        }
Beispiel #8
0
        private static IList <LibraryDependency> GetDependencies(NuGetFramework targetFramework,
                                                                 PackageDependencyGroup dependencies,
                                                                 FrameworkSpecificGroup frameworkAssemblies)
        {
            var libraryDependencies = new List <LibraryDependency>();

            if (dependencies != null)
            {
                foreach (var d in dependencies.Packages)
                {
                    libraryDependencies.Add(new LibraryDependency
                    {
                        LibraryRange = new LibraryRange
                        {
                            Name         = d.Id,
                            VersionRange = d.VersionRange
                        }
                    });
                }
            }

            if (frameworkAssemblies == null)
            {
                return(libraryDependencies);
            }

            if (!targetFramework.IsDesktop())
            {
                // REVIEW: This isn't 100% correct since none *can* mean
                // any in theory, but in practice it means .NET full reference assembly
                // If there's no supported target frameworks and we're not targeting
                // the desktop framework then skip it.

                // To do this properly we'll need all reference assemblies supported
                // by each supported target framework which isn't always available.
                return(libraryDependencies);
            }

            foreach (var name in frameworkAssemblies.Items)
            {
                libraryDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = name,
                        TypeConstraint = LibraryTypes.Reference
                    }
                });
            }

            return(libraryDependencies);
        }
Beispiel #9
0
        /// <summary>
        /// COMPAT: Support lib/contract so older packages can be consumed
        /// </summary>
        private static void ApplyLibContract(LocalPackageInfo package, LockFileTargetLibrary lockFileLib, NuGetFramework framework, IList <string> files)
        {
            if (lockFileLib.RuntimeAssemblies.Count > 0 && !framework.IsDesktop())
            {
                var contractPath = "lib/contract/" + package.Id + ".dll";

                if (files.Any(path => path == contractPath))
                {
                    lockFileLib.CompileTimeAssemblies.Clear();
                    lockFileLib.CompileTimeAssemblies.Add(new LockFileItem(contractPath));
                }
            }
        }
        public ProjectDescription GetDescription(NuGetFramework targetFramework, Project project, LockFileTargetLibrary targetLibrary)
        {
            // This never returns null
            var targetFrameworkInfo = project.GetTargetFramework(targetFramework);
            var dependencies        = new List <LibraryRange>(targetFrameworkInfo.Dependencies);

            // Add all of the project's dependencies
            dependencies.AddRange(project.Dependencies);

            if (targetFramework != null && targetFramework.IsDesktop())
            {
                dependencies.Add(new LibraryRange("mscorlib", LibraryType.ReferenceAssembly, LibraryDependencyType.Build));

                dependencies.Add(new LibraryRange("System", LibraryType.ReferenceAssembly, LibraryDependencyType.Build));

                if (targetFramework.Version >= new Version(3, 5))
                {
                    dependencies.Add(new LibraryRange("System.Core", LibraryType.ReferenceAssembly, LibraryDependencyType.Build));

                    if (targetFramework.Version >= new Version(4, 0))
                    {
                        if (!dependencies.Any(dep => string.Equals(dep.Name, "Microsoft.CSharp", StringComparison.OrdinalIgnoreCase)))
                        {
                            dependencies.Add(new LibraryRange("Microsoft.CSharp", LibraryType.ReferenceAssembly, LibraryDependencyType.Build));
                        }
                    }
                }
            }

            if (targetLibrary != null)
            {
                // The lock file entry might have a filtered set of dependencies
                var lockFileDependencies = targetLibrary.Dependencies.ToDictionary(d => d.Id);

                // Remove all non-framework dependencies that don't appear in the lock file entry
                dependencies.RemoveAll(m => !lockFileDependencies.ContainsKey(m.Name) && m.Target != LibraryType.ReferenceAssembly);
            }

            // Mark the library as unresolved if there were specified frameworks
            // and none of them resolved
            bool unresolved = targetFrameworkInfo.FrameworkName == null;

            return(new ProjectDescription(
                       new LibraryRange(project.Name, LibraryType.Unspecified),
                       project,
                       dependencies,
                       targetFrameworkInfo,
                       !unresolved));
        }
Beispiel #11
0
        public ProjectDescription GetDescription(NuGetFramework targetFramework, Project project, LockFileTargetLibrary targetLibrary)
        {
            // This never returns null
            var targetFrameworkInfo = project.GetTargetFramework(targetFramework);
            var dependencies        = new List <ProjectLibraryDependency>(targetFrameworkInfo.Dependencies);

            // Add all of the project's dependencies
            dependencies.AddRange(project.Dependencies);

            if (targetFramework != null && targetFramework.IsDesktop())
            {
                AddIfMissing(dependencies, "mscorlib");
                AddIfMissing(dependencies, "System");

                if (targetFramework.Version >= new Version(3, 5))
                {
                    AddIfMissing(dependencies, "System.Core");
                    if (targetFramework.Version >= new Version(4, 0))
                    {
                        AddIfMissing(dependencies, "Microsoft.CSharp");
                    }
                }
            }

            if (targetLibrary != null)
            {
                // The lock file entry might have a filtered set of dependencies
                var lockFileDependencies = targetLibrary.Dependencies.ToDictionary(
                    d => d.Id,
                    StringComparer.OrdinalIgnoreCase);

                // Remove all non-framework dependencies that don't appear in the lock file entry
                dependencies.RemoveAll(m =>
                                       !lockFileDependencies.ContainsKey(m.Name) &&
                                       m.LibraryRange.TypeConstraint != LibraryDependencyTarget.Reference);
            }

            // Mark the library as unresolved if there were specified frameworks
            // and none of them resolved
            bool unresolved = targetFrameworkInfo.FrameworkName == null;

            return(new ProjectDescription(
                       new LibraryRange(project.Name, LibraryDependencyTarget.All),
                       project,
                       dependencies,
                       targetFrameworkInfo,
                       !unresolved));
        }
Beispiel #12
0
        private ProjectContext GetCompatibleStartupProjectContext(string startupProjectPath, NuGetFramework projectFramework)
        {
            var startupProject = ProjectReader.GetProject(startupProjectPath);
            var frameworks     = startupProject.GetTargetFrameworks()
                                 .Select(f => f.FrameworkName);

            var startupFramework = frameworks.FirstOrDefault(f => f.Equals(projectFramework));

            if (startupFramework == null)
            {
                if (projectFramework.IsDesktop())
                {
                    startupFramework = frameworks.FirstOrDefault(f => f.IsDesktop())
                                       ?? NuGetFrameworkUtility.GetNearest(
                        frameworks,
                        FrameworkConstants.CommonFrameworks.NetStandard15,
                        f => f);
                }
                else
                {
                    startupFramework = NuGetFrameworkUtility.GetNearest(
                        frameworks,
                        FrameworkConstants.CommonFrameworks.NetCoreApp10,
                        f => f)
                                       // TODO remove fallback to dnxcore50
                                       ?? NuGetFrameworkUtility.GetNearest(
                        frameworks,
                        FrameworkConstants.CommonFrameworks.DnxCore50,
                        f => f);
                }
            }

            if (startupFramework == null)
            {
                throw new OperationException(
                          ToolsCliStrings.IncompatibleStartupProject(startupProject.Name, projectFramework.GetShortFolderName()));
            }

            Reporter.Verbose.WriteLine(
                ToolsCliStrings.LogUsingFramework(startupFramework.GetShortFolderName(), startupProject.Name).Bold().Black());

            return(new ProjectContextBuilder()
                   .WithProject(startupProject)
                   .WithTargetFramework(startupFramework)
                   .WithRuntimeIdentifiers(PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers())
                   .Build());
        }
        public CompilationOutputFiles(
            string basePath,
            Project project,
            string configuration,
            NuGetFramework framework)
        {
            BasePath        = basePath;
            Project         = project;
            Configuration   = configuration;
            Framework       = framework;
            OutputExtension = FileNameSuffixes.DotNet.DynamicLib;

            var compilerOptions = Project.GetCompilerOptions(framework, configuration);

            if (framework.IsDesktop() && compilerOptions.EmitEntryPoint.GetValueOrDefault())
            {
                OutputExtension = FileNameSuffixes.DotNet.Exe;
            }
        }
        private static CompilationSettings ToCompilationSettings(CommonCompilerOptions compilerOptions,
                                                                 NuGetFramework targetFramework,
                                                                 string projectDirectory)
        {
            var options = GetCompilationOptions(compilerOptions, projectDirectory);

            // Disable 1702 until roslyn turns this off by default
            options = options.WithSpecificDiagnosticOptions(new Dictionary <string, ReportDiagnostic>
            {
                { "CS1701", ReportDiagnostic.Suppress }, // Binding redirects
                { "CS1702", ReportDiagnostic.Suppress },
                { "CS1705", ReportDiagnostic.Suppress }
            });

            AssemblyIdentityComparer assemblyIdentityComparer =
                targetFramework.IsDesktop() ?
                DesktopAssemblyIdentityComparer.Default :
                null;

            options = options.WithAssemblyIdentityComparer(assemblyIdentityComparer);

            LanguageVersion languageVersion;

            if (!Enum.TryParse <LanguageVersion>(value: compilerOptions.LanguageVersion,
                                                 ignoreCase: true,
                                                 result: out languageVersion))
            {
                languageVersion = LanguageVersion.CSharp6;
            }

            var settings = new CompilationSettings
            {
                LanguageVersion    = languageVersion,
                Defines            = compilerOptions.Defines ?? Enumerable.Empty <string>(),
                CompilationOptions = options
            };

            return(settings);
        }
Beispiel #15
0
        public ProjectDescription GetDescription(NuGetFramework targetFramework, Project project)
        {
            // This never returns null
            var targetFrameworkInfo         = project.GetTargetFramework(targetFramework);
            var targetFrameworkDependencies = new List <LibraryRange>(targetFrameworkInfo.Dependencies);

            if (targetFramework != null && targetFramework.IsDesktop())
            {
                targetFrameworkDependencies.Add(new LibraryRange("mscorlib", LibraryType.ReferenceAssembly));

                targetFrameworkDependencies.Add(new LibraryRange("System", LibraryType.ReferenceAssembly));

                if (targetFramework.Version >= new Version(3, 5))
                {
                    targetFrameworkDependencies.Add(new LibraryRange("System.Core", LibraryType.ReferenceAssembly));

                    if (targetFramework.Version >= new Version(4, 0))
                    {
                        targetFrameworkDependencies.Add(new LibraryRange("Microsoft.CSharp", LibraryType.ReferenceAssembly));
                    }
                }
            }

            var dependencies = project.Dependencies.Concat(targetFrameworkDependencies).ToList();

            // Mark the library as unresolved if there were specified frameworks
            // and none of them resolved
            bool unresolved = targetFrameworkInfo.FrameworkName == null;

            return(new ProjectDescription(
                       new LibraryRange(project.Name, LibraryType.Unspecified),
                       project,
                       dependencies,
                       targetFrameworkInfo,
                       !unresolved));
        }
Beispiel #16
0
        private ICommandResolver GetProjectDependenciesCommandResolver(NuGetFramework framework)
        {
            var environment = new EnvironmentProvider();

            if (framework.IsDesktop())
            {
                IPlatformCommandSpecFactory platformCommandSpecFactory = null;
                if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Windows)
                {
                    platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
                }
                else
                {
                    platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
                }

                return(new OutputPathCommandResolver(environment, platformCommandSpecFactory));
            }
            else
            {
                var packagedCommandSpecFactory = new PackagedCommandSpecFactory();
                return(new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory));
            }
        }
        public LibraryDescription GetDescription(LibraryRange libraryRange, NuGetFramework targetFramework)
        {
            if (libraryRange.IsGacOrFrameworkReference)
            {
                return(null);
            }

            string name = libraryRange.Name;

            Project project;

            // Can't find a project file with the name so bail
            if (!_projectResolver.TryResolveProject(name, out project))
            {
                return(null);
            }

            // This never returns null
            var targetFrameworkInfo         = project.GetTargetFramework(targetFramework);
            var targetFrameworkDependencies = targetFrameworkInfo.Dependencies;

            if (targetFramework.IsDesktop())
            {
                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name = "mscorlib",
                        IsGacOrFrameworkReference = true
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name = "System",
                        IsGacOrFrameworkReference = true
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name = "System.Core",
                        IsGacOrFrameworkReference = true
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name = "Microsoft.CSharp",
                        IsGacOrFrameworkReference = true
                    }
                });
            }

            var dependencies = project.Dependencies.Concat(targetFrameworkDependencies).ToList();

            // Mark the library as unresolved if there were specified frameworks
            // and none of them resolved
            bool unresolved = targetFrameworkInfo.FrameworkName == null &&
                              project.TargetFrameworks.Any();

            return(new LibraryDescription
            {
                LibraryRange = libraryRange,
                Identity = new Library
                {
                    Name = project.Name,
                    Version = project.Version
                },
                Path = project.ProjectFilePath,
                Dependencies = dependencies,
                Resolved = !unresolved
            });
        }
        public static bool TryPackageOnlyTagHelperResolution(
            CommandArgument assemblyNamesArgument,
            CommandOption protocolOption,
            CommandOption buildBasePathOption,
            CommandOption configurationOption,
            Project project,
            NuGetFramework framework,
            out int exitCode)
        {
            exitCode = 0;

            if (framework.IsDesktop())
            {
                return(false);
            }

            var runtimeIdentifiers = RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers();
            var projectContext     = new ProjectContextBuilder()
                                     .WithProject(project)
                                     .WithTargetFramework(framework)
                                     .WithRuntimeIdentifiers(runtimeIdentifiers)
                                     .Build();
            var configuration = configurationOption.Value() ?? Constants.DefaultConfiguration;

            var projectRuntimeOutputPath = projectContext.GetOutputPaths(configuration, buildBasePathOption.Value())?.RuntimeOutputPath;
            var projectAssemblyName      = project.GetCompilerOptions(framework, configuration).OutputName;
            var projectOutputAssembly    = Path.Combine(projectRuntimeOutputPath, projectAssemblyName + ".dll");

            if (File.Exists(projectOutputAssembly))
            {
                // There's already build output. Dispatch to build output; this ensures dependencies have been resolved.
                return(false);
            }

            var projectLibraries = projectContext.LibraryManager.GetLibraries();
            var libraryLookup    = projectLibraries.ToDictionary(
                library => library.Identity.Name,
                library => library,
                StringComparer.Ordinal);

            foreach (var assemblyName in assemblyNamesArgument.Values)
            {
                if (!IsPackageOnly(assemblyName, libraryLookup))
                {
                    return(false);
                }
            }

            var loadContext = projectContext.CreateLoadContext(
                projectContext.RuntimeIdentifier,
                configuration: "Debug",
                outputPath: null);
            var runner = new PackageOnlyResolveTagHelpersRunCommand(loadContext)
            {
                AssemblyNamesArgument = assemblyNamesArgument,
                ProtocolOption        = protocolOption
            };

            exitCode = runner.OnExecute();

            return(true);
        }
        public Library GetLibrary(LibraryRange libraryRange, NuGetFramework targetFramework)
        {
            var name = libraryRange.Name;

            PackageSpec packageSpec;

            // Can't find a project file with the name so bail
            if (!_resolver.TryResolvePackageSpec(name, out packageSpec))
            {
                return(null);
            }

            // This never returns null
            var targetFrameworkInfo         = packageSpec.GetTargetFramework(targetFramework);
            var targetFrameworkDependencies = targetFrameworkInfo.Dependencies;

            if (targetFramework.IsDesktop())
            {
                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = "mscorlib",
                        TypeConstraint = LibraryTypes.Reference
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = "System",
                        TypeConstraint = LibraryTypes.Reference
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = "System.Core",
                        TypeConstraint = LibraryTypes.Reference
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = "Microsoft.CSharp",
                        TypeConstraint = LibraryTypes.Reference
                    }
                });
            }

            var dependencies = packageSpec.Dependencies.Concat(targetFrameworkDependencies).ToList();

            if (string.Equals(_projectReference?.Name, libraryRange.Name, StringComparison.OrdinalIgnoreCase))
            {
                dependencies.AddRange(_projectReference.ExternalProjectReferences
                                      .Select(reference => new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = reference,
                        VersionRange   = VersionRange.All,
                        TypeConstraint = LibraryTypes.ExternalProject
                    }
                }));
            }

            // Mark the library as unresolved if there were specified frameworks
            // and none of them resolved
            var unresolved = targetFrameworkInfo.FrameworkName == null &&
                             packageSpec.TargetFrameworks.Any();

            var library = new Library
            {
                LibraryRange = libraryRange,
                Identity     = new LibraryIdentity
                {
                    Name    = packageSpec.Name,
                    Version = packageSpec.Version,
                    Type    = LibraryTypes.Project,
                },
                Path         = packageSpec.FilePath,
                Dependencies = dependencies,
                Resolved     = !unresolved,
                [KnownLibraryProperties.PackageSpec] = packageSpec
            };

            return(library);
        }
Beispiel #20
0
        private IEnumerable <LibraryDependency> GetDependencies(LocalPackageInfo package, NuGetFramework targetFramework)
        {
            NuspecReader nuspecReader = null;

            using (var stream = File.OpenRead(package.ManifestPath))
            {
                nuspecReader = new NuspecReader(stream);
            }

            var reducer = new FrameworkReducer();

            var deps = nuspecReader.GetDependencyGroups()
                       .ToDictionary(g => new NuGetFramework(g.TargetFramework),
                                     g => g.Packages);


            var nearest = reducer.GetNearest(targetFramework, deps.Keys);

            if (nearest != null)
            {
                foreach (var d in deps[nearest])
                {
                    yield return(new LibraryDependency
                    {
                        LibraryRange = new LibraryRange
                        {
                            Name = d.Id,
                            VersionRange = d.VersionRange == null ? null : new NuGetVersionRange(d.VersionRange)
                        }
                    });
                }
            }

            // TODO: Remove this when we do #596
            // ASP.NET Core isn't compatible with generic PCL profiles
            //if (string.Equals(targetFramework.Identifier, VersionUtility.AspNetCoreFrameworkIdentifier, StringComparison.OrdinalIgnoreCase))
            //{
            //    yield break;
            //}

            var frameworks = nuspecReader.GetFrameworkReferenceGroups()
                             .ToDictionary(f => f.TargetFramework,
                                           f => f.Items);

            nearest = reducer.GetNearest(targetFramework, frameworks.Keys) ?? frameworks.Keys.FirstOrDefault(f => f.AnyPlatform);

            if (nearest != null)
            {
                if (nearest.AnyPlatform && !targetFramework.IsDesktop())
                {
                    // REVIEW: This isn't 100% correct since none *can* mean
                    // any in theory, but in practice it means .NET full reference assembly
                    // If there's no supported target frameworks and we're not targeting
                    // the desktop framework then skip it.

                    // To do this properly we'll need all reference assemblies supported
                    // by each supported target framework which isn't always available.
                    yield break;
                }

                foreach (var name in frameworks[nearest])
                {
                    yield return(new LibraryDependency
                    {
                        LibraryRange = new LibraryRange
                        {
                            Name = name,
                            IsGacOrFrameworkReference = true
                        }
                    });
                }
            }
        }
Beispiel #21
0
        public Library GetDescription(LibraryRange libraryRange, NuGetFramework targetFramework)
        {
            string name = libraryRange.Name;

            PackageSpec packageSpec;

            // Can't find a project file with the name so bail
            if (!_resolver.TryResolvePackageSpec(name, out packageSpec))
            {
                return(null);
            }

            // This never returns null
            var targetFrameworkInfo         = packageSpec.GetTargetFramework(targetFramework);
            var targetFrameworkDependencies = targetFrameworkInfo.Dependencies;

            if (targetFramework.IsDesktop())
            {
                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = "mscorlib",
                        TypeConstraint = LibraryTypes.Reference
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = "System",
                        TypeConstraint = LibraryTypes.Reference
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = "System.Core",
                        TypeConstraint = LibraryTypes.Reference
                    }
                });

                targetFrameworkDependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name           = "Microsoft.CSharp",
                        TypeConstraint = LibraryTypes.Reference
                    }
                });
            }

            var dependencies = packageSpec.Dependencies.Concat(targetFrameworkDependencies).ToList();

            // Mark the library as unresolved if there were specified frameworks
            // and none of them resolved
            bool unresolved = targetFrameworkInfo.FrameworkName == null &&
                              packageSpec.TargetFrameworks.Any();

            var library = new Library
            {
                LibraryRange = libraryRange,
                Identity     = new LibraryIdentity
                {
                    Name    = packageSpec.Name,
                    Version = packageSpec.Version,
                    Type    = LibraryTypes.Project,
                },
                Path         = packageSpec.FilePath,
                Dependencies = dependencies,
                Resolved     = !unresolved,

                [KnownLibraryProperties.PackageSpec] = packageSpec
            };

            return(library);
        }
        private static Library GetLibrary(LibraryExport export,
                                          NuGetFramework target,
                                          string configuration,
                                          bool runtime,
                                          IDictionary <string, Dependency> dependencyLookup)
        {
            var type = export.Library.Identity.Type.Value.ToLowerInvariant();

            var serviceable         = (export.Library as PackageDescription)?.Library.IsServiceable ?? false;
            var libraryDependencies = new List <Dependency>();

            var libraryAssets = runtime ? export.RuntimeAssemblies : export.CompilationAssemblies;

            foreach (var libraryDependency in export.Library.Dependencies)
            {
                Dependency dependency;
                if (dependencyLookup.TryGetValue(libraryDependency.Name, out dependency))
                {
                    libraryDependencies.Add(dependency);
                }
            }

            string[] assemblies;
            if (type == "project")
            {
                var isExe = ((ProjectDescription)export.Library)
                            .Project
                            .GetCompilerOptions(target, configuration)
                            .EmitEntryPoint
                            .GetValueOrDefault(false);

                isExe &= target.IsDesktop();

                assemblies = new[] { export.Library.Identity.Name + (isExe ? ".exe" : ".dll") };
            }
            else
            {
                assemblies = libraryAssets.Select(libraryAsset => libraryAsset.RelativePath).ToArray();
            }

            if (runtime)
            {
                return(new RuntimeLibrary(
                           type,
                           export.Library.Identity.Name,
                           export.Library.Identity.Version.ToString(),
                           export.Library.Hash,
                           assemblies,
                           libraryDependencies.ToArray(),
                           serviceable
                           ));
            }
            else
            {
                return(new CompilationLibrary(
                           type,
                           export.Library.Identity.Name,
                           export.Library.Identity.Version.ToString(),
                           export.Library.Hash,
                           assemblies,
                           libraryDependencies.ToArray(),
                           serviceable
                           ));
            }
        }