|
| 1 | +package chalkbox.commands; |
| 2 | + |
| 3 | +import chalkbox.config.Config; |
| 4 | +import chalkbox.source.Solution; |
| 5 | +import chalkbox.source.Submission; |
| 6 | +import chalkbox.stages.Result; |
| 7 | +import chalkbox.stages.Stage; |
| 8 | +import chalkbox.stages.StageException; |
| 9 | +import chalkbox.stages.StageResult; |
| 10 | +import chalkbox.stages.ai.Ai; |
| 11 | +import chalkbox.stages.header.Header; |
| 12 | +import com.google.common.flogger.FluentLogger; |
| 13 | +import de.bsommerfeld.jshepherd.core.ConfigurationLoader; |
| 14 | +import picocli.CommandLine; |
| 15 | +import picocli.CommandLine.Command; |
| 16 | +import picocli.CommandLine.Mixin; |
| 17 | + |
| 18 | +import java.nio.file.Path; |
| 19 | +import java.nio.file.Paths; |
| 20 | + |
| 21 | +@Command(name = "grade", |
| 22 | + description = "Runs a sequence of stages and generates a Gradescope submission result") |
| 23 | +public class Grade implements Runnable { |
| 24 | + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
| 25 | + |
| 26 | + @CommandLine.Option(names = { "--stages" }, required = true, description = "Comma seperated stages to run") |
| 27 | + public String stages; |
| 28 | + |
| 29 | + @Mixin Shared shared = new Shared(); |
| 30 | + |
| 31 | + @Override |
| 32 | + public void run() { |
| 33 | + Path configFile = Paths.get(shared.configFile); |
| 34 | + var config = ConfigurationLoader.load(configFile, Config::new); |
| 35 | + |
| 36 | + logger.atInfo().log("Running the following stages: " + String.join(" ,", stages)); |
| 37 | + |
| 38 | + //todo(mh): Config this |
| 39 | + var solution = new Solution("./test/resources/csse2002/solutions/correct", |
| 40 | + "./test/resources/csse2002/lib/junit-4.12.jar"); |
| 41 | + var submission = new Submission(shared.submissionPath, "./test/resources/csse2002/lib/junit-4.12.jar"); |
| 42 | + |
| 43 | + // for each stage in the config |
| 44 | + var stages = this.stages.split(","); |
| 45 | + for (var name : stages) { |
| 46 | + var stage = getStage(name, config); |
| 47 | + if (stage == null) { |
| 48 | + logger.atWarning().log("Unable to find stage for " + name); |
| 49 | + continue; |
| 50 | + } |
| 51 | + StageResult result = null; |
| 52 | + try { |
| 53 | + switch (stage.getType()) { |
| 54 | + case SUBMISSION_ONLY -> result = stage.run(submission); |
| 55 | + case SUBMISSION_AND_SOLUTION -> result = stage.run(submission, solution); |
| 56 | + } |
| 57 | + } catch (StageException e) { |
| 58 | + result = StageResult.fromOverview(new Result(stage.getName())); |
| 59 | + } |
| 60 | + |
| 61 | + logger.atInfo().log(stage.getName()); |
| 62 | + if (result == null) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + logger.atInfo().log(result.overview().getOutput()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private Stage getStage(String name, Config config) { |
| 70 | + return switch (name) { |
| 71 | + case "header" -> new Header(); |
| 72 | + case "ai" -> new Ai(); |
| 73 | + case "codestyle" -> config.toCodestyle(); |
| 74 | + case "conformance" -> config.toConformance(); |
| 75 | + case "functionality" -> config.toFunctionality(); |
| 76 | + case "mutation" -> config.toMutation(); |
| 77 | + default -> null; |
| 78 | + }; |
| 79 | + } |
| 80 | +} |
0 commit comments