private static int TargetFrameworkName_NearestCompareTest(object projectFramework, object criteria, object available) { var projectFrameworkName = projectFramework as FrameworkName; var criteriaFrameworkName = criteria as FrameworkName; var availableFrameworkName = available as FrameworkName; if (criteriaFrameworkName != null && availableFrameworkName != null && projectFrameworkName != null) { var frameworks = new[] { criteriaFrameworkName, availableFrameworkName }; // Find the nearest compatible framework to the project framework. var nearest = DnxVersionUtility.GetNearest(projectFrameworkName, frameworks); if (nearest == null) { return(-1); } if (criteriaFrameworkName.Equals(nearest)) { return(-1); } if (availableFrameworkName.Equals(nearest)) { return(1); } } return(0); }
/// <summary> /// Checks if package supports any of project's target frameworks /// </summary> public static bool SupportsProjectTargetFrameworks(IPackageIndexModelInfo typeInfo, IEnumerable <TargetFrameworkMetadata> projectTargetFrameworks) { // if we find at least any framework in package that current project supports, // we show this package to user. if (typeInfo.TargetFrameworks == null || !typeInfo.TargetFrameworks.Any() || typeInfo.TargetFrameworks.Any(x => x.Equals("Unsupported", StringComparison.OrdinalIgnoreCase))) { // In this case: // if package did not specify any target frameworks // or one of the frameworks is new and Nuget.Core can not recognize it - returning Unsupported // we follow our default behavior and display as much as possible to the user, return true to show the package return(true); } else { var packageFrameworkNames = typeInfo.TargetFrameworks.Select(x => DnxVersionUtility.ParseFrameworkName(x)).ToList(); foreach (var projectFramework in projectTargetFrameworks) { var projectFrameworkName = DnxVersionUtility.ParseFrameworkName(projectFramework.TargetFrameworkShortName); if (packageFrameworkNames.Any(x => DnxVersionUtility.IsCompatible(projectFrameworkName, x))) { // if at least any project target framework supports package - display it return(true); } } } return(false); }
public void InstallPackage(Workspace workspace, Document document, IPackageIndexModelInfo packageInfo, IEnumerable <ProjectMetadata> projects, CancellationToken cancellationToken = default(CancellationToken)) { Debug.Assert(packageInfo != null); ThreadHelper.JoinableTaskFactory.RunAsync(async delegate { foreach (var project in projects) { try { var container = _serviceProvider.GetService <IComponentModel, SComponentModel>(); var projectSpecificInstallers = container.DefaultExportProvider.GetExportedValues <IProjectPackageInstaller>(); if (projectSpecificInstallers != null && projectSpecificInstallers.Any()) { var supportedInstaller = projectSpecificInstallers.FirstOrDefault(x => x.SupportsProject(project.ProjectPath)); if (supportedInstaller != null) { if (await SafeExecuteActionAsync( delegate { var frameworksToInstall = new List <FrameworkName>(); var packageFrameworkNames = packageInfo.TargetFrameworks.Select(x => DnxVersionUtility.ParseFrameworkName(x)) .ToList(); foreach (var projectFrameworkMetadata in project.TargetFrameworks) { var projectFrameworkName = DnxVersionUtility.ParseFrameworkName(projectFrameworkMetadata.TargetFrameworkShortName); if (projectFrameworkName != null && packageFrameworkNames.Any(x => DnxVersionUtility.IsCompatible(projectFrameworkName, x))) { frameworksToInstall.Add(projectFrameworkName); } } return(supportedInstaller.InstallPackageAsync(project.ProjectPath, packageInfo.PackageName, packageInfo.PackageVersion, frameworksToInstall, cancellationToken)); })) { continue; // package installed successfully } } } } catch (Exception e) { // we should not throw here, since it would create an exception that may be // visible to the user, instead just dump into debugger output or to package // manager console. // TODO Package manager console? Debug.Write(e.ToString()); } } }); }
internal static object TargetFrameworkName_Parser(string name) { var result = DnxVersionUtility.ParseFrameworkName(name); if (result != DnxVersionUtility.UnsupportedFrameworkName) { return(result); } return(new FrameworkName(name, new Version(0, 0))); }
internal static bool TargetFrameworkName_IsCriteriaSatisfied(object criteria, object available) { var criteriaFrameworkName = criteria as FrameworkName; var availableFrameworkName = available as FrameworkName; if (criteriaFrameworkName != null && availableFrameworkName != null) { return(DnxVersionUtility.IsCompatible(criteriaFrameworkName, availableFrameworkName)); } return(false); }
public MockGenerator MockLibExplicitReferencesDll() { var mockPackageFile = new Mock <IPackageFile>(MockBehavior.Strict); mockPackageFile.Setup(x => x.Path).Returns(@"lib\dnx451\mylibfxsample.dll"); var mockPackageFileCore = new Mock <IPackageFile>(MockBehavior.Strict); mockPackageFileCore.Setup(x => x.Path).Returns(@"lib\dnx451\mylibfxsample2.dll"); PackageFiles.Add(mockPackageFile); PackageFiles.Add(mockPackageFileCore); FileSystem.Setup(x => x.FileExists(Path.Combine(Path.GetDirectoryName(NupkgFile), @"lib\dnx451\mylibfxsample.dll"))).Returns(true); var packageFiles = new List <IPackageFile> { mockPackageFile.Object, mockPackageFileCore.Object }; var assemblyMetadataDnx451 = new AssemblyMetadata { FullPath = Path.Combine(Path.GetDirectoryName(NupkgFile), @"lib\dnx451\mylibfxsample.dll"), TargetFrameworks = new List <string> { "dnx451" } }; PackageAssemblyMetadata.Add(assemblyMetadataDnx451); NugetPackage.Setup(x => x.PackageAssemblyReferences).Returns(new List <PackageReferenceSet> { new PackageReferenceSet(DnxVersionUtility.ParseFrameworkName("dnx451"), new [] { @"mylibfxsample.dll" }) }); var packageDir = Path.GetDirectoryName(NupkgFile); FileSystem.Setup(x => x.DirectoryGetFiles(packageDir, "*.dll", SearchOption.AllDirectories)) .Returns(PackageFiles.Select(x => Path.Combine(packageDir, x.Object.Path)).ToArray()); return(this); }
public PatternDefinitions() { Properties = new PropertyDefinitions(); ManagedAssemblies = new ContentPatternDefinition { GroupPatterns = { "runtimes/{rid}/lib/{tfm}/{any?}", "lib/{tfm}/{any?}" }, PathPatterns = { "runtimes/{rid}/lib/{tfm}/{assembly}", "lib/{tfm}/{assembly}" }, PropertyDefinitions = Properties.Definitions }; ManagedAssemblies.GroupPatterns.Add(new PatternDefinition { Pattern = "lib/{assembly?}", Defaults = new Dictionary <string, object> { { "tfm", DnxVersionUtility.ParseFrameworkName("net") } } }); ManagedAssemblies.PathPatterns.Add(new PatternDefinition { Pattern = "lib/{assembly}", Defaults = new Dictionary <string, object> { { "tfm", DnxVersionUtility.ParseFrameworkName("net") } } }); CompileTimeAssemblies = new ContentPatternDefinition { GroupPatterns = { "ref/{tfm}/{any?}", }, PathPatterns = { "ref/{tfm}/{assembly}", }, PropertyDefinitions = Properties.Definitions, }; ResourceAssemblies = new ContentPatternDefinition { GroupPatterns = { "runtimes/{rid}/lib/{tfm}/{locale?}/{any?}", "lib/{tfm}/{locale?}/{any?}" }, PathPatterns = { "runtimes/{rid}/lib/{tfm}/{locale}/{resources}", "lib/{tfm}/{locale}/{resources}" }, PropertyDefinitions = Properties.Definitions }; ResourceAssemblies.GroupPatterns.Add(new PatternDefinition { Pattern = "lib/{locale}/{resources?}", Defaults = new Dictionary <string, object> { { "tfm", DnxVersionUtility.ParseFrameworkName("net") } } }); ResourceAssemblies.PathPatterns.Add(new PatternDefinition { Pattern = "lib/{locale}/{resources}", Defaults = new Dictionary <string, object> { { "tfm", DnxVersionUtility.ParseFrameworkName("net") } } }); NativeLibraries = new ContentPatternDefinition { GroupPatterns = { "runtimes/{rid}/native/{any?}", "native/{any?}", }, PathPatterns = { "runtimes/{rid}/native/{any}", "native/{any}", }, PropertyDefinitions = Properties.Definitions, }; }
public static IEnumerable <string> GetAssembliesForFramework(FrameworkName framework, IPackage package, IEnumerable <string> packageAssemblies) { var patterns = PatternDefinitions.DotNetPatterns; if (packageAssemblies == null) { return(null); } var files = packageAssemblies.Select(p => p.Replace(Path.DirectorySeparatorChar, '/')).ToList(); var contentItems = new ContentItemCollection(); contentItems.Load(files); var criteriaBuilderWithTfm = new SelectionCriteriaBuilder(patterns.Properties.Definitions); criteriaBuilderWithTfm = criteriaBuilderWithTfm .Add["tfm", framework]; var criteria = criteriaBuilderWithTfm.Criteria; var allReferencesGroup = contentItems.FindBestItemGroup(criteria, patterns.CompileTimeAssemblies, patterns.ManagedAssemblies); List <string> allReferencesGroupAssemblies = null; if (allReferencesGroup != null) { allReferencesGroupAssemblies = allReferencesGroup.Items.Select(t => t.Path).ToList(); } if (allReferencesGroupAssemblies == null) { return(null); } IEnumerable <string> oldLibGroupAssemblies = null; var oldLibGroup = contentItems.FindBestItemGroup(criteria, patterns.ManagedAssemblies); if (oldLibGroup != null) { oldLibGroupAssemblies = oldLibGroup.Items.Select(p => p.Path).ToList(); } // COMPAT: Support lib/contract so older packages can be consumed string contractPath = "lib/contract/" + package.Id + ".dll"; var hasContract = files.Any(path => contractPath.Equals(path, StringComparison.OrdinalIgnoreCase)); var hasLib = oldLibGroupAssemblies != null && oldLibGroupAssemblies.Any(); if (hasContract && hasLib && !DnxVersionUtility.IsDesktop(framework)) { allReferencesGroupAssemblies.Clear(); allReferencesGroupAssemblies.Add(contractPath); } // See if there's a list of specific references defined for this target framework IEnumerable <PackageReferenceSet> referenceSets; if (DnxVersionUtility.GetNearest(framework, package.PackageAssemblyReferences.ToList(), out referenceSets)) { // Get the first compatible reference set var referenceSet = referenceSets.FirstOrDefault(); if (referenceSet != null) { // Remove all assemblies of which names do not appear in the References list allReferencesGroupAssemblies.RemoveAll(path => path.StartsWith("lib/") && !referenceSet.References.Contains(Path.GetFileName(path), StringComparer.OrdinalIgnoreCase)); } } return(allReferencesGroupAssemblies.Select(p => p.Replace('/', Path.DirectorySeparatorChar)).ToList()); }
/// <summary> /// Returns metadata info for given nupkg file. /// Note: Don't hold any object referenecs for ZipPackage data, since it might hold whole package in the memory. /// </summary> public IPackageMetadata GetPackageMetadataFromPath(string nupkgFilePath, Func <string, bool> shouldIncludeFunc) { if (string.IsNullOrEmpty(nupkgFilePath)) { return(null); } if (!_fileSystem.FileExists(nupkgFilePath)) { return(null); } // Note: don't use ZipPackage ctor that takes Stream, it stores package contents in memory and they // are not collected after even though we are not referencing any of ZipPackage objects. Instead use // ZipPackage(filePath) ctor. var package = _nugetHelper.OpenPackage(nupkgFilePath, (p) => { return(new NuGet.ZipPackage(p)); }); // check if package id should be excluded and exit early if (shouldIncludeFunc != null && !shouldIncludeFunc(package.Id)) { return(null); } var packageFolder = Path.GetDirectoryName(nupkgFilePath) ?? string.Empty; // Note: If using this commented code to get package assemblies, it unpacks whole package into memory, // and then it is never garbage collected even though we don't keep any references to it. It is also // should not be cached since ctor that we use sets _enableCaching = false for ZipPackage. So it might // have some unmanaged objects that are not collected by some reason. // var allAssemblies = package.GetFiles() // .Where(x => ".dll".Equals(Path.GetExtension(x.EffectivePath), StringComparison.OrdinalIgnoreCase)) // .Select(x => x.Path.ToString()) // .ToList(); // we assume that nupkg file lives in the package dir and has lib, ref an dother package dirs in the same dir var allAssemblies = _fileSystem.DirectoryGetFiles(packageFolder, "*.dll", SearchOption.AllDirectories) .Select(x => x.Substring(packageFolder.Length + 1)) .ToList(); var packageTargetFrameworkNames = package.GetSupportedFrameworks().ToList() ?? Enumerable.Empty <FrameworkName>(); var tfmAssemblyGroups = new Dictionary <FrameworkName, IList <string> >(); // here we construct a list of the form { TFM }, { list of target assemblies } foreach (var tfm in packageTargetFrameworkNames) { var dnxResult = TfmPackageAssemblyMatcher.GetAssembliesForFramework(tfm, package, allAssemblies); if (dnxResult != null) { tfmAssemblyGroups.Add(new FrameworkName(tfm.Identifier, tfm.Version, tfm.Profile), dnxResult.ToList()); } } // now we need to convert it to the list of the form: { assembly }, { list of TFM } var assembliesMetadata = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase); foreach (var kvp in tfmAssemblyGroups) { foreach (var assemblyPath in kvp.Value) { List <string> existingAssemblyTfms = null; if (assembliesMetadata.TryGetValue(assemblyPath, out existingAssemblyTfms)) { existingAssemblyTfms.Add(DnxVersionUtility.GetShortFrameworkName(kvp.Key)); } else { assembliesMetadata.Add(assemblyPath, new List <string> { DnxVersionUtility.GetShortFrameworkName(kvp.Key) }); } } } var selectedAssemblies = new List <AssemblyMetadata>(); foreach (var am in assembliesMetadata) { var fullPath = Path.Combine(packageFolder, am.Key); if (!_fileSystem.FileExists(fullPath)) { fullPath = Path.Combine(packageFolder, package.Id.ToString(), am.Key); if (!_fileSystem.FileExists(fullPath)) { continue; } } selectedAssemblies.Add(new AssemblyMetadata { FullPath = fullPath, TargetFrameworks = am.Value }); } var newPackageMetadata = new PackageMetadata { Id = package.Id.ToString(), Version = package.Version.ToString(), LocalPath = nupkgFilePath, TargetFrameworks = packageTargetFrameworkNames.Select(x => DnxVersionUtility.GetShortFrameworkName(x)).ToList(), Assemblies = selectedAssemblies }; return(newPackageMetadata); }