forked from MinecraftForge/ForgeGradle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrowdinDownload.java
More file actions
280 lines (237 loc) · 8.55 KB
/
CrowdinDownload.java
File metadata and controls
280 lines (237 loc) · 8.55 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
/*
* 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 groovy.lang.Closure;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import net.minecraftforge.gradle.common.Constants;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.FileCollection;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFiles;
import org.gradle.api.tasks.TaskAction;
import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
public class CrowdinDownload extends DefaultTask
{
@Input
private Object projectId;
@Input
private Object apiKey;
@Input
private boolean extract = true;
private Object output;
// format these with the projectId and apiKey
private static final String EXPORT_URL = "https://api.crowdin.com/api/project/%s/export?key=%s";
private static final String DOWNLOAD_URL = "https://api.crowdin.com/api/project/%s/download/all.zip?key=%s";
@SuppressWarnings({ "unchecked", "rawtypes" })
public CrowdinDownload()
{
super();
this.onlyIf(new Spec() {
@Override
public boolean isSatisfiedBy(Object arg0)
{
CrowdinDownload task = (CrowdinDownload) arg0;
// no API key? skip
if (Strings.isNullOrEmpty(task.getApiKey()))
{
getLogger().lifecycle("Crowdin api key is null, skipping task.");
return false;
}
// offline? skip.
if (getProject().getGradle().getStartParameter().isOffline())
{
getLogger().lifecycle("Gradle is in offline mode, skipping task.");
return false;
}
return true;
}
});
}
@TaskAction
public void doTask() throws IOException
{
String project = getProjectId();
String key = getApiKey();
exportLocalizations(project, key);
getLocalizations(project, key, getOutput());
}
private void exportLocalizations(String projectId, String key) throws IOException
{
getLogger().debug("Exporting crowdin localizations.");
URL url = new URL(String.format(EXPORT_URL, projectId, key));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", Constants.USER_AGENT);
con.setInstanceFollowRedirects(true);
try
{
con.connect();
}
catch (IOException e)
{
// just in case people dont have internet at the moment.
throw new RuntimeException(e);
}
int reponse = con.getResponseCode();
con.disconnect();
if (reponse == 401)
throw new RuntimeException("Invalid Crowdin API-Key!");
}
private void getLocalizations(String projectId, String key, File output) throws IOException
{
getLogger().info("Downloading crowdin localizations.");
URL url = new URL(String.format(DOWNLOAD_URL, projectId, key));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", Constants.USER_AGENT);
con.setInstanceFollowRedirects(true);
ZipOutputStream zOut = null;
try (ZipInputStream zStream = new ZipInputStream(con.getInputStream()))
{
if (!extract)
{
Files.createParentDirs(output);
Files.touch(output);
zOut = new ZipOutputStream(new FileOutputStream(output));
}
ZipEntry entry;
while ((entry = zStream.getNextEntry()) != null)
{
try
{
if (entry.isDirectory() || entry.getSize() == 0)
{
continue;
}
String data = CharStreams.readLines(new InputStreamReader(zStream), new LineProcessor<String>()
{
StringBuilder out = new StringBuilder();
Splitter SPLITTER = Splitter.on('=').limit(2);
@Override
public boolean processLine(String line) throws IOException
{
String[] pts = Iterables.toArray(SPLITTER.split(line), String.class);
if (pts.length == 2)
{
out.append(pts[0]).append('=').append(
pts[1].replace("\\!", "!")
.replace("\\:", ":"))
.append('\n');
}
else
out.append(line).append('\n');
return true;
}
@Override
public String getResult()
{
return out.toString();
}
});
if (extract)
{
getLogger().debug("Extracting file: " + entry.getName());
File out = new File(output, entry.getName());
Files.createParentDirs(out);
Files.touch(out);
Files.write(data.getBytes(Charsets.UTF_8), out);
}
else
{
zOut.putNextEntry(new ZipEntry(entry.getName()));
zOut.write(data.getBytes(Charsets.UTF_8));
}
} finally
{
zStream.closeEntry();
}
}
}
finally
{
if (zOut != null)
zOut.close();
}
con.disconnect();
}
@SuppressWarnings("rawtypes")
public String getProjectId()
{
if (projectId == null)
throw new NullPointerException("ProjectID must be set for crowdin!");
while (projectId instanceof Closure)
projectId = ((Closure) projectId).call();
return projectId.toString();
}
public void setProjectId(Object projectId)
{
this.projectId = projectId;
}
@SuppressWarnings("rawtypes")
public String getApiKey()
{
while (apiKey instanceof Closure)
apiKey = ((Closure) apiKey).call();
if (apiKey == null)
return null;
return apiKey.toString();
}
public void setApiKey(Object apiKey)
{
this.apiKey = apiKey;
}
@OutputFiles
public FileCollection getOutputFiles()
{
if (isExtract())
return getProject().fileTree(getOutput());
else
return getProject().files(getOutput());
}
public File getOutput()
{
return getProject().file(output);
}
public void setOutput(Object output)
{
this.output = output;
}
public boolean isExtract()
{
return extract;
}
public void setExtract(boolean extract)
{
this.extract = extract;
}
}