forked from MinecraftForge/ForgeGradle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskProcessJson.java
More file actions
130 lines (105 loc) · 3.57 KB
/
TaskProcessJson.java
File metadata and controls
130 lines (105 loc) · 3.57 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
/*
* 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.patcher;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraftforge.gradle.common.Constants;
import org.gradle.api.DefaultTask;
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.Files;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class TaskProcessJson extends DefaultTask
{
private Map<String, Object> replacements = Maps.newHashMap();
@InputFile
private Object releaseJson;
@OutputFile
private Object installerJson;
@OutputFile
private Object universalJson;
//@formatter:off
public TaskProcessJson() {}
//@formatter:on
@TaskAction
public void doTask() throws IOException
{
File inputFile = getReleaseJson();
File outFile = getInstallerJson();
File truncatedFile = getUniversalJson();
String input = Files.toString(inputFile, Constants.CHARSET);
for (Entry<String, Object> e : replacements.entrySet())
{
input = input.replace(e.getKey(), Constants.resolveString(e.getValue()));
}
// write the replaced output
Files.write(input.getBytes(Constants.CHARSET), outFile);
// get just the useful data
Gson gson = (new GsonBuilder()).setPrettyPrinting().create();
input = gson.toJson(gson.fromJson(input, Map.class).get("versionInfo"));
// write the useful truncated data
Files.write(input.getBytes(Constants.CHARSET), truncatedFile);
}
public Map<String, Object> getReplacements()
{
return replacements;
}
public void addReplacement(Object key, Object value)
{
replacements.put(Constants.resolveString(key), value);
}
public void addReplacements(Map<String, Object> things)
{
replacements.putAll(things);
}
public File getReleaseJson()
{
return getProject().file(releaseJson);
}
public void setReleaseJson(Object releaseJson)
{
this.releaseJson = releaseJson;
}
protected boolean isReleaseJsonNull()
{
return releaseJson == null;
}
protected File getInstallerJson()
{
return getProject().file(installerJson);
}
protected void setInstallerJson(Object installerJson)
{
this.installerJson = installerJson;
}
protected File getUniversalJson()
{
return getProject().file(universalJson);
}
protected void setUniversalJson(Object universalJson)
{
this.universalJson = universalJson;
}
}