forked from MinecraftForge/ForgeGradle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractEditJarTask.java
More file actions
286 lines (245 loc) · 9.11 KB
/
AbstractEditJarTask.java
File metadata and controls
286 lines (245 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* A Gradle plugin for the creation of Minecraft mods and MinecraftForge plugins.
* Copyright (C) 2013-2019 Minecraft Forge
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package net.minecraftforge.gradle.tasks;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import net.minecraftforge.gradle.common.Constants;
import net.minecraftforge.gradle.util.caching.Cached;
import net.minecraftforge.gradle.util.caching.CachedTask;
import org.apache.commons.io.FilenameUtils;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
public abstract class AbstractEditJarTask extends CachedTask
{
@InputFile
private Object inJar;
@Cached
@OutputFile
private Object outJar;
protected File resolvedInJar;
protected File resolvedOutJar;
@TaskAction
public void doTask() throws Throwable
{
resolvedInJar = getInJar();
resolvedOutJar = getOutJar();
doStuffBefore();
if (storeJarInRam())
{
getLogger().debug("Reading jar: " + resolvedInJar);
Map<String, String> sourceMap = Maps.newHashMap();
Map<String, byte[]> resourceMap = Maps.newHashMap();
readAndStoreJarInRam(resolvedInJar, sourceMap, resourceMap);
doStuffMiddle(sourceMap, resourceMap);
saveJar(resolvedOutJar, sourceMap, resourceMap);
getLogger().debug("Saving jar: " + resolvedOutJar);
}
else
{
copyJar(resolvedInJar, resolvedOutJar);
}
doStuffAfter();
}
/**
* Do Stuff before the jar is read
* @throws Exception for convenience
*/
public abstract void doStuffBefore() throws Exception;
/**
* Called as the .java files of the jar are read from the jar
* @param name name of the current entry
* @param file current contents of the entry
* @return new new contents of the file
* @throws Exception as a convenience for any potential exceptions thrown in this method
*/
public abstract String asRead(String name, String file) throws Exception;
/**
* Do Stuff after the jar is read, but before it is written.
* @param sourceMap name->contents for all java files in the jar
* @param resourceMap name->contents for everything else
* @throws Exception for convenience
*/
public abstract void doStuffMiddle(Map<String, String> sourceMap, Map<String, byte[]> resourceMap) throws Exception;
/**
* Do Stuff after the jar is Written
* @throws Exception for convenience
*/
public abstract void doStuffAfter() throws Exception;
/**
* Called immediately after every file is written to the jar.
* @param jarOut The jar output stream
* @param entryName The path to the file in the jar
* @throws IOException IOException
*/
protected void postWriteEntry(JarOutputStream jarOut, String entryName) throws IOException {}
/**
* Called after all entries have been written to the jar. This can be useful for adding any additional entries
* @param jarOut The jar output stream
* @throws IOException IOException
*/
protected void postWrite(JarOutputStream jarOut) throws IOException {}
/**
* Whether to store the contents of the jar in RAM.
* If this returns false, then the doStuffMiddle method is not called.
* @return store jar in ram
*/
protected abstract boolean storeJarInRam();
final void readAndStoreJarInRam(File jar, Map<String, String> sourceMap, Map<String, byte[]> resourceMap) throws Exception
{
try (ZipInputStream zin = new ZipInputStream(new FileInputStream(jar)))
{
ZipEntry entry;
String fileStr;
while ((entry = zin.getNextEntry()) != null)
{
// ignore META-INF, it shouldnt be here. If it is we remove it from the output jar.
if (entry.getName().contains("META-INF"))
{
continue;
}
// resources or directories.
if (!isSourceFile(entry))
{
resourceMap.put(entry.getName(), ByteStreams.toByteArray(zin));
}
else
{
// source!
fileStr = new String(ByteStreams.toByteArray(zin), Constants.CHARSET);
fileStr = asRead(entry.getName(), fileStr);
sourceMap.put(entry.getName(), fileStr);
}
}
}
}
protected void saveJar(File output, Map<String, String> sourceMap, Map<String, byte[]> resourceMap) throws IOException
{
output.getParentFile().mkdirs();
try (JarOutputStream zout = new JarOutputStream(new FileOutputStream(output)))
{
// write in resources
for (Map.Entry<String, byte[]> entry : resourceMap.entrySet())
{
zout.putNextEntry(new JarEntry(entry.getKey()));
zout.write(entry.getValue());
zout.closeEntry();
postWriteEntry(zout, entry.getKey());
}
// write in sources
for (Map.Entry<String, String> entry : sourceMap.entrySet())
{
zout.putNextEntry(new JarEntry(entry.getKey()));
zout.write(entry.getValue().getBytes());
zout.closeEntry();
postWriteEntry(zout, entry.getKey());
}
postWrite(zout);
}
}
/**
* Checks whether the given entry should be treated as a source file
*
* This can be overridden to change the types your task treats as source files
*
* @param entry the entry to check
* @return true if entry is a source file
*/
protected boolean isSourceFile(ZipEntry entry) {
if (entry.isDirectory())
return false;
String extension = FilenameUtils.getExtension(entry.getName());
switch (extension) {
case "java":
case "scala":
case "groovy":
case "kt":
return true;
}
return false;
}
private void copyJar(File input, File output) throws Exception
{
// begin reading jar
try (ZipInputStream zin = new ZipInputStream(new FileInputStream(input));
JarOutputStream zout = new JarOutputStream(new FileOutputStream(output)))
{
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null)
{
// no META or dirs. wel take care of dirs later.
if (entry.getName().contains("META-INF"))
{
continue;
}
// resources or directories.
try
{
if (!isSourceFile(entry))
{
zout.putNextEntry(new JarEntry(entry));
ByteStreams.copy(zin, zout);
zout.closeEntry();
postWriteEntry(zout, entry.getName());
}
else
{
// source
zout.putNextEntry(new JarEntry(entry.getName()));
zout.write(asRead(entry.getName(), new String(ByteStreams.toByteArray(zin), Constants.CHARSET)).getBytes());
zout.closeEntry();
postWriteEntry(zout, entry.getName());
}
}
catch (ZipException ex)
{
getLogger().debug("Duplicate zip entry " + entry.getName() + " in " + input + " writing " + output);
}
}
postWrite(zout);
}
}
public File getInJar()
{
return getProject().file(inJar);
}
public void setInJar(Object inJar)
{
this.inJar = inJar;
}
public File getOutJar()
{
return getProject().file(outJar);
}
public void setOutJar(Object outJar)
{
this.outJar = outJar;
}
}