@ -15,8 +15,11 @@ import org.slf4j.LoggerFactory;
import java.io.File ;
import java.io.IOException ;
import java.nio.charset.Charset ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.util.List ;
import java.util.concurrent.ExecutorService ;
import java.util.function.Function ;
/ * *
@ -41,9 +44,9 @@ public class QualityInspectRunnable implements Runnable {
private SonarService sonarService ;
public QualityInspectRunnable ( String taskId , String language , String homeworkId ,
TaskInfoDetail taskInfoDetail ,
QualityInspectUserDataVO userData ,
ExecutorService queryResultPool ) {
TaskInfoDetail taskInfoDetail ,
QualityInspectUserDataVO userData ,
ExecutorService queryResultPool ) {
this . taskId = taskId ;
this . language = language ;
this . homeworkId = homeworkId ;
@ -59,43 +62,17 @@ public class QualityInspectRunnable implements Runnable {
String path = String . format ( "/tmp/%s/%s/" , homeworkId , projectName ) ;
List < Long > codeIds = userData . getCodeIds ( ) ;
boolean constantCFlag = true , constantCPPFlag = true , constantJavaFlag = true , constantPyFlag = true ;
for ( Long codeId : codeIds ) {
File file = new File ( path ) ;
try {
FileUtils . forceMkdir ( file ) ;
} catch ( IOException e ) {
}
List < Long > shiXunCodeIds = userData . getStudentWorkShixunCodeIds ( ) ;
GameCodes gameCodes = dbOperateService . queryGameCodesById ( codeId ) ;
if ( gameCodes = = null ) {
LOGGER . error ( "projectName:{}, game_codes_id:{}找不到对应的学员代码" , projectName , codeId ) ;
continue ;
}
FileUtil . writeString ( gameCodes . getCode ( ) , path + gameCodes . getPath ( ) , Charset . forName ( "UTF-8" ) ) ;
if ( constantCFlag & & gameCodes . getPath ( ) . toLowerCase ( ) . contains ( ".c" ) ) {
constantCFlag = false ;
} else if ( constantCPPFlag & & gameCodes . getPath ( ) . toLowerCase ( ) . contains ( ".cpp" ) ) {
constantCPPFlag = false ;
} else if ( constantJavaFlag & & gameCodes . getPath ( ) . toLowerCase ( ) . contains ( ".java" ) ) {
constantJavaFlag = false ;
} else if ( constantPyFlag & & gameCodes . getPath ( ) . toLowerCase ( ) . contains ( ".py" ) ) {
constantPyFlag = false ;
}
for ( Long codeId : codeIds ) {
processCodeId ( codeId , path , projectName , dbOperateService : : queryGameCodesById ) ;
}
// 需要自己判别语言,Java优先
if ( ! constantJavaFlag ) {
language = Constant . JAVA ;
} else if ( ! constantPyFlag ) {
language = Constant . PYTHON ;
} else if ( ! constantCFlag ) {
language = Constant . C ;
} else if ( ! constantCPPFlag ) {
language = Constant . CXX ;
for ( Long codeId : shiXunCodeIds ) {
processCodeId ( codeId , path , projectName , dbOperateService : : queryShiXunCodesById ) ;
}
// 写完所有文件开始用sonar进行质量分析
SonarScannerParam param = new SonarScannerParam ( projectName , path ) ;
if ( Constant . C . equalsIgnoreCase ( language ) | | Constant . CXX . equalsIgnoreCase ( language ) ) {
@ -133,4 +110,45 @@ public class QualityInspectRunnable implements Runnable {
public void setSonarService ( SonarService sonarService ) {
this . sonarService = sonarService ;
}
private void processCodeId ( Long codeId , String path , String projectName , Function < Long , GameCodes > queryMethod ) {
boolean constantCFlag = true , constantCPPFlag = true , constantJavaFlag = true , constantPyFlag = true ;
File file = new File ( path ) ;
try {
FileUtils . forceMkdir ( file ) ;
} catch ( IOException e ) {
LOGGER . error ( "创建目录失败: {}" , path , e ) ;
}
GameCodes gameCodes = queryMethod . apply ( codeId ) ;
if ( gameCodes = = null ) {
LOGGER . error ( "projectName:{}, game_codes_id:{} 找不到对应的学员代码" , projectName , codeId ) ;
return ;
}
Path filePath = Paths . get ( path , gameCodes . getPath ( ) ) ;
FileUtil . writeString ( gameCodes . getCode ( ) , filePath . toString ( ) , Charset . forName ( "UTF-8" ) ) ;
String filePathLower = gameCodes . getPath ( ) . toLowerCase ( ) ;
if ( constantCFlag & & filePathLower . matches ( ".*\\.c$" ) ) {
constantCFlag = false ;
} else if ( constantCPPFlag & & filePathLower . matches ( ".*\\.cpp$" ) ) {
constantCPPFlag = false ;
} else if ( constantJavaFlag & & filePathLower . matches ( ".*\\.java$" ) ) {
constantJavaFlag = false ;
} else if ( constantPyFlag & & filePathLower . matches ( ".*\\.py$" ) ) {
constantPyFlag = false ;
}
// 需要自己判别语言,Java优先
if ( ! constantJavaFlag ) {
language = Constant . JAVA ;
} else if ( ! constantPyFlag ) {
language = Constant . PYTHON ;
} else if ( ! constantCFlag ) {
language = Constant . C ;
} else if ( ! constantCPPFlag ) {
language = Constant . CXX ;
}
}
}