From d3130c77e378b4606ad13b005a078f2b29219c4e Mon Sep 17 00:00:00 2001 From: Gandifil Date: Thu, 21 Dec 2023 16:50:03 +0300 Subject: [PATCH] Change error logic - no return --- Contentless/Program.cs | 57 ++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/Contentless/Program.cs b/Contentless/Program.cs index 90005c3..3edffd0 100644 --- a/Contentless/Program.cs +++ b/Contentless/Program.cs @@ -47,30 +47,10 @@ public static class Program { var referencesVersions = config.References.ToDictionary(x => x, x => (string)null, StringComparer.OrdinalIgnoreCase); if (config.References.Length > 0) { - if (args.Length < 2) { - Console.WriteLine("You supplied references but there is no project file, this isn't compatible. Please specify the full path of project file, if you want to sync references"); - return; - } - var csprojPath = args[1]; - Console.WriteLine($"Using project file {csprojPath}"); - var projectRootElement = ProjectRootElement.Open(csprojPath); - foreach (var property in projectRootElement.AllChildren.Where(x => x.ElementName == "PackageReference").Select(x => x as ProjectItemElement)) - { - var libraryName = property.Include; - var version = (property.Children.First() as ProjectMetadataElement).Value; - if (referencesVersions.Keys.Contains(libraryName)) - { - referencesVersions[libraryName] = version; - Console.WriteLine($"Found library version for sync: {libraryName}, {version}"); - } - } - - foreach (var library in referencesVersions) - if (library.Value is null) - { - Console.WriteLine($"Unable to find library {library.Key}"); - return; - } + if (args.Length > 1) + ExtractVersions(args[1], referencesVersions); + else + Console.Error.WriteLine("You supplied references but there is no project file, this isn't compatible. Please specify the full path of project file, if you want to sync references"); } var changed = false; @@ -86,9 +66,9 @@ public static class Program { var reference = line.Substring(ReferenceHeader.Length); var libraryName = Path.GetFileName(reference)[..^4]; - if (referencesVersions.Keys.Contains(libraryName)) + if (referencesVersions.TryGetValue(libraryName, out var version) && version is not null) { - var fullLibraryPath = CalculateFullPathToLibrary(libraryName, referencesVersions[libraryName]); + var fullLibraryPath = CalculateFullPathToLibrary(libraryName, version); if (reference != fullLibraryPath) { Console.WriteLine($"Changing library reference from {reference} to {fullLibraryPath}"); @@ -114,10 +94,7 @@ public static class Program { // check references not in .mgcb now foreach (var reference in referencesVersions) if (!referencesSyncs.Contains(reference.Key)) - { - Console.WriteLine($"Please, add reference for {reference.Key} in .mgcb file or remove it from Contentless! Reference was skipped!"); - return; - } + Console.Error.WriteLine($"Please, add reference for {reference.Key} in .mgcb file or remove it from Contentless! Reference was skipped!"); // load content importers @@ -204,6 +181,26 @@ public static class Program { Console.Write("Done"); } + private static void ExtractVersions(string csprojPath, Dictionary referencesVersions) + { + Console.WriteLine($"Using project file {csprojPath}"); + var projectRootElement = ProjectRootElement.Open(csprojPath); + foreach (var property in projectRootElement.AllChildren.Where(x => x.ElementName == "PackageReference").Select(x => x as ProjectItemElement)) + { + var libraryName = property.Include; + var version = (property.Children.First() as ProjectMetadataElement).Value; + if (referencesVersions.Keys.Contains(libraryName)) + { + referencesVersions[libraryName] = version; + Console.WriteLine($"Found library version for sync: {libraryName}, {version}"); + } + } + + foreach (var library in referencesVersions) + if (library.Value is null) + Console.Error.WriteLine($"Unable to find library {library.Key} in .csproj"); + } + private static string CalculateFullPathToLibrary(string libraryName, string referencesVersion) { var settings = Settings.LoadDefaultSettings(null);