private static bool IsUpdateRequired(string currentVersion, PackageUpdateOptions updateOptions, out Version parsedVersion)
        {
            parsedVersion = Version.Parse(currentVersion);

            if (parsedVersion < updateOptions.NewPackageVersion)
            {
                // Version in the project file is older; update it
                return(true);
            }

            if (parsedVersion == updateOptions.NewPackageVersion)
            {
                // Already up-to-date
                ShowDebugMessage(string.Format("    version is already up-to-date: {0}", parsedVersion), 0);
                return(false);
            }

            // Version in the project file is newer; only update if Rollback is enabled
            if (updateOptions.Rollback)
            {
                return(true);
            }

            ShowWarning(string.Format("    referenced version {0} is newer than {1}; will not update",
                                      parsedVersion, updateOptions.NewPackageVersion), 0);
            return(false);
        }
示例#2
0
 public IPackageUpdateResult UpdateSystemPackages(IEnumerable <IPackageRepository> sourceRepositories,
                                                  IPackageRepository systemRepository,
                                                  string packageName,
                                                  PackageUpdateOptions options = PackageUpdateOptions.Default)
 {
     return(new PackageUpdateResultIterator(UpdateSystemPackageCore(sourceRepositories, systemRepository, x => x.EqualsNoCase(packageName))));
 }
