build.cake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #addin nuget:?package=Cake.FileHelpers&version=3.2.1
  2. #addin nuget:?package=Cake.Git&version=0.21.0
  3. #addin nuget:?package=SharpZipLib&version=1.2.0
  4. #addin nuget:?package=Cake.Compression&version=0.2.4
  5. #addin nuget:?package=Cake.Json&version=4.0.0
  6. #addin nuget:?package=Newtonsoft.Json&version=11.0.2
  7. var target = Argument("target", "Build");
  8. var isBleedingEdge = Argument("bleeding_edge", false);
  9. var buildId = Argument("build_id", 0);
  10. var buildVersion = "";
  11. Task("Cleanup")
  12. .Does(() =>
  13. {
  14. Information("Removing old binaries");
  15. CreateDirectory("./bin");
  16. CleanDirectory("./bin");
  17. Information("Cleaning up old build objects");
  18. CleanDirectories(GetDirectories("./**/bin/"));
  19. CleanDirectories(GetDirectories("./**/obj/"));
  20. });
  21. Task("PullDependencies")
  22. .Does(() =>
  23. {
  24. Information("Updating git submodules");
  25. StartProcess("git", "submodule update --init --recursive");
  26. Information("Restoring NuGet packages");
  27. NuGetRestore("./BepInEx.sln");
  28. });
  29. Task("Build")
  30. .IsDependentOn("Cleanup")
  31. .IsDependentOn("PullDependencies")
  32. .Does(() =>
  33. {
  34. var bepinExProperties = Directory("./BepInEx/Properties");
  35. if(isBleedingEdge)
  36. {
  37. CopyFile(bepinExProperties + File("AssemblyInfo.cs"), bepinExProperties + File("AssemblyInfo.cs.bak"));
  38. ReplaceRegexInFiles(bepinExProperties + File("AssemblyInfo.cs"), "([0-9]+\\.[0-9]+\\.[0-9]+\\.)[0-9]+", "${1}" + buildId);
  39. FileAppendText(bepinExProperties + File("AssemblyInfo.cs"),
  40. TransformText("\n[assembly: BuildInfo(\"BLEEDING EDGE Build #<%buildNumber%> from <%shortCommit%> at <%branchName%>\")]\n")
  41. .WithToken("buildNumber", buildId)
  42. .WithToken("shortCommit", GitLogTip("./").Sha)
  43. .WithToken("branchName", GitBranchCurrent("./").FriendlyName)
  44. .ToString());
  45. }
  46. buildVersion = FindRegexMatchInFile(bepinExProperties + File("AssemblyInfo.cs"), "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", System.Text.RegularExpressions.RegexOptions.None);
  47. MSBuild("./BepInEx.sln", new MSBuildSettings {
  48. Configuration = "Release"
  49. });
  50. })
  51. .Finally(() =>
  52. {
  53. var bepinExProperties = Directory("./BepInEx/Properties");
  54. if(isBleedingEdge)
  55. {
  56. DeleteFile(bepinExProperties + File("AssemblyInfo.cs"));
  57. MoveFile(bepinExProperties + File("AssemblyInfo.cs.bak"), bepinExProperties + File("AssemblyInfo.cs"));
  58. }
  59. });
  60. const string DOORSTOP_VER = "2.11.1.0";
  61. const string DOORSTOP_DLL = "winhttp.dll";
  62. Task("DownloadDoorstop")
  63. .Does(() =>
  64. {
  65. Information("Downloading Doorstop");
  66. var doorstopPath = Directory("./bin/doorstop");
  67. var doorstopX64Path = doorstopPath + File("doorstop_x64.zip");
  68. var doorstopX86Path = doorstopPath + File("doorstop_x86.zip");
  69. CreateDirectory(doorstopPath);
  70. DownloadFile($"https://github.com/NeighTools/UnityDoorstop/releases/download/v{DOORSTOP_VER}/Doorstop_x64_{DOORSTOP_VER}.zip", doorstopX64Path);
  71. DownloadFile($"https://github.com/NeighTools/UnityDoorstop/releases/download/v{DOORSTOP_VER}/Doorstop_x86_{DOORSTOP_VER}.zip", doorstopX86Path);
  72. Information("Extracting Doorstop");
  73. ZipUncompress(doorstopX86Path, doorstopPath + Directory("x86"));
  74. ZipUncompress(doorstopX64Path, doorstopPath + Directory("x64"));
  75. });
  76. Task("MakeDist")
  77. .IsDependentOn("Build")
  78. .IsDependentOn("DownloadDoorstop")
  79. .Does(() =>
  80. {
  81. var distDir = Directory("./bin/dist");
  82. var distPatcherDir = distDir + Directory("patcher");
  83. var doorstopPath = Directory("./bin/doorstop");
  84. CreateDirectory(distDir);
  85. CreateDirectory(distPatcherDir);
  86. var latestTag = GitTags(Directory(".")).Last();
  87. var commits = GitLog(Directory("."), latestTag.CanonicalName);
  88. var changelog = TransformText("<%commit_count%> commits since <%last_tag%>\r\n\r\nChangelog (excluding merges):\r\n<%commit_log%>")
  89. .WithToken("commit_count", commits.Count)
  90. .WithToken("last_tag", latestTag.FriendlyName)
  91. .WithToken("commit_log",
  92. string.Join("\r\n", commits.Where(commit => !commit.MessageShort.StartsWith("Merge branch"))
  93. .Select(commit => TransformText("* (<%commit_sha%>) [<%commit_author%>] <%commit_message%>")
  94. .WithToken("commit_sha", commit.Sha.Substring(0, 16))
  95. .WithToken("commit_author", commit.Author.Name)
  96. .WithToken("commit_message", commit.MessageShort)
  97. .ToString())))
  98. .ToString();
  99. void PackageBepin(string arch)
  100. {
  101. var distArchDir = distDir + Directory(arch);
  102. var doorstopArchPath = doorstopPath + Directory(arch) + File(DOORSTOP_DLL);
  103. var bepinDir = distArchDir + Directory("BepInEx");
  104. CreateDirectory(distArchDir);
  105. CreateDirectory(bepinDir + Directory("core"));
  106. CreateDirectory(bepinDir + Directory("plugins"));
  107. CreateDirectory(bepinDir + Directory("patchers"));
  108. CopyFiles("./bin/*.*", bepinDir + Directory("core"));
  109. CopyFileToDirectory(doorstopArchPath, distArchDir);
  110. FileWriteText(distArchDir + File("changelog.txt"), changelog);
  111. }
  112. PackageBepin("x86");
  113. PackageBepin("x64");
  114. CopyFileToDirectory(File("./bin/patcher/BepInEx.Patcher.exe"), distPatcherDir);
  115. });
  116. Task("Pack")
  117. .IsDependentOn("MakeDist")
  118. .Does(() =>
  119. {
  120. var distDir = Directory("./bin/dist");
  121. var currentCommit = GitLogTip(Directory("."));
  122. var commitPrefix = isBleedingEdge ? $"_{currentCommit.Sha.Substring(0, 8)}_" : "_";
  123. Information("Packing BepInEx");
  124. ZipCompress(distDir + Directory("x86"), distDir + File($"BepInEx_x86{commitPrefix}{buildVersion}.zip"));
  125. ZipCompress(distDir + Directory("x64"), distDir + File($"BepInEx_x64{commitPrefix}{buildVersion}.zip"));
  126. Information("Packing BepInEx.Patcher");
  127. ZipCompress(distDir + Directory("patcher"), distDir + File($"BepInEx_Patcher{commitPrefix}{buildVersion}.zip"));
  128. if(isBleedingEdge)
  129. {
  130. FileWriteText(distDir + File("info.json"),
  131. SerializeJsonPretty(new Dictionary<string, object>{
  132. ["id"] = buildId.ToString(),
  133. ["date"] = DateTime.Now.ToString("o"),
  134. ["changelog"] = "",
  135. ["artifacts"] = new Dictionary<string, object>[] {
  136. new Dictionary<string, object> {
  137. ["file"] = $"BepInEx_x64{commitPrefix}{buildVersion}.zip",
  138. ["description"] = "BepInEx for x64 machines"
  139. },
  140. new Dictionary<string, object> {
  141. ["file"] = $"BepInEx_x86{commitPrefix}{buildVersion}.zip",
  142. ["description"] = "BepInEx for x86 machines"
  143. },
  144. new Dictionary<string, object> {
  145. ["file"] = $"BepInEx_Patcher{commitPrefix}{buildVersion}.zip",
  146. ["description"] = "Hardpatcher for BepInEx. IMPORTANT: USE ONLY IF DOORSTOP DOES NOT WORK FOR SOME REASON!"
  147. }
  148. },
  149. ["commit"] = currentCommit.Sha
  150. }));
  151. }
  152. });
  153. RunTarget(target);