mirror of
https://github.com/Ellpeck/Contentless.git
synced 2024-11-27 17:28:35 +01:00
Compare commits
No commits in common. "0c42ff440992b88ddcdef0f8354d381ef9b03f46" and "bfe6849171a22bf3421e18288542828156b2dfcd" have entirely different histories.
0c42ff4409
...
bfe6849171
3 changed files with 47 additions and 95 deletions
|
@ -1,15 +0,0 @@
|
|||
using NuGet.Configuration;
|
||||
|
||||
namespace Contentless;
|
||||
|
||||
public class NuGetHelper
|
||||
{
|
||||
private readonly ISettings _settings;
|
||||
|
||||
public NuGetHelper(string projectFolder)
|
||||
{
|
||||
_settings = Settings.LoadDefaultSettings(projectFolder);
|
||||
}
|
||||
|
||||
public string PackageFolder => SettingsUtility.GetGlobalPackagesFolder(_settings);
|
||||
}
|
|
@ -7,6 +7,7 @@ using System.Text.RegularExpressions;
|
|||
using Microsoft.Build.Construction;
|
||||
using Microsoft.Xna.Framework.Content.Pipeline;
|
||||
using Newtonsoft.Json;
|
||||
using NuGet.Configuration;
|
||||
|
||||
namespace Contentless;
|
||||
|
||||
|
@ -44,29 +45,52 @@ public static class Program {
|
|||
var overrides = Program.GetOverrides(config.Overrides).ToArray();
|
||||
|
||||
var referencesVersions = config.References.ToDictionary(x => x, x => (string)null, StringComparer.OrdinalIgnoreCase);
|
||||
if (config.References.Length > 0) {
|
||||
if (args.Length > 1) {
|
||||
ExtractVersions(args[1], referencesVersions);
|
||||
_nuGetHelper = new NuGetHelper(Path.GetDirectoryName(args[1]));
|
||||
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;
|
||||
}
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
const string ReferenceHeader = "/reference:";
|
||||
var changed = false;
|
||||
var referencesSyncs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
// 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];
|
||||
if (!line.StartsWith(ReferenceHeader))
|
||||
continue;
|
||||
var reference = line.Substring(ReferenceHeader.Length);
|
||||
var libraryName = Path.GetFileName(reference)[..^4];
|
||||
|
||||
if (referencesVersions.TryGetValue(libraryName, out var version) && version is not null) {
|
||||
var fullLibraryPath = CalculateFullPathToLibrary(libraryName, version);
|
||||
if (reference != fullLibraryPath) {
|
||||
if (referencesVersions.Keys.Contains(libraryName))
|
||||
{
|
||||
var fullLibraryPath = CalculateFullPathToLibrary(libraryName, referencesVersions[libraryName]);
|
||||
if (reference != fullLibraryPath)
|
||||
{
|
||||
Console.WriteLine($"Changing library reference from {reference} to {fullLibraryPath}");
|
||||
reference = fullLibraryPath;
|
||||
content[i] = ReferenceHeader + fullLibraryPath;
|
||||
|
@ -79,37 +103,20 @@ public static class Program {
|
|||
}
|
||||
|
||||
var refPath = Path.GetFullPath(Path.Combine(contentFile.DirectoryName, reference));
|
||||
SafeAssemblyLoad(refPath);
|
||||
try {
|
||||
Assembly.LoadFrom(refPath);
|
||||
Console.WriteLine($"Using reference {refPath}");
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine($"Error loading reference {refPath}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// 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) && 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}");
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -197,43 +204,10 @@ 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<string, string> 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;
|
||||
if (property.Children.FirstOrDefault(x => x.ElementName == "Version") is not ProjectMetadataElement versionElement)
|
||||
continue;
|
||||
var version = versionElement.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 NuGetHelper _nuGetHelper;
|
||||
|
||||
private static string CalculateFullPathToLibrary(string libraryName, string referencesVersion)
|
||||
{
|
||||
return Path.Combine(_nuGetHelper.PackageFolder, libraryName.ToLower(), referencesVersion, "tools", libraryName + ".dll");
|
||||
var settings = Settings.LoadDefaultSettings(null);
|
||||
return Path.Combine(SettingsUtility.GetGlobalPackagesFolder(settings), libraryName.ToLower(), referencesVersion, "tools", libraryName + ".dll");
|
||||
}
|
||||
|
||||
private static (List<ImporterInfo>, List<string>) GetContentData() {
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||
<add key="lithiumtoast" value="https://www.myget.org/F/lithiumtoast/api/v3/index.json" />
|
||||
</packageSources>
|
||||
</configuration>
|
Loading…
Reference in a new issue