Add adding new references.

This commit is contained in:
Gandifil 2023-12-21 17:22:20 +03:00
parent d3130c77e3
commit 4879c17252

View file

@ -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"); 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 changed = false;
var referencesSyncs = new HashSet<string>(StringComparer.OrdinalIgnoreCase); var referencesSyncs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
// load any references to be able to include custom content types as well // load any references to be able to include custom content types as well
for (int i = 0; i < content.Count; i++) for (int i = 0; i < content.Count; i++)
{ {
const string ReferenceHeader = "/reference:";
var line = content[i]; var line = content[i];
if (!line.StartsWith(ReferenceHeader)) if (!line.StartsWith(ReferenceHeader))
continue; continue;
@ -83,18 +82,41 @@ public static class Program {
} }
var refPath = Path.GetFullPath(Path.Combine(contentFile.DirectoryName, reference)); var refPath = Path.GetFullPath(Path.Combine(contentFile.DirectoryName, reference));
try { SafeAssemblyLoad(refPath);
Assembly.LoadFrom(refPath);
Console.WriteLine($"Using reference {refPath}");
} catch (Exception e) {
Console.WriteLine($"Error loading reference {refPath}: {e}");
}
} }
// check references not in .mgcb now // 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) foreach (var reference in referencesVersions)
if (!referencesSyncs.Contains(reference.Key)) if (!referencesSyncs.Contains(reference.Key) && reference.Value is not null)
Console.Error.WriteLine($"Please, add reference for {reference.Key} in .mgcb file or remove it from Contentless! Reference was skipped!"); {
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 // load content importers
@ -181,6 +203,16 @@ public static class Program {
Console.Write("Done"); 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<string, string> referencesVersions) private static void ExtractVersions(string csprojPath, Dictionary<string, string> referencesVersions)
{ {
Console.WriteLine($"Using project file {csprojPath}"); Console.WriteLine($"Using project file {csprojPath}");