public void AlignReferences()
        {
            Logger logger = LogManager.GetCurrentClassLogger();

              var projectFolder = Path.GetDirectoryName( _projectFile );
              var packagesRelativePath = NuGetExtensions.FindRelativePathOfPackagesFolder( projectFolder );

              var oldXml = ProjectPackages.TryLoadPackagesConfig( projectFolder );
              if ( oldXml == null ) {
            return;
              }

              ProjectPackages oldPackagesConfig = new ProjectPackages( projectFolder, packagesRelativePath, oldXml );

              var projectPath = _projectFile;
              ProjectFile project;

              using ( var fs = new FileStream( projectPath, FileMode.Open ) ) {
            project = CsProjParser.Parse( Path.GetFileName( projectPath ), fs );
              }

              if ( !UpgradePackagesConfig( oldPackagesConfig, project ) ) {
            return;
              }

              var newXml = ProjectPackages.TryLoadPackagesConfig( projectFolder );
              if ( newXml == null ) {
            return;
              }

              // reparse packages
              ProjectPackages updatedPackagesConfig = new ProjectPackages( projectFolder, packagesRelativePath, newXml );

              bool changed = false;

              Dictionary<Item, Item> itemMap = new Dictionary<Item, Item>( new XmlComparer( oldXml.Root.Name.Namespace ) );

              foreach ( var itemGroup in project.ItemGroups ) {

            foreach ( var item in itemGroup.Items ) {
              if ( !( item is Reference ) ) {
            itemMap[item] = item;
            continue;
              }
              Reference reference = item as Reference;

              if ( !oldPackagesConfig.IsPackageReference( reference.HintPath ) ) {
            if ( itemMap.ContainsKey( item ) ) {
              logger.Error(
                $"{_projectFile} has duplicate entry\n{item.ToElement( XNamespace.None )}\nThis has been fixed automatically." );
            }
            itemMap.Add( item, item ); // we keep any non package references (ie System.Xml)
            continue;
              }

              var referencedInOldPackage = oldPackagesConfig.IsPackageReferenced( reference.HintPath );

              if ( !referencedInOldPackage
               && !updatedPackagesConfig.IsPackageReferenced( reference.HintPath ) ) {
            logger.Info( "Removing " + reference + " as package not listed in packages.config" );
            changed = true;
            continue; // remove reference
              }

              PackageIdentity refPackage = oldPackagesConfig.PackageIdentityFromHintPath( reference.HintPath );

              PackageReference newestPackage = updatedPackagesConfig[refPackage.Id];

              if ( !refPackage.Equals( newestPackage.PackageIdentity ) ) {
            if ( new VersionComparer( VersionComparison.Default ).Compare( refPackage.Version, newestPackage.PackageIdentity.Version ) > 0 ) {
              logger.Warn( "The installed version "
                + newestPackage.PackageIdentity.Version
                + "(that we are aligning to) of " + refPackage.Id + " is lower than the referenced package version " + refPackage.Version );
            }
            var updatedReference = UpdateReference( projectFolder, packagesRelativePath, refPackage, newestPackage, reference );
            if ( updatedReference != null ) {
              reference = updatedReference;
              changed = true;
            }
            else {
              logger.Error( "Could not reconstruct hintpath " + reference.HintPath + " original reference will not be changed" );
            }
              }

              itemMap.Add( item, reference );
            }
              }

              if ( !changed ) {
            logger.Info( "No changes made to " + _projectFile );
            return;
              }

              XDocument projectXml = XDocument.Load( _projectFile );

              projectXml = ReplaceItems( projectXml, itemMap );

              using ( var textWriter = new StreamWriter( _projectFile ) ) {
            logger.Info( "Writing " + _projectFile );
            projectXml.WriteXml( textWriter );
              }
        }
 public static void ApplyIsResolveOption( this Reference reference, ProjectPackages projectPackages )
 {
     reference.IsResolveOption = !projectPackages.IsPackageReference( reference.HintPath ) ||
                            projectPackages.IsPackageReferenced( reference.HintPath );
 }
Exemplo n.º 3
0
 public void ApplyIsResolveOption( ProjectPackages projectPackages )
 {
     IsResolveOption = !projectPackages.IsPackageReference( this ) || projectPackages.IsPackageReferenced( this );
 }