Skip to content

Commit

Permalink
Add macOS and Windows builds to GHA CI build matrix
Browse files Browse the repository at this point in the history
Signed-off-by: Wouter Born <[email protected]>
  • Loading branch information
wborn committed Aug 27, 2023
1 parent 9cc7b58 commit 87861b2
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 55 deletions.
196 changes: 196 additions & 0 deletions .github/java/MavenBuild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MavenBuild {

static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
static final String BUILD_LOG = "build.log";

static class Command {
final String command;

Command(String command) {
this.command = command;
}

String getCommandForCurrentOS() {
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
return "cmd.exe /c " + command;
} else {
return command;
}
}

@Override
public String toString() {
return command;
}
}

static class MaxCapacityList<E> extends ArrayList<E> {

final int maxCapacity;

public MaxCapacityList(int maxCapacity) {
super(maxCapacity);
this.maxCapacity = maxCapacity;
}

@Override
public boolean add(E e) {
if (size() > maxCapacity) {
remove(0);
}
return super.add(e);
}
}

static class CommandProcess {

final ProcessBuilder builder = new ProcessBuilder();
final Command command;

CommandProcess(Command command) {
this.command = command;
builder.command(command.getCommandForCurrentOS().split(" "));
builder.directory(new File(System.getProperty("user.dir")));
builder.environment().putAll(System.getenv());
builder.redirectErrorStream(true);
}

void execute() throws Exception {
Process process = builder.start();
handleInputStream(process.getInputStream()).get();
int exitCode = process.waitFor();

if (exitCode != 0) {
handleError();
System.exit(exitCode);
}

handleSuccess();
}

Future<?> handleInputStream(InputStream inputStream) {
return CompletableFuture.completedFuture(null);
}

void handleError() {
}

void handleSuccess() {
}
}

static class MavenVersionCommandProcess extends CommandProcess {
MavenVersionCommandProcess() {
super(new Command("mvn -v"));
builder.inheritIO();
}
}

static class MavenBuildCommandProcess extends CommandProcess {

final MaxCapacityList<String> lastLines = new MaxCapacityList<>(2000);
final Pattern pattern = Pattern.compile("^\\[INFO\\] Building (?<name>.+) \\[(?<index>\\d+)/(?<size>\\d+)\\]$");

MavenBuildCommandProcess() {
super(new Command("mvn clean verify -B -T 1.5C -U"));
}

@Override
void execute() throws Exception {
List.of("Building all projects", "", "+ " + command, "").stream().forEach(System.out::println);
super.execute();
}

void printProgress(String line) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
String index = matcher.group("index");
String size = matcher.group("size");
String name = matcher.group("name");
String padding = " ".repeat(size.length() - index.length());
System.out.println(String.format("%s%s/%s| %s", padding, index, size, name));
}
}

@Override
Future<?> handleInputStream(InputStream inputStream) {
return EXECUTOR.submit(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter writer = Files.newBufferedWriter(Path.of(BUILD_LOG), StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING)) {
String line;
while ((line = reader.readLine()) != null) {
printProgress(line);
writer.append(line + System.lineSeparator());
lastLines.add(line);
}
} catch (IOException e) {
}
});
}

@Override
void handleError() {
System.out.println();

int start = 0;
for (int i = 0; i < lastLines.size(); i++) {
if (lastLines.get(i).startsWith("[ERROR] ")) {
start = i;
break;
}
}

lastLines.subList(start, lastLines.size()).stream().forEach(System.out::println);
}

@Override
void handleSuccess() {
String separatorPrefix = "[INFO] " + "-".repeat(70);
String summaryPrefix = "[INFO] Reactor Summary";

int start = 0;
int end = lastLines.size();

for (int i = 0; i < lastLines.size(); i++) {
String line = lastLines.get(i);
if (line.startsWith(summaryPrefix)) {
start = i - 1;
} else if (line.startsWith(separatorPrefix)) {
end = i + 1;
}
}

System.out.println();
lastLines.subList(start, end).stream() //
.map(s -> s.replaceFirst("\\[INFO\\] ", "")) //
.forEach(System.out::println);
}

}

public static void main(String[] args) throws Exception {
new MavenVersionCommandProcess().execute();
System.out.println();
new MavenBuildCommandProcess().execute();
System.exit(0);
}
}
51 changes: 0 additions & 51 deletions .github/scripts/maven-build

This file was deleted.

17 changes: 13 additions & 4 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- 'main'
- 'gha-macos-windows'
paths-ignore:
- '.github/**/*.md'
pull_request:
Expand All @@ -20,10 +21,16 @@ jobs:
matrix:
java: [ '17' ]
maven: [ '3.8.6' ]
os: [ 'ubuntu-22.04' ]
os: [ 'macos-12', 'ubuntu-22.04', 'windows-2022' ]
name: Build (Java ${{ matrix.java }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
steps:
- name: Set up line endings
if: ${{ matrix.os == 'windows-2022' }}
run: |
git config --global core.autocrlf false
git config --global core.eol crlf
- name: Checkout
if: github.head_ref == ''
uses: actions/checkout@v3
Expand Down Expand Up @@ -56,14 +63,16 @@ jobs:
maven-version: ${{ matrix.maven }}

- name: Register Problem Matchers
if: ${{ matrix.java == '17' }}
if: ${{ matrix.java == '17' && matrix.os == 'ubuntu-22.04' }}
id: problem_matchers
run: |
echo "::add-matcher::.github/openhab-compile-problems.json"
- name: Build
id: build
run: './.github/scripts/maven-build'
run: |
javac .github/java/MavenBuild.java
java -cp .github/java MavenBuild
env:
MAVEN_OPTS: >-
-Xmx2g
Expand All @@ -86,7 +95,7 @@ jobs:
path: target/summary_report.html

- name: Report SAT Errors as Annotations
if: ${{ matrix.java == '17' && always() && ((steps.build.outcome == 'success') || (steps.build.outcome == 'failure')) }}
if: ${{ matrix.java == '17' && matrix.os == 'ubuntu-22.04' && always() && ((steps.build.outcome == 'success') || (steps.build.outcome == 'failure')) }}
uses: ghys/checkstyle-github-action@main
with:
title: CheckStyle Violations
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.class
*.log
target/
src-gen/
xtend-gen/
Expand Down

0 comments on commit 87861b2

Please sign in to comment.