From 3d04a8183d47eecc5988a8108e311835bdedc7d3 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 2 Jan 2022 15:15:55 +0100 Subject: [PATCH] Doing First Release --- pom.xml | 23 ++++++--- src/main/java/de/chaos/swlnmngr/Main.java | 5 +- .../java/de/chaos/swlnmngr/UpdateChecker.java | 51 +++++++++++++++++++ .../de/chaos/swlnmngr/config/CLIConfig.java | 12 +++-- .../java/de/chaos/swlnmngr/config/Config.java | 6 ++- .../java/de/chaos/swlnmngr/route/Router.java | 11 ++-- .../swlnmngr/route/routes/InstallRoute.java | 44 +++++++++++++++- .../swlnmngr/route/routes/LinkRoute.java | 50 ++++++++++++++++++ .../chaos/swlnmngr/route/routes/NewRoute.java | 43 ++++++++++++++++ src/main/resources/default_config.json | 7 ++- src/main/resources/jar.version | 1 + test.sh | 2 +- 12 files changed, 231 insertions(+), 24 deletions(-) create mode 100644 src/main/java/de/chaos/swlnmngr/UpdateChecker.java create mode 100644 src/main/java/de/chaos/swlnmngr/route/routes/LinkRoute.java create mode 100644 src/main/java/de/chaos/swlnmngr/route/routes/NewRoute.java create mode 100644 src/main/resources/jar.version diff --git a/pom.xml b/pom.xml index 83d15b9..1c13204 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.chaos swlnmngr - 1.0-SNAPSHOT + 0.1 SteamWarLinkManager @@ -40,11 +40,21 @@ + + src/main/resources + + *.version + + true + src/main/resources *.* + + *.version + false @@ -57,12 +67,6 @@ json 20211205 - - junit - junit - 4.13.2 - test - org.projectlombok lombok @@ -88,5 +92,10 @@ httpclient 4.5.13 + + commons-lang + commons-lang + 2.6 + \ No newline at end of file diff --git a/src/main/java/de/chaos/swlnmngr/Main.java b/src/main/java/de/chaos/swlnmngr/Main.java index 8a7ecdc..ae529c9 100644 --- a/src/main/java/de/chaos/swlnmngr/Main.java +++ b/src/main/java/de/chaos/swlnmngr/Main.java @@ -23,11 +23,14 @@ public class Main { if(CLIConfig.IS_DEBUG) { Configurator.setRootLevel(Level.DEBUG); } + if(!CLIConfig.NO_UPDATE) { + UpdateChecker.checkForUpdates(); + } logger.debug("Arguments: {}", Arrays.toString(allArgs)); if(CLIConfig.ARGS.length > 0) { Router.route(CLIConfig.ARGS); } else { - + Router.printRoutes(); } } } diff --git a/src/main/java/de/chaos/swlnmngr/UpdateChecker.java b/src/main/java/de/chaos/swlnmngr/UpdateChecker.java new file mode 100644 index 0000000..5f7bcd8 --- /dev/null +++ b/src/main/java/de/chaos/swlnmngr/UpdateChecker.java @@ -0,0 +1,51 @@ +package de.chaos.swlnmngr; + +import de.chaos.swlnmngr.config.Config; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONTokener; + +import java.io.IOException; +import java.net.URI; + +public class UpdateChecker { + + private static final URI repoUrl = URI.create("https://steamwar.de/devlabs/api/v1/repos/Chaoscaot/SteamwarLinkManager/releases?draft=false&pre-release=" + Config.PRE_RELEASES + "&page=1&limit=1"); + + public static final String CURRENT_VERSION; + + static { + try { + CURRENT_VERSION = new String(UpdateChecker.class.getResourceAsStream("/jar.version").readAllBytes()); + } catch (IOException e) { + Main.getLogger().error("Could not read Version", e); + System.exit(1); + throw new SecurityException(e); + } + } + + public static void checkForUpdates() { + try (CloseableHttpClient client = HttpClients.createMinimal()) { + HttpGet get = new HttpGet(repoUrl); + try (CloseableHttpResponse response = client.execute(get)) { + JSONArray array = new JSONArray(new JSONTokener(response.getEntity().getContent())); + JSONObject latestObject = array.getJSONObject(0); + String latestVersion = latestObject.getString("name"); + if(!latestVersion.equals(CURRENT_VERSION)) { + Main.getLogger().info("The Running Jar is not the Latest release Version\n\tYour Version: {}\n\tLatest Release Version: {}", CURRENT_VERSION, latestVersion); + Main.getLogger().info("Download the Latest Jar here: https://steamwar.de/devlabs/Chaoscaot/SteamwarLinkManager/releases"); + } + } + } catch (IOException e) { + Main.getLogger().error("Could not fetch Updates", e); + } + } + + public static void update() { + + } +} diff --git a/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java b/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java index 9f15fa6..c0b95c2 100644 --- a/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java +++ b/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java @@ -8,17 +8,18 @@ import java.io.File; public class CLIConfig { public static final boolean IS_DEBUG; - public static final boolean UPDATE; + public static final boolean NO_UPDATE; public static final File CONFIG; public static final File INSTALL_DIR; public static final String[] ARGS; static { Options options = new Options(); + options.addOption(new Option("h", "help", false, "Display this Message")); options.addOption(new Option("d", "debug", false, "Set the Log Level to Debug")); - options.addOption(new Option("u", "update", false, "Update the Default libs")); options.addOption(new Option("c", "config", true, "Use another Config File")); options.addOption(new Option("i", "installdir", true, "Use other Install Dir")); + options.addOption(new Option("u", "no-update", false, "Disable the Auto-Update Checker")); CommandLine cli = null; CommandLineParser parser = new DefaultParser(); @@ -32,9 +33,14 @@ public class CLIConfig { System.exit(1); } + if(cli.hasOption("h")) { + formatter.printHelp("swlnmngr", options, true); + System.exit(0); + } + ARGS = cli.getArgs(); IS_DEBUG = cli.hasOption("d"); - UPDATE = cli.hasOption("u"); + NO_UPDATE = cli.hasOption("u"); if(cli.hasOption("i")) { INSTALL_DIR = new File(cli.getOptionValue("i")); } else { diff --git a/src/main/java/de/chaos/swlnmngr/config/Config.java b/src/main/java/de/chaos/swlnmngr/config/Config.java index dcb52b6..16ce300 100644 --- a/src/main/java/de/chaos/swlnmngr/config/Config.java +++ b/src/main/java/de/chaos/swlnmngr/config/Config.java @@ -16,10 +16,11 @@ public class Config { private static final File CONFIG_FILE = new File(CLIConfig.INSTALL_DIR, "config.json"); - public static final String PROJECT_PATH; + public static final File PROJECT_PATH; public static final File LIB_PATH; public static final String DEFAULTS; public static final URI LIB_URL; + public static final boolean PRE_RELEASES; public static final String USERNAME; public static final String PASSWORD; @@ -40,9 +41,10 @@ public class Config { System.exit(1); } - PROJECT_PATH = config.getString("projectPath"); + PROJECT_PATH = new File(config.getString("projectPath")); LIB_PATH = new File(config.getString("libPath")); DEFAULTS = config.getString("defaultName"); + PRE_RELEASES = config.getBoolean("preReleases"); try { LIB_URL = new URI(config.getString("libUrl")); } catch (URISyntaxException e) { diff --git a/src/main/java/de/chaos/swlnmngr/route/Router.java b/src/main/java/de/chaos/swlnmngr/route/Router.java index 4fff1c4..c00f495 100644 --- a/src/main/java/de/chaos/swlnmngr/route/Router.java +++ b/src/main/java/de/chaos/swlnmngr/route/Router.java @@ -1,9 +1,7 @@ package de.chaos.swlnmngr.route; import de.chaos.swlnmngr.Main; -import de.chaos.swlnmngr.route.routes.InstallRoute; -import de.chaos.swlnmngr.route.routes.Route; -import de.chaos.swlnmngr.route.routes.UpdateRoute; +import de.chaos.swlnmngr.route.routes.*; import java.util.*; @@ -12,7 +10,7 @@ public class Router { private static final List ROUTES; static { - ROUTES = List.of(new InstallRoute(), new UpdateRoute()); + ROUTES = List.of(new InstallRoute(), new UpdateRoute(), new LinkRoute(), new NewRoute()); } public static void route(String[] args) { @@ -29,4 +27,9 @@ public class Router { } } } + + public static void printRoutes() { + Main.getLogger().info("Available Routes: "); + Main.getLogger().info("\t{}", ROUTES.stream().map(Route::getName).reduce((s, s2) -> s + ", " + s2).get()); + } } diff --git a/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java b/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java index e4e942e..57adbb1 100644 --- a/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java +++ b/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java @@ -1,19 +1,22 @@ package de.chaos.swlnmngr.route.routes; -import com.sun.nio.file.ExtendedCopyOption; import de.chaos.swlnmngr.Main; import de.chaos.swlnmngr.config.CLIConfig; +import org.apache.commons.lang.SystemUtils; +import org.json.JSONObject; +import org.json.JSONTokener; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; import java.util.Objects; public class InstallRoute implements Route { - private static final String[] defaultFiles = new String[] {"default_config.json", "default_swlnmngr.bat", "default_swlnmngr.sh"}; + private static final String[] defaultFiles = new String[] {"default_swlnmngr.bat", "default_swlnmngr.sh"}; @Override public String getName() { @@ -37,6 +40,43 @@ public class InstallRoute implements Route { } } + if(SystemUtils.IS_OS_UNIX) { + try { + Files.deleteIfExists(new File(CLIConfig.INSTALL_DIR, "swlnmngr").toPath()); + Files.createSymbolicLink(new File(CLIConfig.INSTALL_DIR, "swlnmngr").toPath(), new File(CLIConfig.INSTALL_DIR, "swlnmngr.sh").toPath()); + } catch (IOException e) { + Main.getLogger().error("Could not create SymLink", e); + return false; + } + } + + File configFile = new File(installDir, "config.json"); + if(!configFile.exists()) { + try { + Files.copy(Objects.requireNonNull(InstallRoute.class.getResourceAsStream("/default_config.json")), configFile.toPath()); + } catch (IOException e) { + Main.getLogger().error("Could not copy Config File", e); + return false; + } + } else { + try { + JSONObject defaultConfig = new JSONObject(new JSONTokener(Objects.requireNonNull(InstallRoute.class.getResourceAsStream("/default_config.json")))); + Main.getLogger().info(defaultConfig); + JSONObject currentConfig = new JSONObject(Files.readString(configFile.toPath())); + Main.getLogger().info(currentConfig); + for (String s : defaultConfig.keySet()) { + if(currentConfig.has(s)) continue; + currentConfig.put(s, defaultConfig.get(s)); + } + Files.writeString(configFile.toPath(), + currentConfig.toString(2), + StandardOpenOption.TRUNCATE_EXISTING); + } catch (IOException e) { + Main.getLogger().error("Could write Config File", e); + return false; + } + } + try { File jar = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); Main.getLogger().debug(jar); diff --git a/src/main/java/de/chaos/swlnmngr/route/routes/LinkRoute.java b/src/main/java/de/chaos/swlnmngr/route/routes/LinkRoute.java new file mode 100644 index 0000000..3e26bca --- /dev/null +++ b/src/main/java/de/chaos/swlnmngr/route/routes/LinkRoute.java @@ -0,0 +1,50 @@ +package de.chaos.swlnmngr.route.routes; + +import de.chaos.swlnmngr.Main; +import de.chaos.swlnmngr.config.Config; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class LinkRoute implements Route { + @Override + public String getName() { + return "link"; + } + + @Override + public boolean route(String[] args) { + if(args.length < 1) { + Main.getLogger().error("Usage: swlnmngr [Project] {Libs}"); + return false; + } + String libs = args.length>1?args[1]:Config.DEFAULTS; + File libsFile = new File(Config.LIB_PATH, libs); + if(!libsFile.exists()) { + Main.getLogger().error("No Libs with name: {}", libs); + if(libs.equals(Config.DEFAULTS)) { + Main.getLogger().info("To Create the {} Folder run 'swlnmngr update'", Config.DEFAULTS); + } + return false; + } + File projectDir = new File(Config.PROJECT_PATH, args[0]); + if(!projectDir.exists()) { + Main.getLogger().error("No Project with name: {}", args[0]); + return false; + } + File link = new File(projectDir, "libs"); + try { + Files.deleteIfExists(link.toPath()); + Main.getLogger().debug(libsFile); + Main.getLogger().debug(link); + Path linkPath = Files.createSymbolicLink(link.toPath(), libsFile.toPath()); + Main.getLogger().debug(linkPath); + } catch (IOException e) { + Main.getLogger().error("Could not Create SymLink", e); + return false; + } + return true; + } +} diff --git a/src/main/java/de/chaos/swlnmngr/route/routes/NewRoute.java b/src/main/java/de/chaos/swlnmngr/route/routes/NewRoute.java new file mode 100644 index 0000000..8b5abd6 --- /dev/null +++ b/src/main/java/de/chaos/swlnmngr/route/routes/NewRoute.java @@ -0,0 +1,43 @@ +package de.chaos.swlnmngr.route.routes; + +import de.chaos.swlnmngr.Main; +import de.chaos.swlnmngr.config.Config; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class NewRoute implements Route { + @Override + public String getName() { + return "new"; + } + + @Override + public boolean route(String[] args) { + if(args.length < 1) { + Main.getLogger().error("Usage: swlnmngr [Name]"); + return false; + } + String name = args[0]; + File newFile = new File(Config.LIB_PATH, name); + Main.getLogger().debug(newFile); + if(newFile.exists()) { + Main.getLogger().error("Directory {} already exists", name); + return false; + } + newFile.mkdir(); + File defaultFile = new File(Config.LIB_PATH, Config.DEFAULTS); + Main.getLogger().debug(defaultFile); + try { + for (File file : defaultFile.listFiles()) { + Main.getLogger().debug(file); + Files.createSymbolicLink(new File(newFile, file.getName()).toPath(), file.toPath()); + } + } catch (IOException e) { + Main.getLogger().error("Could not create SymLink", e); + return false; + } + return true; + } +} diff --git a/src/main/resources/default_config.json b/src/main/resources/default_config.json index 62115ca..66afcfd 100644 --- a/src/main/resources/default_config.json +++ b/src/main/resources/default_config.json @@ -3,8 +3,7 @@ "libPath": "~/libs/", "defaultName": "default", "libUrl": "https://steamwar.de/lib.php", - "credentials": { - "username": "[EnterName]", - "password": "[EnterPW]" - } + "preReleases": false, + "username": "[EnterName]", + "password": "[EnterPW]" } \ No newline at end of file diff --git a/src/main/resources/jar.version b/src/main/resources/jar.version new file mode 100644 index 0000000..e78e522 --- /dev/null +++ b/src/main/resources/jar.version @@ -0,0 +1 @@ +${version} \ No newline at end of file diff --git a/test.sh b/test.sh index 16cc0ab..7b55f9f 100644 --- a/test.sh +++ b/test.sh @@ -1 +1 @@ -java -jar target/SteamWarLinkManager.jar -i /home/chaos/IdeaProjects/swlnmngr/test/ install \ No newline at end of file +java -jar target/SteamWarLinkManager.jar -i ~/IdeaProjects/swlnmngr/test -du install \ No newline at end of file