build.cake 7.4 KB

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