All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vlinx.java.wrapper.JavaWrapper Maven / Gradle / Ivy

package io.vlinx.java.wrapper;

import io.vlinx.communicate.app.IAppMsgListener;
import io.vlinx.communicate.logging.Logger;
import io.vlinx.communicate.process.ProcessUtil;
import io.vlinx.configutils.JSONUtils;
import io.vlinx.processutils.exception.ProcessException;
import io.vlinx.utils.FileUtil;
import io.vlinx.utils.StringUtil;
import io.vlinx.utils.SystemInfo;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

public class JavaWrapper {

    private Config config = null;
    private IAppMsgListener listener;

    public JavaWrapper(Config config) {
        this.config = config;
    }

    public void run() throws Exception {
        if (config == null) {
            throw new Exception("Null config");
        }

        if (!FileUtil.isExist(config.getAppFolder())) {
            throw new Exception("App dir: " + config.getAppFolder() + "not exists");
        }

        if (!FileUtil.isExist(config.getJreFolder())) {
            throw new Exception("JRE dir:" + config.getJreFolder() + "no exists");
        }

        if (FileUtil.isExist(config.getOutputFolder())) {
            Logger.INFO("Create directory", config.getOutputFolder(), listener);
            FileUtils.forceMkdir(new File(config.getOutputFolder()));
        }

        String targetAppFolder = config.getOutputFolder() + File.separator + new File(config.getAppFolder()).getName();
        Logger.INFO("Copy app folder to " + targetAppFolder, listener);
        FileUtils.copyDirectory(new File(config.getAppFolder()), new File(targetAppFolder), new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (!StringUtil.isEmpty(config.getJreFolder()) && pathname.equals(new File(config.getJreFolder()))) {
                    return false;
                }

                return true;
            }
        });

        if (!StringUtil.isEmpty(config.getJreFolder())) {
            String targetJreFolder = targetAppFolder + File.separator + "jre";
            Logger.INFO("Copy jre to " + targetJreFolder, listener);
            FileUtils.copyDirectory(new File(config.getJreFolder()), new File(targetJreFolder));
        }


        WrapperSettings settings = config.getWrapperSettings();

        JSONUtils.saveObject(settings, targetAppFolder + File.separator + config.getExeFileName() + ".json");

        String wrapperPath = "";
        String platform = SystemInfo.getPlatform();

        if (!StringUtil.isEmpty(config.getTargetPlatform())) {
            platform = config.getTargetPlatform();
        }

        switch (platform) {
            case Constants.PLATFORM_WIN64:
                wrapperPath = Resources.WIN64_WRAPPER;
                break;
            case Constants.PLATFORM_WIN32:
                wrapperPath = Resources.WIN32_WRAPPER;
                break;
            case Constants.PLATFORM_LINUX64:
                wrapperPath = Resources.LINUX64_WRAPPER;
                break;
            case Constants.PLATFORM_LINUX32:
                wrapperPath = Resources.LINUX32_WRAPPER;
                break;
            case Constants.PLATFORM_MAC:
                wrapperPath = Resources.MAC_WRAPPER;
                break;
            default:
                throw new Exception("Unknown platform");
        }

        if (config.isHideConsole()) {
            switch (platform) {
                case Constants.PLATFORM_WIN64:
                    wrapperPath = Resources.WIN64_WRAPPER_HIDE_CONSOLE;
                    break;
                case Constants.PLATFORM_WIN32:
                    wrapperPath = Resources.WIN32_WRAPPER_HIDE_CONSOLE;
                    break;
            }
        }

        wrapperPath = App.resourceManager.getResourcePath(wrapperPath);
        String exeFileName = config.getExeFileName();

        if (platform.startsWith("win")) {
            exeFileName += ".exe";
        }

        Logger.INFO("Copy wrapper file", listener);
        String exeFilePath = targetAppFolder + File.separator + exeFileName;

        FileUtils.copyFile(new File(wrapperPath), new File(exeFilePath));

        if (platform.startsWith("win") && SystemInfo.getPlatform().startsWith("win")) {
            rcedit(config, exeFilePath);
        }

        Logger.INFO("Task complete", listener);

    }

    public void rcedit(Config config, String targetExeFile) throws Exception {

        String rcedit = "";

        String platform = SystemInfo.getPlatform();
        switch (platform) {
            case Constants.PLATFORM_WIN64:
                rcedit = Resources.WIN64_RCEDIT;
                break;
            case Constants.PLATFORM_WIN32:
                rcedit = Resources.WIN32_RCEDIT;
                break;
            default:
                throw new Exception("rcedit only can run in windows");
        }

        rcedit = App.resourceManager.getResourcePath(rcedit);

        if (!StringUtil.isEmpty(config.getIcon())) {
            String command = rcedit + " \"" + targetExeFile + "\" --set-icon \"" + config.getIcon() + "\"";
            Logger.INFO("Command", command, listener);
            try {
                ProcessUtil.runWithListener(command, listener);
            } catch (Exception e) {
                Logger.ERROR(e, listener);
            }
        }

        if (!StringUtil.isEmpty(config.getFileVersion())) {
            String command = rcedit + " \"" + targetExeFile + "\" --set-file-version \"" + config.getFileVersion()
                    + "\"";
            Logger.INFO("Command", command, listener);
            try {
                ProcessUtil.runWithListener(command, listener);
            } catch (Exception e) {
                Logger.ERROR(e, listener);
            }
        }

        if (!StringUtil.isEmpty(config.getProductVersion())) {
            String command = rcedit + " \"" + targetExeFile + "\" --set-product-version \"" + config.getProductVersion()
                    + "\"";
            Logger.INFO("Command", command, listener);
            try {
                ProcessUtil.runWithListener(command, listener);
            } catch (IOException | InterruptedException | ProcessException e) {
                Logger.ERROR(e, listener);
            }
        }

        if (!StringUtil.isEmpty(config.getCompanyName())) {
            String command = rcedit + " \"" + targetExeFile + "\" --set-version-string \"CompanyName\" \""
                    + config.getCompanyName() + "\"";
            Logger.INFO("Command", command, listener);
            try {
                ProcessUtil.runWithListener(command, listener);
            } catch (IOException | InterruptedException | ProcessException e) {
                Logger.ERROR(e, listener);
            }
        }

        if (!StringUtil.isEmpty(config.getProductName())) {
            String command = rcedit + " \"" + targetExeFile + "\" --set-version-string \"ProductName\" \""
                    + config.getProductName() + "\"";
            Logger.INFO("Command", command, listener);
            try {
                ProcessUtil.runWithListener(command, listener);
            } catch (IOException | InterruptedException | ProcessException e) {
                Logger.ERROR(e, listener);
            }
        }

        if (!StringUtil.isEmpty(config.getLegalCopyright())) {
            String command = rcedit + " \"" + targetExeFile + "\" --set-version-string \"LegalCopyright\" \""
                    + config.getLegalCopyright() + "\"";
            Logger.INFO("Command", command, listener);
            try {
                ProcessUtil.runWithListener(command, listener);
            } catch (IOException | InterruptedException | ProcessException e) {
                Logger.ERROR(e, listener);
            }
        }

        if (!StringUtil.isEmpty(config.getFileDescription())) {
            String command = rcedit + " \"" + targetExeFile + "\" --set-version-string \"FileDescription\" \""
                    + config.getFileDescription() + "\"";
            Logger.INFO("Command", command, listener);
            try {
                ProcessUtil.runWithListener(command, listener);
            } catch (IOException | InterruptedException | ProcessException e) {
                Logger.ERROR(e, listener);
            }
        }


    }

    public Config getConfig() {
        return config;
    }

    public IAppMsgListener getListener() {
        return listener;
    }

    public void setListener(IAppMsgListener listener) {
        this.listener = listener;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy