build.cake 7.0 KB

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