public void UpdatePackageReferenceFromTransitiveDependency(RequiredNugetUpdate update, string solutionFolder)
        {
            ColorConsole.WriteEmbeddedColorLine($"Update {update.Library.Name}\t\t from [red]{update.Library.Version}[/red]" +
                                                $" to [green]{update.TargetVersion}[/green] [yellow](Transitive)[/yellow] in\t\t{Path.GetFileName(update.ProjectPath)}");

            var projectName   = Path.GetFileName(update.ProjectPath);
            var projectFolder = Path.GetDirectoryName(update.ProjectPath);

            if (projectFolder == null)
            {
                ColorConsole.WriteWarning("Nothing patched - project was null");
                return;
            }
            var packageReferenceFile = Path.Combine(projectFolder, PROJECT_FILE_PROPS_FILE_NAME);
            var document             = CreateOrOpenBuildPropsFile(packageReferenceFile);
            var itemGroup            = document.Root?.Descendants("ItemGroup").FirstOrDefault();

            if (itemGroup != null)
            {
                itemGroup.Add(new XComment($"Required to update transitive reference of {update.RootReferenceName}\r\n" +
                                           $"\t\t\tin {projectName} from {update.Library.Version}\r\n" +
                                           $"\t\t\tbecause {update.TargetVersion} is used in {update.UpateCausedBy}\r\n" +
                                           "\t\t"),
                              new XElement("PackageReference", new XAttribute("Include", update.Library.Name), new XAttribute("Version", update.TargetVersion.ToString())));
                using XmlWriter w = XmlWriter.Create(packageReferenceFile, new XmlWriterSettings()
                {
                    OmitXmlDeclaration = true, Indent = true
                });
                document.Save(w);
            }
            else
            {
                ColorConsole.WriteError($"File {packageReferenceFile} not patched - invalid. Run this tool again with clean flag");
            }
        }
        public void UpdateDirectPackageReference(RequiredNugetUpdate update)
        {
            ColorConsole.WriteEmbeddedColorLine($"Update {update.Library.Name}\t\t from [red]{update.Library.Version}[/red] to" +
                                                $" [green]{update.TargetVersion}[/green] [green](Direct)[/green] in\t\t{Path.GetFileName(update.ProjectPath)}");
            var doc     = XDocument.Load(update.ProjectPath);
            var element = doc.Root?
                          .Descendants("PackageReference")
                          .SingleOrDefault(e => e.Attribute("Include")?.Value.Equals(update.Library.Name, StringComparison.OrdinalIgnoreCase) == true);

            if (element != null)
            {
                var versionAttribute = element.Attribute("Version");
                if (versionAttribute != null)
                {
                    if (versionAttribute.Value != update.Library.Version.ToString())
                    {
                        ColorConsole.WriteWarning(
                            $"Expected version {update.Library.Version} but {versionAttribute.Value} found");
                    }
                }

                element.SetAttributeValue("Version", update.TargetVersion);
                doc.Save(update.ProjectPath);
            }
            else
            {
                ColorConsole.WriteError("No package reference node found");
            }
        }
        public IEnumerable <RequiredNugetUpdate> IdentifyRequiredNugetUpdates(bool verbose)
        {
            if (verbose)
            {
                if (m_assemblyLookup.Any(a => a.Value.OccurrenceList.Count > 1))
                {
                    foreach (var duplicateVersions in m_assemblyLookup.Where(a => a.Value.OccurrenceList.Count > 1))
                    {
                        ColorConsole.WriteLine($"Duplicate dependency: {duplicateVersions.Key}", ConsoleColor.Red);
                        foreach (var occurrences in duplicateVersions.Value.OccurrenceList)
                        {
                            ColorConsole.WriteLine($"\t{occurrences.Key}: used in", ConsoleColor.Green);
                            foreach (var projectAndOccurrence in occurrences.Value.GroupBy(x => x.Root))
                            {
                                ColorConsole.WriteLine($"\t\t{projectAndOccurrence.Key}:");
                                foreach (var directReferencedNugets in projectAndOccurrence
                                         .GroupBy(x => x.DirectReference)
                                         .Select(x => new
                                {
                                    MaxLvl = x.Max(v => v.Lvl),
                                    DirectAssembly = x.Key
                                })
                                         .OrderBy(x => x.MaxLvl))
                                {
                                    if (directReferencedNugets.MaxLvl == 0)
                                    {
                                        ColorConsole.WriteLine("\t\t\tdirect reference", ConsoleColor.Green);
                                    }
                                    else
                                    {
                                        ColorConsole.WriteEmbeddedColorLine(
                                            $"\t\t\t{duplicateVersions.Key} transitive reference({directReferencedNugets.MaxLvl})" +
                                            $" of [Yellow]{directReferencedNugets.DirectAssembly.Name}-{directReferencedNugets.DirectAssembly.Version}[/Yellow]");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            ColorConsole.WriteInfo($"{m_assemblyLookup.Count} dependencies found in {m_dependencyGraphSpec.Projects.Count} projects");
            var duplicates            = m_assemblyLookup.Where(a => a.Value.OccurrenceList.Count > 1).ToList();
            var maxVersionLookup      = duplicates.ToDictionary(x => x.Key, x => x.Value.OccurrenceList.Keys.Max());
            var projectWithMaxVersion = duplicates.ToDictionary(x => x.Key, x => x.Value.ProjectWithMaxVersion);

            var updateRequired = duplicates.SelectMany(kvp =>
                                                       kvp.Value.OccurrenceList.Where(x => x.Key < maxVersionLookup[kvp.Key]).SelectMany(x => x.Value)).ToList();

            return(updateRequired.Select(x =>
                                         new RequiredNugetUpdate(x.ProjectPath, x.Target, maxVersionLookup[x.Target.Name], x.Lvl == 0, x.DirectReference.Name, projectWithMaxVersion[x.Target.Name]))
                   .Distinct(new RequiredNugetUpdateEqualityComparer()));
        }
Esempio n. 4
0
        private static void ColorTests()
        {
            ColorConsole.WriteWrappedHeader("Color Console Examples");

            Console.WriteLine("\nUsing a splash of color in your Console code more easily... (plain text)\n");

            ColorConsole.WriteLine("Color me this - in Red", ConsoleColor.Red);

            ColorConsole.WriteWrappedHeader("Off with their green Heads!", headerColor: ConsoleColor.Green);

            ColorConsole.WriteWarning("\nWorking...\n");

            Console.WriteLine("Writing some mixed colors: (plain text)");
            ColorConsole.WriteEmbeddedColorLine("Launch the site with [darkcyan]https://localhost:5200[/darkcyan] and press [yellow]Ctrl-c[/yellow] to exit.\n");

            ColorConsole.WriteSuccess("The operation completed successfully.");
        }