build.cake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.1.0.0";
  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. if (platform == "NetLauncher")
  164. {
  165. DeleteFile(Directory(bepinDir) + Directory("core") + File("BepInEx.NetLauncher.exe.config"));
  166. MoveFiles(Directory(bepinDir) + Directory("core") + File("BepInEx.NetLauncher.*"), Directory(distArchDir));
  167. }
  168. }
  169. PackageBepin("UnityMono", "x86", "Unity", "doorstop_config_mono.ini");
  170. PackageBepin("UnityMono", "x64", "Unity", "doorstop_config_mono.ini");
  171. PackageBepin("UnityMono", "unix", "Unity", "run_bepinex.sh");
  172. PackageBepin("UnityIL2CPP", "x86", "IL2CPP", "doorstop_config_il2cpp.ini", copyMono: true);
  173. PackageBepin("UnityIL2CPP", "x64", "IL2CPP", "doorstop_config_il2cpp.ini", copyMono: true);
  174. PackageBepin("NetLauncher", null, "NetLauncher");
  175. //CopyFileToDirectory(File("./bin/patcher/BepInEx.Patcher.exe"), distPatcherDir);
  176. });
  177. Task("Pack")
  178. .IsDependentOn("MakeDist")
  179. .Does(() =>
  180. {
  181. var distDir = Directory("./bin/dist");
  182. var commitPrefix = isBleedingEdge ? $"_{currentCommitShort}_" : "_";
  183. Information("Packing BepInEx");
  184. ZipCompress(distDir + Directory("UnityMono_x86"), distDir + File($"BepInEx_UnityMono_x86{commitPrefix}{buildVersion}.zip"));
  185. ZipCompress(distDir + Directory("UnityMono_x64"), distDir + File($"BepInEx_UnityMono_x64{commitPrefix}{buildVersion}.zip"));
  186. ZipCompress(distDir + Directory("UnityMono_unix"), distDir + File($"BepInEx_UnityMono_unix{commitPrefix}{buildVersion}.zip"));
  187. ZipCompress(distDir + Directory("UnityIL2CPP_x86"), distDir + File($"BepInEx_UnityIL2CPP_x86{commitPrefix}{buildVersion}.zip"));
  188. ZipCompress(distDir + Directory("UnityIL2CPP_x64"), distDir + File($"BepInEx_UnityIL2CPP_x64{commitPrefix}{buildVersion}.zip"));
  189. ZipCompress(distDir + Directory("NetLauncher"), distDir + File($"BepInEx_NetLauncher{commitPrefix}{buildVersion}.zip"));
  190. // Information("Packing BepInEx.Patcher");
  191. // ZipCompress(distDir + Directory("patcher"), distDir + File($"BepInEx_Patcher{commitPrefix}{buildVersion}.zip"));
  192. if(isBleedingEdge)
  193. {
  194. var changelog = "";
  195. if(!string.IsNullOrEmpty(lastBuildCommit)) {
  196. changelog = TransformText("<ul><%changelog%></ul>")
  197. .WithToken("changelog", RunGit($"--no-pager log --no-merges --pretty=\"format:<li>(<code>%h</code>) [%an] %s</li>\" {lastBuildCommit}..HEAD"))
  198. .ToString();
  199. }
  200. FileWriteText(distDir + File("info.json"),
  201. SerializeJsonPretty(new Dictionary<string, object>{
  202. ["id"] = buildId.ToString(),
  203. ["date"] = DateTime.Now.ToString("o"),
  204. ["changelog"] = changelog,
  205. ["hash"] = currentCommit,
  206. ["artifacts"] = new Dictionary<string, object>[] {
  207. new Dictionary<string, object> {
  208. ["file"] = $"BepInEx_UnityMono_x64{commitPrefix}{buildVersion}.zip",
  209. ["description"] = "BepInEx Unity Mono for Windows x64 machines"
  210. },
  211. new Dictionary<string, object> {
  212. ["file"] = $"BepInEx_UnityMono_x86{commitPrefix}{buildVersion}.zip",
  213. ["description"] = "BepInEx Unity Mono for Windows x86 machines"
  214. },
  215. new Dictionary<string, object> {
  216. ["file"] = $"BepInEx_unix{commitPrefix}{buildVersion}.zip",
  217. ["description"] = "BepInEx Unity Mono for Unix machines with GCC (Linux, MacOS)"
  218. },
  219. new Dictionary<string, object> {
  220. ["file"] = $"BepInEx_UnityIL2CPP_x64{commitPrefix}{buildVersion}.zip",
  221. ["description"] = "BepInEx Unity IL2CPP for Windows x64 machines"
  222. },
  223. new Dictionary<string, object> {
  224. ["file"] = $"BepInEx_UnityIL2CPP_x86{commitPrefix}{buildVersion}.zip",
  225. ["description"] = "BepInEx Unity IL2CPP for Windows x86 machines"
  226. },
  227. new Dictionary<string, object> {
  228. ["file"] = $"BepInEx_NetLauncher{commitPrefix}{buildVersion}.zip",
  229. ["description"] = "BepInEx .NET Launcher for .NET Framework/XNA games"
  230. },
  231. // new Dictionary<string, object> {
  232. // ["file"] = $"BepInEx_Patcher{commitPrefix}{buildVersion}.zip",
  233. // ["description"] = "Hardpatcher for BepInEx. IMPORTANT: USE ONLY IF DOORSTOP DOES NOT WORK FOR SOME REASON!"
  234. // }
  235. }
  236. }));
  237. }
  238. });
  239. RunTarget(target);