diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/BintrayDeployAutomator.java b/src/main/java/com/massivedisaster/bintraydeployautomator/BintrayDeployAutomator.java index aadb4db..0479e33 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/BintrayDeployAutomator.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/BintrayDeployAutomator.java @@ -3,13 +3,22 @@ import com.massivedisaster.bintraydeployautomator.model.Configuration; import com.massivedisaster.bintraydeployautomator.utils.FileUtils; import com.massivedisaster.bintraydeployautomator.utils.GradleUtils; + import org.gradle.tooling.GradleConnector; import org.gradle.tooling.ProjectConnection; import java.io.File; +/** + * Bintray deploy automator. + */ public class BintrayDeployAutomator extends FileUtils { + /** + * Main method. + * + * @param args command line arguments. + */ public static void main(String args[]) { ProjectConnection gradleConnection = null; @@ -22,12 +31,12 @@ public static void main(String args[]) { .forProjectDirectory(new File(configuration.getBasePath())) .connect(); - // Clean and build all projects. + // Clean build and deploy all projects. rebuildAndBintrayDeploy(gradleConnection, configuration); - // Replace version if needed. - if (configuration.UpdateReadmeVersion()) { - FileUtils.replaceSemVerInFile(configuration.getVersion(), configuration.getReadmePath()); + // Replace version in readme if needed. + if (configuration.canUpdateReadmeWithVersion()) { + FileUtils.replaceAllSemVerInFile(configuration.getVersion(), configuration.getReadmePath()); } } catch (Exception e) { @@ -39,7 +48,13 @@ public static void main(String args[]) { } } + /** + * Rubuild and upload to bintray. + * + * @param gradleConnection the gradle connection. + * @param configuration the configuration model. + */ private static void rebuildAndBintrayDeploy(ProjectConnection gradleConnection, Configuration configuration) { - GradleUtils.runGradle(gradleConnection, configuration.getRebuildAndBintrayDeployTasks(), configuration.getRebuildAndBintrayDeployArguments()); + GradleUtils.runGradle(gradleConnection, configuration.getTasks(), configuration.getArguments()); } } diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/model/Configuration.java b/src/main/java/com/massivedisaster/bintraydeployautomator/model/Configuration.java index ab8f935..e0f9f82 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/model/Configuration.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/model/Configuration.java @@ -1,13 +1,17 @@ package com.massivedisaster.bintraydeployautomator.model; import com.google.gson.Gson; + import org.gradle.internal.impldep.org.apache.commons.lang.StringUtils; import java.io.IOException; import java.util.List; -import static com.massivedisaster.bintraydeployautomator.utils.FileUtils.readFile; +import static com.massivedisaster.bintraydeployautomator.utils.FileUtils.readFileAsString; +/** + * Model representation of configuration. + */ public class Configuration { private String basePath = "./"; private String readmePath; @@ -17,49 +21,101 @@ public class Configuration { private String bintrayKey; private String[] bintrayTasks; + /** + * Parses the configuration file. + * + * @param path path to the configuration file. + * @return returns an instance of {@link Configuration}. + * @throws IOException if the file don't exist. + */ public static Configuration parseConfiguration(String path) throws IOException { - return new Gson().fromJson(readFile(path), Configuration.class); + return new Gson().fromJson(readFileAsString(path), Configuration.class); } + /** + * Gets the base path of execution. + * + * @return the base path of execution. + */ public String getBasePath() { return basePath; } + /** + * Gets de readme path. + * + * @return the readme path. + */ public String getReadmePath() { return readmePath; } + /** + * Gets the version. + * + * @return the version. + */ public String getVersion() { return version; } + /** + * Gets the modules. + * + * @return the modules. + */ public List getModules() { return modules; } + /** + * Gets bintray username. + * + * @return bintray username. + */ public String getBintrayUsername() { return bintrayUsername; } + /** + * Gets bintray key. + * + * @return bintray key. + */ public String getBintrayKey() { return bintrayKey; } - public String[] getRebuildAndBintrayDeployArguments() { - return new String[] { + /** + * Gets the arguments. + * + * @return list of arguments. + */ + public String[] getArguments() { + return new String[]{ String.format("-PbintrayUser=%s", bintrayUsername), String.format("-PbintrayKey=%s", bintrayKey), String.format("-PlibraryVersionName=%s", version), "-PdryRun=false", - "-Pskippasswordprompts" + "-Pskippasswordprompts" }; } - public String[] getRebuildAndBintrayDeployTasks() { + /** + * Gets task to run. + * + * @return tasks to run. + */ + public String[] getTasks() { return new String[]{"clean", "build", "bintrayUpload"}; } - public boolean UpdateReadmeVersion() { + /** + * Tells if readme needs to be updated with version. + * + * @return true if readme needs to be updated, else false. + */ + public boolean canUpdateReadmeWithVersion() { return !StringUtils.isEmpty(readmePath); } } diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/FileUtils.java b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/FileUtils.java index b48c213..74131b1 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/FileUtils.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/FileUtils.java @@ -8,17 +8,34 @@ import java.nio.file.Files; import java.nio.file.Paths; +/** + * File utilities. + */ public class FileUtils { - public static String readFile(@NotNull String path) throws IOException { + /** + * Reads a file as string. + * + * @param path path to the file. + * @return the string with the file contents. + * @throws IOException if file don't exist. + */ + public static String readFileAsString(@NotNull String path) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, Charset.defaultCharset()); } - public static void replaceSemVerInFile(@NotNull String newVersion, @NotNull String fileName) throws IOException { - String readme = readFile(fileName); + /** + * Replace every semantic version occurrence in specified file. + * + * @param newVersion version to replace. + * @param fileName the file to search. + * @throws IOException if file don't exist. + */ + public static void replaceAllSemVerInFile(@NotNull String newVersion, @NotNull String fileName) throws IOException { + String readme = readFileAsString(fileName); String readmeVersionReplaced = readme.replaceAll("(\\d+(\\.\\d+){2})(\\-[\\w\\d\\.\\-]*)?(\\+[\\w\\d\\.\\-]*)?", newVersion); - try(PrintWriter output = new PrintWriter(fileName)){ + try (PrintWriter output = new PrintWriter(fileName)) { output.print(readmeVersionReplaced); } } diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/GradleUtils.java b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/GradleUtils.java index 66e6e15..fccbda1 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/GradleUtils.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/GradleUtils.java @@ -1,9 +1,20 @@ package com.massivedisaster.bintraydeployautomator.utils; import com.sun.istack.internal.NotNull; + import org.gradle.tooling.ProjectConnection; +/** + * Gradle utilities. + */ public class GradleUtils { + /** + * Runs gradle with specified parameters. + * + * @param gradleConnection the gradle connection. + * @param tasks gradle tasks to execute. + * @param arguments arguments. + */ public static void runGradle(@NotNull ProjectConnection gradleConnection, @NotNull String[] tasks, @NotNull String... arguments) { gradleConnection.newBuild() .forTasks(tasks)