build.cake 12 KB

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