Skip to content

Commit 65d80c2

Browse files
committed
make plugin download runner (broken atm)
1 parent dd803eb commit 65d80c2

File tree

1 file changed

+71
-12
lines changed

1 file changed

+71
-12
lines changed

tmc-langs-csharp/src/main/java/fi/helsinki/cs/tmc/langs/csharp/CSharpPlugin.java

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,36 @@
2323
import com.google.common.collect.Maps;
2424

2525
import org.apache.commons.io.FileUtils;
26+
import org.apache.commons.io.IOUtils;
27+
2628
import org.slf4j.Logger;
2729
import org.slf4j.LoggerFactory;
2830

2931
import java.io.File;
32+
import java.io.FileOutputStream;
3033
import java.io.FileReader;
3134
import java.io.IOException;
35+
import java.io.InputStream;
36+
import java.io.OutputStream;
37+
import java.io.UnsupportedEncodingException;
38+
import java.net.URL;
39+
import java.net.URLDecoder;
3240
import java.nio.file.FileSystems;
3341
import java.nio.file.Files;
3442
import java.nio.file.Path;
3543
import java.nio.file.PathMatcher;
3644
import java.nio.file.Paths;
3745

3846
import java.util.ArrayList;
47+
import java.util.Enumeration;
3948
import java.util.HashMap;
4049
import java.util.List;
4150
import java.util.Locale;
4251
import java.util.Map;
4352
import java.util.Scanner;
44-
53+
import java.util.logging.Level;
54+
import java.util.zip.ZipEntry;
55+
import java.util.zip.ZipFile;
4556

4657
public class CSharpPlugin extends AbstractLanguagePlugin {
4758

@@ -50,18 +61,18 @@ public class CSharpPlugin extends AbstractLanguagePlugin {
5061
private static final String CANNOT_RUN_TESTS_MESSAGE = "Failed to run tests.";
5162
private static final String CANNOT_PARSE_TEST_RESULTS_MESSAGE = "Failed to read test results.";
5263
private static final String CANNOT_SCAN_EXERCISE_MESSAGE = "Failed to scan exercise.";
53-
private static final String CANNOT_PARSE_EXERCISE_DESCRIPTION_MESSAGE =
54-
"Failed to parse exercise description.";
64+
private static final String CANNOT_PARSE_EXERCISE_DESCRIPTION_MESSAGE
65+
= "Failed to parse exercise description.";
5566
private static final String CANNOT_LOCATE_RUNNER_MESSAGE = "Failed to locate runner.";
56-
private static final String CANNOT_PURGE_OLD_RESULTS_MESSAGE =
57-
"Failed to purge old test results.";
58-
private static final String CANNOT_SCAN_PROJECT_TYPE_MESSAGE =
59-
"Failed to scan project files.";
67+
private static final String CANNOT_PURGE_OLD_RESULTS_MESSAGE
68+
= "Failed to purge old test results.";
69+
private static final String CANNOT_SCAN_PROJECT_TYPE_MESSAGE
70+
= "Failed to scan project files.";
6071
private static final String COMPILATION_FAILED_MESSAGE = "Failed to compile excercise.";
61-
private static final String CANNOT_CLEANUP =
62-
"Failed to run cleanup task.";
63-
private static final String CANNOT_CLEANUP_DIR =
64-
"Failed to run cleanup task on a directory.";
72+
private static final String CANNOT_CLEANUP
73+
= "Failed to run cleanup task.";
74+
private static final String CANNOT_CLEANUP_DIR
75+
= "Failed to run cleanup task on a directory.";
6576

6677
private static Logger log = LoggerFactory.getLogger(CSharpPlugin.class);
6778

@@ -95,7 +106,7 @@ public Optional<ExerciseDesc> scanExercise(Path path, String exerciseName) {
95106

96107
try {
97108
ProcessResult result = runner.call();
98-
109+
99110
if (result.statusCode != 0) {
100111
log.error(COMPILATION_FAILED_MESSAGE);
101112
return Optional.absent();
@@ -231,4 +242,52 @@ private RunResult runResultFromFailedCompilation(ProcessResult result) {
231242
ImmutableList.copyOf(new ArrayList<TestResult>()),
232243
ImmutableMap.copyOf(logs));
233244
}
245+
246+
private void ensureRunnerAvailability() {
247+
String jarPathString = CSharpPlugin.class.getProtectionDomain().getCodeSource().getLocation().getPath();
248+
249+
try {
250+
String decodedPath = URLDecoder.decode(jarPathString, "UTF-8");
251+
} catch (UnsupportedEncodingException e) {
252+
log.error("how did you end up here?", e);
253+
}
254+
255+
Path jarPath = Paths.get(jarPathString);
256+
257+
try {
258+
if (!Files.exists(jarPath.resolve(Paths.get("tmc-csharp-runner", "Bootstrap.dll")))) {
259+
File runnerZip = new File("tmc-csharp-runner.zip");
260+
FileUtils.copyURLToFile(new URL("https://github.com/TMC-C/tmc-csharp-runner/releases/download/v1.0.2/tmc-csharp-runner.zip"), runnerZip);
261+
262+
File runnerDir = new File("tmc-csharp-runner");
263+
runnerDir.mkdir();
264+
unzip(runnerZip, runnerDir);
265+
266+
267+
}
268+
} catch (Exception e) {
269+
log.error("lol", e);
270+
}
271+
}
272+
273+
private void unzip(File zip, File targetDir) {
274+
try (java.util.zip.ZipFile zipFile = new ZipFile(zip)) {
275+
Enumeration<? extends ZipEntry> entries = zipFile.entries();
276+
while (entries.hasMoreElements()) {
277+
ZipEntry entry = entries.nextElement();
278+
File entryDestination = new File(targetDir, entry.getName());
279+
if (entry.isDirectory()) {
280+
entryDestination.mkdirs();
281+
} else {
282+
entryDestination.getParentFile().mkdirs();
283+
try (InputStream in = zipFile.getInputStream(entry);
284+
OutputStream out = new FileOutputStream(entryDestination)) {
285+
IOUtils.copy(in, out);
286+
}
287+
}
288+
}
289+
} catch (IOException e) {
290+
log.error("asd", e);
291+
}
292+
}
234293
}

0 commit comments

Comments
 (0)