private static bool SupportsProjectTargetFrameworks(TypeInfo 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()) { // In this case package did not specify any target frameworks and we follow our default // behavior and display as much as possible to the user return(true); } else { var packageFrameworkNames = typeInfo.TargetFrameworks.Select(x => VersionUtility.ParseFrameworkName(x)).ToList(); foreach (var projectFramework in projectTargetFrameworks) { var projectFrameworkName = VersionUtility.ParseFrameworkName(projectFramework.TargetFrameworkShortName); if (VersionUtility.IsCompatible(projectFrameworkName, packageFrameworkNames)) { // if at least any project target framework supports package - display it return(true); } } } return(false); }
public AddPackageOperation(IPackageInstaller packageInstaller, Document document, TypeInfo typeInfo, string title) { _packageInstaller = packageInstaller; _document = document; _typeInfo = typeInfo; _title = title; }
public AddPackageCodeAction(IPackageInstaller packageInstaller, TypeInfo typeInfo, string titleFormat, Func <CancellationToken, Task <Document> > createChangedDocument) { _packageInstaller = packageInstaller; _typeInfo = typeInfo; _createChangedDocument = createChangedDocument; _titleFormat = titleFormat; }
private static bool PackageExistsInTheProject(TypeInfo typeInfo, IEnumerable <TargetFrameworkMetadata> projectTargetFrameworks, bool allowHigherVersions) { if (allowHigherVersions) { var packageVersion = new SemanticVersion(typeInfo.PackageVersion); return(projectTargetFrameworks.Any(x => x.Packages.Any(p => { var projecsPackageVersion = new SemanticVersion(p.Value); // if project has package with given name and version less than in index return p.Key.Equals(typeInfo.PackageName) && projecsPackageVersion < packageVersion; } ))); } else { return(projectTargetFrameworks.Any(x => x.Packages.Any(p => p.Key.Equals(typeInfo.PackageName)))); } }
public void InstallPackage(Workspace workspace, Document document, TypeInfo typeInfo, CancellationToken cancellationToken = default(CancellationToken)) { Debug.Assert(typeInfo != null); ThreadHelper.JoinableTaskFactory.RunAsync(async delegate { // Switch to main thread await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); try { var container = _serviceProvider.GetService <IComponentModel, SComponentModel>(); var docHierarchy = document.GetVsHierarchy(_serviceProvider); if (docHierarchy == null) { return; } var project = docHierarchy.GetDTEProject(); var projectSpecificInstallers = container.DefaultExportProvider.GetExportedValues <IProjectPackageInstaller>(); if (projectSpecificInstallers != null && projectSpecificInstallers.Any()) { var supportedInstaller = projectSpecificInstallers.FirstOrDefault(x => x.SupportsProject(project)); if (supportedInstaller != null) { if (await SafeExecuteActionAsync( delegate { var frameworks = typeInfo.TargetFrameworks == null ? null : typeInfo.TargetFrameworks.Select(x => VersionUtility.ParseFrameworkName(x)).ToList(); return(supportedInstaller.InstallPackageAsync(project, typeInfo.PackageName, typeInfo.PackageVersion, frameworks, cancellationToken)); })) { return; // package installed successfully } } } // if there no project specific installer use nuget default IVsPackageInstaller var installer = container.DefaultExportProvider.GetExportedValue <IVsPackageInstaller>() as IVsPackageInstaller2; var packageSourceProvider = container.DefaultExportProvider.GetExportedValue <IVsPackageSourceProvider>(); var sources = packageSourceProvider.GetSources(includeUnOfficial: true, includeDisabled: false).Select(x => x.Value).ToList(); // if pass all sources to InstallPackageAsync it would throw Central Directory corrupt // if one of the feeds is not supported and stop installation - nuget's bug foreach (var source in sources) { if (await SafeExecuteActionAsync( delegate { return(installer.InstallPackageAsync(project, new[] { source }, typeInfo.PackageName, typeInfo.PackageVersion, true, cancellationToken)); })) { break; } } } 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(string.Format("{0} \r\n {1}", e.Message, e.StackTrace)); } }); }