]> git.lizzy.rs Git - LightOverlay.git/blob - build.gradle
Update Cloth Config and architectury
[LightOverlay.git] / build.gradle
1 buildscript {
2     repositories {
3         mavenCentral()
4     }
5     dependencies {
6         classpath("commons-io:commons-io:2.6")
7     }
8 }
9
10 import java.nio.file.FileVisitResult
11 import java.nio.file.Files
12 import java.nio.file.Path
13 import java.nio.file.SimpleFileVisitor
14 import java.nio.file.attribute.BasicFileAttributes
15 import java.util.stream.Stream
16 import java.util.zip.ZipEntry
17 import java.util.zip.ZipInputStream
18 import java.util.zip.ZipOutputStream
19
20 plugins {
21     id "architectury-plugin" version "3.0.76"
22     id "forgified-fabric-loom" version "0.6.67" apply false
23 }
24
25 architectury {
26     minecraft = minecraft_version
27 }
28
29 subprojects {
30     apply plugin: "forgified-fabric-loom"
31
32     loom {
33         silentMojangMappingsLicense()
34     }
35 }
36
37 allprojects {
38     apply plugin: "java"
39     apply plugin: "architectury-plugin"
40
41     group "me.shedaniel"
42     archivesBaseName = rootProject.name
43     version = rootProject.mod_version
44
45     tasks.withType(JavaCompile) {
46         options.encoding = "UTF-8"
47     }
48 }
49
50 task buildMerged {
51     allprojects {
52         dependsOn it.tasks.getByName("build")
53     }
54     doLast {
55         def folder = file(".gradle/.mergemods")
56         folder.mkdirs()
57         def fabricJar = file("fabric/build/libs/${rootProject.name}-${rootProject.mod_version}-fabric.jar")
58         def forgeJar = file("forge/build/libs/${rootProject.name}-${rootProject.mod_version}-forge.jar")
59         def fabricFolder = new File(folder, ".tempFabric")
60         def forgeFolder = new File(folder, ".tempForge")
61         def mergeFolder = new File(folder, ".tempMerge")
62         def policyMap = new HashMap<String, String>()
63         file("merging.policy").eachLine {
64             if (it.isBlank() || it.startsWith("#")) return
65             def env = it.substring(0, it.indexOf(' '))
66             if (env == "FABRIC")
67                 policyMap.put(it.substring(env.length() + 1), "Fabric")
68             else if (env == "FORGE")
69                 policyMap.put(it.substring(env.length() + 1), "Forge")
70             else throw new IllegalStateException("Illegal env $env at $it")
71         }
72         forgeFolder.deleteDir()
73         fabricFolder.deleteDir()
74         mergeFolder.deleteDir()
75         unzip(fabricJar, fabricFolder)
76         unzip(forgeJar, forgeFolder)
77         mergeFolder.mkdirs()
78         Stream.of(forgeFolder, fabricFolder).each { useFolder ->
79             try {
80                 Files.walkFileTree(useFolder.toPath(), new SimpleFileVisitor<Path>() {
81                     @Override
82                     FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
83                         try {
84                             File ogFile = file.toFile()
85                             File outFile = new File(mergeFolder, ogFile.getAbsolutePath().replace(useFolder.getAbsolutePath(), ""))
86                             outFile.getParentFile().mkdirs()
87                             if (outFile.exists()) {
88                                 def env = useFolder.getName().substring(5)
89                                 def fileName = outFile.getAbsolutePath().replace(mergeFolder.getAbsolutePath(), "")
90                                 if (!ogFile.isFile() || !outFile.isFile() || !Arrays.equals(ogFile.readBytes(), outFile.readBytes())) {
91                                     def policyEnv = policyMap.get(fileName)
92                                     if (policyEnv == null) {
93                                         throw new IllegalStateException("Unhandled duplicate file: $fileName")
94                                     }
95                                     println "Chose env ${policyEnv.toUpperCase(Locale.ROOT)} for duplicate file: $fileName"
96                                     if (policyEnv != env)
97                                         return FileVisitResult.CONTINUE
98                                 }
99                             }
100                             if (!ogFile.isDirectory()) {
101                                 org.apache.commons.io.FileUtils.copyFile(ogFile, outFile)
102                             } else {
103                                 org.apache.commons.io.FileUtils.copyDirectory(ogFile, outFile)
104                             }
105                         } catch (IOException e) {
106                             e.printStackTrace()
107                             System.exit(0)
108                         }
109                         return FileVisitResult.CONTINUE
110                     }
111                 })
112             } catch (IOException e) {
113                 e.printStackTrace()
114                 System.exit(0)
115             }
116         }
117         File finalMerge = file("build/libs/${rootProject.name}-${rootProject.mod_version}.jar")
118         finalMerge.parentFile.mkdirs()
119         finalMerge.delete()
120         compress(mergeFolder.toPath(), finalMerge)
121         folder.deleteDir()
122     }
123 }
124
125 rootProject.subprojects.forEach {
126     buildMerged.mustRunAfter it.tasks.getByName("build")
127 }
128
129 static def compress(Path sourceDir, File zipFile) {
130     try {
131         final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFile))
132         Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
133             @Override
134             FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
135                 try {
136                     Path targetFile = sourceDir.relativize(file)
137                     outputStream.putNextEntry(new ZipEntry(targetFile.toString()))
138                     byte[] bytes = Files.readAllBytes(file)
139                     outputStream.write(bytes, 0, bytes.length)
140                     outputStream.closeEntry()
141                 } catch (IOException e) {
142                     e.printStackTrace()
143                 }
144                 return FileVisitResult.CONTINUE
145             }
146         })
147         outputStream.close()
148     } catch (IOException e) {
149         e.printStackTrace()
150     }
151 }
152
153 static def unzip(File zipFile, File destDir) {
154     if (!destDir.exists())
155         destDir.mkdirs()
156     FileInputStream fis
157     byte[] buffer = new byte[1024]
158     try {
159         fis = new FileInputStream(zipFile)
160         ZipInputStream zis = new ZipInputStream(fis)
161         ZipEntry zipEntry = zis.getNextEntry()
162         while (zipEntry != null) {
163             if (!zipEntry.isDirectory()) {
164                 File newFile = new File(destDir, zipEntry.getName())
165                 new File(newFile.getParent()).mkdirs()
166                 FileOutputStream fos = new FileOutputStream(newFile)
167                 int len
168                 while ((len = zis.read(buffer)) > 0) {
169                     fos.write(buffer, 0, len)
170                 }
171                 fos.close()
172             }
173             zis.closeEntry()
174             zipEntry = zis.getNextEntry()
175         }
176         zis.closeEntry()
177         zis.close()
178         fis.close()
179     } catch (IOException e) {
180         e.printStackTrace()
181     }
182 }