|
| 1 | +using ModFramework; |
| 2 | +using OTAPI.UnifiedServerProcess.Core.Patching.Framework; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace OTAPI.UnifiedServerProcess |
| 12 | +{ |
| 13 | + public class NugetPackageBuilder(string packageName, string nugetDocPath) |
| 14 | + { |
| 15 | + string GetNugetVersionFromAssembly(Assembly assembly) |
| 16 | + => assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion; |
| 17 | + |
| 18 | + string GetNugetVersionFromAssembly<TType>() |
| 19 | + => GetNugetVersionFromAssembly(typeof(TType).Assembly); |
| 20 | + |
| 21 | + public readonly string PackageFileName = packageName + ".nupkg"; |
| 22 | + public readonly string PackageMDFilePath = Path.Combine(nugetDocPath, packageName + ".md"); |
| 23 | + public readonly string NuspecFilePath = Path.Combine(nugetDocPath, packageName + ".nuspec"); |
| 24 | + |
| 25 | + public void Build(ModFwModder modder, string otapiVersion, DirectoryInfo outputDir) { |
| 26 | + var nuspec_xml = File.ReadAllText(NuspecFilePath); |
| 27 | + var md = File.ReadAllText(PackageMDFilePath); |
| 28 | + |
| 29 | + md = md.Replace("[INJECT_OTAPI_VERSION]", otapiVersion); |
| 30 | + nuspec_xml = nuspec_xml.Replace("[INJECT_OTAPI_VERSION]", otapiVersion); |
| 31 | + |
| 32 | + var version = GetNugetVersionFromAssembly<Patcher>(); |
| 33 | + var gitIndex = version.IndexOf('+'); |
| 34 | + if (gitIndex > -1) { |
| 35 | + var gitCommitSha = version[(gitIndex + 1)..]; |
| 36 | + version = version[..gitIndex]; |
| 37 | + nuspec_xml = nuspec_xml.Replace("[INJECT_GIT_HASH]", $" git#{gitCommitSha}"); |
| 38 | + } |
| 39 | + else { |
| 40 | + nuspec_xml = nuspec_xml.Replace("[INJECT_GIT_HASH]", ""); |
| 41 | + } |
| 42 | + nuspec_xml = nuspec_xml.Replace("[INJECT_VERSION]", version); |
| 43 | + |
| 44 | + var platforms = new[] { "net9.0" }; |
| 45 | + var steamworks = modder.Module.AssemblyReferences.First(x => x.Name == "Steamworks.NET"); |
| 46 | + var newtonsoft = modder.Module.AssemblyReferences.First(x => x.Name == "Newtonsoft.Json"); |
| 47 | + var dependencies = new[] |
| 48 | + { |
| 49 | + (typeof(ModFwModder).Assembly.GetName().Name, Version: GetNugetVersionFromAssembly<ModFwModder>()), |
| 50 | + (typeof(MonoMod.MonoModder).Assembly.GetName().Name, Version: typeof(MonoMod.MonoModder).Assembly.GetName().Version!.ToString()), |
| 51 | + (typeof(MonoMod.RuntimeDetour.Hook).Assembly.GetName().Name, Version: typeof(MonoMod.RuntimeDetour.Hook).Assembly.GetName().Version!.ToString()), |
| 52 | + (steamworks.Name, Version: steamworks.Version.ToString()), |
| 53 | + (newtonsoft.Name, Version: GetNugetVersionFromAssembly<Newtonsoft.Json.JsonConverter>().Split('+')[0] ), |
| 54 | + }; |
| 55 | + |
| 56 | + var xml_dependency = string.Join("", dependencies.Select(dep => $"\n\t <dependency id=\"{dep.Name}\" version=\"{dep.Version}\" />")); |
| 57 | + var xml_group = string.Join("", platforms.Select(platform => $"\n\t<group targetFramework=\"{platform}\">{xml_dependency}\n\t</group>")); |
| 58 | + var xml_dependencies = $"<dependencies>{xml_group}\n </dependencies>"; |
| 59 | + |
| 60 | + nuspec_xml = nuspec_xml.Replace("[INJECT_DEPENDENCIES]", xml_dependencies); |
| 61 | + |
| 62 | + nuspec_xml = nuspec_xml.Replace("[INJECT_YEAR]", DateTime.UtcNow.Year.ToString()); |
| 63 | + |
| 64 | + File.WriteAllText(Path.Combine(outputDir.FullName, "COPYING.txt"), File.ReadAllText("../../../../../LICENSE")); |
| 65 | + File.WriteAllText(packageName + ".md", md); |
| 66 | + |
| 67 | + using (var nuspec = new MemoryStream(Encoding.UTF8.GetBytes(nuspec_xml))) { |
| 68 | + var manifest = NuGet.Packaging.Manifest.ReadFrom(nuspec, validateSchema: true); |
| 69 | + var packageBuilder = new NuGet.Packaging.PackageBuilder(); |
| 70 | + packageBuilder.Populate(manifest.Metadata); |
| 71 | + |
| 72 | + packageBuilder.AddFiles(outputDir.FullName, "COPYING.txt", ""); |
| 73 | + |
| 74 | + foreach (var platform in platforms) { |
| 75 | + var dest = Path.Combine("lib", platform); |
| 76 | + packageBuilder.AddFiles(outputDir.FullName, "OTAPI.dll", dest); |
| 77 | + packageBuilder.AddFiles(outputDir.FullName, "OTAPI.Runtime.dll", dest); |
| 78 | + } |
| 79 | + |
| 80 | + if (File.Exists(PackageFileName)) |
| 81 | + File.Delete(PackageFileName); |
| 82 | + |
| 83 | + using (var srm = File.OpenWrite(PackageFileName)) { |
| 84 | + packageBuilder.Save(srm); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments