public TargetPackageIndex( 
      IEnumerable<string> projectFiles, 
      string targetPrefix = null, 
      string targetVersion = null, 
      string targetFramework = null )
        {
            TargetPrefix = targetPrefix;
              TargetVersion = targetVersion;
              TargetFramework = targetFramework;
              List<PackageReference> packageReferences = new List<PackageReference>();

              foreach ( var folder in projectFiles.Select( p => new DirectoryInfo( Path.GetDirectoryName( p ) ) ) ) {
            var packagesConfig = folder.GetFiles( "packages.config" ).SingleOrDefault();
            if ( packagesConfig == null ) {
              continue;
            }
            using ( var fs = new FileStream( packagesConfig.FullName, FileMode.Open ) ) {
              PackagesConfigReader reader = new PackagesConfigReader( fs );
              packageReferences.AddRange( reader.GetPackages() );
            }
              }

              // TODO: perhaps look at difference in target framework?
              IndexNewestPackages( packageReferences.Select( pr => pr.PackageIdentity ) );
        }
Пример #2
0
        private IEnumerable<string> GetPackageAssemblyNames(string path)
        {
            var compatibilityProvider = new DefaultCompatibilityProvider();
            var folderReader = new PackageFolderReader(Path.Combine(path, _fileSystem.PackagesFolder));
            var nupkgFiles = folderReader.GetFiles().Where(x => Path.GetExtension(x).ToLowerInvariant() == ".nupkg");

            var packagesConfig = XDocument.Parse(File.ReadAllText(Path.Combine(path, _fileSystem.PackagesFile)));

            var reader = new PackagesConfigReader(packagesConfig);
            var contents = reader.GetPackages();

            var result = new List<string>();

            foreach (var nupkg in nupkgFiles)
            {
                var stream = folderReader.GetStream(nupkg);
                var packageReader = new PackageReader(stream);

                var identity = packageReader.GetIdentity();
                var packagesConfigReference = contents.FirstOrDefault(x => x.PackageIdentity.Id == identity.Id && x.PackageIdentity.Version == identity.Version);

                if (packagesConfigReference == null)
                {
                    break;
                }

                var packageContents = packageReader.GetLibItems().Where(x => compatibilityProvider.IsCompatible(x.TargetFramework, packagesConfigReference.TargetFramework)).
                    SelectMany(x => x.Items.Where(i => Path.GetExtension(i).ToLowerInvariant() == ".dll"));

                result.AddRange(packageContents);
            }

            return result;
        }
        public static PackageReference ParseAsPackage( this XElement e )
        {
            var ns = e.Name.Namespace;

              var document = new XDocument();
              document.Add( new XElement( ns.GetName( "packages" ), e ) );

              var reader = new PackagesConfigReader( document );
              return reader.GetPackages().Single();
        }
        private static List<PackageReference> GetAllPackageReferences( IEnumerable<string> projectFiles )
        {
            List<PackageReference> packageReferences = new List<PackageReference>();

              foreach ( var folder in projectFiles.Select( p => new DirectoryInfo( Path.GetDirectoryName( p ) ) ) ) {
            var packagesConfig = folder.GetFiles( "packages.config" ).SingleOrDefault();
            if ( packagesConfig == null ) {
              continue;
            }
            using ( var fs = new FileStream( packagesConfig.FullName, FileMode.Open ) ) {
              PackagesConfigReader reader = new PackagesConfigReader( fs );
              packageReferences.AddRange( reader.GetPackages() );
            }
              }
              return packageReferences;
        }
 public static IReadOnlyList<ConfigitPackageReference> ReadPackageReferences( string content )
 {
     var result =
     new PackagesConfigReader( XDocument.Parse( content ) ).GetPackages().Select<PackageReference, ConfigitPackageReference>( p => (ConfigitPackageReference) p ).ToArray();
       return result;
 }
        /// <summary>
        /// Return list of package identities parsed from packages.config
        /// </summary>
        /// <returns></returns>
        private IEnumerable<PackageIdentity> CreatePackageIdentitiesFromPackagesConfig()
        {
            IEnumerable<PackageIdentity> identities = Enumerable.Empty<PackageIdentity>();

            try
            {
                // Example: install-package https://raw.githubusercontent.com/NuGet/json-ld.net/master/src/JsonLD/packages.config
                if (Id.ToLowerInvariant().StartsWith("http"))
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Id);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    // Read data via the response stream
                    Stream resStream = response.GetResponseStream();

                    PackagesConfigReader reader = new PackagesConfigReader(resStream);
                    IEnumerable<PackageReference> packageRefs = reader.GetPackages();
                    identities = packageRefs.Select(v => v.PackageIdentity);
                }
                else
                {
                    // Example: install-package c:\temp\packages.config
                    using (FileStream stream = new FileStream(Id, FileMode.Open))
                    {
                        PackagesConfigReader reader = new PackagesConfigReader(stream);
                        IEnumerable<PackageReference> packageRefs = reader.GetPackages();
                        identities = packageRefs.Select(v => v.PackageIdentity);
                    }
                }

                // Set _allowPrerelease to true if any of the identities is prerelease version.
                if (identities != null && identities.Any())
                {
                    foreach (PackageIdentity identity in identities)
                    {
                        if (identity.Version.IsPrerelease)
                        {
                            _allowPrerelease = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogCore(MessageLevel.Error, string.Format(Resources.Cmdlet_FailToParsePackages, Id, ex.Message));
            }

            return identities;
        }
        private HashSet<PackageReference> GetInstalledPackageReferences(string projectConfigFilePath)
        {
            if (File.Exists(projectConfigFilePath))
            {
                var reader = new PackagesConfigReader(XDocument.Load(projectConfigFilePath));
                return new HashSet<PackageReference>(reader.GetPackages(), new PackageReferenceComparer());
            }

            return new HashSet<PackageReference>(new PackageReferenceComparer());
        }