mirror of
https://github.com/Ellpeck/Contentless.git
synced 2024-11-28 01:38:34 +01:00
Compare commits
No commits in common. "93bbcf5cbd85c2f5c65d54aff4990ba207e4c90f" and "c45138a08409827166666d245f9321f666148d1d" have entirely different histories.
93bbcf5cbd
...
c45138a084
4 changed files with 69 additions and 92 deletions
20
.github/workflows/main.yml
vendored
20
.github/workflows/main.yml
vendored
|
@ -1,20 +0,0 @@
|
||||||
on: [push, pull_request]
|
|
||||||
jobs:
|
|
||||||
main:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Clone repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
submodules: recursive
|
|
||||||
- name: Setup .NET
|
|
||||||
uses: actions/setup-dotnet@v3
|
|
||||||
with:
|
|
||||||
dotnet-version: '8.0.x'
|
|
||||||
- name: Build
|
|
||||||
run: dotnet build Contentless
|
|
||||||
- name: Build test project
|
|
||||||
run: |
|
|
||||||
dotnet restore Test
|
|
||||||
dotnet run --project Contentless Test/Content/Content.mgcb Test/Test.csproj
|
|
||||||
dotnet build Test
|
|
11
.woodpecker/main.yml
Normal file
11
.woodpecker/main.yml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
steps:
|
||||||
|
build:
|
||||||
|
image: mcr.microsoft.com/dotnet/sdk:8.0-jammy
|
||||||
|
commands:
|
||||||
|
- dotnet build Contentless
|
||||||
|
test:
|
||||||
|
image: mcr.microsoft.com/dotnet/sdk:8.0-jammy
|
||||||
|
commands:
|
||||||
|
- dotnet restore Test
|
||||||
|
- dotnet run --project Contentless Test/Content/Content.mgcb Test/Test.csproj
|
||||||
|
- dotnet build Test
|
|
@ -10,7 +10,7 @@
|
||||||
<repository type="git" url="https://github.com/Ellpeck/Contentless" />
|
<repository type="git" url="https://github.com/Ellpeck/Contentless" />
|
||||||
<readme>README.md</readme>
|
<readme>README.md</readme>
|
||||||
<icon>Logo.png</icon>
|
<icon>Logo.png</icon>
|
||||||
<version>3.2.0</version>
|
<version>3.1.3</version>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<group targetFramework="net8.0" />
|
<group targetFramework="net8.0" />
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
@ -19,36 +19,7 @@ public static class Program {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find global packages folder and installed packages if we supplied a project file
|
var contentFile = new FileInfo(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, args[0])));
|
||||||
string packagesFolder = null;
|
|
||||||
Dictionary<string, string> installedPackages = null;
|
|
||||||
if (args.Length >= 2) {
|
|
||||||
var projectPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, args[1]));
|
|
||||||
if (!File.Exists(projectPath) || Path.GetExtension(projectPath) != ".csproj") {
|
|
||||||
Console.Error.WriteLine($"Unable to find valid project file at {projectPath}");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
installedPackages = Program.ExtractPackagesFromProject(projectPath);
|
|
||||||
Console.WriteLine($"Found package dependencies {string.Join(", ", installedPackages.Select(kv => $"{kv.Key}@{kv.Value}"))} in project {projectPath}");
|
|
||||||
|
|
||||||
var settings = Settings.LoadDefaultSettings(Path.GetDirectoryName(projectPath));
|
|
||||||
packagesFolder = SettingsUtility.GetGlobalPackagesFolder(settings);
|
|
||||||
Console.WriteLine($"Using global packages folder {packagesFolder}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// iterate all project files (semicolon-delimited) and process them
|
|
||||||
var contentFiles = args[0].Split(';').Select(p => Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, p.Trim()))).Distinct();
|
|
||||||
foreach (var file in contentFiles) {
|
|
||||||
var ret = Program.ProcessContentFile(new FileInfo(file), packagesFolder, installedPackages);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int ProcessContentFile(FileInfo contentFile, string packagesFolder, Dictionary<string, string> installedPackages) {
|
|
||||||
if (!contentFile.Exists || contentFile.Extension != ".mgcb") {
|
if (!contentFile.Exists || contentFile.Extension != ".mgcb") {
|
||||||
Console.Error.WriteLine($"Unable to find valid content file at {contentFile}");
|
Console.Error.WriteLine($"Unable to find valid content file at {contentFile}");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -72,14 +43,30 @@ public static class Program {
|
||||||
} else {
|
} else {
|
||||||
Console.WriteLine("Using default config");
|
Console.WriteLine("Using default config");
|
||||||
}
|
}
|
||||||
if (config.References.Length > 0 && packagesFolder == null)
|
var excluded = config.ExcludedFiles.Select(Program.MakeFileRegex).ToArray();
|
||||||
Console.Error.WriteLine("The config file contains references, but no valid project data was found. Please specify the location of the project file you want to use for gathering references as the second argument.");
|
var overrides = Program.GetOverrides(config.Overrides).ToArray();
|
||||||
|
|
||||||
var changed = false;
|
string packagesFolder = null;
|
||||||
|
var referencesVersions = config.References.ToDictionary(x => x, _ => (string) null, StringComparer.OrdinalIgnoreCase);
|
||||||
|
if (config.References.Length > 0) {
|
||||||
|
if (args.Length > 1) {
|
||||||
|
var csprojFullPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, args[1]));
|
||||||
|
if (!File.Exists(csprojFullPath) || Path.GetExtension(csprojFullPath) != ".csproj") {
|
||||||
|
Console.Error.WriteLine($"Unable to find valid project file at {contentFile}");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
Program.ExtractVersions(csprojFullPath, referencesVersions);
|
||||||
|
var settings = Settings.LoadDefaultSettings(Path.GetDirectoryName(csprojFullPath));
|
||||||
|
packagesFolder = SettingsUtility.GetGlobalPackagesFolder(settings);
|
||||||
|
} else {
|
||||||
|
Console.Error.WriteLine("The config file contains references, but no project file was specified. Please specify the location of the content file you want to use for gathering references as the second argument.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// load refereces and replace reference paths if they're mismatched
|
|
||||||
var referencesNotInContentFile = config.References.ToHashSet();
|
|
||||||
const string referenceHeader = "/reference:";
|
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 (var i = 0; i < content.Count; i++) {
|
for (var i = 0; i < content.Count; i++) {
|
||||||
var line = content[i];
|
var line = content[i];
|
||||||
if (!line.StartsWith(referenceHeader))
|
if (!line.StartsWith(referenceHeader))
|
||||||
|
@ -87,21 +74,18 @@ public static class Program {
|
||||||
var reference = line[referenceHeader.Length..];
|
var reference = line[referenceHeader.Length..];
|
||||||
var libraryName = Path.GetFileName(reference)[..^4];
|
var libraryName = Path.GetFileName(reference)[..^4];
|
||||||
|
|
||||||
if (referencesNotInContentFile.Remove(libraryName)) {
|
if (referencesVersions.TryGetValue(libraryName, out var version) && version is not null) {
|
||||||
if (installedPackages.TryGetValue(libraryName, out var version)) {
|
var fullLibraryPath = Program.CalculateFullPathToLibrary(packagesFolder, libraryName, version);
|
||||||
var fullLibraryPath = Program.CalculateFullPathToLibrary(packagesFolder, libraryName, version);
|
if (reference != fullLibraryPath) {
|
||||||
if (reference != fullLibraryPath) {
|
Console.WriteLine($"Changing reference from {reference} to {fullLibraryPath}");
|
||||||
Console.WriteLine($"Changing reference from {reference} to {fullLibraryPath}");
|
reference = fullLibraryPath;
|
||||||
reference = fullLibraryPath;
|
content[i] = referenceHeader + fullLibraryPath;
|
||||||
content[i] = referenceHeader + fullLibraryPath;
|
changed = true;
|
||||||
changed = true;
|
|
||||||
} else {
|
|
||||||
if (config.LogSkipped)
|
|
||||||
Console.WriteLine($"Skipping reference replacement for {fullLibraryPath} which already matched");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Console.Error.WriteLine($"Unable to find existing reference {libraryName} in project file");
|
if (config.LogSkipped)
|
||||||
|
Console.WriteLine($"Skipping reference replacement for {fullLibraryPath} which already matched");
|
||||||
}
|
}
|
||||||
|
referencesSyncs.Add(libraryName);
|
||||||
}
|
}
|
||||||
|
|
||||||
var refPath = Path.GetFullPath(Path.Combine(contentFile.DirectoryName, reference));
|
var refPath = Path.GetFullPath(Path.Combine(contentFile.DirectoryName, reference));
|
||||||
|
@ -109,42 +93,37 @@ public static class Program {
|
||||||
Console.WriteLine($"Using reference {refPath}");
|
Console.WriteLine($"Using reference {refPath}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the line we want to start adding new references from
|
// check references not in .mgcb now
|
||||||
var lastReferenceLine = 0;
|
var referencesLastIndex = 0;
|
||||||
|
// find place where I can add new reference
|
||||||
for (var i = 0; i < content.Count; i++) {
|
for (var i = 0; i < content.Count; i++) {
|
||||||
var line = content[i];
|
var line = content[i];
|
||||||
if (line.StartsWith(referenceHeader)) {
|
if (line.StartsWith(referenceHeader)) {
|
||||||
lastReferenceLine = i + 1;
|
referencesLastIndex = i + 1;
|
||||||
} else if (line.StartsWith("/importer:") || line.StartsWith("/processor:") || line.StartsWith("/build:") || line.Contains("-- Content --")) {
|
} else if (line.StartsWith("/importer:") || line.StartsWith("/processor:") || line.StartsWith("/build:") || line.Contains("-- Content --")) {
|
||||||
if (lastReferenceLine == 0)
|
if (referencesLastIndex == 0)
|
||||||
lastReferenceLine = i;
|
referencesLastIndex = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// add references that aren't in the content file yet
|
foreach (var reference in referencesVersions)
|
||||||
foreach (var reference in referencesNotInContentFile) {
|
if (!referencesSyncs.Contains(reference.Key) && reference.Value is not null) {
|
||||||
if (installedPackages.TryGetValue(reference, out var version)) {
|
|
||||||
try {
|
try {
|
||||||
var path = Program.CalculateFullPathToLibrary(packagesFolder, reference, version);
|
var path = Program.CalculateFullPathToLibrary(packagesFolder, reference.Key, reference.Value);
|
||||||
content.Insert(lastReferenceLine++, referenceHeader + path);
|
content.Insert(referencesLastIndex++, referenceHeader + path);
|
||||||
changed = true;
|
changed = true;
|
||||||
Program.SafeAssemblyLoad(path);
|
Program.SafeAssemblyLoad(path);
|
||||||
Console.WriteLine($"Adding reference {path}");
|
Console.WriteLine($"Adding reference {path}");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Console.Error.WriteLine($"Error adding reference {reference}: {e}");
|
Console.Error.WriteLine($"Error adding reference {reference.Key} {e}");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Console.Error.WriteLine($"Unable to find configured reference {reference} in project file");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// load content importers
|
// load content importers
|
||||||
var (importers, processors) = Program.GetContentData();
|
var (importers, processors) = Program.GetContentData();
|
||||||
Console.WriteLine($"Found possible importer types {string.Join(", ", importers)}");
|
Console.WriteLine($"Found possible importer types {string.Join(", ", importers)}");
|
||||||
Console.WriteLine($"Found possible processor types {string.Join(", ", processors)}");
|
Console.WriteLine($"Found possible processor types {string.Join(", ", processors)}");
|
||||||
|
|
||||||
var excluded = config.ExcludedFiles.Select(Program.MakeFileRegex).ToArray();
|
|
||||||
var overrides = Program.GetOverrides(config.Overrides).ToArray();
|
|
||||||
foreach (var file in contentFile.Directory.EnumerateFiles("*", SearchOption.AllDirectories)) {
|
foreach (var file in contentFile.Directory.EnumerateFiles("*", SearchOption.AllDirectories)) {
|
||||||
// is the file the content or config file?
|
// is the file the content or config file?
|
||||||
if (file.Name == contentFile.Name || file.Name == configFile.Name)
|
if (file.Name == contentFile.Name || file.Name == configFile.Name)
|
||||||
|
@ -221,7 +200,7 @@ public static class Program {
|
||||||
}
|
}
|
||||||
Console.WriteLine("Wrote changes to content file");
|
Console.WriteLine("Wrote changes to content file");
|
||||||
}
|
}
|
||||||
Console.WriteLine("Done");
|
Console.Write("Done");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,20 +208,27 @@ public static class Program {
|
||||||
try {
|
try {
|
||||||
Assembly.LoadFrom(refPath);
|
Assembly.LoadFrom(refPath);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Console.Error.WriteLine($"Error loading reference {refPath}: {e}");
|
Console.Error.WriteLine($"Error loading reference {refPath} {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<string, string> ExtractPackagesFromProject(string csprojPath) {
|
private static void ExtractVersions(string csprojPath, Dictionary<string, string> referencesVersions) {
|
||||||
var ret = new Dictionary<string, string>();
|
Console.WriteLine($"Using project file {csprojPath}");
|
||||||
var projectRootElement = ProjectRootElement.Open(csprojPath);
|
var projectRootElement = ProjectRootElement.Open(csprojPath);
|
||||||
foreach (var property in projectRootElement.AllChildren.Where(x => x.ElementName == "PackageReference").Select(x => x as ProjectItemElement)) {
|
foreach (var property in projectRootElement.AllChildren.Where(x => x.ElementName == "PackageReference").Select(x => x as ProjectItemElement)) {
|
||||||
var libraryName = property.Include;
|
var libraryName = property.Include;
|
||||||
if (property.Children.FirstOrDefault(x => x.ElementName == "Version") is not ProjectMetadataElement versionElement)
|
if (property.Children.FirstOrDefault(x => x.ElementName == "Version") is not ProjectMetadataElement versionElement)
|
||||||
continue;
|
continue;
|
||||||
ret.Add(libraryName, versionElement.Value);
|
var version = versionElement.Value;
|
||||||
|
if (referencesVersions.ContainsKey(libraryName)) {
|
||||||
|
referencesVersions[libraryName] = version;
|
||||||
|
Console.WriteLine($"Found reference {libraryName} {version} in project file");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
|
foreach (var library in referencesVersions)
|
||||||
|
if (library.Value is null)
|
||||||
|
Console.Error.WriteLine($"Unable to find reference {library.Key} in project file");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string CalculateFullPathToLibrary(string packageFolder, string libraryName, string referencesVersion) {
|
private static string CalculateFullPathToLibrary(string packageFolder, string libraryName, string referencesVersion) {
|
||||||
|
|
Loading…
Reference in a new issue