build.cake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.Core/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.Unity/BepInEx.Unity.csproj", buildSettings);
  65. //buildSettings.Properties["TargetFrameworks"] = new []{ "net452" };
  66. MSBuild("./BepInEx.NetLauncher/BepInEx.NetLauncher.csproj", buildSettings);
  67. //buildSettings.Properties["TargetFrameworks"] = new []{ "net472" };
  68. MSBuild("./BepInEx.IL2CPP/BepInEx.IL2CPP.csproj", buildSettings);
  69. })
  70. .Finally(() =>
  71. {
  72. var bepinExProperties = Directory("./BepInEx.Core/Properties");
  73. if(isBleedingEdge)
  74. {
  75. DeleteFile(bepinExProperties + File("AssemblyInfo.cs"));
  76. MoveFile(bepinExProperties + File("AssemblyInfo.cs.bak"), bepinExProperties + File("AssemblyInfo.cs"));
  77. }
  78. });
  79. const string DOORSTOP_VER_WIN = "3.0.2.2";
  80. const string DOORSTOP_VER_UNIX = "1.3.0.0";
  81. const string MONO_VER = "2020.11.08";
  82. const string DOORSTOP_DLL = "winhttp.dll";
  83. Task("DownloadDependencies")
  84. .Does(() =>
  85. {
  86. Information("Downloading Doorstop");
  87. var doorstopPath = Directory("./bin/doorstop");
  88. var doorstopX64Path = doorstopPath + File("doorstop_x64.zip");
  89. var doorstopX86Path = doorstopPath + File("doorstop_x86.zip");
  90. var doorstopLinuxPath = doorstopPath + File("doorstop_linux.zip");
  91. var doorstopMacPath = doorstopPath + File("doorstop_macos.zip");
  92. CreateDirectory(doorstopPath);
  93. DownloadFile($"https://github.com/NeighTools/UnityDoorstop/releases/download/v{DOORSTOP_VER_WIN}/Doorstop_x64_{DOORSTOP_VER_WIN}.zip", doorstopX64Path);
  94. DownloadFile($"https://github.com/NeighTools/UnityDoorstop/releases/download/v{DOORSTOP_VER_WIN}/Doorstop_x86_{DOORSTOP_VER_WIN}.zip", doorstopX86Path);
  95. DownloadFile($"https://github.com/NeighTools/UnityDoorstop.Unix/releases/download/v{DOORSTOP_VER_UNIX}/doorstop_v{DOORSTOP_VER_UNIX}_linux.zip", doorstopLinuxPath);
  96. DownloadFile($"https://github.com/NeighTools/UnityDoorstop.Unix/releases/download/v{DOORSTOP_VER_UNIX}/doorstop_v{DOORSTOP_VER_UNIX}_macos.zip", doorstopMacPath);
  97. Information("Extracting Doorstop");
  98. ZipUncompress(doorstopX86Path, doorstopPath + Directory("x86"));
  99. ZipUncompress(doorstopX64Path, doorstopPath + Directory("x64"));
  100. ZipUncompress(doorstopLinuxPath, doorstopPath + Directory("unix"));
  101. ZipUncompress(doorstopMacPath, doorstopPath + Directory("unix"));
  102. Information("Downloading Mono");
  103. var monoPath = Directory("./bin/doorstop/mono");
  104. var monoX64Path = doorstopPath + File("mono_x64.zip");
  105. var monoX86Path = doorstopPath + File("mono_x86.zip");
  106. CreateDirectory(monoPath);
  107. DownloadFile($"https://github.com/BepInEx/mono/releases/download/{MONO_VER}/mono-x64.zip", monoX64Path);
  108. DownloadFile($"https://github.com/BepInEx/mono/releases/download/{MONO_VER}/mono-x86.zip", monoX86Path);
  109. Information("Extracting Mono");
  110. ZipUncompress(monoX64Path, monoPath + Directory("x64"));
  111. ZipUncompress(monoX86Path, monoPath + Directory("x86"));
  112. });
  113. Task("MakeDist")
  114. .IsDependentOn("Build")
  115. .IsDependentOn("DownloadDependencies")
  116. .Does(() =>
  117. {
  118. var distDir = Directory("./bin/dist");
  119. //var distPatcherDir = distDir + Directory("patcher");
  120. var doorstopPath = Directory("./bin/doorstop");
  121. CreateDirectory(distDir);
  122. //CreateDirectory(distPatcherDir);
  123. var changelog = TransformText("<%commit_count%> commits since <%last_tag%>\r\n\r\nChangelog (excluding merges):\r\n<%commit_log%>")
  124. .WithToken("commit_count", RunGit($"rev-list --count {latestTag}..HEAD"))
  125. .WithToken("last_tag", latestTag)
  126. .WithToken("commit_log", RunGit($"--no-pager log --no-merges --pretty=\"format:* (%h) [%an] %s\" {latestTag}..HEAD", "\r\n"))
  127. .ToString();
  128. void PackageBepin(string platform, string arch, string originDir, string doorstopConfigFile = null, bool copyMono = false)
  129. {
  130. string platformName = platform + (arch == null ? "" : "_" + arch);
  131. bool isUnix = arch == "unix";
  132. Information("Creating distributions for platform \"" + platformName + "\"...");
  133. string doorstopArchPath = null;
  134. if (arch != null)
  135. {
  136. doorstopArchPath = doorstopPath + Directory(arch)
  137. + File(isUnix ? "*.*" : DOORSTOP_DLL);
  138. }
  139. var distArchDir = distDir + Directory(platformName);
  140. var bepinDir = distArchDir + Directory("BepInEx");
  141. var doorstopDir = distArchDir;
  142. if (isUnix) doorstopDir += Directory("doorstop_libs");
  143. CreateDirectory(distArchDir);
  144. CreateDirectory(doorstopDir);
  145. CreateDirectory(bepinDir + Directory("core"));
  146. CreateDirectory(bepinDir + Directory("plugins"));
  147. CreateDirectory(bepinDir + Directory("patchers"));
  148. if (doorstopArchPath != null)
  149. {
  150. CopyFile("./doorstop/" + doorstopConfigFile, Directory(distArchDir) + File(isUnix ? "run_bepinex.sh" : "doorstop_config.ini"));
  151. CopyFiles(doorstopArchPath, doorstopDir);
  152. if (isUnix)
  153. {
  154. ReplaceTextInFiles($"{distArchDir}/run_bepinex.sh", "\r\n", "\n");
  155. }
  156. if (copyMono)
  157. {
  158. CopyDirectory("./bin/doorstop/mono/" + arch + "/mono", Directory(distArchDir) + Directory("mono"));
  159. }
  160. }
  161. CopyFiles("./bin/" + Directory(originDir) + "/*.*", Directory(bepinDir) + Directory("core"));
  162. FileWriteText(distArchDir + File("changelog.txt"), changelog);
  163. }
  164. PackageBepin("UnityMono", "x86", "Unity", "doorstop_config_mono.ini");
  165. PackageBepin("UnityMono", "x64", "Unity", "doorstop_config_mono.ini");
  166. PackageBepin("UnityMono", "unix", "Unity", "run_bepinex.sh");
  167. PackageBepin("UnityIL2CPP", "x86", "IL2CPP", "doorstop_config_il2cpp.ini", copyMono: true);
  168. PackageBepin("UnityIL2CPP", "x64", "IL2CPP", "doorstop_config_il2cpp.ini", copyMono: true);
  169. PackageBepin("NetLauncher", null, "NetLauncher");
  170. //CopyFileToDirectory(File("./bin/patcher/BepInEx.Patcher.exe"), distPatcherDir);
  171. });
  172. Task("Pack")
  173. .IsDependentOn("MakeDist")
  174. .Does(() =>
  175. {
  176. var distDir = Directory("./bin/dist");
  177. var commitPrefix = isBleedingEdge ? $"_{currentCommitShort}_" : "_";
  178. Information("Packing BepInEx");
  179. ZipCompress(distDir + Directory("UnityMono_x86"), distDir + File($"BepInEx_UnityMono_x86{commitPrefix}{buildVersion}.zip"));
  180. ZipCompress(distDir + Directory("UnityMono_x64"), distDir + File($"BepInEx_UnityMono_x64{commitPrefix}{buildVersion}.zip"));
  181. ZipCompress(distDir + Directory("UnityMono_unix"), distDir + File($"BepInEx_UnityMono_unix{commitPrefix}{buildVersion}.zip"));
  182. ZipCompress(distDir + Directory("UnityIL2CPP_x86"), distDir + File($"BepInEx_UnityIL2CPP_x86{commitPrefix}{buildVersion}.zip"));
  183. ZipCompress(distDir + Directory("UnityIL2CPP_x64"), distDir + File($"BepInEx_UnityIL2CPP_x64{commitPrefix}{buildVersion}.zip"));
  184. ZipCompress(distDir + Directory("NetLauncher"), distDir + File($"BepInEx_NetLauncher{commitPrefix}{buildVersion}.zip"));
  185. // Information("Packing BepInEx.Patcher");
  186. // ZipCompress(distDir + Directory("patcher"), distDir + File($"BepInEx_Patcher{commitPrefix}{buildVersion}.zip"));
  187. if(isBleedingEdge)
  188. {
  189. var changelog = "";
  190. if(!string.IsNullOrEmpty(lastBuildCommit)) {
  191. changelog = TransformText("<ul><%changelog%></ul>")
  192. .WithToken("changelog", RunGit($"--no-pager log --no-merges --pretty=\"format:<li>(<code>%h</code>) [%an] %s</li>\" {lastBuildCommit}..HEAD"))
  193. .ToString();
  194. }
  195. FileWriteText(distDir + File("info.json"),
  196. SerializeJsonPretty(new Dictionary<string, object>{
  197. ["id"] = buildId.ToString(),
  198. ["date"] = DateTime.Now.ToString("o"),
  199. ["changelog"] = changelog,
  200. ["hash"] = currentCommit,
  201. ["artifacts"] = new Dictionary<string, object>[] {
  202. new Dictionary<string, object> {
  203. ["file"] = $"BepInEx_UnityMono_x64{commitPrefix}{buildVersion}.zip",
  204. ["description"] = "BepInEx Unity Mono for Windows x64 machines"
  205. },
  206. new Dictionary<string, object> {
  207. ["file"] = $"BepInEx_UnityMono_x86{commitPrefix}{buildVersion}.zip",
  208. ["description"] = "BepInEx Unity Mono for Windows x86 machines"
  209. },
  210. new Dictionary<string, object> {
  211. ["file"] = $"BepInEx_unix{commitPrefix}{buildVersion}.zip",
  212. ["description"] = "BepInEx Unity Mono for Unix machines with GCC (Linux, MacOS)"
  213. },
  214. new Dictionary<string, object> {
  215. ["file"] = $"BepInEx_UnityIL2CPP_x64{commitPrefix}{buildVersion}.zip",
  216. ["description"] = "BepInEx Unity IL2CPP for Windows x64 machines"
  217. },
  218. new Dictionary<string, object> {
  219. ["file"] = $"BepInEx_UnityIL2CPP_x86{commitPrefix}{buildVersion}.zip",
  220. ["description"] = "BepInEx Unity IL2CPP for Windows x86 machines"
  221. },
  222. new Dictionary<string, object> {
  223. ["file"] = $"BepInEx_NetLauncher{commitPrefix}{buildVersion}.zip",
  224. ["description"] = "BepInEx .NET Launcher for .NET Framework/XNA games"
  225. },
  226. // new Dictionary<string, object> {
  227. // ["file"] = $"BepInEx_Patcher{commitPrefix}{buildVersion}.zip",
  228. // ["description"] = "Hardpatcher for BepInEx. IMPORTANT: USE ONLY IF DOORSTOP DOES NOT WORK FOR SOME REASON!"
  229. // }
  230. }
  231. }));
  232. }
  233. });
  234. RunTarget(target);