From 9c0a22b23d27670f10211059d0d90660849a8e47 Mon Sep 17 00:00:00 2001 From: Jachou Date: Wed, 31 May 2023 17:22:15 +0200 Subject: [PATCH 1/5] JavaVersionDownloader for an external execution (not API) and java_dl.py in python --- .github/workflows/gradle-publish.yml | 43 +----- .idea/inspectionProfiles/Project_Default.xml | 2 + .idea/uiDesigner.xml | 124 +++++++++++++++ build.gradle.kts | 2 +- .../fr/jachou/jvm/JavaVersionDownloader.java | 141 ++++++++++++++++++ src/main/java/fr/jachou/jvm/Logger.java | 19 +++ src/main/java/fr/jachou/jvm/Main.java | 30 ---- .../jvm/managers/JavaVersionManager.java | 21 ++- src/main/java/fr/jachou/jvm/py/java_dl.py | 84 +++++++++++ 9 files changed, 382 insertions(+), 84 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/fr/jachou/jvm/JavaVersionDownloader.java create mode 100644 src/main/java/fr/jachou/jvm/Logger.java delete mode 100644 src/main/java/fr/jachou/jvm/Main.java create mode 100644 src/main/java/fr/jachou/jvm/py/java_dl.py diff --git a/.github/workflows/gradle-publish.yml b/.github/workflows/gradle-publish.yml index 0a12036..a3de5e1 100644 --- a/.github/workflows/gradle-publish.yml +++ b/.github/workflows/gradle-publish.yml @@ -5,45 +5,4 @@ # This workflow will build a package using Gradle and then publish it to GitHub packages when a release is created # For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-gradle -name: Gradle Package - -on: - push: - branches: - - master - -jobs: - build: - - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - - steps: - - uses: actions/checkout@v3 - - name: Set up JDK 8 - uses: actions/setup-java@v3 - with: - java-version: '8' - distribution: 'temurin' - server-id: github # Value of the distributionManagement/repository/id field of the pom.xml - settings-path: ${{ github.workspace }} # location for the settings.xml file - - - name: Chmod gradlew - run: chmod +x gradlew - - - name: Build with Gradle - uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 - with: - arguments: build - - # The USERNAME and TOKEN need to correspond to the credentials environment variables used in - # the publishing section of your build.gradle - - name: Publish to GitHub Packages - uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 - with: - arguments: publish - env: - USERNAME: ${{ github.actor }} - TOKEN: ${{ secrets.GITHUB_TOKEN }} +# No Gradle publish workflow is needed for this branch diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index d4a00d6..700bf3a 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -5,6 +5,8 @@ diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 8c26901..32e593c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,7 +22,7 @@ publishing { } group = "fr.jachou" -version = "0.0.2" +version = "0.0.3" repositories { mavenCentral() diff --git a/src/main/java/fr/jachou/jvm/JavaVersionDownloader.java b/src/main/java/fr/jachou/jvm/JavaVersionDownloader.java new file mode 100644 index 0000000..158b24d --- /dev/null +++ b/src/main/java/fr/jachou/jvm/JavaVersionDownloader.java @@ -0,0 +1,141 @@ +package fr.jachou.jvm; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.Scanner; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class JavaVersionDownloader { + // Class + + /** + * Colors class + * This class is used to color the text in the console + */ + static class Colors { + public static final String HEADER = "\033[95m"; + public static final String OKBLUE = "\033[94m"; + public static final String OKCYAN = "\033[96m"; + public static final String OKGREEN = "\033[92m"; + public static final String WARNING = "\033[93m"; + public static final String FAIL = "\033[91m"; + public static final String ENDC = "\033[0m"; + public static final String BOLD = "\033[1m"; + public static final String UNDERLINE = "\033[4m"; + } + + /** + * Main method + * @param args The arguments + */ + + public static void main(String[] args) { + boolean loop = true; + + while (loop) { + Scanner scanner = new Scanner(System.in); + + // Message JVM = JavaVersionManager + System.out.println(Colors.HEADER + " \n" + + "____________ ________ ___\n" + + "______ /__ | / /___ |/ /\n" + + "___ _ / __ | / / __ /|_/ /\n" + + "/ /_/ / __ |/ / _ / / / \n" + + "\\____/ _____/ /_/ /_/ \n" + + " \n"); + + try { + System.out.print(Colors.OKCYAN + "Choose your version (only number) : "); + int version = Integer.parseInt(scanner.nextLine()); + downloader(version); + } catch (NumberFormatException e) { + System.out.println(Colors.FAIL + "You must enter an integer!"); + } + + System.out.println(); + } + } + + /** + * Unzip a file + * @param version The version of java to unzip + */ + + private static void downloader(int version) { + System.out.print(Colors.BOLD + "[~] Checking if the link is good..."); + String url = String.format("https://chiss.fr/jvm/download/Java_%d.zip", version); + try { + URL link = new URL(url); + link.openStream().close(); + System.out.println(Colors.OKGREEN + "[✓] The link is good!"); + Thread.sleep(1000); + String path = System.getProperty("user.home") + "/.jdks"; + System.out.println(Colors.OKCYAN + "The JDK " + version + " will be downloaded in the folder '" + path + "'"); + File directory = new File(path); + if (!directory.exists()) { + if (directory.mkdirs()) { + System.out.println(Colors.OKGREEN + "[✓] The folder has been created!"); + } + } else { + System.out.println(Colors.WARNING + "[~] The folder already exists"); + } + Thread.sleep(1000); + System.out.print(Colors.BOLD + "[~] Downloading..."); + Path destination = Paths.get(path + "/Java_" + version + ".zip"); + Files.copy(link.openStream(), destination, StandardCopyOption.REPLACE_EXISTING); + System.out.println(Colors.OKGREEN + "[✓] The JDK " + version + " has been downloaded!"); + Thread.sleep(1000); + System.out.print(Colors.BOLD + "[~] Unzipping..."); + unzip(destination.toString(), path + "/Java_" + version); + System.out.println(Colors.OKGREEN + "[✓] The JDK " + version + " has been unzipped!"); + Thread.sleep(1000); + System.out.print(Colors.BOLD + "[~] Deleting zip file..."); + Files.delete(destination); + System.out.println(Colors.OKGREEN + "[✓] The JDK " + version + " has been deleted!"); + Thread.sleep(1000); + System.out.println(Colors.OKGREEN + "[✓] The JDK " + version + " has been downloaded and installed!"); + Thread.sleep(1000); + } catch (IOException e) { + System.out.println(Colors.FAIL + "[X] The link does not exist!"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + /** + * Unzip it + * @param zipFilePath input zip file + * @param destinationDir zip file output folder + * @throws IOException if an I/O error has occurred + */ + + private static void unzip(String zipFilePath, String destinationDir) throws IOException { + File dir = new File(destinationDir); + if (!dir.exists()) { + dir.mkdirs(); + } + byte[] buffer = new byte[1024]; + try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Paths.get(zipFilePath)))) { + ZipEntry entry = zis.getNextEntry(); + while (entry != null) { + String fileName = entry.getName(); + File newFile = new File(destinationDir + File.separator + fileName); + if (entry.isDirectory()) { + newFile.mkdirs(); + } else { + new File(newFile.getParent()).mkdirs(); + Files.copy(zis, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + zis.closeEntry(); + entry = zis.getNextEntry(); + } + } + } +} + diff --git a/src/main/java/fr/jachou/jvm/Logger.java b/src/main/java/fr/jachou/jvm/Logger.java new file mode 100644 index 0000000..932b26e --- /dev/null +++ b/src/main/java/fr/jachou/jvm/Logger.java @@ -0,0 +1,19 @@ +package fr.jachou.jvm; + +import java.io.File; +import java.net.URISyntaxException; + +public class Logger { + private static fr.flowarg.flowlogger.Logger logger; + + static { + try { + logger = new fr.flowarg.flowlogger.Logger("[JavaVersionManager]", new File(Logger.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(), "jvm.log").toPath()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } + public static fr.flowarg.flowlogger.Logger getLogger() { + return logger; + } +} diff --git a/src/main/java/fr/jachou/jvm/Main.java b/src/main/java/fr/jachou/jvm/Main.java deleted file mode 100644 index d6125f5..0000000 --- a/src/main/java/fr/jachou/jvm/Main.java +++ /dev/null @@ -1,30 +0,0 @@ -package fr.jachou.jvm; - -import fr.flowarg.flowlogger.Logger; -import fr.jachou.jvm.managers.JavaVersionManager; -import fr.jachou.jvm.managers.utils.JavaVersionList; - -import java.io.File; -import java.net.URISyntaxException; - -public class Main { - private static Logger logger; - - static { - try { - logger = new Logger("[JavaVersionManager]", new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(), "jvm.log").toPath()); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } - - public static void main(String[] args) { - JavaVersionManager javaVersionManager = new JavaVersionManager(); - javaVersionManager.downloadVersion(JavaVersionList.Java_8, "C:\\"); - javaVersionManager.unzipJavaVersion(); - } - - public static Logger getLogger() { - return logger; - } -} diff --git a/src/main/java/fr/jachou/jvm/managers/JavaVersionManager.java b/src/main/java/fr/jachou/jvm/managers/JavaVersionManager.java index 904e2b8..6f61a19 100644 --- a/src/main/java/fr/jachou/jvm/managers/JavaVersionManager.java +++ b/src/main/java/fr/jachou/jvm/managers/JavaVersionManager.java @@ -1,10 +1,9 @@ package fr.jachou.jvm.managers; import fr.flowarg.flowzipper.ZipUtils; -import fr.jachou.jvm.Main; +import fr.jachou.jvm.Logger; import fr.jachou.jvm.managers.utils.JavaVersionList; -import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Files; @@ -48,9 +47,9 @@ public void downloadVersion(JavaVersionList version, Path path) { Path downloadPath = Paths.get(version + ".zip"); Files.copy(url.openStream(), downloadPath); - Main.getLogger().info("Downloaded " + version + " to " + path.toAbsolutePath().toString() + " successfully!"); + Logger.getLogger().info("Downloaded " + version + " to " + path.toAbsolutePath().toString() + " successfully!"); } catch (IOException e) { - Main.getLogger().err("Failed to download " + version + " to " + path.toAbsolutePath().toString() + "!"); + Logger.getLogger().err("Failed to download " + version + " to " + path.toAbsolutePath().toString() + "!"); } } @@ -69,10 +68,10 @@ public void downloadVersion(JavaVersionList version, String path) { Files.copy(url.openStream(), downloadPath); - Main.getLogger().info("Downloaded " + version + " to " + path + " successfully!"); + Logger.getLogger().info("Downloaded " + version + " to " + path + " successfully!"); } catch (IOException e) { e.getMessage(); - Main.getLogger().err("Failed to download " + version + " to " + path + "!"); + Logger.getLogger().err("Failed to download " + version + " to " + path + "!"); } } @@ -82,17 +81,17 @@ public void downloadVersion(JavaVersionList version, String path) { public void unzipJavaVersion() { if (this.path == null) { - Main.getLogger().err("Failed to unzip java version because the path is null!"); + Logger.getLogger().err("Failed to unzip java version because the path is null!"); return; } if (this.list == null) { - Main.getLogger().err("Failed to unzip java version because the list is null!"); + Logger.getLogger().err("Failed to unzip java version because the list is null!"); return; } try { ZipUtils.unzip(this.path, Paths.get(this.path + this.list.toString() +".zip")); } catch (IOException e) { - Main.getLogger().err("Failed to unzip java version " + this.list + "!"); + Logger.getLogger().err("Failed to unzip java version " + this.list + "!"); } } @@ -104,11 +103,11 @@ public void unzipJavaVersion() { public void unzipJavaVersion(Path path, JavaVersionList list) { try { - Main.getLogger().info(list.toString()); + Logger.getLogger().info(list.toString()); ZipUtils.unzip(path, Paths.get(path + "\\" + list.toString() +".zip")); } catch (IOException e) { e.printStackTrace(); - Main.getLogger().err("Failed to unzip java version " + list + "!"); + Logger.getLogger().err("Failed to unzip java version " + list + "!"); } } diff --git a/src/main/java/fr/jachou/jvm/py/java_dl.py b/src/main/java/fr/jachou/jvm/py/java_dl.py new file mode 100644 index 0000000..9da96bd --- /dev/null +++ b/src/main/java/fr/jachou/jvm/py/java_dl.py @@ -0,0 +1,84 @@ +# -------------------------------------------- # +# JavaVersionDownloader Python by JavaVersionManager +# Download faster java version in python +# -------------------------------------------- # + +# Class +class colors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + + +# Imports +from time import sleep +import requests +import os +import urllib.request +import zipfile + +# Def & Variables +loop = True + + +def downloader(version): + print(colors.BOLD + "[~] Checking if the link is good...") + response = requests.get(f"https://chiss.fr/jvm/download/Java_{version}.zip") + if response.status_code == 200: + print(colors.OKGREEN + "[✓] The link is good!") + sleep(1) + path = f"{os.path.expanduser('~')}/.jdks" + print(colors.OKCYAN + f"The JDK {version} will be download in the folder '{path}'") + if not os.path.exists(path): + os.makedirs(path) + print(colors.OKGREEN + "[✓] The folder has been created!") + else: + print(colors.WARNING + "[~] The folder already exists") + sleep(1) + print(colors.BOLD + "[~] Downloading...") + urllib.request.urlretrieve(f"https://chiss.fr/jvm/download/Java_{version}.zip", f"{path}/Java_{version}.zip") + print(colors.OKGREEN + f"[✓] The JDK {version} has been downloaded!") + sleep(1) + print(colors.BOLD + "[~] Unzipping...") + with zipfile.ZipFile(f"{path}/Java_{version}.zip", 'r') as zip_ref: + zip_ref.extractall(f"{path}/Java_{version}") + print(colors.OKGREEN + f"[✓] The JDK {version} has been unzipped!") + sleep(1) + print(colors.BOLD + "[~] Deleting zip file...") + os.remove(f"{path}/Java_{version}.zip") + print(colors.OKGREEN + f"[✓] The JDK {version} has been deleted!") + sleep(1) + print(colors.OKGREEN + f"[✓] The JDK {version} has been downloaded and installed!") + sleep(1) + else: + print(colors.FAIL + "[X] The link does not exists!") + sleep(1) + + +# Message JVM = JavaVersionManager + +print(colors.HEADER + """ +____________ ________ ___ +______ /__ | / /___ |/ / +___ _ / __ | / / __ /|_/ / +/ /_/ / __ |/ / _ / / / +\____/ _____/ /_/ /_/ + + """) + +while loop: + # Check if the value is an integer + try: + version = int(input(colors.OKCYAN + "Choose your version (only number) : ")) + except: + # else return fail + print(colors.FAIL + "You must enter an integer!") + + # Download + downloader(version) From 1df08b3900b1636f2fc34aefe605c984ce1b83ca Mon Sep 17 00:00:00 2001 From: Jachou Date: Wed, 31 May 2023 17:24:35 +0200 Subject: [PATCH 2/5] README.md update --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 8be63cb..f77c89b 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,13 @@ Available for Windows, Linux and Mac.
Download the latest release [here](https://github.com/Jachou-yt/JavaVersionManager/releases/) +# Features branch +- [x] Download Java version +- [x] Unzip Java version +- [ ] Delete Java version +- [ ] Get Java version list +- [ ] Get Java version list from a specific version + # How to use it ## Executable Version 1. Download the latest release from [here](https://github.com/Jachou-yt/JavaVersionManager/releases/) From 8db1a53ccf2de94a4acf3da103a42645639c5856 Mon Sep 17 00:00:00 2001 From: Jachou Date: Wed, 31 May 2023 18:53:51 +0200 Subject: [PATCH 3/5] Add shield, LICENSE and README update --- .idea/discord.xml | 2 +- .idea/gradle.xml | 1 + LICENSE | 7 +++++++ README.md | 17 +++++++++++++---- 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 LICENSE diff --git a/.idea/discord.xml b/.idea/discord.xml index 30bab2a..d8e9561 100644 --- a/.idea/discord.xml +++ b/.idea/discord.xml @@ -1,7 +1,7 @@ - \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 14746e7..2a65317 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,5 +1,6 @@ +