public static NuGetDependencyInfo GetDependencyInfo(FileInfo nupkgPath) { if (!nupkgPath.Exists) { throw new FileNotFoundException(nupkgPath.FullName); } using (var stream = nupkgPath.OpenRead()) { ZipFileSystem zip = new ZipFileSystem(stream); using (PackageReader packageReader = new PackageReader(zip)) { using (var nuspecStream = packageReader.GetNuspec()) { NuspecReader reader = new NuspecReader(nuspecStream); NuGetPackageId package = CreateIdentity(reader, nupkgPath.FullName); List <NuGetDependencyGroup> dependencyGroups = new List <NuGetDependencyGroup>(); foreach (var depGroup in reader.GetDependencyGroups()) { FrameworkName framework = Utilities.GetFrameworkName(depGroup.TargetFramework); NuGetDependency[] dependencies = depGroup.Packages.Select(d => new NuGetDependency(d.Id, VersionRange.Parse(d.VersionRange))).ToArray(); dependencyGroups.Add(new NuGetDependencyGroup(framework, dependencies)); } return(new NuGetDependencyInfo(package, dependencyGroups)); } } } }
public async Task RefreshNuspecFile(PackageRetrieveOptions options) { if (!File.Exists(options.NuspecFilePath) || options.AlwaysLoadFromInternet) { await Task.Run(() => { try { IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2"); var package = repo.FindPackage(options.ProjectName); using (var pkgStream = package.GetStream()) { var pkgReader = new PackageReader(pkgStream); using (Stream nuspecStream = pkgReader.GetNuspec()) using (var fileStream = new FileStream(options.NuspecFilePath, FileMode.Create)) { nuspecStream.CopyTo(fileStream); } } } catch (Exception ex) { throw new Exception("Unable to download read from internet. " + ex.Message); } }); } }
private List <CachedPackageInfo> GetPackageInfos(string id) { List <CachedPackageInfo> cachedPackageInfos; if (_packageInfoCache.TryGetValue(id, out cachedPackageInfos)) { cachedPackageInfos = cachedPackageInfos.ToList(); } var result = new List <CachedPackageInfo>(); // packages\{packageId}.{version}.nupkg foreach (var nupkgInfo in GetNupkgFiles(_source, id)) { var cachedPackageInfo = cachedPackageInfos?.FirstOrDefault(package => string.Equals(package.Path, nupkgInfo.FullName, StringComparison.OrdinalIgnoreCase)); if (cachedPackageInfo != null && cachedPackageInfo.LastWriteTimeUtc == nupkgInfo.LastWriteTimeUtc) { result.Add(cachedPackageInfo); } using (var stream = nupkgInfo.OpenRead()) { var packageReader = new PackageReader(stream); NuspecReader reader; try { reader = new NuspecReader(packageReader.GetNuspec()); } catch (XmlException ex) { var message = string.Format(CultureInfo.CurrentCulture, Strings.Protocol_PackageMetadataError, nupkgInfo.Name, _source); throw new NuGetProtocolException(message, ex); } catch (PackagingException ex) { var message = string.Format(CultureInfo.CurrentCulture, Strings.Protocol_PackageMetadataError, nupkgInfo.Name, _source); throw new NuGetProtocolException(message, ex); } if (string.Equals(reader.GetId(), id, StringComparison.Ordinal)) { result.Add(new CachedPackageInfo { Path = nupkgInfo.FullName, Reader = reader }); } } } _packageInfoCache.TryAdd(id, result); return(result); }
public static void CheckMinClientVersion(Stream packageStream, PackageIdentity packageIdentity) { var packageZipArchive = new ZipArchive(packageStream); var packageReader = new PackageReader(packageZipArchive); var nuspecReader = new NuspecReader(packageReader.GetNuspec()); var packageMinClientVersion = nuspecReader.GetMinClientVersion(); // validate that the current version of NuGet satisfies the minVersion attribute specified in the .nuspec if (Constants.NuGetSemanticVersion < packageMinClientVersion) { throw new NuGetVersionNotSatisfiedException( String.Format(CultureInfo.CurrentCulture, Strings.PackageMinVersionNotSatisfied, packageIdentity, packageMinClientVersion.ToNormalizedString(), Constants.NuGetSemanticVersion.ToNormalizedString())); } }
private List <CachedPackageInfo> GetPackageInfos(string id) { List <CachedPackageInfo> cachedPackageInfos; if (_packageInfoCache.TryGetValue(id, out cachedPackageInfos)) { cachedPackageInfos = cachedPackageInfos.ToList(); } var result = new List <CachedPackageInfo>(); // packages\{packageId}.{version}.nupkg foreach (var nupkgInfo in GetNupkgFiles(_source, id)) { var cachedPackageInfo = cachedPackageInfos?.FirstOrDefault(package => string.Equals(package.Path, nupkgInfo.FullName, StringComparison.OrdinalIgnoreCase)); if (cachedPackageInfo != null && cachedPackageInfo.LastWriteTimeUtc == nupkgInfo.LastWriteTimeUtc) { result.Add(cachedPackageInfo); } using (var stream = nupkgInfo.OpenRead()) { var packageReader = new PackageReader(stream); var reader = new NuspecReader(packageReader.GetNuspec()); if (string.Equals(reader.GetId(), id, StringComparison.Ordinal)) { result.Add(new CachedPackageInfo { Path = nupkgInfo.FullName, Reader = reader }); } } } _packageInfoCache.TryAdd(id, result); return(result); }
public static IVsPackageMetadata CreateMetadata(string nupkgPath, PackageIdentity package) { IEnumerable <string> authors = Enumerable.Empty <string>(); string description = string.Empty; string title = package.Id; string installPath = string.Empty; try { // installPath is the nupkg path FileInfo file = new FileInfo(nupkgPath); installPath = file.Directory.FullName; PackageReader reader = new PackageReader(file.OpenRead()); using (var nuspecStream = reader.GetNuspec()) { NuspecReader nuspec = new NuspecReader(nuspecStream); var metadata = nuspec.GetMetadata(); authors = GetNuspecValue(metadata, "authors").Split(',').ToArray(); title = GetNuspecValue(metadata, "title"); description = GetNuspecValue(metadata, "description"); } } catch (Exception ex) { // ignore errors from reading the extra fields Debug.Fail(ex.ToString()); } if (String.IsNullOrEmpty(title)) { title = package.Id; } return(new VsPackageMetadata(package, title, authors, description, installPath)); }