11use std:: {
22 fs:: { self , File } ,
3- io:: Write ,
4- os:: unix:: process:: ExitStatusExt ,
3+ io:: { Write } ,
4+ os:: unix:: { process:: ExitStatusExt } ,
55 path:: { Path , PathBuf } ,
66 process:: { Command , ExitStatus } ,
77} ;
@@ -11,7 +11,7 @@ use axum::Json;
1111use base64:: { prelude:: BASE64_STANDARD , Engine } ;
1212use regex:: Regex ;
1313use serde:: { Deserialize , Serialize } ;
14- use tempdir :: TempDir ;
14+ use tempfile :: tempdir ;
1515
1616use crate :: {
1717 error:: AppError ,
@@ -90,8 +90,8 @@ fn precompile_headers(compile_request: &CompileRequest) -> Result<()> {
9090}
9191
9292pub fn compile ( compile_request : CompileRequest ) -> Result < CompileResponse > {
93- let tmp_dir = TempDir :: new ( "compile" ) ?;
94- let tmp_out_dir = TempDir :: new ( "compile-out" ) ?;
93+ let tmp_dir = tempdir ( ) ?;
94+ let tmp_out_dir = tempdir ( ) ?;
9595
9696 let program_filename: PathBuf = match compile_request. language {
9797 Language :: Cpp => "program.cpp" . into ( ) ,
@@ -157,39 +157,35 @@ pub fn compile(compile_request: CompileRequest) -> Result<CompileResponse> {
157157 } ,
158158 ) ?;
159159
160- let executable = if ExitStatus :: from_raw ( compile_output. exit_code ) . success ( ) {
161- Some ( match compile_request. language {
162- Language :: Cpp => Executable :: Binary {
163- value : BASE64_STANDARD . encode ( fs:: read (
164- tmp_out_dir. path ( ) . join ( program_filename. with_extension ( "" ) ) ,
165- ) ?) ,
166- } ,
167- Language :: Java21 => Executable :: JavaClass {
168- class_name : program_filename
169- . file_stem ( )
170- . unwrap ( )
171- . to_str ( )
172- . unwrap ( )
173- . to_owned ( ) ,
174- value : BASE64_STANDARD . encode ( fs:: read (
175- tmp_out_dir
176- . path ( )
177- . join ( program_filename)
178- . with_extension ( "class" ) ,
179- ) ?) ,
180- } ,
181- Language :: Py12 => Executable :: Script {
182- language : Language :: Py12 ,
183- source_code : BASE64_STANDARD
184- . encode ( fs:: read ( tmp_out_dir. path ( ) . join ( program_filename) ) ?) ,
185- } ,
186- } )
160+ let run_command = match compile_request. language {
161+ Language :: Cpp => "./program" . to_owned ( ) ,
162+ Language :: Java21 => format ! (
163+ "java {}" ,
164+ program_filename. file_stem( ) . unwrap( ) . to_str( ) . unwrap( )
165+ ) ,
166+ Language :: Py12 => "python3.12 program.py" . to_owned ( ) ,
167+ } ;
168+
169+ let base64_files = if ExitStatus :: from_raw ( compile_output. exit_code ) . success ( ) {
170+ if !Command :: new ( "sh" )
171+ . arg ( "-c" )
172+ . arg ( "tar czf executable.tar.gz *" )
173+ . current_dir ( tmp_out_dir. path ( ) )
174+ . status ( ) ?
175+ . success ( )
176+ {
177+ return Err ( anyhow ! ( "Failed to tar executable file" ) ) ;
178+ }
179+ Some ( BASE64_STANDARD . encode ( fs:: read ( tmp_out_dir. path ( ) . join ( "executable.tar.gz" ) ) ?) )
187180 } else {
188- Option :: None
181+ None
189182 } ;
190183
191184 let response = CompileResponse {
192- executable,
185+ executable : base64_files. map ( |files| Executable {
186+ files,
187+ run_command,
188+ } ) ,
193189 compile_output,
194190 } ;
195191
0 commit comments