From ae82fcf56c539d03a829449deb5841454a58ea22 Mon Sep 17 00:00:00 2001 From: aSky17 Date: Mon, 4 Mar 2024 22:13:58 +0530 Subject: [PATCH 1/2] Wrote the funcationality to generated plugins-lock-file --- .../tools/pluginmanager/cli/CliOptions.java | 10 ++++ .../tools/pluginmanager/config/Config.java | 12 ++++ .../pluginmanager/impl/PluginManager.java | 60 +++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/plugin-management-cli/src/main/java/io/jenkins/tools/pluginmanager/cli/CliOptions.java b/plugin-management-cli/src/main/java/io/jenkins/tools/pluginmanager/cli/CliOptions.java index bcfe0bd3..71008a13 100644 --- a/plugin-management-cli/src/main/java/io/jenkins/tools/pluginmanager/cli/CliOptions.java +++ b/plugin-management-cli/src/main/java/io/jenkins/tools/pluginmanager/cli/CliOptions.java @@ -18,6 +18,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -162,6 +163,7 @@ class CliOptions { */ Config setup() { return Config.builder() + .withPluginFile(getPluginFilePath()) .withPlugins(getPlugins()) .withPluginDir(getPluginDir()) .withCleanPluginsDir(isCleanPluginDir()) @@ -210,6 +212,14 @@ private File getPluginFile() { } return pluginFile; } + private Path getPluginFilePath() { + File pluginFile = getPluginFile(); + if (pluginFile != null) { + return pluginFile.toPath(); + } else { + return null; + } + } /** * Gets the user specified plugin download directory from the CLI option and sets this in the configuration class diff --git a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/config/Config.java b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/config/Config.java index fe499c9f..230b5218 100644 --- a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/config/Config.java +++ b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/config/Config.java @@ -22,6 +22,7 @@ * Defaults for update centers will be set for you */ public class Config { + private final Path pluginFilePath; private final File pluginDir; private final boolean cleanPluginDir; private final boolean showWarnings; @@ -58,6 +59,7 @@ public class Config { private final LogOutput logOutput; private Config( + Path pluginFilePath, File pluginDir, boolean cleanPluginDir, boolean showWarnings, @@ -81,6 +83,7 @@ private Config( List credentials, Path cachePath, boolean hideWarnings) { + this.pluginFilePath = pluginFilePath; this.pluginDir = pluginDir; this.cleanPluginDir = cleanPluginDir; this.showWarnings = showWarnings; @@ -107,6 +110,9 @@ private Config( this.hideWarnings = hideWarnings; } + public Path getPluginFilePath(){ + return pluginFilePath; + } public File getPluginDir() { return pluginDir; } @@ -214,6 +220,7 @@ public LogOutput getLogOutput() { } public static class Builder { + private Path pluginFilePath; private File pluginDir; private boolean cleanPluginDir; private boolean showWarnings; @@ -241,6 +248,10 @@ public static class Builder { private Builder() { } + public Builder withPluginFile(Path pluginFilePath) { + this.pluginFilePath = pluginFilePath; + return this; + } public Builder withPluginDir(File pluginDir) { this.pluginDir = pluginDir; return this; @@ -370,6 +381,7 @@ public Builder withCachePath(@NonNull Path cachePath) { public Config build() { return new Config( + pluginFilePath, pluginDir, cleanPluginDir, showWarnings, diff --git a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java index c5be5a14..602176ca 100644 --- a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java +++ b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java @@ -14,14 +14,17 @@ import io.jenkins.tools.pluginmanager.parsers.YamlPluginOutputConverter; import io.jenkins.tools.pluginmanager.util.FileDownloadResponseHandler; import io.jenkins.tools.pluginmanager.util.ManifestTools; +import java.io.BufferedWriter; import java.io.Closeable; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -90,6 +93,9 @@ public class PluginManager implements Closeable { private static final VersionNumber LATEST = new VersionNumber(Plugin.LATEST); private final List failedPlugins; + private final Path pluginFilePath; + private File pluginLockFile; + /** * Directory where the plugins will be downloaded */ @@ -126,6 +132,7 @@ public class PluginManager implements Closeable { public PluginManager(Config cfg) { this.cfg = cfg; logOutput = cfg.getLogOutput(); + pluginFilePath = cfg.getPluginFilePath(); pluginDir = cfg.getPluginDir(); jenkinsVersion = cfg.getJenkinsVersion(); final String warArg = cfg.getJenkinsWar(); @@ -198,6 +205,7 @@ public void start() { * @since TODO */ public void start(boolean downloadUc) { + createPluginLockFile(); if (cfg.isCleanPluginDir() && pluginDir.exists()) { try { logVerbose("Cleaning up the target plugin directory: " + pluginDir); @@ -234,6 +242,8 @@ public void start(boolean downloadUc) { effectivePlugins = findEffectivePlugins(pluginsToBeDownloaded); listPlugins(); + // Writing to plugin lock file + writeToPluginLockFile(allPluginsAndDependencies); showSpecificSecurityWarnings(pluginsToBeDownloaded); checkVersionCompatibility(jenkinsVersion, pluginsToBeDownloaded, exceptions); if (!exceptions.isEmpty()) { @@ -245,6 +255,31 @@ public void start(boolean downloadUc) { logMessage("Done"); } + void createPluginLockFile() { + if (pluginFilePath != null) { + Path pluginLockFilePath = pluginFilePath.resolveSibling("plugins-lock.txt"); + pluginLockFile = pluginLockFilePath.toFile(); + + try { + if (!pluginLockFile.exists()) { + boolean created = pluginLockFile.createNewFile(); + if (created) { + logVerbose("Plugin lock file created successfully: " + pluginLockFile.getAbsolutePath()); + } else { + logVerbose("Plugin lock file already exists: " + pluginLockFile.getAbsolutePath()); + } + } else { + logVerbose("Plugin lock file already exists: " + pluginLockFile.getAbsolutePath()); + } + } catch (IOException e) { + logVerbose("Error creating plugin lock file: " + e.getMessage()); + e.printStackTrace(); + } + } else { + logVerbose("Error: pluginFilePath is null. Cannot create plugin lock file."); + } + } + void createPluginDir(boolean failIfExists) { if (pluginDir.exists()) { if (failIfExists) { @@ -334,6 +369,31 @@ private void sortEffectivePlugins(Map effectivePlugins, } } + /** + * Writes the plugins and their versions to a plugin-lock.txt file. + * + * @param allPluginsAndDependencies List of plugins that will be downloaded. + */ + public void writeToPluginLockFile(Map allPluginsAndDependencies) { + if (pluginLockFile != null) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(pluginLockFile,StandardCharsets.UTF_8))) { + for (Map.Entry entry : allPluginsAndDependencies.entrySet()) { + Plugin plugin = entry.getValue(); + String pluginLine = String.format("%s:%s", plugin.getName(), plugin.getVersion()); + writer.write(pluginLine); + writer.newLine(); + } + logVerbose("Plugin lock file (" + pluginLockFile + ") has been successfully created."); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + System.err.println("pluginLockFile is null"); + } + } + /** * Lists installed plugins, bundled plugins, set of all recurively determined requested plugins, which plugins will * actually be downloaded based on the requested plugins and currently installed plugins, and the effective plugin From 4c71b38ee4ab0cbf873bdd207cb7917e745bd6d6 Mon Sep 17 00:00:00 2001 From: aSky17 Date: Tue, 5 Mar 2024 20:06:38 +0530 Subject: [PATCH 2/2] Added funtionality to write into plugins-lock-yaml file --- .../tools/pluginmanager/impl/FileType.java | 7 ++ .../pluginmanager/impl/PluginManager.java | 114 +++++++++++++++--- 2 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/FileType.java diff --git a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/FileType.java b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/FileType.java new file mode 100644 index 00000000..ff20564c --- /dev/null +++ b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/FileType.java @@ -0,0 +1,7 @@ +package io.jenkins.tools.pluginmanager.impl; + +public enum FileType { + TXT, + YAML, + UNKNOWN +} diff --git a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java index 602176ca..82a32aa0 100644 --- a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java +++ b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java @@ -94,7 +94,8 @@ public class PluginManager implements Closeable { private static final VersionNumber LATEST = new VersionNumber(Plugin.LATEST); private final List failedPlugins; private final Path pluginFilePath; - private File pluginLockFile; + private File pluginLockTxtFile; + private File pluginLockYamlFile; /** * Directory where the plugins will be downloaded @@ -188,6 +189,23 @@ private HttpClient getHttpClient() { return httpClient; } + public FileType getFileType(String filePath) { + int lastIndex = filePath.lastIndexOf('.'); + if (lastIndex != -1) { + String extension = filePath.substring(lastIndex + 1); + switch (extension.toLowerCase()) { + case "txt": + return FileType.TXT; + case "yaml": + case "yml": + return FileType.YAML; + default: + return FileType.UNKNOWN; + } + } + return FileType.UNKNOWN; + } + /** * Drives the process to download plugins. Calls methods to find installed plugins, download plugins, and output * the failed plugins @@ -205,7 +223,6 @@ public void start() { * @since TODO */ public void start(boolean downloadUc) { - createPluginLockFile(); if (cfg.isCleanPluginDir() && pluginDir.exists()) { try { logVerbose("Cleaning up the target plugin directory: " + pluginDir); @@ -242,8 +259,20 @@ public void start(boolean downloadUc) { effectivePlugins = findEffectivePlugins(pluginsToBeDownloaded); listPlugins(); - // Writing to plugin lock file - writeToPluginLockFile(allPluginsAndDependencies); + //Generating and Writing to plugin lock file + FileType fileType = getFileType(String.valueOf(pluginFilePath)); + switch (fileType) { + case YAML: + createPluginLockYamlFile(); + writeToPluginLockYamlFile(allPluginsAndDependencies); + break; + case TXT: + createPluginLockTxtFile(); + writeToPluginLockTxtFile(allPluginsAndDependencies); + break; + default: + System.err.println("Unsupported output format: " + cfg.getOutputFormat()); + } showSpecificSecurityWarnings(pluginsToBeDownloaded); checkVersionCompatibility(jenkinsVersion, pluginsToBeDownloaded, exceptions); if (!exceptions.isEmpty()) { @@ -255,28 +284,52 @@ public void start(boolean downloadUc) { logMessage("Done"); } - void createPluginLockFile() { + void createPluginLockTxtFile() { if (pluginFilePath != null) { Path pluginLockFilePath = pluginFilePath.resolveSibling("plugins-lock.txt"); - pluginLockFile = pluginLockFilePath.toFile(); + pluginLockTxtFile = pluginLockFilePath.toFile(); + + try { + if (!pluginLockTxtFile.exists()) { + boolean created = pluginLockTxtFile.createNewFile(); + if (created) { + logVerbose("plugins-lock.txt file created successfully: " + pluginLockTxtFile.getAbsolutePath()); + } else { + logVerbose("plugins-lock.txt file already exists: " + pluginLockTxtFile.getAbsolutePath()); + } + } else { + logVerbose("plugins-lock.txt file already exists: " + pluginLockTxtFile.getAbsolutePath()); + } + } catch (IOException e) { + logVerbose("Error creating plugins-lock.txt file: " + e.getMessage()); + e.printStackTrace(); + } + } else { + logVerbose("Error: pluginFilePath is null. Cannot create plugins-lock.txt file."); + } + } + void createPluginLockYamlFile() { + if (pluginFilePath != null) { + Path pluginLockFilePath = pluginFilePath.resolveSibling("plugins-lock.yaml"); + pluginLockYamlFile = pluginLockFilePath.toFile(); try { - if (!pluginLockFile.exists()) { - boolean created = pluginLockFile.createNewFile(); + if (!pluginLockYamlFile.exists()) { + boolean created = pluginLockYamlFile.createNewFile(); if (created) { - logVerbose("Plugin lock file created successfully: " + pluginLockFile.getAbsolutePath()); + logVerbose("plugins-lock.yaml file created successfully: " + pluginLockYamlFile.getAbsolutePath()); } else { - logVerbose("Plugin lock file already exists: " + pluginLockFile.getAbsolutePath()); + logVerbose("plugins-lock.yaml file already exists: " + pluginLockYamlFile.getAbsolutePath()); } } else { - logVerbose("Plugin lock file already exists: " + pluginLockFile.getAbsolutePath()); + logVerbose("plugins-lock.yaml file already exists: " + pluginLockYamlFile.getAbsolutePath()); } } catch (IOException e) { - logVerbose("Error creating plugin lock file: " + e.getMessage()); + logVerbose("Error creating plugins-lock.yaml file: " + e.getMessage()); e.printStackTrace(); } } else { - logVerbose("Error: pluginFilePath is null. Cannot create plugin lock file."); + logVerbose("Error: pluginFilePath is null. Cannot create plugins-lock.yaml file"); } } @@ -374,16 +427,16 @@ private void sortEffectivePlugins(Map effectivePlugins, * * @param allPluginsAndDependencies List of plugins that will be downloaded. */ - public void writeToPluginLockFile(Map allPluginsAndDependencies) { - if (pluginLockFile != null) { - try (BufferedWriter writer = new BufferedWriter(new FileWriter(pluginLockFile,StandardCharsets.UTF_8))) { + public void writeToPluginLockTxtFile(Map allPluginsAndDependencies) { + if (pluginLockTxtFile != null) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(pluginLockTxtFile,StandardCharsets.UTF_8))) { for (Map.Entry entry : allPluginsAndDependencies.entrySet()) { Plugin plugin = entry.getValue(); String pluginLine = String.format("%s:%s", plugin.getName(), plugin.getVersion()); writer.write(pluginLine); writer.newLine(); } - logVerbose("Plugin lock file (" + pluginLockFile + ") has been successfully created."); + logVerbose("plugins-lock.txt file (" + pluginLockTxtFile + ") has been successfully created."); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { @@ -393,6 +446,33 @@ public void writeToPluginLockFile(Map allPluginsAndDependencies) System.err.println("pluginLockFile is null"); } } + public void writeToPluginLockYamlFile(Map allPluginsAndDependencies) { + if (pluginLockYamlFile != null) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(pluginLockYamlFile, StandardCharsets.UTF_8))) { + writer.write("plugins:"); + writer.newLine(); + for (Map.Entry entry : allPluginsAndDependencies.entrySet()) { + Plugin plugin = entry.getValue(); + writer.write(" - artifactId: " + plugin.getName()); + writer.newLine(); + writer.write(" source:"); + writer.newLine(); + if (plugin.getVersion() != null) { + writer.write(" version: " + plugin.getVersion()); + writer.newLine(); + } else if (plugin.getUrl() != null) { + writer.write(" url: " + plugin.getUrl()); + writer.newLine(); + } + } + logVerbose("plugins-lock.yaml file (" + pluginLockYamlFile + ") has been successfully created."); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + System.err.println("pluginLockFile is null"); + } + } /** * Lists installed plugins, bundled plugins, set of all recurively determined requested plugins, which plugins will