示例#3
0
        public IPackageUpdateResult UpdateProjectPackages(IEnumerable <IPackageRepository> sourceRepositories,
                                                          IPackageRepository projectRepository,
                                                          IPackageDescriptor projectDescriptor,
                                                          string packageName,
                                                          PackageUpdateOptions options = PackageUpdateOptions.Default)
        {
            if (sourceRepositories == null)
            {
                throw new ArgumentNullException("sourceRepositories");
            }
            if (projectRepository == null)
            {
                throw new ArgumentNullException("projectRepository");
            }
            if (projectDescriptor == null)
            {
                throw new ArgumentNullException("projectDescriptor");
            }
            if (sourceRepositories.Any(x => x == null))
            {
                throw new ArgumentException("Some repositories are null.", "sourceRepositories");
            }

            var resultIterator = UpdateProjectPackageCore(sourceRepositories, projectRepository, projectDescriptor, x => x.EqualsNoCase(packageName));

            if ((options & PackageUpdateOptions.Hooks) == PackageUpdateOptions.Hooks)
            {
                resultIterator = WrapWithHooks(resultIterator, projectDescriptor, projectRepository, "project");
            }
            return(new PackageUpdateResultIterator(resultIterator));
        }
        private static bool SearchForProjectFiles(
            string searchDirectoryPath,
            bool recurse,
            PackageUpdateOptions updateOptions)
        {
            try
            {
                var newPackageVersion = Version.Parse(updateOptions.NuGetPackageVersion);

                updateOptions.NewPackageVersion = newPackageVersion;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(string.Format("Error parsing the NuGet Package Version '{0}', {1}", updateOptions.NuGetPackageVersion, ex.Message));
                return(false);
            }

            try
            {
                if (string.IsNullOrWhiteSpace(searchDirectoryPath))
                {
                    searchDirectoryPath = ".";
                }

                var searchDirectory = new DirectoryInfo(searchDirectoryPath);

                Console.WriteLine("Searching for Visual Studio projects referencing {0} to assure each uses version {1}",
                                  updateOptions.NuGetPackageName, updateOptions.NuGetPackageVersion);

                if (recurse)
                {
                    Console.WriteLine("Searching {0} and subdirectories", searchDirectory);
                }
                else
                {
                    Console.WriteLine("Only search {0} -- to recurse, use /S", searchDirectory);
                }

                var baseDirectoryPath = searchDirectory.Parent?.FullName ?? string.Empty;
                mLastProgressTime        = DateTime.Now;
                mProgressNewlineRequired = false;

                var success = SearchForProjectFiles(searchDirectory, baseDirectoryPath, recurse, updateOptions, out _);

                return(success);
            }
            catch (Exception ex)
            {
                ShowErrorMessage("Error instantiating a DirectoryInfo object for " + searchDirectoryPath + ": " + ex.Message);
                return(false);
            }
        }
        private static bool UpdateVersionElementIfRequired(XElement element, PackageUpdateOptions updateOptions)
        {
            var currentVersion = element.Value;

            var updateVersion = IsUpdateRequired(currentVersion, updateOptions, out var parsedVersion);

            if (!updateVersion)
            {
                return(false);
            }

            element.Value = updateOptions.NewPackageVersion.ToString();

            ShowUpdateInfo(updateOptions, parsedVersion);

            return(true);
        }
        private static void ShowUpdateInfo(PackageUpdateOptions updateOptions, Version parsedVersion)
        {
            string updateVerb;

            if (updateOptions.Preview)
            {
                updateVerb = "would update";
            }
            else
            {
                updateVerb = "updating";
            }

            ShowWarning(string.Format("      {0} version from {1} to {2} for {3}",
                                      updateVerb,
                                      parsedVersion,
                                      updateOptions.NewPackageVersion,
                                      updateOptions.NuGetPackageName), 0);
        }
        public IPackageUpdateResult UpdateProjectPackages(IEnumerable <IPackageRepository> sourceRepositories,
                                                          IPackageRepository projectRepository,
                                                          IPackageDescriptor projectDescriptor,
                                                          PackageUpdateOptions options = PackageUpdateOptions.Recurse)
        {
            if (sourceRepositories == null)
            {
                throw new ArgumentNullException("sourceRepositories");
            }
            if (projectRepository == null)
            {
                throw new ArgumentNullException("projectRepository");
            }
            if (projectDescriptor == null)
            {
                throw new ArgumentNullException("projectDescriptor");
            }
            if (sourceRepositories.Any(x => x == null))
            {
                throw new ArgumentException("Some repositories are null.", "sourceRepositories");
            }

            return(new PackageUpdateResultIterator(UpdateProjectPackageCore(sourceRepositories, projectRepository, projectDescriptor, x => true)));
        }
        private static int Main()
        {
            var commandLineParse = new clsParseCommandLine();

            mSearchDirectoryPath = ".";
            mRecurse             = false;

            mUpdateOptions = new PackageUpdateOptions
            {
                NuGetPackageName    = "",
                NuGetPackageVersion = "",
                Rollback            = false,
                Preview             = true
            };

            mVerboseLogging = false;

            try
            {
                var success = false;

                if (commandLineParse.ParseCommandLine())
                {
                    if (SetOptionsUsingCommandLineParameters(commandLineParse))
                    {
                        success = true;
                    }
                }

                if (!success ||
                    commandLineParse.NeedToShowHelp)
                {
                    ShowProgramHelp();
                    return(-1);
                }

                if (string.IsNullOrWhiteSpace(mSearchDirectoryPath))
                {
                    mSearchDirectoryPath = ".";
                }

                if (string.IsNullOrWhiteSpace(mUpdateOptions.NuGetPackageName))
                {
                    ShowErrorMessage("NuGet package must be defined using /P or /Package");
                    return(-3);
                }

                if (string.IsNullOrWhiteSpace(mUpdateOptions.NuGetPackageVersion))
                {
                    ShowErrorMessage("NuGet package version must be defined using /V or /Version");
                    return(-4);
                }

                success = SearchForProjectFiles(mSearchDirectoryPath, mRecurse, mUpdateOptions);

                if (!success)
                {
                    Thread.Sleep(1500);
                    return(-1);
                }

                Console.WriteLine();
                Console.WriteLine("Search complete");

                Thread.Sleep(250);
            }
            catch (Exception ex)
            {
                ShowErrorMessage("Error occurred in Program->Main", ex);
                Thread.Sleep(1500);
                return(-1);
            }

            return(0);
        }
        private static bool SearchForProjectFiles(
            DirectoryInfo searchDirectory,
            string baseDirectoryPath,
            bool recurse,
            PackageUpdateOptions updateOptions,
            out bool pathTooLong)
        {
            var mostRecentPathLength = searchDirectory.FullName.Length;

            pathTooLong = false;

            try
            {
                var projectFiles = searchDirectory.GetFiles("*.csproj").ToList();
                projectFiles.AddRange(searchDirectory.GetFiles("*.vbproj"));

                if (recurse && DateTime.Now.Subtract(mLastProgressTime).TotalMilliseconds > 200)
                {
                    mLastProgressTime = DateTime.Now;
                    Console.Write(".");
                    mProgressNewlineRequired = true;
                }

                foreach (var projectFile in projectFiles)
                {
                    if (mProgressNewlineRequired)
                    {
                        Console.WriteLine();
                        mProgressNewlineRequired = false;
                    }

                    mLastProgressTime = DateTime.Now;

                    if (mVerboseLogging)
                    {
                        ShowProcessingFileMessage(projectFile, baseDirectoryPath);
                    }

                    ProcessProjectFile(projectFile, baseDirectoryPath, updateOptions);
                }

                var packageConfigFiles = searchDirectory.GetFiles("packages.config").ToList();
                foreach (var configFile in packageConfigFiles)
                {
                    if (mProgressNewlineRequired)
                    {
                        Console.WriteLine();
                        mProgressNewlineRequired = false;
                    }

                    mLastProgressTime = DateTime.Now;

                    if (mVerboseLogging)
                    {
                        ShowProcessingFileMessage(configFile, baseDirectoryPath);
                    }

                    ProcessPackageConfigFile(configFile, baseDirectoryPath, updateOptions);
                }

                if (!recurse)
                {
                    return(true);
                }

                var successOverall = true;

                foreach (var subDirectory in searchDirectory.GetDirectories())
                {
                    mostRecentPathLength = subDirectory.FullName.Length;

                    var success = SearchForProjectFiles(subDirectory, baseDirectoryPath, true, updateOptions, out pathTooLong);

                    if (success || pathTooLong)
                    {
                        continue;
                    }

                    if (mProgressNewlineRequired)
                    {
                        Console.WriteLine();
                        mProgressNewlineRequired = false;
                    }

                    ShowWarning("Error processing directory " + subDirectory.FullName + "; will continue searching");
                    successOverall = false;
                }

                return(successOverall);
            }
            catch (Exception ex) when(ex is DirectoryNotFoundException || ex is PathTooLongException)
            {
                if (mostRecentPathLength > 240 ||
                    ex.Message.Contains("Could not find a part of the path") && ex.Message.Length > 285)
                {
                    ShowWarning("Skipping directory since path length is too long:\n  " + searchDirectory.FullName);
                    pathTooLong = true;
                    return(true);
                }

                ShowErrorMessage("Error in SearchForProjectFiles: " + ex.Message, ex);
                return(false);
            }
            catch (Exception ex)
            {
                ShowErrorMessage("Error in SearchForProjectFiles: " + ex.Message, ex);
                return(false);
            }
        }
        private static bool UpdateVersionAttributeIfRequired(XAttribute versionAttribute, PackageUpdateOptions updateOptions)
        {
            var currentVersion = versionAttribute.Value;

            var updateVersion = IsUpdateRequired(currentVersion, updateOptions, out var parsedVersion);

            if (!updateVersion)
            {
                return(false);
            }

            versionAttribute.Value = updateOptions.NewPackageVersion.ToString();

            ShowUpdateInfo(updateOptions, parsedVersion);

            return(true);
        }
        private static void ProcessProjectFile(FileSystemInfo projectFile, string baseDirectoryPath, PackageUpdateOptions updateOptions)
        {
            try
            {
                // Open the Visual Studio project file and look for XML like this:
                //
                //  <PackageReference Include="PRISM-Library">
                //     <Version>2.4.93</Version>
                //  </PackageReference>

                // Or like this
                //  <PackageReference Include="PRISM-Library" Version="2.4.93" />

                var saveRequired = false;

                var doc = XDocument.Load(projectFile.FullName);

                foreach (var packageRef in doc.Descendants().Where(p => p.Name.LocalName == "PackageReference"))
                {
                    if (!packageRef.HasAttributes)
                    {
                        continue;
                    }

                    var refName = (string)packageRef.Attribute("Include");
                    if (refName == null)
                    {
                        // The PackageReference element does not have attribute Include
                        continue;
                    }

                    if (!string.Equals(refName, updateOptions.NuGetPackageName, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    if (!mVerboseLogging)
                    {
                        ShowProcessingFileMessage(projectFile, baseDirectoryPath);
                    }

                    // Examine the version
                    var versionElementFound = false;

                    foreach (var element in packageRef.Elements())
                    {
                        if (element.Name.LocalName != "Version")
                        {
                            continue;
                        }

                        // Found XML like this:
                        // <PackageReference Include="PRISM-Library">
                        //   <Version>2.5.2</Version>
                        // </PackageReference>

                        saveRequired = UpdateVersionElementIfRequired(element, updateOptions);

                        versionElementFound = true;
                    }

                    if (versionElementFound || !packageRef.HasAttributes)
                    {
                        continue;
                    }

                    var versionAttribute = packageRef.Attribute("Version");
                    if (versionAttribute != null)
                    {
                        // Found XML like this:
                        // <PackageReference Include="PRISM-Library" Version="2.4.93" />

                        saveRequired = UpdateVersionAttributeIfRequired(versionAttribute, updateOptions);
                    }
                }

                if (updateOptions.Preview)
                {
                    return;
                }

                if (!saveRequired)
                {
                    return;
                }

                WriteXmlFile(projectFile, doc);
            }
            catch (Exception ex)
            {
                ShowErrorMessage("Error processing file " + projectFile.FullName + ": " + ex.Message);
            }
        }
        private static void ProcessPackageConfigFile(FileSystemInfo packageConfigFile, string baseDirectoryPath, PackageUpdateOptions updateOptions)
        {
            try
            {
                // Open the packages.config file and look for XML like this:
                //
                //  <package id="PRISM-Library" version="2.5.10" targetFramework="net451" />

                var saveRequired = false;

                var doc = XDocument.Load(packageConfigFile.FullName);

                foreach (var packageRef in doc.Descendants().Where(p => p.Name.LocalName == "package"))
                {
                    if (!packageRef.HasAttributes)
                    {
                        continue;
                    }

                    var refName = (string)packageRef.Attribute("id");
                    if (refName == null)
                    {
                        // The package element does not have attribute id
                        continue;
                    }

                    if (!string.Equals(refName, updateOptions.NuGetPackageName, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    if (!mVerboseLogging)
                    {
                        ShowProcessingFileMessage(packageConfigFile, baseDirectoryPath);
                    }

                    // Examine the version
                    var versionAttribute = packageRef.Attribute("version");
                    if (versionAttribute == null)
                    {
                        // The package element does not have attribute version
                        ConsoleMsgUtils.ShowWarning("package element has id=\"{0}\" but does not have version=\"x.y.z\": {1}",
                                                    updateOptions.NuGetPackageName, packageConfigFile.FullName);
                        continue;
                    }

                    // Found XML like this:
                    // <package id="PRISM-Library" version="2.5.10" targetFramework="net451" />

                    saveRequired = UpdateVersionAttributeIfRequired(versionAttribute, updateOptions);
                }

                if (updateOptions.Preview)
                {
                    return;
                }

                if (!saveRequired)
                {
                    return;
                }

                WriteXmlFile(packageConfigFile, doc);
            }
            catch (Exception ex)
            {
                ShowErrorMessage("Error processing file " + packageConfigFile.FullName + ": " + ex.Message);
            }
        }
 public IPackageUpdateResult UpdateSystemPackages(IEnumerable<IPackageRepository> sourceRepositories,
     IPackageRepository systemRepository,
     string packageName,
     PackageUpdateOptions options = PackageUpdateOptions.Default)
 {
     return new PackageUpdateResultIterator(UpdateSystemPackageCore(sourceRepositories, systemRepository, x => x.EqualsNoCase(packageName)));
 }
 public IPackageUpdateResult UpdateSystemPackages(IEnumerable<IPackageRepository> sourceRepositories,
     IPackageRepository systemRepository,
     PackageUpdateOptions options = PackageUpdateOptions.Default)
 {
     return new PackageUpdateResultIterator(UpdateSystemPackageCore(sourceRepositories, systemRepository, x => true));
 }
        public IPackageUpdateResult UpdateProjectPackages(IEnumerable<IPackageRepository> sourceRepositories,
            IPackageRepository projectRepository,
            PackageDescriptor projectDescriptor,
            string packageName,
            PackageUpdateOptions options = PackageUpdateOptions.Default)
        {
            if (sourceRepositories == null) throw new ArgumentNullException("sourceRepositories");
            if (projectRepository == null) throw new ArgumentNullException("projectRepository");
            if (projectDescriptor == null) throw new ArgumentNullException("projectDescriptor");
            if (sourceRepositories.Any(x => x == null)) throw new ArgumentException("Some repositories are null.", "sourceRepositories");

            return new PackageUpdateResultIterator(CopyPackageCore(sourceRepositories, new[] { projectRepository }, projectDescriptor, x => x.EqualsNoCase(packageName)));
        }
示例#16
0
 public IPackageUpdateResult UpdateSystemPackages(IEnumerable <IPackageRepository> sourceRepositories,
                                                  IPackageRepository systemRepository,
                                                  PackageUpdateOptions options = PackageUpdateOptions.Default)
 {
     return(new PackageUpdateResultIterator(UpdateSystemPackageCore(sourceRepositories, systemRepository, x => true)));
 }