private async Task <IPackage> CreateVirtualPackage(string id, NuGetVersion version) { // Load JSON from source var package = await _source.GetPackageMetadata(id, version); return(new CoreInteropPackage(package)); }
/// <summary> /// Resolve package from online and local repository /// Used for Install-Package and Update-Package command to verify the specified package version exists in the repo. /// </summary> /// <param name="sourceRepository"></param> /// <param name="localRepository"></param> /// <param name="identity"></param> /// <param name="allowPrereleaseVersions"></param> /// <returns></returns> public static PackageIdentity ResolvePackage(SourceRepository sourceRepository, IPackageRepository localRepository, PackageIdentity identity, bool allowPrereleaseVersions) { string packageId = identity.Id; NuGetVersion nVersion = identity.Version; string version = identity.Version.ToNormalizedString(); if (String.IsNullOrEmpty(identity.Id)) { throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "packageId"); } PackageIdentity resolvedIdentity = null; // If we're looking for an exact version of a package then try local first if (version != null) { SemanticVersion sVersion = new SemanticVersion(version); IPackage package = localRepository.FindPackage(packageId, sVersion, allowPrereleaseVersions, allowUnlisted: false); if (package != null) { resolvedIdentity = new PackageIdentity(packageId, NuGetVersion.Parse(package.Version.ToString())); } } if (resolvedIdentity == null) { if (nVersion == null) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, NuGetResources.UnknownPackageSpecificVersion, packageId, version)); } else { Task<JObject> task = sourceRepository.GetPackageMetadata(packageId, nVersion); JObject package = task.Result; if (package == null) { if (version != null) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, NuGetResources.UnknownPackageSpecificVersion, packageId, version)); } throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, NuGetResources.UnknownPackage, packageId)); } else { resolvedIdentity = new PackageIdentity(packageId, nVersion); } } } return resolvedIdentity; }
/// <summary> /// Get latest JObject for package identity /// </summary> /// <param name="repo"></param> /// <param name="identity"></param> /// <param name="allowPrerelease"></param> /// <returns></returns> public static List <JObject> GetLastestJObjectsForPackage(SourceRepository repo, JObject jObject, VsProject project, bool allowPrerelease, int skip, int take, bool takeAllVersions) { List <JObject> jObjects = new List <JObject>(); string id = jObject.Value <string>(Properties.PackageId); string version = jObject.Value <string>(Properties.Version); NuGetVersion nVersion = GetNuGetVersionFromString(version); if (!takeAllVersions) { string latestVersion = GetLastestVersionForPackage(repo, id, project.GetSupportedFrameworks(), allowPrerelease, nVersion, false); if (latestVersion != null) { NuGetVersion laVersion = GetNuGetVersionFromString(latestVersion); if (laVersion > nVersion) { Task <JObject> task = repo.GetPackageMetadata(id, laVersion); JObject latestJObject = task.Result; jObjects.Add(latestJObject); } } } else { JObject package = GetJObjectForPackageId(repo, id, project.GetSupportedFrameworks(), allowPrerelease, skip, take); IEnumerable <NuGetVersion> versionList = GetAllVersionsForPackageId(package).OrderByDescending(v => v); // Work around a bug in repo.Search(), where prerelease versions are not filtered out. if (!allowPrerelease) { versionList = versionList.Where(p => p > nVersion && p.IsPrerelease == false); } else { versionList = versionList.Where(p => p > nVersion); } foreach (NuGetVersion updateVersion in versionList) { JObject updateObject = GetPackageByIdAndVersion(repo, id, updateVersion.ToNormalizedString(), allowPrerelease); jObjects.Add(updateObject); } } return(jObjects); }
/// <summary> /// Get the JObject of a package with known packageId and version /// </summary> /// <param name="sourceRepository"></param> /// <param name="packageId"></param> /// <param name="version"></param> /// <param name="allowPrereleaseVersions"></param> /// <returns></returns> public static JObject GetPackageByIdAndVersion(SourceRepository sourceRepository, string packageId, string version, bool allowPrereleaseVersions) { NuGetVersion nVersion = GetNuGetVersionFromString(version); if (String.IsNullOrEmpty(packageId)) { throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "packageId"); } Task <JObject> task = sourceRepository.GetPackageMetadata(packageId, nVersion); JObject package = task.Result; if (package == null) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, NuGetResources.UnknownPackageSpecificVersion, packageId, version)); } return(package); }
/// <summary> /// Get the JObject of a package with known packageId and version /// </summary> /// <param name="sourceRepository"></param> /// <param name="packageId"></param> /// <param name="version"></param> /// <param name="allowPrereleaseVersions"></param> /// <returns></returns> public static JObject GetPackageByIdAndVersion(SourceRepository sourceRepository, string packageId, string version, bool allowPrereleaseVersions) { NuGetVersion nVersion = GetNuGetVersionFromString(version); if (String.IsNullOrEmpty(packageId)) { throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "packageId"); } Task<JObject> task = sourceRepository.GetPackageMetadata(packageId, nVersion); JObject package = task.Result; if (package == null) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, NuGetResources.UnknownPackageSpecificVersion, packageId, version)); } return package; }
/// <summary> /// Get latest JObject for package identity /// </summary> /// <param name="repo"></param> /// <param name="identity"></param> /// <param name="allowPrerelease"></param> /// <returns></returns> public static List<JObject> GetLastestJObjectsForPackage(SourceRepository repo, JObject jObject, VsProject project, bool allowPrerelease, int skip, int take, bool takeAllVersions) { List<JObject> jObjects = new List<JObject>(); string id = jObject.Value<string>(Properties.PackageId); string version = jObject.Value<string>(Properties.Version); NuGetVersion nVersion = GetNuGetVersionFromString(version); if (!takeAllVersions) { string latestVersion = GetLastestVersionForPackage(repo, id, project.GetSupportedFrameworks(), allowPrerelease, nVersion, false); if (latestVersion != null) { NuGetVersion laVersion = GetNuGetVersionFromString(latestVersion); if (laVersion > nVersion) { Task<JObject> task = repo.GetPackageMetadata(id, laVersion); JObject latestJObject = task.Result; jObjects.Add(latestJObject); } } } else { JObject package = GetJObjectForPackageId(repo, id, project.GetSupportedFrameworks(), allowPrerelease, skip, take); IEnumerable<NuGetVersion> versionList = GetAllVersionsForPackageId(package).OrderByDescending(v => v); // Work around a bug in repo.Search(), where prerelease versions are not filtered out. if (!allowPrerelease) { versionList = versionList.Where(p => p > nVersion && p.IsPrerelease == false); } else { versionList = versionList.Where(p => p > nVersion); } foreach (NuGetVersion updateVersion in versionList) { JObject updateObject = GetPackageByIdAndVersion(repo, id, updateVersion.ToNormalizedString(), allowPrerelease); jObjects.Add(updateObject); } } return jObjects; }
private async Task InstallPackage( IFileSystem installPathFileSystem, string packageId, NuGetVersion version) { if (version == null) { NoCache = true; } var packageManager = CreatePackageManager(installPathFileSystem, useSideBySidePaths: true); // BUGBUG: When adding support for 'AllowMultipleVersions', remember to add PackageInstallNeeded method JObject packageMetadata; if (version == null) { packageMetadata = await SourceRepositoryHelper.GetLatestVersionMetadata(SourceRepository, packageId, prerelease : Prerelease); version = NuGetVersion.Parse(packageMetadata["version"].ToString()); } else { packageMetadata = await SourceRepository.GetPackageMetadata(packageId, version); } if (packageMetadata == null) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, NuGet.Resources.NuGetResources.UnknownPackageSpecificVersion, packageId, version)); } var actionResolver = new ActionResolver( SourceRepository, SourceRepository, new ResolutionContext() { AllowPrerelease = Prerelease, DependencyBehavior = DependencyBehavior, }); var packageActions = await actionResolver.ResolveActionsAsync(new PackageIdentity(packageId, version), PackageActionType.Install, new FilesystemInstallationTarget(packageManager)); if (Verbosity == NuGet.Verbosity.Detailed) { Console.WriteLine("Actions returned by resolver"); foreach (var action in packageActions) { Console.WriteLine(action.ActionType.ToString() + "-" + action.PackageIdentity.ToString()); } } packageActions = packageActions.Where(a => a.ActionType == PackageActionType.Download || a.ActionType == PackageActionType.Purge); if (Verbosity == NuGet.Verbosity.Detailed) { Console.WriteLine("After reducing actions to just Download and Purge"); foreach (var action in packageActions) { Console.WriteLine(action.ActionType.ToString() + "-" + action.PackageIdentity.ToString()); } } var actionExecutor = new ActionExecutor(); actionExecutor.ExecuteActions(packageActions, Console); }
private bool RestorePackage( IFileSystem packagesFolderFileSystem, PackageIdentity packageIdentity, bool packageRestoreConsent, ConcurrentQueue <JObject> satellitePackages) { // BUGBUG: Looks like we are creating PackageManager for every single restore. This is likely done to support execution in parallel var packageManager = CreatePackageManager(packagesFolderFileSystem, useSideBySidePaths: true); if (IsPackageInstalled(packageManager.LocalRepository, packagesFolderFileSystem, packageIdentity)) { return(false); } // BUGBUG: Investigate if the following lock is needed EnsurePackageRestoreConsent(packageRestoreConsent); if (RequireConsent && _outputOptOutMessage) { lock (_outputOptOutMessageLock) { if (_outputOptOutMessage) { string message = String.Format( CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("RestoreCommandPackageRestoreOptOutMessage"), NuGet.Resources.NuGetResources.PackageRestoreConsentCheckBoxText.Replace("&", "")); Console.WriteLine(message); _outputOptOutMessage = false; } } } SourceRepository = SourceRepositoryHelper.CreateSourceRepository(SourceProvider, Source); // BUGBUG: TO BE REMOVED AFTER INVESTIGATION using (packageManager.SourceRepository.StartOperation( RepositoryOperationNames.Restore, packageIdentity.Id, packageIdentity.Version == null ? null : packageIdentity.Version.ToString())) { // BUGBUG: Satellite packages should only be restored at the end. Find out Why first?? // And, then, handle it here var filesystemInstallationTarget = new FilesystemInstallationTarget(packageManager); // BUGBUG: Should consider using async method and await var task = SourceRepository.GetPackageMetadata(packageIdentity.Id, packageIdentity.Version); task.Wait(); var packageJSON = task.Result; if (IsSatellitePackage(packageJSON)) { // Satellite packages would necessarily have to be installed later than the corresponding package. // We'll collect them in a list to keep track and then install them later. satellitePackages.Enqueue(packageJSON); return(true); } var packageAction = new NewPackageAction(NuGet.Client.PackageActionType.Download, packageIdentity, packageJSON, filesystemInstallationTarget, SourceRepository, null); // BUGBUG: See PackageExtractor.cs for locking mechanism used to handle concurrency NuGet.Client.Installation.ActionExecutor actionExecutor = new Client.Installation.ActionExecutor(); // BUGBUG: This is likely inefficient. Consider collecting all actions first and executing them in 1 shot var packageActions = new List <NewPackageAction>() { packageAction }; actionExecutor.ExecuteActions(packageActions, Console); return(true); } }