private bool UpgradePackagesConfig(
      ProjectPackages oldPackagesConfig,
      ProjectFile project )
        {
            List<PackageReference> updatedPackages = new List<PackageReference>();

              bool changed = false;
              var logger = LogManager.GetCurrentClassLogger();

              foreach ( var package in oldPackagesConfig ) {
            var targetVersion = _packageIndex.GetTargetVersionOf( package.PackageIdentity.Id );

            PackageReference newPackage = new PackageReference(
              new PackageIdentity( package.PackageIdentity.Id, targetVersion ),
              package.TargetFramework,
              package.IsUserInstalled,
              package.IsDevelopmentDependency,
              package.RequireReinstallation,
              package.AllowedVersions );

            updatedPackages.Add( newPackage );

            if ( targetVersion.Equals( package.PackageIdentity.Version ) ) {
              continue;
            }

            changed = true;
            logger.Info( project.Name +
              ": Changing " + package.PackageIdentity.Id +
              " version " + package.PackageIdentity.Version + " to " + targetVersion );

            NuGetExtensions.InstallPackage( Path.GetDirectoryName( _projectFile ),  newPackage );
              }

              if ( !changed ) {
            return false;
              }

              var config = Path.Combine( Path.GetDirectoryName( _projectFile ), "packages.config" );
              logger.Info( "Writing changes to " + config );
              updatedPackages.Write( config );
              return true;
        }
        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 );

              List<ItemGroup> output = new List<ItemGroup>();

              bool changed = false;

              foreach ( var itemGroup in project.ItemGroups ) {
            List<Item> items = new List<Item>();
            foreach ( var item in itemGroup.Items ) {
              if ( !( item is Reference ) ) {
            items.Add( item );
            continue;
              }
              Reference reference = item as Reference;

              if ( !oldPackagesConfig.IsPackageReference( reference.HintPath ) ) {
            items.Add( 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" );
            }
              }

              items.Add( reference );
            }
            output.Add( new ItemGroup( items ) );
              }

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

              XDocument projectXml = XDocument.Load( _projectFile );

              ProjectFile.DeleteItems( projectXml );
              ProjectFile.AddItems( projectXml, output.SelectMany( ig => ig.Items ).ToArray() );

              using ( var textWriter = new StreamWriter( _projectFile ) ) {
            logger.Info( "Writing " + _projectFile );
            SerialisationHelper.WriteXml( projectXml, textWriter );
              }
        }