Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now also support Gradle builds #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
nbactions.xml
release.sh
release.sh
/target/
/bin/
.classpath
.project
.settings/
171 changes: 84 additions & 87 deletions src/main/java/com/airhacks/wad/boundary/WADFlow.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@

package com.airhacks.wad.boundary;

import com.airhacks.wad.control.Builder;
import com.airhacks.wad.control.Copier;
import com.airhacks.wad.control.FolderWatchService;
import com.airhacks.wad.control.TerminalColors;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import java.util.ArrayList;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.MavenInvocationException;

import com.airhacks.wad.control.Builder;
import com.airhacks.wad.control.Copier;
import com.airhacks.wad.control.FolderWatchService;
import com.airhacks.wad.control.TerminalColors;

/**
*
* @author airhacks.com
Expand All @@ -34,111 +37,105 @@ public class WADFlow {
private final Copier copier;
private final Builder builder;

public WADFlow(Path dir, Path war, List<Path> deploymentTargets) throws IOException {
this.builder = new Builder();
this.buildTimes = new ArrayList<>();
this.copier = new Copier(war, deploymentTargets);
Runnable changeListener = this::buildAndDeploy;
changeListener.run();
registerEnterListener(changeListener);
FolderWatchService.listenForChanges(dir, changeListener);
public WADFlow(Path dir, Path war, List<Path> deploymentTargets, Builder builder) {
this.builder = builder;
this.buildTimes = new ArrayList<>();
this.copier = new Copier(war, deploymentTargets);
Runnable changeListener = this::buildAndDeploy;
changeListener.run();
registerEnterListener(changeListener);
FolderWatchService.listenForChanges(dir, changeListener);
}

void registerEnterListener(Runnable listener) {
InputStream in = System.in;
Runnable task = () -> {
int c;
try {
while ((c = in.read()) != -1) {
listener.run();
}
} catch (IOException ex) {
}
};
new Thread(task).start();
InputStream in = System.in;
Runnable task = () -> {
int c;
try {
while ((c = in.read()) != -1) {
listener.run();
}
} catch (IOException ex) {
}
};
new Thread(task).start();
}

void buildAndDeploy() {
this.build();
this.deploy();
this.build();
this.deploy();
}

void build() {
long start = System.currentTimeMillis();
try {
System.out.printf("[%s%s%s]", TerminalColors.TIME.value(), currentFormattedTime(), TerminalColors.RESET.value());
InvocationResult result = this.builder.build();
if (result.getExitCode() == 0) {
System.out.printf("[%d]", successCounter.incrementAndGet());
System.out.print("\uD83D\uDC4D");
long buildTime = (System.currentTimeMillis() - start);
buildTimes.add(buildTime);
System.out.println(" built in " + buildTime + " ms");
if (buildTimes.size() % 10 == 0) {
this.printStatistics();
}
} else {
System.out.printf("[%d] ", buildErrorCounter.incrementAndGet());
System.out.println("\uD83D\uDC4E ");
}
} catch (MavenInvocationException ex) {
System.err.println(ex.getClass().getName() + " " + ex.getMessage());
}
long start = System.currentTimeMillis();
try {
System.out.printf("[%s%s%s]", TerminalColors.TIME.value(), currentFormattedTime(),
TerminalColors.RESET.value());
InvocationResult result = this.builder.build();
if (result.getExitCode() == 0) {
System.out.printf("[%d]", successCounter.incrementAndGet());
System.out.print("\uD83D\uDC4D");
long buildTime = System.currentTimeMillis() - start;
buildTimes.add(buildTime);
System.out.println(" built in " + buildTime + " ms");
if (buildTimes.size() % 10 == 0) {
this.printStatistics();
}
} else {
System.out.printf("[%d] ", buildErrorCounter.incrementAndGet());
System.out.println("\uD83D\uDC4E ");
}
} catch (MavenInvocationException ex) {
System.err.println(ex.getClass().getName() + " " + ex.getMessage());
}
}

void deploy() {
long start = System.currentTimeMillis();
this.copier.copy();
System.out.print("\uD83D\uDE80 ");
System.out.println(" copied in " + (System.currentTimeMillis() - start) + " ms");
long start = System.currentTimeMillis();
this.copier.copy();
System.out.print("\uD83D\uDE80 ");
System.out.println(" copied in " + (System.currentTimeMillis() - start) + " ms");

}

static String currentFormattedTime() {
DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder()
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.toFormatter();

return LocalTime.now().format(timeFormatter);
DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder().appendValue(HOUR_OF_DAY, 2).appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2).optionalStart().appendLiteral(':').appendValue(SECOND_OF_MINUTE, 2)
.toFormatter();

return LocalTime.now().format(timeFormatter);
}

public LongSummaryStatistics buildTimeStatistics() {
return this.buildTimes.
stream().
mapToLong(t -> t).
summaryStatistics();
return this.buildTimes.stream().mapToLong(t -> t).summaryStatistics();
}

String statisticsSummary() {
LongSummaryStatistics warSizeStatistics = this.copier.warSizeStatistics();
long maxKb = warSizeStatistics.getMax();
long minKb = warSizeStatistics.getMin();
long totalKb = warSizeStatistics.getSum();
String warStats = String.format("WAR sizes: min %d kB, max %d kB, total %d kB\n", minKb, maxKb, totalKb);

LongSummaryStatistics buildTimeStatistics = this.buildTimeStatistics();
long maxTime = buildTimeStatistics.getMax();
long minTime = buildTimeStatistics.getMin();
long totalTime = buildTimeStatistics.getSum();
String buildTimeStats = String.format("Build times: min %d ms, max %d ms, total %d ms\n", minTime, maxTime, totalTime);

String failureStats;
long failedBuilds = buildErrorCounter.get();
if (failedBuilds == 0) {
failureStats = "Great! Every build was a success!";
} else {
failureStats = String.format("%d builds failed", buildErrorCounter.get());
}
return warStats + buildTimeStats + failureStats;
LongSummaryStatistics warSizeStatistics = this.copier.warSizeStatistics();
long maxKb = warSizeStatistics.getMax();
long minKb = warSizeStatistics.getMin();
long totalKb = warSizeStatistics.getSum();
String warStats = String.format("WAR sizes: min %d kB, max %d kB, total %d kB\n", minKb, maxKb, totalKb);

LongSummaryStatistics buildTimeStatistics = this.buildTimeStatistics();
long maxTime = buildTimeStatistics.getMax();
long minTime = buildTimeStatistics.getMin();
long totalTime = buildTimeStatistics.getSum();
String buildTimeStats = String.format("Build times: min %d ms, max %d ms, total %d ms\n", minTime, maxTime,
totalTime);

String failureStats;
long failedBuilds = buildErrorCounter.get();
if (failedBuilds == 0) {
failureStats = "Great! Every build was a success!";
} else {
failureStats = String.format("%d builds failed", buildErrorCounter.get());
}
return warStats + buildTimeStats + failureStats;
}

void printStatistics() {
System.out.println(statisticsSummary());
System.out.println(statisticsSummary());
}

}
42 changes: 6 additions & 36 deletions src/main/java/com/airhacks/wad/control/Builder.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@

package com.airhacks.wad.control;

import com.airhacks.wad.control.SilentLogger;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import java.nio.file.Path;

import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.MavenInvocationException;

/**
*
* @author airhacks.com
*/
public class Builder {

private final DefaultInvoker invoker;
private final DefaultInvocationRequest request;
public interface Builder {

public Builder() {
this.invoker = new DefaultInvoker();
this.invoker.setLogger(new SilentLogger());
this.invoker.setOutputHandler((line) -> {
});
List<String> goals = Arrays.asList("clean", "package");
Properties properties = new Properties();
properties.put("maven.test.skip", String.valueOf(true));
this.request = new DefaultInvocationRequest();
this.request.setPomFile(new File("./pom.xml"));
this.request.setGoals(goals);
this.request.setBatchMode(true);
this.request.setProperties(properties);
this.request.setThreads(System.getProperty("threads", "1"));
this.request.setShowErrors(true);
}
InvocationResult build() throws MavenInvocationException;

public InvocationResult build() throws MavenInvocationException {
return this.invoker.execute(request);
}
Path getThinWarPath();

}
}
47 changes: 22 additions & 25 deletions src/main/java/com/airhacks/wad/control/Copier.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,44 @@
*/
public class Copier {

private List<Long> warSizes;
private final List<Long> warSizes;
private final List<Path> deploymentTargets;
private final Path from;

public Copier(Path from, List<Path> deploymentTargets) {
this.warSizes = new ArrayList<>();
this.from = from;
this.deploymentTargets = deploymentTargets;
this.warSizes = new ArrayList<>();
this.from = from;
this.deploymentTargets = deploymentTargets;
}



String shortenForDisplay(Path path, int maxLength) {
String message = path.toString();
int length = message.length();
if (length > maxLength) {
return "(...)" + message.substring(length - maxLength);
} else {
return message;
}
String message = path.toString();
int length = message.length();
if (length > maxLength)
return "(...)" + message.substring(length - maxLength);
return message;
}

public void copy() {
deploymentTargets.forEach(target -> copySingle(this.from, target));
deploymentTargets.forEach(target -> copySingle(this.from, target));
}

Path copySingle(Path from, Path to) {
long kb;
try {
kb = Files.size(from) / 1024;
warSizes.add(kb);
System.out.printf("Copying %dkB ThinWAR to %s %s %s \n", kb, TerminalColors.FILE.value(), shortenForDisplay(to, 40), TerminalColors.RESET.value());
return Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);

} catch (IOException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
long kb;
try {
kb = Files.size(from) / 1024;
warSizes.add(kb);
System.out.printf("Copying %dkB ThinWAR to %s %s %s \n", kb, TerminalColors.FILE.value(),
shortenForDisplay(to, 40), TerminalColors.RESET.value());
return Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);

} catch (IOException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}

public LongSummaryStatistics warSizeStatistics() {
return this.warSizes.stream().mapToLong(s -> s).summaryStatistics();
return this.warSizes.stream().mapToLong(s -> s).summaryStatistics();
}

}
Loading