Skip to content

Commit

Permalink
Merge branch 'feature/documenting' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jzeferino committed Jun 10, 2017
2 parents 02ff0e6 + e5a69cf commit 76fa9ab
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 76fa9ab

Please sign in to comment.