@@ -9,71 +9,46 @@ import { CompileError, RuntimeError } from '/common/error';
9
9
const router = express . Router ( ) ;
10
10
11
11
const repoPath = path . resolve ( __dirname , '..' , 'public' , 'tracers' ) ;
12
- const getLibsPath = ( ...args ) => path . resolve ( repoPath , 'libs' , ...args ) ;
13
12
const getCodesPath = ( ...args ) => path . resolve ( __dirname , '..' , 'public' , 'codes' , ...args ) ;
14
13
15
14
const buildRelease = release => (
16
15
fs . pathExistsSync ( repoPath ) ?
17
16
execute ( `git fetch && ! git diff-index --quiet ${ release . target_commitish } ` , repoPath ) :
18
17
execute ( `git clone git@github.com:algorithm-visualizer/tracers ${ repoPath } ` , __dirname )
19
- ) . then ( ( ) => execute ( `git reset --hard ${ release . target_commitish } && npm install && npm run build` , repoPath ) ) ;
18
+ ) . then ( ( ) => execute ( `git reset --hard ${ release . target_commitish } && npm install && npm run build && ./bin/build ` , repoPath ) ) ;
20
19
21
20
GitHubApi . getLatestRelease ( 'algorithm-visualizer' , 'tracers' ) . then ( buildRelease ) ;
22
21
// TODO: build release when webhooked
23
22
24
23
const getJsWorker = ( req , res , next ) => {
25
- res . sendFile ( getLibsPath ( 'js ', 'tracers.js' ) ) ;
24
+ res . sendFile ( path . resolve ( repoPath , 'src' , 'languages' , 'js' , 'tracers' , 'build ', 'tracers.js' ) ) ;
26
25
} ;
27
26
28
- const executeInDocker = ( imageId , ext , srcPath , command ) => {
29
- // TODO: memory limit + time limit + space limit?
30
- const libsPath = getLibsPath ( ext ) ;
31
- const dockerCommand = [
32
- `docker run --rm` ,
33
- '-w=/usr/judge' ,
34
- `-v=${ libsPath } :/usr/bin/tracers:ro` ,
35
- `-v=${ srcPath } :/usr/judge:rw` ,
36
- `-e MAX_TRACES=${ 1e6 } -e MAX_TRACERS=${ 1e2 } ` ,
37
- imageId ,
38
- ] . join ( ' ' ) ;
39
- return execute ( `${ dockerCommand } ${ command } ` , repoPath , { stdout : null , stderr : null } ) ;
40
- } ;
41
-
42
- const trace = ( { ext, imageId, compileCommand, runCommand } ) => ( req , res , next ) => {
27
+ const trace = lang => ( req , res , next ) => {
43
28
const { code } = req . body ;
44
- const srcPath = getCodesPath ( uuid . v4 ( ) ) ;
45
- fs . outputFile ( path . resolve ( srcPath , `Main.${ ext } ` ) , code )
46
- . then ( ( ) => executeInDocker ( imageId , ext , srcPath , compileCommand )
29
+ const tempPath = getCodesPath ( uuid . v4 ( ) ) ;
30
+ fs . outputFile ( path . resolve ( tempPath , `Main.${ lang } ` ) , code )
31
+ . then ( ( ) => execute ( `LANG= ${ lang } TEMP_PATH= ${ tempPath } ./bin/compile` , repoPath , { stdout : null , stderr : null } )
47
32
. catch ( error => {
48
33
throw new CompileError ( error ) ;
49
34
} ) )
50
- . then ( ( ) => executeInDocker ( imageId , ext , srcPath , runCommand )
35
+ . then ( ( ) => execute ( `LANG= ${ lang } TEMP_PATH= ${ tempPath } ./bin/run` , repoPath , { stdout : null , stderr : null } )
51
36
. catch ( error => {
52
37
throw new RuntimeError ( error ) ;
53
38
} ) )
54
- . then ( ( ) => res . sendFile ( path . resolve ( srcPath , 'traces.json' ) ) )
39
+ . then ( ( ) => res . sendFile ( path . resolve ( tempPath , 'traces.json' ) ) )
55
40
. catch ( next )
56
- . finally ( ( ) => fs . remove ( srcPath ) ) ;
41
+ . finally ( ( ) => fs . remove ( tempPath ) ) ;
57
42
} ;
58
43
59
44
60
45
router . route ( '/js' )
61
46
. get ( getJsWorker ) ;
62
47
63
48
router . route ( '/java' )
64
- . post ( trace ( {
65
- ext : 'java' ,
66
- imageId : 'openjdk:8' ,
67
- compileCommand : 'javac -cp /usr/bin/tracers/tracers.jar Main.java' ,
68
- runCommand : 'java -cp /usr/bin/tracers/tracers.jar:. Main' ,
69
- } ) ) ;
49
+ . post ( trace ( 'java' ) ) ;
70
50
71
51
router . route ( '/cpp' )
72
- . post ( trace ( {
73
- ext : 'cpp' ,
74
- imageId : 'gcc:8.1' ,
75
- compileCommand : 'g++ Main.cpp -o Main -O2 -std=c++11 -L/usr/bin/tracers/lib -l:tracers.a -I/usr/bin/tracers/include' ,
76
- runCommand : './Main' ,
77
- } ) ) ;
52
+ . post ( trace ( 'cpp' ) ) ;
78
53
79
54
export default router ;
0 commit comments