Files
VisualSapfor/src/_VisualDVM/Passes/All/CreateTestFromFile.java

88 lines
3.1 KiB
Java
Raw Normal View History

2024-10-14 12:14:01 +03:00
package _VisualDVM.Passes.All;
2024-10-14 15:19:13 +03:00
import Common.Passes.Pass;
import Common.Passes.PassException;
2024-10-11 00:00:30 +03:00
import Common.Utils.Utils_;
import _VisualDVM.Global;
2024-10-09 22:21:57 +03:00
import _VisualDVM.ProjectData.Files.FileType;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Test.Test;
import _VisualDVM.TestingSystem.Common.TestFile.TestFile;
2024-10-14 15:19:13 +03:00
import _VisualDVM.Utils;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Vector;
2024-10-09 23:37:58 +03:00
public class CreateTestFromFile extends Pass<Test> {
//----
Group group;
TestFile testFile;
File file;
//----
@Override
protected boolean needsAnimation() {
return true;
}
@Override
public String getIconPath() {
return "/icons/AddFile.png";
}
@Override
public String getButtonText() {
return "";
}
@Override
protected boolean canStart(Object... args) throws Exception {
testFile = null;
file = (File) args[0];
group = (Group) args[1];
//--
if (Utils_.ContainsCyrillic(file.getName()) || Utils_.ContainsForbiddenName(file.getName())) {
Log.Writeln_("Имя файла " + Utils_.Brackets(file.getName())
2024-10-07 22:04:09 +03:00
+ " содержит запрещённые символы " +
2024-10-11 00:00:30 +03:00
Utils_.printAllForbiddenCharacters() + ", или кириллицу.");
return false;
}
//--
testFile = new TestFile(file);
if (!testFile.fileType.equals(FileType.program) || !testFile.languageName.equals(group.language)) {
Log.Writeln_("Не удалось распознать файл " + Utils_.Brackets(file.getName()) +
" как программу на языке " + group.language.getDescription());
return false;
}
//--
target = new Test();
target.sender_address = Global.mainModule.getAccount().email;
target.sender_name = Global.mainModule.getAccount().name;
target.group_id = group.id;
target.description = Utils_.getNameWithoutExtension(file.getName());
target.files = new Vector<>();
target.files.add(testFile);
//--
return true;
}
public File packTestCode() throws Exception {
2024-10-11 00:00:30 +03:00
target.temp_project_name = Utils_.getDateName("test");
//-
File tempProject = target.getTempProject();
File tempArchive = target.getTempArchive();
//- создать бд.
FileUtils.forceMkdir(tempProject);
//--
File dst = new File(tempProject, testFile.name);
FileUtils.copyFile(file, dst);
//---
Utils.ClearProjectData(tempProject);
//--
ZipFolderPass zip = new ZipFolderPass();
if (zip.Do(tempProject.getAbsolutePath(), tempArchive.getAbsolutePath())) {
2024-10-11 00:00:30 +03:00
target.project_archive_bytes = Utils_.fileToBytes(tempArchive);
} else throw new PassException("Не удалось создать архив папки с кодом.");
return tempProject;
}
@Override
protected void body() throws Exception {
ShowMessage1(testFile.name);
packTestCode();
}
}