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

156 lines
6.4 KiB
Java
Raw Normal View History

2024-10-14 12:14:01 +03:00
package _VisualDVM.Passes.All;
2024-10-11 00:00:30 +03:00
import Common.Utils.Utils_;
2024-10-14 15:19:13 +03:00
import Common.Visual.Controls.PassMenuItem;
2024-10-11 00:00:30 +03:00
import Common.Visual.UI_;
2024-10-09 22:01:19 +03:00
import _VisualDVM.Current;
2024-10-12 00:17:51 +03:00
import _VisualDVM.Global;
2024-10-09 22:21:57 +03:00
import _VisualDVM.GlobalData.Settings.SettingName;
2024-10-14 15:19:13 +03:00
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Server.ComponentsRepositoryPass;
2024-10-09 22:21:57 +03:00
import _VisualDVM.ProjectData.Files.DBProjectFile;
import _VisualDVM.ProjectData.Project.db_project_info;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Repository.Server.ServerExchangeUnit_2021;
2024-10-14 15:19:13 +03:00
import _VisualDVM.Utils;
2023-09-17 22:13:42 +03:00
import org.apache.commons.io.FileUtils;
import javax.swing.*;
import java.io.File;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Vector;
public class DVMConvertProject extends ComponentsRepositoryPass<db_project_info> {
File workspace = null;
File archive = null;
Vector<DBProjectFile> programsToConvert = null;
Vector<String> programsNames = null;
String output = null;
File versionArchive = null;
File version = null;
String badFiles = "";
@Override
protected boolean canStart(Object... args) throws Exception {
programsToConvert = null;
programsNames = null;
output = "";
workspace = null;
archive = null;
versionArchive = null;
version = null;
badFiles = "";
//--
if (Global.mainModule.Check(Log, Current.Project)) {
target = Global.mainModule.getProject();
2023-09-17 22:13:42 +03:00
programsToConvert = target.getPrograms().get(target.languageName);
programsNames = new Vector<>();
if (programsToConvert.size() > 100) {
Log.Writeln_("Количество активных файлов кода на языке " + target.languageName + " в проекте превышает 100.");
return false;
}
for (DBProjectFile program : programsToConvert) {
2024-10-11 00:00:30 +03:00
if (Utils_.getFileSizeMegaBytes(program.file) > 1) {
Log.Writeln_("Размер файла кода " + Utils_.Brackets(program.name) + " превышает 1 Мб.");
2023-09-17 22:13:42 +03:00
return false;
2024-10-11 00:00:30 +03:00
} else programsNames.add(Utils_.toU(program.name));
2023-09-17 22:13:42 +03:00
}
workspace = Utils.getTempFileName("convertation");
FileUtils.forceMkdir(workspace);
File cleanCopy = Paths.get(workspace.getAbsolutePath(), target.name).toFile();
FileUtils.forceMkdir(cleanCopy);
archive = Paths.get(workspace.getAbsolutePath(), target.name + ".zip").toFile();
target.Clone(cleanCopy, false);
2024-10-14 15:19:13 +03:00
if (!Global.mainModule.getPass(PassCode.ZipFolderPass).Do(cleanCopy.getAbsolutePath(), archive.getAbsolutePath())) {
2023-09-17 22:13:42 +03:00
Log.Writeln_("Не удалось создать архив проекта!");
return false;
}
;
2024-10-11 00:00:30 +03:00
if (Utils_.getFileSizeMegaBytes(archive) > 500) {
2023-09-17 22:13:42 +03:00
Log.Writeln_("Размер архива проекта превышает 500 МБ");
return false;
}
return true;
}
return false;
}
@Override
protected void performPreparation() throws Exception {
super.performPreparation();
db_project_info.ResetNewVersions();
}
//-
@Override
public String getIconPath() {
2024-10-14 01:50:33 +03:00
return "/icons/Transformations/" + getName() + ".png";
2023-09-17 22:13:42 +03:00
}
@Override
public boolean hasStats() {
2023-09-17 22:13:42 +03:00
return true;
}
@Override
protected int getTimeout() {
return 0;
}
@Override
protected void ServerAction() throws Exception {
ServerExchangeUnit_2021 unit = new ServerExchangeUnit_2021(ServerCode.DVMConvertProject);
2024-10-11 00:00:30 +03:00
unit.object = Utils_.fileToBytes(archive);
2023-09-17 22:13:42 +03:00
Vector<String> unit_args = new Vector<>();
unit_args.add(target.name);
unit_args.add(target.languageName.toString());
2024-10-12 00:17:51 +03:00
unit_args.add((Global.mainModule.getDb()).settings.get(SettingName.DVMConvertationOptions).toString());
2023-09-17 22:13:42 +03:00
unit_args.addAll(programsNames);
unit.arg = String.join("\n", unit_args);
Command(unit);
output = response.arg;
versionArchive = new File(workspace, target.name + "_result.zip");
response.Unpack(versionArchive);
File result = new File(workspace, "result");
2024-10-14 15:19:13 +03:00
if (Global.mainModule.getPass(PassCode.UnzipFolderPass).Do(versionArchive.getAbsolutePath(), result.getAbsolutePath())) {
2023-09-17 22:13:42 +03:00
if (target.last_modification == null) {
target.last_modification = new db_project_info(target,
"m",
"копия от " + new Date(),
"");
target.last_modification.CloneParent();
}
File resultProject = new File(result, target.name);
String versionName = target.GenerateVersionName("v");
version = new File(target.Home, versionName);
FileUtils.moveDirectory(resultProject, version);
if (!version.exists()) Log.Writeln_("Не удалось перенести результат");
} else Log.Writeln_("Не удалось распаковать результат");
}
@Override
protected void performFinish() throws Exception {
super.performFinish();
String[] data = output.split("\\|");
if (data.length > 1) {
badFiles = "Не удалось сконвертировать " + data[0].split("\n").length + " файлов:\n" + data[0];
target.updateCompilationOut(data[1]);
} else
target.updateCompilationOut(output);
}
@Override
protected void showFinish() throws Exception {
Global.mainModule.getUI().getMainWindow().getProjectWindow().RefreshProjectTreeAndMessages();
if (Global.mainModule.HasFile()) {
Global.mainModule.getFile().form.ShowCompilationOutput();
2023-09-17 22:13:42 +03:00
if (!output.isEmpty())
Global.mainModule.getFile().form.FocusCompilationOut();
2023-09-17 22:13:42 +03:00
}
if (!badFiles.isEmpty())
2024-10-11 00:00:30 +03:00
UI_.Error(badFiles);
2023-09-17 22:13:42 +03:00
}
@Override
protected void performDone() throws Exception {
target.joinExistingVersion(version, getDescription());
2024-10-14 15:19:13 +03:00
target.migrateFilesSettings(target.last_version, false, false);
2023-09-17 22:13:42 +03:00
}
@Override
public JMenuItem createMenuItem() {
if (menuItem == null)
menuItem = new PassMenuItem(this);
return menuItem;
}
}