mirror of
https://github.com/Ellpeck/Contentless.git
synced 2024-11-27 17:28:35 +01:00
Compare commits
5 commits
bfe6849171
...
0c42ff4409
Author | SHA1 | Date | |
---|---|---|---|
|
0c42ff4409 | ||
|
a8ed920103 | ||
|
783fc8683e | ||
|
4879c17252 | ||
|
d3130c77e3 |
3 changed files with 95 additions and 47 deletions
15
Contentless/NuGetHelper.cs
Normal file
15
Contentless/NuGetHelper.cs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
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,7 +7,6 @@ using System.Text.RegularExpressions;
|
||||||
using Microsoft.Build.Construction;
|
using Microsoft.Build.Construction;
|
||||||
using Microsoft.Xna.Framework.Content.Pipeline;
|
using Microsoft.Xna.Framework.Content.Pipeline;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NuGet.Configuration;
|
|
||||||
|
|
||||||
namespace Contentless;
|
namespace Contentless;
|
||||||
|
|
||||||
|
@ -45,52 +44,29 @@ public static class Program {
|
||||||
var overrides = Program.GetOverrides(config.Overrides).ToArray();
|
var overrides = Program.GetOverrides(config.Overrides).ToArray();
|
||||||
|
|
||||||
var referencesVersions = config.References.ToDictionary(x => x, x => (string)null, StringComparer.OrdinalIgnoreCase);
|
var referencesVersions = config.References.ToDictionary(x => x, x => (string)null, StringComparer.OrdinalIgnoreCase);
|
||||||
if (config.References.Length > 0)
|
if (config.References.Length > 0) {
|
||||||
{
|
if (args.Length > 1) {
|
||||||
if (args.Length < 2) {
|
ExtractVersions(args[1], referencesVersions);
|
||||||
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");
|
_nuGetHelper = new NuGetHelper(Path.GetDirectoryName(args[1]));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
var csprojPath = args[1];
|
else
|
||||||
Console.WriteLine($"Using project file {csprojPath}");
|
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 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 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;
|
||||||
var reference = line.Substring(ReferenceHeader.Length);
|
var reference = line.Substring(ReferenceHeader.Length);
|
||||||
var libraryName = Path.GetFileName(reference)[..^4];
|
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, version);
|
||||||
var fullLibraryPath = CalculateFullPathToLibrary(libraryName, referencesVersions[libraryName]);
|
if (reference != fullLibraryPath) {
|
||||||
if (reference != fullLibraryPath)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Changing library reference from {reference} to {fullLibraryPath}");
|
Console.WriteLine($"Changing library reference from {reference} to {fullLibraryPath}");
|
||||||
reference = fullLibraryPath;
|
reference = fullLibraryPath;
|
||||||
content[i] = ReferenceHeader + fullLibraryPath;
|
content[i] = ReferenceHeader + fullLibraryPath;
|
||||||
|
@ -103,20 +79,37 @@ 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) {
|
||||||
{
|
try {
|
||||||
Console.WriteLine($"Please, add reference for {reference.Key} in .mgcb file or remove it from Contentless! Reference was skipped!");
|
var path = CalculateFullPathToLibrary(reference.Key, reference.Value);
|
||||||
return;
|
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}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -204,10 +197,43 @@ 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)
|
||||||
|
{
|
||||||
|
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)
|
private static string CalculateFullPathToLibrary(string libraryName, string referencesVersion)
|
||||||
{
|
{
|
||||||
var settings = Settings.LoadDefaultSettings(null);
|
return Path.Combine(_nuGetHelper.PackageFolder, libraryName.ToLower(), referencesVersion, "tools", libraryName + ".dll");
|
||||||
return Path.Combine(SettingsUtility.GetGlobalPackagesFolder(settings), libraryName.ToLower(), referencesVersion, "tools", libraryName + ".dll");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (List<ImporterInfo>, List<string>) GetContentData() {
|
private static (List<ImporterInfo>, List<string>) GetContentData() {
|
||||||
|
|
7
Test/NuGet.Config
Normal file
7
Test/NuGet.Config
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<?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