Launcher/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java

96 lines
3.6 KiB
Java
Raw Normal View History

2018-09-17 10:07:32 +03:00
package ru.gravit.launchserver.binary;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
2018-12-02 15:21:27 +03:00
import java.util.ArrayList;
import java.util.List;
2019-01-08 16:36:05 +03:00
import java.util.concurrent.atomic.AtomicLong;
import ru.gravit.launcher.Launcher;
2018-12-26 15:33:49 +03:00
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.binary.tasks.AttachJarsTask;
import ru.gravit.launchserver.binary.tasks.LauncherBuildTask;
import ru.gravit.launchserver.binary.tasks.MainBuildTask;
import ru.gravit.launchserver.binary.tasks.ProGuardBuildTask;
import ru.gravit.launchserver.binary.tasks.AdditionalFixesApplyTask;
2019-01-09 11:29:54 +03:00
import ru.gravit.launchserver.binary.tasks.PrepareBuildTask;
2019-01-08 16:36:05 +03:00
import ru.gravit.utils.helper.CommonHelper;
2018-12-26 15:33:49 +03:00
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
2018-09-17 10:07:32 +03:00
public final class JARLauncherBinary extends LauncherBinary {
2019-01-08 16:36:05 +03:00
public final AtomicLong count;
public final Path runtimeDir;
public final Path guardDir;
2019-01-08 16:36:05 +03:00
public final Path buildDir;
public List<LauncherBuildTask> tasks;
public List<Path> coreLibs;
2019-01-08 16:41:13 +03:00
2018-12-20 18:45:01 +03:00
public JARLauncherBinary(LaunchServer server) throws IOException {
super(server);
2019-01-08 16:36:05 +03:00
count = new AtomicLong(0);
syncBinaryFile = server.dir.resolve(server.config.binaryName + ".jar");
runtimeDir = server.dir.resolve(Launcher.RUNTIME_DIR);
guardDir = server.dir.resolve(Launcher.GUARD_DIR);
2019-01-08 16:36:05 +03:00
buildDir = server.dir.resolve("build");
2019-01-08 16:41:13 +03:00
tasks = new ArrayList<>();
coreLibs = new ArrayList<>();
2019-01-09 11:29:54 +03:00
if (!Files.isDirectory(buildDir)) {
Files.deleteIfExists(buildDir);
Files.createDirectory(buildDir);
}
2019-01-08 16:57:01 +03:00
}
@Override
public void init() {
2019-01-09 11:29:54 +03:00
tasks.add(new PrepareBuildTask(server));
2019-01-08 16:41:13 +03:00
tasks.add(new MainBuildTask(server));
tasks.add(new ProGuardBuildTask(server));
tasks.add(new AttachJarsTask(server));
tasks.add(new AdditionalFixesApplyTask(server));
2018-12-20 18:45:01 +03:00
}
2019-01-08 16:57:01 +03:00
2018-12-20 18:45:01 +03:00
@Override
public void build() throws IOException {
LogHelper.info("Building launcher binary file");
2019-01-09 11:29:54 +03:00
count.set(0); // set jar number
Path thisPath = null;
boolean isNeedDelete = false;
long time_start = System.currentTimeMillis();
long time_this = time_start;
for(LauncherBuildTask task : tasks)
{
LogHelper.subInfo("Task %s",task.getName());
Path oldPath = thisPath;
thisPath = task.process(oldPath);
long time_task_end = System.currentTimeMillis();
long time_task = time_task_end - time_this;
time_this = time_task_end;
if (isNeedDelete && server.config.deleteTempFiles) Files.deleteIfExists(oldPath);
isNeedDelete = task.allowDelete();
LogHelper.subInfo("Task %s processed from %d millis",task.getName(), time_task);
}
long time_end = System.currentTimeMillis();
2019-01-08 16:50:40 +03:00
if (isNeedDelete && server.config.deleteTempFiles) IOHelper.move(thisPath, syncBinaryFile);
else IOHelper.copy(thisPath, syncBinaryFile);
LogHelper.info("Build successful from %d millis",time_end - time_start);
2018-12-20 18:45:01 +03:00
}
2019-01-08 16:36:05 +03:00
public String nextName(String taskName) {
return String.format("Launcher-%s-%d.jar", server.config.projectName, taskName, count.getAndIncrement());
2019-01-08 16:36:05 +03:00
}
public Path nextPath(String taskName) {
return buildDir.resolve(nextName(taskName));
}
public Path nextPath(LauncherBuildTask task) {
return nextPath(task.getName());
}
public Path nextLowerPath(LauncherBuildTask task) {
return nextPath(CommonHelper.low(task.getName()));
}
2018-09-17 10:07:32 +03:00
}