diff --git a/.gitignore b/.gitignore index c4bb6e3..284ed59 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,7 @@ .mtj.tmp/ # Package Files # -*.jar +#*.jar *.war *.ear *.zip diff --git a/README.md b/README.md index f9ee6fa..66f8957 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,42 @@ -Bintray deploy automator -=============== -This project is a tool to publish artifacts to Bintray for gradle projects with multiple modules that use [Gradle Bintray Plugin](https://github.com/bintray/gradle-bintray-plugin). - -Usage ------ -With this automator you just have to create a configuration file like the one below, build the jar and run it. It will clean and build your project and then for each module will run `bintrayUpload` task. -At the end if no errors occurred it will run the extra tasks from configuration. - -Configuration -------------- -### Install -Run `jar` task from gradle 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`. -```js -// The json configuration +# Bintray deploy automator + +This tool to publish artifacts to Bintray for gradle projects with multiple modules that use [Gradle Bintray Plugin](https://github.com/bintray/gradle-bintray-plugin) or [Novoda Bintray Release](https://github.com/novoda/bintray-release). + +## How it works + +Bintray deploy automator clean and build your project's modules and then for each module will run `bintrayUpload` task. At the end if no errors occurred it will run the extra tasks. + +## How to run + +1. Create a configuration file named `configuration.json` like the one below. + +``` { "basePath": "./project", // The path of your project - "version": "0.1.9", // The new version of project to be uploaded + "version": "0.1.0", // The new version of project to be uploaded // The list of modules to be uploaded to the bintray "modules": [ - "module-1", "module-2", "module-3" ], + // Extra tasks "extraTasks": [ "task-1", "module-1:task-2" ] } ``` + +2. Download the latest [release](https://github.com/massivedisaster/bintray-deploy-automator/releases). +3. Execute `java -jar BintrayDeployAutomator-0.1.0.jar -u Username -k Key` + +### Generate release JAR + +Run `jar` task from gradle. + +* Linux/macOS: `./gradlew jar` +* Windows: `gradlew.bat jar` + ### License + [MIT LICENSE](LICENSE.md) diff --git a/build.gradle b/build.gradle index 5053de9..8e00f78 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ group 'com.massivedisaster.bintraydeployautomator.Automator' -version '0.0.5' +version '0.1.0' +apply from: "$project.rootDir/updateReadme.gradle" apply plugin: 'java' @@ -11,12 +12,47 @@ repositories { } dependencies { - compile "org.gradle:gradle-tooling-api:4.0" + compile "org.gradle:gradle-tooling-api:4.5" compile group: 'commons-cli', name: 'commons-cli', version: '1.4' - compile 'com.google.code.gson:gson:2.8.0' + compile 'com.google.code.gson:gson:2.8.2' + compile 'me.tongfei:progressbar:0.5.5' runtime 'org.slf4j:slf4j-simple:1.7.10' } +// Task to create an dynamic class with configuration data. +task generateConfig { + inputs.property "version", project.version + doFirst { + def versionFile = file("$buildDir/generated/source/Config.java") + versionFile.parentFile.mkdirs() + versionFile.text = + """ +package com.massivedisaster.bintraydeployautomator.utils; + +/** + * Automatically generated file. DO NOT MODIFY + */ +public class Config { + public static final String VERSION = "$project.version"; + public static final String NAME = "BintrayDeployAutomator-$project.version"; +} +""" + } +} + +project.sourceSets.matching { it.name == "main" } .all { + it.java.srcDir "$buildDir/generated/source" +} + +task updateReadme { + doLast { + processUpdateReadme file("$project.rootDir"), ".md", version.toString() + } +} + +compileJava.dependsOn generateConfig +compileJava.dependsOn updateReadme + jar { baseName 'BintrayDeployAutomator' diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..13372ae Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 833624a..d5afa11 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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-4.0-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-all.zip diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/Automator.java b/src/main/java/com/massivedisaster/bintraydeployautomator/Automator.java index 67e2379..69d1217 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/Automator.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/Automator.java @@ -1,14 +1,19 @@ package com.massivedisaster.bintraydeployautomator; +import com.massivedisaster.bintraydeployautomator.model.Arguments; import com.massivedisaster.bintraydeployautomator.model.Configuration; +import com.massivedisaster.bintraydeployautomator.output.BarProgress; +import com.massivedisaster.bintraydeployautomator.output.ConsoleProgress; +import com.massivedisaster.bintraydeployautomator.output.ProgressManager; import com.massivedisaster.bintraydeployautomator.utils.CommandLineUtils; -import com.massivedisaster.bintraydeployautomator.utils.FileUtils; +import com.massivedisaster.bintraydeployautomator.utils.Config; +import com.massivedisaster.bintraydeployautomator.utils.ConsoleUtils; import com.massivedisaster.bintraydeployautomator.utils.GradleUtils; -import javafx.util.Pair; import org.gradle.tooling.GradleConnector; import org.gradle.tooling.ProjectConnection; -import java.io.File; +import java.io.*; +import java.util.List; /** * Bintray deploy automator. @@ -20,56 +25,83 @@ public class Automator { * * @param args command line arguments. */ - public static void main(String args[]) { + public static void main(String args[]) throws IOException { ProjectConnection gradleConnection = null; + ProgressManager progressManager = null; + String outputFile = null; try { - Pair auth = CommandLineUtils.commandLineArgs(args); + Arguments auth = CommandLineUtils.commandLineArgs(args); // Get configuration from .json. Configuration configuration = Configuration.parseConfiguration("configuration.json"); - configuration.setBintrayUsername(auth.getKey()); - configuration.setBintrayKey(auth.getValue()); + List modules = configuration.getModules(); + boolean hasModules = modules != null; + int steps = hasModules ? modules.size() * 2 : 1; - gradleConnection = GradleConnector.newConnector() - .forProjectDirectory(new File(configuration.getBasePath())) + configuration.setBintrayUsername(auth.getUser()); + configuration.setBintrayKey(auth.getKey()); + configuration.setVerbose(auth.isVerbose()); + outputFile = auth.getLogFile(); + if (outputFile != null) { + File output = new File(outputFile); + if (output.exists()) { + new FileWriter(outputFile).close(); + } + progressManager = new ProgressManager(new BarProgress(), Config.NAME, steps); + } else { + progressManager = new ProgressManager(new ConsoleProgress(), Config.NAME, steps); + } + + gradleConnection = GradleConnector.newConnector().forProjectDirectory(new File(configuration.getBasePath())) .connect(); - // Clean build and deploy all projects. - rebuildAndBintrayDeploy(gradleConnection, configuration); + ConsoleUtils.DrawInConsoleBox("Start process"); + + if (hasModules) { + progressManager.start(); + //clean and build + for (String module : modules) { + progressManager.logMessage("Clean and building module " + module + "..."); + GradleUtils.runGradle(gradleConnection, outputFile, new String[]{module + ":clean", module + ":build"}, + configuration.getArguments()); + progressManager.step(); + } + + //clean and build + for (String module : modules) { + progressManager.logMessage("Uploading to bintray module " + module + "..."); + GradleUtils.runGradle(gradleConnection, outputFile, new String[]{module + ":bintrayUpload"}, configuration.getArguments()); + progressManager.step(); + } + progressManager.stop(); + } else { + GradleUtils.runGradle(gradleConnection, outputFile, configuration.getTasks(), configuration.getArguments()); + } if (configuration.canRunExtraTasks()) { - runExtraTasks(gradleConnection, configuration); + ConsoleUtils.DrawInConsoleBox("Executing extra tasks"); + GradleUtils.runGradle(gradleConnection, auth.getLogFile(), configuration.getExtraTasks(), configuration.getArguments()); } + ConsoleUtils.DrawInConsoleBox("End process"); + } catch (Exception e) { - System.out.println("Automator Error: " + e.toString()); + progressManager.stop(); + if (outputFile == null) { + progressManager.logMessage("Automator Error: " + e.toString()); + } else { + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true))); + out.append("Automator Error: "); + out.append(e.toString()); + out.append("\n"); + out.close(); + } } 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.getTasks(), configuration.getArguments()); - } - - /** - * Run extra tasks from project. - * - * @param gradleConnection the gradle connection. - * @param configuration the configuration model. - */ - private static void runExtraTasks(ProjectConnection gradleConnection, Configuration configuration) { - GradleUtils.runGradle(gradleConnection, configuration.getExtraTasks(), configuration.getArguments()); - } } diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/model/Arguments.java b/src/main/java/com/massivedisaster/bintraydeployautomator/model/Arguments.java new file mode 100644 index 0000000..1254ffd --- /dev/null +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/model/Arguments.java @@ -0,0 +1,49 @@ +package com.massivedisaster.bintraydeployautomator.model; + +public class Arguments { + + private String mUser; + private String mKey; + private boolean isVerbose; + private String mLogFile; + + public Arguments(String user, String key, boolean verbose, String logFile) { + mUser = user; + mKey = key; + isVerbose = verbose; + mLogFile = logFile; + } + + public String getUser() { + return mUser; + } + + public void setUser(String user) { + this.mUser = user; + } + + public String getKey() { + return mKey; + } + + public void setKey(String key) { + this.mKey = key; + } + + public boolean isVerbose() { + return isVerbose; + } + + public void setVerbose(boolean verbose) { + isVerbose = verbose; + } + + public String getLogFile() { + return mLogFile; + } + + public void setLogFile(String logFile) { + this.mLogFile = logFile; + } + +} diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/model/Configuration.java b/src/main/java/com/massivedisaster/bintraydeployautomator/model/Configuration.java index 1336e45..34eb347 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/model/Configuration.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/model/Configuration.java @@ -1,10 +1,9 @@ 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.ArrayList; import java.util.List; import static com.massivedisaster.bintraydeployautomator.utils.FileUtils.readFileAsString; @@ -20,6 +19,8 @@ public class Configuration { private String bintrayUsername; private String bintrayKey; private String[] bintrayTasks; + private String[] buildTasks; + private boolean isVerbose; /** * Parses the configuration file. @@ -67,19 +68,30 @@ public void setBintrayKey(String bintrayKey) { this.bintrayKey = bintrayKey; } + public void setVerbose(boolean verbose) { + isVerbose = verbose; + } + /** * 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" - }; + List args = new ArrayList<>(); + args.add(String.format("-PbintrayUser=%s", bintrayUsername)); + args.add(String.format("-PbintrayKey=%s", bintrayKey)); + args.add(String.format("-PlibraryVersionName=%s", version)); + args.add("-PdryRun=false"); + args.add("-Pskippasswordprompts"); + + if (isVerbose) { + args.add("--stacktrace"); + args.add("--info"); + args.add("--debug"); + } + + return args.toArray(new String[args.size()]); } /** @@ -88,7 +100,7 @@ public String[] getArguments() { * @return tasks to run. */ public String[] getTasks() { - return ArrayUtils.addAll(new String[]{"clean", "build"}, getBintrayTasks()); + return new String[]{"clean", "build", "bintrayUpload"}; } /** @@ -101,27 +113,7 @@ public String[] getExtraTasks() { } /** - * Get the bintray tasks from modules. - * - * @return list of bintray upload tasks. - */ - private String[] getBintrayTasks() { - if (modules == null) { - return new String[]{"bintrayUpload"}; - } - - 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. + * Tells if exists extra task to be executed. * * @return true if readme needs to be updated, else false. */ diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/output/BarProgress.java b/src/main/java/com/massivedisaster/bintraydeployautomator/output/BarProgress.java new file mode 100644 index 0000000..f0f022c --- /dev/null +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/output/BarProgress.java @@ -0,0 +1,43 @@ +package com.massivedisaster.bintraydeployautomator.output; + +import me.tongfei.progressbar.ProgressBar; + +public class BarProgress implements IProgress { + + private ProgressBar mPb = null; + + @Override + public void setup(String name, int steps) { + mPb = new ProgressBar(name, steps); + } + + private void validateSetupCalled() { + if (mPb == null) { + throw new ExceptionInInitializerError("It's necessary to call BarProgress.setup first"); + } + } + + @Override + public void logMessage(String message) { + validateSetupCalled(); + mPb.setExtraMessage(message); + } + + @Override + public void start() { + validateSetupCalled(); + mPb.start(); + } + + @Override + public void step() { + validateSetupCalled(); + mPb.step(); + } + + @Override + public void stop() { + validateSetupCalled(); + mPb.stop(); + } +} diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/output/ConsoleProgress.java b/src/main/java/com/massivedisaster/bintraydeployautomator/output/ConsoleProgress.java new file mode 100644 index 0000000..6000126 --- /dev/null +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/output/ConsoleProgress.java @@ -0,0 +1,45 @@ +package com.massivedisaster.bintraydeployautomator.output; + +import com.massivedisaster.bintraydeployautomator.utils.ConsoleUtils; + +public class ConsoleProgress implements IProgress { + + private long mCurrent = 0; + private long mMax = 0; + private boolean mSetup = false; + + @Override + public void setup(String name, int steps) { + mMax = steps; + System.out.println(name); + mSetup = true; + } + + private void validateSetupCalled() { + if (!mSetup) { + throw new ExceptionInInitializerError("It's necessary to call BarProgress.setup first"); + } + } + + @Override + public void logMessage(String message) { + validateSetupCalled(); + ConsoleUtils.DrawInConsoleBox(message); + } + + @Override + public void start() { + } + + @Override + public void step() { + validateSetupCalled(); + mCurrent++; + if (mCurrent > mMax) mMax = mCurrent; + ConsoleUtils.DrawInConsoleBox("Step: " + mCurrent + "/" + mMax); + } + + @Override + public void stop() { + } +} diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/output/IProgress.java b/src/main/java/com/massivedisaster/bintraydeployautomator/output/IProgress.java new file mode 100644 index 0000000..76afa02 --- /dev/null +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/output/IProgress.java @@ -0,0 +1,15 @@ +package com.massivedisaster.bintraydeployautomator.output; + +public interface IProgress { + + void setup(String name, int steps); + + void logMessage(String message); + + void start(); + + void step(); + + void stop(); + +} diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/output/ProgressManager.java b/src/main/java/com/massivedisaster/bintraydeployautomator/output/ProgressManager.java new file mode 100644 index 0000000..894a855 --- /dev/null +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/output/ProgressManager.java @@ -0,0 +1,27 @@ +package com.massivedisaster.bintraydeployautomator.output; + +public class ProgressManager { + + private IProgress mProgress; + + public ProgressManager(IProgress progress, String name, int steps) { + mProgress = progress; + mProgress.setup(name, steps); + } + + public void logMessage(String message) { + mProgress.logMessage(message); + } + + public void start() { + mProgress.start(); + } + + public void step() { + mProgress.step(); + } + + public void stop() { + mProgress.stop(); + } +} diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/CommandLineUtils.java b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/CommandLineUtils.java index 285e268..8981e77 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/CommandLineUtils.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/CommandLineUtils.java @@ -1,19 +1,27 @@ package com.massivedisaster.bintraydeployautomator.utils; -import javafx.util.Pair; +import com.massivedisaster.bintraydeployautomator.model.Arguments; import org.apache.commons.cli.*; public class CommandLineUtils { - public static Pair commandLineArgs(String[] args) { + public static Arguments commandLineArgs(String[] args) { Options options = new Options(); - Option input = new Option("u", "user", true, "Bintray Username"); - input.setRequired(true); - options.addOption(input); + Option user = new Option("u", "user", true, "Bintray Username (Required)"); + user.setRequired(true); + options.addOption(user); - Option output = new Option("k", "key", true, "Bintray Key"); - output.setRequired(true); + Option key = new Option("k", "key", true, "Bintray Key (Required)"); + key.setRequired(true); + options.addOption(key); + + Option verbose = new Option("v", "verbose", false, "Show more logs"); + verbose.setRequired(false); + options.addOption(verbose); + + Option output = new Option("o", "output", true, "Log output file"); + output.setRequired(false); options.addOption(output); CommandLineParser parser = new DefaultParser(); @@ -24,16 +32,18 @@ public static Pair commandLineArgs(String[] args) { cmd = parser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); - formatter.printHelp("Java -jar BintrayDeployAutomator-0.0.4.jar", options); + formatter.printHelp("Java -jar " + Config.NAME + ".jar", options); System.exit(1); return null; } - String user = cmd.getOptionValue("user"); - String key = cmd.getOptionValue("key"); + String userArg = cmd.getOptionValue("user"); + String keyArg = cmd.getOptionValue("key"); + boolean isVerbose = cmd.hasOption("verbose"); + String outputArg = cmd.getOptionValue("output"); - return new Pair<>(user, key); + return new Arguments(userArg, keyArg, isVerbose, outputArg); } } diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/ConsoleUtils.java b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/ConsoleUtils.java new file mode 100644 index 0000000..ad2ec10 --- /dev/null +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/ConsoleUtils.java @@ -0,0 +1,73 @@ +package com.massivedisaster.bintraydeployautomator.utils; + +public class ConsoleUtils { + + public static void DrawInConsoleBox(String s) { + String ulCorner = "╔"; + String llCorner = "╚"; + String urCorner = "╗"; + String lrCorner = "╝"; + String vertical = "║"; + String horizontal = "═"; + + String[] lines = s.split("[\r\n]"); + + int longest = 0; + for (String line : lines) { + if (line.length() > longest) { + longest = line.length(); + } + } + int width = longest + 2; // 1 space on each side + + // box top + StringBuilder sb = new StringBuilder(); + sb.append(ulCorner); + for (int i = 0; i < width; i++) { + sb.append(horizontal); + } + sb.append(urCorner); + sb.append("\n"); + + // box contents + for (String line : lines) { + double dblSpaces = ((width - line.length()) / (double) 2); + int iSpaces = (int) dblSpaces; + + // not an even amount of chars + if (dblSpaces > iSpaces) { + iSpaces += 1; // round up to next whole number + } + + String beginSpacing = ""; + String endSpacing = ""; + for (int i = 0; i < iSpaces; i++) { + beginSpacing += " "; + + // if there is an extra space somewhere, it should be in the beginning + if (!(iSpaces > dblSpaces && i == iSpaces - 1)) { + endSpacing += " "; + } + } + // add the text line to the box + sb.append(vertical) + .append(beginSpacing) + .append(line) + .append(endSpacing) + .append(vertical) + .append("\n"); + } + + // box bottom + sb.append(llCorner); + for (int i = 0; i < width; i++) { + sb.append(horizontal); + } + sb.append(lrCorner) + .append("\n"); + + // the finished box + System.out.println(sb); + } + +} diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/FileUtils.java b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/FileUtils.java index a822dfa..895fd61 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/FileUtils.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/FileUtils.java @@ -2,8 +2,8 @@ import com.massivedisaster.bintraydeployautomator.annotations.NotNull; +import java.io.File; import java.io.IOException; -import java.io.PrintWriter; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; @@ -23,5 +23,15 @@ public static String readFileAsString(@NotNull String path) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, Charset.defaultCharset()); } - + + + public static File createFile(String output) throws IOException { + File outFile = new File(output); + File parent = outFile.getParentFile(); + if (parent != null) { + parent.mkdirs(); + } + outFile.createNewFile(); + return outFile; + } } diff --git a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/GradleUtils.java b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/GradleUtils.java index 28d44c0..72f8f8e 100644 --- a/src/main/java/com/massivedisaster/bintraydeployautomator/utils/GradleUtils.java +++ b/src/main/java/com/massivedisaster/bintraydeployautomator/utils/GradleUtils.java @@ -4,6 +4,10 @@ import com.massivedisaster.bintraydeployautomator.annotations.NotNull; import org.gradle.tooling.ProjectConnection; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + /** * Gradle utilities. */ @@ -15,12 +19,27 @@ public class GradleUtils { * @param tasks gradle tasks to execute. * @param arguments arguments. */ - public static void runGradle(@NotNull ProjectConnection gradleConnection, @NotNull String[] tasks, @NotNull String... arguments) { + public static void runGradle(@NotNull ProjectConnection gradleConnection, @NotNull String output, @NotNull String[] tasks, + @NotNull String... arguments) throws IOException { + OutputStream os; + OutputStream osErr; + if (output != null) { + os = new FileOutputStream(FileUtils.createFile(output)); + osErr = os; + } else { + os = System.out; + osErr = System.err; + } gradleConnection.newBuild() .forTasks(tasks) .withArguments(arguments) - .setStandardOutput(System.out) - .setStandardError(System.err) + .setStandardOutput(os) + .setStandardError(osErr) .run(); + + if (output != null) { + os.close(); + osErr.close(); + } } } diff --git a/updateReadme.gradle b/updateReadme.gradle new file mode 100644 index 0000000..dbeae93 --- /dev/null +++ b/updateReadme.gradle @@ -0,0 +1,41 @@ +import java.nio.charset.Charset +import java.nio.file.Files +import java.nio.file.Paths + +/** + * 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. + */ +private static String readFileAsString(String path) throws IOException { + byte[] encoded = Files.readAllBytes(Paths.get(path)) + return new String(encoded, Charset.defaultCharset()) +} + +/** + * 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. + */ +private static void replaceAllSemVerInFile(String newVersion, String fileName) throws IOException { + String readme = readFileAsString(fileName) + String readmeVersionReplaced = readme.replaceAll("(\\d+(\\.\\d+){2})(\\-[\\w\\d\\.\\-]*)?(\\+[\\w\\d\\.\\-]*)?", newVersion) + + PrintWriter output = new PrintWriter(fileName) + output.print(readmeVersionReplaced) + output.close() +} + +ext { + processUpdateReadme = { File resDir, String extension, String libraryVersionName -> + resDir.eachFileRecurse { file -> + if (file.name.endsWith(extension)) { + replaceAllSemVerInFile(libraryVersionName, file.absolutePath) + } + } + } +} \ No newline at end of file