From 4879c17252a409d8c34d89a7fe022a997f4764d7 Mon Sep 17 00:00:00 2001 From: Gandifil Date: Thu, 21 Dec 2023 17:22:20 +0300 Subject: [PATCH] Add adding new references. --- Contentless/Program.cs | 52 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/Contentless/Program.cs b/Contentless/Program.cs index 3edffd0..ab5d2da 100644 --- a/Contentless/Program.cs +++ b/Contentless/Program.cs @@ -53,13 +53,12 @@ public static class Program { 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"); } + const string ReferenceHeader = "/reference:"; var changed = false; var referencesSyncs = new HashSet(StringComparer.OrdinalIgnoreCase); // load any references to be able to include custom content types as well for (int i = 0; i < content.Count; i++) { - const string ReferenceHeader = "/reference:"; - var line = content[i]; if (!line.StartsWith(ReferenceHeader)) continue; @@ -83,18 +82,41 @@ public static class Program { } var refPath = Path.GetFullPath(Path.Combine(contentFile.DirectoryName, reference)); - try { - Assembly.LoadFrom(refPath); - Console.WriteLine($"Using reference {refPath}"); - } catch (Exception e) { - Console.WriteLine($"Error loading reference {refPath}: {e}"); - } + SafeAssemblyLoad(refPath); } // check references not in .mgcb now + var referencesLastIndex = 0; + // find place where I can add new reference + for (int i = 0; i < content.Count; i++) + { + var line = content[i]; + if (line.StartsWith(ReferenceHeader)) + referencesLastIndex = i + 1; + else if (line.StartsWith("/importer:") || line.StartsWith("/processor:") || line.StartsWith("/build:") || + line.Contains("-- Content --")) + { + if (referencesLastIndex == 0) + referencesLastIndex = i; + break; + } + } foreach (var reference in referencesVersions) - if (!referencesSyncs.Contains(reference.Key)) - Console.Error.WriteLine($"Please, add reference for {reference.Key} in .mgcb file or remove it from Contentless! Reference was skipped!"); + if (!referencesSyncs.Contains(reference.Key) && reference.Value is not null) + { + try + { + var path = CalculateFullPathToLibrary(reference.Key, reference.Value); + content.Insert(referencesLastIndex++, ReferenceHeader + path); + changed = true; + SafeAssemblyLoad(path); + Console.WriteLine($"Adding reference for {path} in .mgcb"); + } + catch (Exception e) + { + Console.Error.WriteLine($"Error adding library {reference.Key} in .mgcb: {e}"); + } + } // load content importers @@ -181,6 +203,16 @@ public static class Program { Console.Write("Done"); } + private static void SafeAssemblyLoad(string refPath) + { + try { + Assembly.LoadFrom(refPath); + Console.WriteLine($"Using reference {refPath}"); + } catch (Exception e) { + Console.WriteLine($"Error loading reference {refPath}: {e}"); + } + } + private static void ExtractVersions(string csprojPath, Dictionary referencesVersions) { Console.WriteLine($"Using project file {csprojPath}");