Skip to content

Commit

Permalink
Merge pull request #17 from massivedisaster/release/0.0.3
Browse files Browse the repository at this point in the history
Release/0.0.3
  • Loading branch information
jzeferino authored Jun 12, 2017
2 parents 02ff0e6 + 11aac7d commit b8642b9
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 35 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ At the end if no errors occurred it will update your readme specified configurat
Configuration
-------------
### Install
Run `jar` task from gradle and create configuration file.

Just build and create configuration file.
### How to run
`java -jar BintrayDeployAutomator.jar -u Username -k Key`

### Configuration file (configuration.json)
The configuration file must be named `configuration.json`.
Expand All @@ -26,9 +28,7 @@ The configuration file must be named `configuration.json`.
"module-1",
"module-2",
"module-3"
],
"bintrayUsername": "{username}", // The bintray username
"bintrayKey": "{key}" // The api key of bintray
]
}
```
### License
Expand Down
18 changes: 16 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.massivedisaster.bintraydeployautomator.BintrayDeployAutomator'
version '1.0-SNAPSHOT'
group 'com.massivedisaster.bintraydeployautomator.Automator'
version '0.0.3'

apply plugin: 'java'

Expand All @@ -12,6 +12,20 @@ repositories {

dependencies {
compile "org.gradle:gradle-tooling-api:3.5"
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
compile 'com.google.code.gson:gson:2.8.0'
runtime 'org.slf4j:slf4j-simple:1.7.10'
}

jar {
baseName 'BintrayDeployAutomator'

manifest {
attributes 'Main-Class': 'com.massivedisaster.bintraydeployautomator.Automator', 'version-number': version
}

from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
3 changes: 0 additions & 3 deletions src/main/java/META-INF/MANIFEST.MF

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,45 +1,66 @@
package com.massivedisaster.bintraydeployautomator;

import com.massivedisaster.bintraydeployautomator.model.Configuration;
import com.massivedisaster.bintraydeployautomator.utils.CommandLineUtils;
import com.massivedisaster.bintraydeployautomator.utils.FileUtils;
import com.massivedisaster.bintraydeployautomator.utils.GradleUtils;
import javafx.util.Pair;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;

import java.io.File;

public class BintrayDeployAutomator extends FileUtils {
/**
* Bintray deploy automator.
*/
public class Automator extends FileUtils {

/**
* Main method.
*
* @param args command line arguments.
*/
public static void main(String args[]) {

ProjectConnection gradleConnection = null;
try {
Pair<String, String> auth = CommandLineUtils.commandLineArgs(args);

// Get configuration from .json.
Configuration configuration = Configuration.parseConfiguration("configuration.json");

configuration.setBintrayUsername(auth.getKey());
configuration.setBintrayKey(auth.getValue());

gradleConnection = GradleConnector.newConnector()
.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) {
System.out.println("BintrayDeployAutomator Error: " + e.toString());
System.out.println("Automator Error: " + e.toString());
} finally {
if (gradleConnection != null) {
gradleConnection.close();
}
}
}


/**
* Run build 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
@@ -0,0 +1,9 @@
package com.massivedisaster.bintraydeployautomator.annotations;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
public @interface NotNull {
}
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 com.massivedisaster.bintraydeployautomator.utils.ArrayUtils;
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,111 @@ 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;
}

public String getBintrayUsername() {
return bintrayUsername;
public void setBintrayUsername(String bintrayUsername) {
this.bintrayUsername = bintrayUsername;
}

public String getBintrayKey() {
return bintrayKey;
public void setBintrayKey(String bintrayKey) {
this.bintrayKey = 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() {
return new String[]{"clean", "build", "bintrayUpload"};
/**
* Gets task to run.
*
* @return tasks to run.
*/
public String[] getTasks() {
return ArrayUtils.addAll(new String[]{"clean", "build"}, getBintrayTasks());
}

public boolean UpdateReadmeVersion() {
/**
* Get the bintray tasks from modules.
*
* @return list of bintray upload tasks.
*/
private String[] getBintrayTasks() {
if (modules == null) {
throw new IllegalArgumentException("modules can't be null");
}

if (bintrayTasks == null) {
int size = modules.size();
bintrayTasks = new String[size];
for (int i = 0; i < size; i++) {
bintrayTasks[i] = modules.get(i) + ":bintrayUpload";
}
}
return bintrayTasks;
}

/**
* 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
@@ -0,0 +1,43 @@
package com.massivedisaster.bintraydeployautomator.utils;

/**
* Array Utils
*/
public class ArrayUtils {

/**
* Adds all the elements of the given arrays into a new array.
* The new array contains all of the element of array1 followed by all of the elements array2. When an array
* is returned, it is always a new array.
*
* @param array1 the first array whose elements are added to the new array.
* @param array2 the second array whose elements are added to the new array.
* @return The new String[] array.
*/
public static String[] addAll(String[] array1, String[] array2) {
if (array1 == null) {
return clone(array2);
} else if (array2 == null) {
return clone(array1);
}
String[] joinedArray = new String[array1.length + array2.length];
System.arraycopy(array1, 0, joinedArray, 0, array1.length);
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
return joinedArray;
}

/**
* Clones an array returning a typecast result and handling null.
* This method returns null for a null input array.
*
* @param array the array to clone, may be null
* @return the cloned array, null if null input
*/
private static String[] clone(String[] array) {
if (array == null) {
return null;
}
return array.clone();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.massivedisaster.bintraydeployautomator.utils;

import javafx.util.Pair;
import org.apache.commons.cli.*;

public class CommandLineUtils {

public static Pair<String, String> commandLineArgs(String[] args) {
Options options = new Options();

Option input = new Option("u", "user", true, "Bintray Username");
input.setRequired(true);
options.addOption(input);

Option output = new Option("k", "key", true, "Bintray Key");
output.setRequired(true);
options.addOption(output);

CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;

try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp("Java -jar BintrayDeployAutomator-0.0.2.jar", options);

System.exit(1);
return null;
}

String user = cmd.getOptionValue("user");
String key = cmd.getOptionValue("key");

return new Pair<>(user, key);
}

}
Loading

0 comments on commit b8642b9

Please sign in to comment.