1717 with pyret-autograder-cli. If not, see <http://www.gnu.org/licenses/>.
1818*/
1919
20- import yaml from "yaml" ;
21- import { readFile , mkdtemp } from "node:fs/promises" ;
20+ import chalk from "chalk" ;
2221import { spawn } from "node:child_process" ;
22+ import { mkdtemp } from "node:fs/promises" ;
2323import path from "node:path" ;
24- import { Config , Spec , z } from "pyret-autograder-pawtograder" ;
25- import chalk from "chalk" ;
2624import os from "os" ;
2725
28- const PKG_ROOT = path . resolve ( import . meta. dirname , ".." ) ;
29- const DEFAULT_COMPILED_PATH =
30- path . join ( PKG_ROOT , "build/pyret/lib-compiled" ) +
31- ":" +
32- path . join ( PKG_ROOT , "build/pyret/cpo-compiled" ) ;
33-
34- async function resolveSpec ( submissionPath : string , solutionPath : string ) {
35- const rawConfig = await readFile (
36- path . join ( solutionPath , "pawtograder.yml" ) ,
37- "utf8" ,
38- ) ;
39-
40- const config : Config = yaml . parse ( rawConfig , { merge : true } ) ;
41-
42- console . dir ( config , { depth : 3 } ) ;
43-
44- const parseRes = Spec . safeParse ( {
45- solution_dir : solutionPath ,
46- submission_dir : submissionPath ,
47- config,
48- } ) ;
49-
50- if ( parseRes . success ) {
51- return parseRes . data ;
52- } else {
53- const pretty = z . prettifyError ( parseRes . error ) ;
54- const err =
55- chalk . redBright . bold `Invalid specification provided` +
56- `:\n${ chalk . yellow ( pretty ) } \n\n` +
57- chalk . bold `See the ` +
58- chalk . blackBright . bold `cause` +
59- chalk . bold ` field for the full error.` ;
60-
61- throw new Error ( err , { cause : parseRes . error } ) ;
26+ class InnerError extends Error {
27+ constructor ( message : string ) {
28+ super ( message ) ;
6229 }
6330}
6431
@@ -68,7 +35,6 @@ export async function pawtograderAction(
6835) {
6936 const submissionPath = path . resolve ( submission ) ;
7037 const solutionPath = path . resolve ( solution ) ;
71- const spec = await resolveSpec ( submissionPath , solutionPath ) ;
7238
7339 console . log (
7440 `Grading submission at ${ submissionPath } with the specification located in ${ solutionPath } ` ,
@@ -81,29 +47,26 @@ export async function pawtograderAction(
8147 } ) ( ) ;
8248 const result = await new Promise ( ( resolve , reject ) => {
8349 const env = {
84- PA_PYRET_LANG_COMPILED_PATH : DEFAULT_COMPILED_PATH ,
8550 PA_CURRENT_LOAD_PATH : submissionPath ,
8651 PA_ARTIFACT_DIR : artifactDir ,
8752 ...process . env ,
8853 PWD : submissionPath ,
8954 } ;
9055
9156 const child = spawn (
92- process . execPath ,
93- [ path . join ( import . meta . dirname , "../src/pawtograder.cjs" ) ] ,
57+ process . env . PAWTOGRADER_PYRET_PATH ?? "pyret-pawtograder" ,
58+ [ solutionPath , submissionPath ] ,
9459 {
9560 env,
9661 cwd : submissionPath ,
9762 // [ stdin, stdout, stderr, custom]
98- stdio : [ "pipe " , "pipe" , "pipe" , "pipe" ] ,
63+ stdio : [ "ignore " , "pipe" , "pipe" , "pipe" ] ,
9964 } ,
10065 ) ;
10166
102- console . log ( "grader started" ) ;
103-
10467 for ( const [ stream , target , name ] of [
105- [ child . stdout , process . stdout , chalk . blue `stdout` ] ,
106- [ child . stderr , process . stderr , chalk . red `stderr` ] ,
68+ [ child . stdout ! , process . stdout , chalk . blue `stdout` ] ,
69+ [ child . stderr ! , process . stderr , chalk . red `stderr` ] ,
10770 ] as const ) {
10871 const prefix = `${ name } » ` ;
10972 let leftover = "" ;
@@ -125,18 +88,22 @@ export async function pawtograderAction(
12588
12689 child . on ( "close" , ( code ) => {
12790 console . log ( "grader ended" ) ;
128- if ( code !== 0 ) {
91+ if ( code === 0 ) {
92+ try {
93+ resolve ( JSON . parse ( output ) ) ;
94+ } catch ( e ) {
95+ reject ( new Error ( `Invalid JSON from grader: ${ output } \n${ e } ` ) ) ;
96+ }
97+ } else if ( code === 1 ) {
98+ try {
99+ reject ( new InnerError ( JSON . parse ( output ) ) ) ;
100+ } catch ( e ) {
101+ reject ( new Error ( `Invalid JSON from grader: ${ output } \n${ e } ` ) ) ;
102+ }
103+ } else {
129104 return reject ( new Error ( `Grader failed with code ${ code } .` ) ) ;
130105 }
131- try {
132- resolve ( JSON . parse ( output ) ) ;
133- } catch ( e ) {
134- reject ( new Error ( `Invalid JSON from grader: ${ output } \n${ e } ` ) ) ;
135- }
136106 } ) ;
137-
138- child . stdin . write ( JSON . stringify ( spec ) ) ;
139- child . stdin . end ( ) ;
140107 } ) ;
141108
142109 console . dir ( result ) ;
0 commit comments