build.cake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, string separator = "")
  16. {
  17. using(var process = StartAndReturnProcess("git", new ProcessSettings { Arguments = command, RedirectStandardOutput = true }))
  18. {
  19. process.WaitForExit();
  20. return string.Join(separator, 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. var buildSettings = new MSBuildSettings {
  60. Configuration = "Release",
  61. Restore = true
  62. };
  63. buildSettings.Properties["TargetFrameworks"] = new []{ "net35" };
  64. MSBuild("./BepInEx.sln", buildSettings);
  65. })
  66. .Finally(() =>
  67. {
  68. var bepinExProperties = Directory("./BepInEx/Properties");
  69. if(isBleedingEdge)
  70. {
  71. DeleteFile(bepinExProperties + File("AssemblyInfo.cs"));
  72. MoveFile(bepinExProperties + File("AssemblyInfo.cs.bak"), bepinExProperties + File("AssemblyInfo.cs"));
  73. }
  74. });
  75. const string DOORSTOP_VER = "2.12.1.0";
  76. const string DOORSTOP_DLL = "winhttp.dll";
  77. Task("DownloadDoorstop")
  78. .Does(() =>
  79. {
  80. Information("Downloading Doorstop");
  81. var doorstopPath = Directory("./bin/doorstop");
  82. var doorstopX64Path = doorstopPath + File("doorstop_x64.zip");
  83. var doorstopX86Path = doorstopPath + File("doorstop_x86.zip");
  84. CreateDirectory(doorstopPath);
  85. DownloadFile($"https://github.com/NeighTools/UnityDoorstop/releases/download/v{DOORSTOP_VER}/Doorstop_x64_{DOORSTOP_VER}.zip", doorstopX64Path);
  86. DownloadFile($"https://github.com/NeighTools/UnityDoorstop/releases/download/v{DOORSTOP_VER}/Doorstop_x86_{DOORSTOP_VER}.zip", doorstopX86Path);
  87. Information("Extracting Doorstop");
  88. ZipUncompress(doorstopX86Path, doorstopPath + Directory("x86"));
  89. ZipUncompress(doorstopX64Path, doorstopPath + Directory("x64"));
  90. });
  91. Task("MakeDist")
  92. .IsDependentOn("Build")
  93. .IsDependentOn("DownloadDoorstop")
  94. .Does(() =>
  95. {
  96. var distDir = Directory("./bin/dist");
  97. var distPatcherDir = distDir + Directory("patcher");
  98. var doorstopPath = Directory("./bin/doorstop");
  99. CreateDirectory(distDir);
  100. CreateDirectory(distPatcherDir);
  101. var changelog = TransformText("<%commit_count%> commits since <%last_tag%>\r\n\r\nChangelog (excluding merges):\r\n<%commit_log%>")
  102. .WithToken("commit_count", RunGit($"rev-list --count {latestTag}..HEAD"))
  103. .WithToken("last_tag", latestTag)
  104. .WithToken("commit_log", RunGit($"--no-pager log --no-merges --pretty=\"format:* (%h) [%an] %s\" {latestTag}..HEAD", "\r\n"))
  105. .ToString();
  106. void PackageBepin(string arch)
  107. {
  108. var distArchDir = distDir + Directory(arch);
  109. var doorstopArchPath = doorstopPath + Directory(arch) + File(DOORSTOP_DLL);
  110. var bepinDir = distArchDir + Directory("BepInEx");
  111. CreateDirectory(distArchDir);
  112. CreateDirectory(bepinDir + Directory("core"));
  113. CreateDirectory(bepinDir + Directory("plugins"));
  114. CreateDirectory(bepinDir + Directory("patchers"));
  115. CopyFiles("./doorstop/*.*", distArchDir);
  116. CopyFiles("./bin/*.*", bepinDir + Directory("core"));
  117. CopyFileToDirectory(doorstopArchPath, distArchDir);
  118. FileWriteText(distArchDir + File("changelog.txt"), changelog);
  119. }
  120. PackageBepin("x86");
  121. PackageBepin("x64");
  122. CopyFileToDirectory(File("./bin/patcher/BepInEx.Patcher.exe"), distPatcherDir);
  123. });
  124. Task("Pack")
  125. .IsDependentOn("MakeDist")
  126. .Does(() =>
  127. {
  128. var distDir = Directory("./bin/dist");
  129. var commitPrefix = isBleedingEdge ? $"_{currentCommitShort}_" : "_";
  130. Information("Packing BepInEx");
  131. ZipCompress(distDir + Directory("x86"), distDir + File($"BepInEx_x86{commitPrefix}{buildVersion}.zip"));
  132. ZipCompress(distDir + Directory("x64"), distDir + File($"BepInEx_x64{commitPrefix}{buildVersion}.zip"));
  133. Information("Packing BepInEx.Patcher");
  134. ZipCompress(distDir + Directory("patcher"), distDir + File($"BepInEx_Patcher{commitPrefix}{buildVersion}.zip"));
  135. if(isBleedingEdge)
  136. {
  137. var changelog = "";
  138. if(!string.IsNullOrEmpty(lastBuildCommit)) {
  139. changelog = TransformText("<ul><%changelog%></ul>")
  140. .WithToken("changelog", RunGit($"--no-pager log --no-merges --pretty=\"format:<li>(<code>%h</code>) [%an] %s</li>\" {lastBuildCommit}..HEAD"))
  141. .ToString();
  142. }
  143. FileWriteText(distDir + File("info.json"),
  144. SerializeJsonPretty(new Dictionary<string, object>{
  145. ["id"] = buildId.ToString(),
  146. ["date"] = DateTime.Now.ToString("o"),
  147. ["changelog"] = changelog,
  148. ["hash"] = currentCommit,
  149. ["artifacts"] = new Dictionary<string, object>[] {
  150. new Dictionary<string, object> {
  151. ["file"] = $"BepInEx_x64{commitPrefix}{buildVersion}.zip",
  152. ["description"] = "BepInEx for x64 machines"
  153. },
  154. new Dictionary<string, object> {
  155. ["file"] = $"BepInEx_x86{commitPrefix}{buildVersion}.zip",
  156. ["description"] = "BepInEx for x86 machines"
  157. },
  158. new Dictionary<string, object> {
  159. ["file"] = $"BepInEx_Patcher{commitPrefix}{buildVersion}.zip",
  160. ["description"] = "Hardpatcher for BepInEx. IMPORTANT: USE ONLY IF DOORSTOP DOES NOT WORK FOR SOME REASON!"
  161. }
  162. }
  163. }));
  164. }
  165. });
  166. RunTarget(target);