23
23
import com .google .common .collect .Maps ;
24
24
25
25
import org .apache .commons .io .FileUtils ;
26
+ import org .apache .commons .io .IOUtils ;
27
+
26
28
import org .slf4j .Logger ;
27
29
import org .slf4j .LoggerFactory ;
28
30
29
31
import java .io .File ;
32
+ import java .io .FileOutputStream ;
30
33
import java .io .FileReader ;
31
34
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 ;
32
40
import java .nio .file .FileSystems ;
33
41
import java .nio .file .Files ;
34
42
import java .nio .file .Path ;
35
43
import java .nio .file .PathMatcher ;
36
44
import java .nio .file .Paths ;
37
45
38
46
import java .util .ArrayList ;
47
+ import java .util .Enumeration ;
39
48
import java .util .HashMap ;
40
49
import java .util .List ;
41
50
import java .util .Locale ;
42
51
import java .util .Map ;
43
52
import java .util .Scanner ;
44
-
53
+ import java .util .logging .Level ;
54
+ import java .util .zip .ZipEntry ;
55
+ import java .util .zip .ZipFile ;
45
56
46
57
public class CSharpPlugin extends AbstractLanguagePlugin {
47
58
@@ -50,18 +61,18 @@ public class CSharpPlugin extends AbstractLanguagePlugin {
50
61
private static final String CANNOT_RUN_TESTS_MESSAGE = "Failed to run tests." ;
51
62
private static final String CANNOT_PARSE_TEST_RESULTS_MESSAGE = "Failed to read test results." ;
52
63
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." ;
55
66
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." ;
60
71
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." ;
65
76
66
77
private static Logger log = LoggerFactory .getLogger (CSharpPlugin .class );
67
78
@@ -95,7 +106,7 @@ public Optional<ExerciseDesc> scanExercise(Path path, String exerciseName) {
95
106
96
107
try {
97
108
ProcessResult result = runner .call ();
98
-
109
+
99
110
if (result .statusCode != 0 ) {
100
111
log .error (COMPILATION_FAILED_MESSAGE );
101
112
return Optional .absent ();
@@ -231,4 +242,52 @@ private RunResult runResultFromFailedCompilation(ProcessResult result) {
231
242
ImmutableList .copyOf (new ArrayList <TestResult >()),
232
243
ImmutableMap .copyOf (logs ));
233
244
}
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
+ }
234
293
}
0 commit comments