build.cake 12 KB

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