diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a3487780..079884ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,8 @@ jobs: container: image: jcxldn/openjdk-alpine:${{matrix.version}}-jdk steps: + - name: Install git + run: apk add --no-cache git - uses: actions/checkout@v2 - name: Grant execute permission for gradlew run: chmod +x gradlew @@ -32,6 +34,8 @@ jobs: container: image: jcxldn/openjdk-alpine:11-jdk steps: + - name: Install git + run: apk add --no-cache git - uses: actions/checkout@v2 - name: Grant execute permission for gradlew run: chmod +x gradlew @@ -42,8 +46,27 @@ jobs: container: image: jcxldn/openjdk-alpine:11-jdk steps: + - name: Install git + run: apk add --no-cache git - uses: actions/checkout@v2 - name: Grant execute permission for gradlew run: chmod +x gradlew - name: Build javadocs run: ./gradlew javadoc + - name: Save javadocs + uses: actions/upload-artifact@v2 + with: + name: javadocs + path: build/docs/ # upload entire directory + - name: Save failed javadocs build output + if: ${{ failure() }} + uses: actions/upload-artifact@v2 + with: + name: failed-javadocs-build-output + path: build/ # upload entire directory + - name: Save failed javadocs build options + if: ${{ failure() }} + uses: actions/upload-artifact@v2 + with: + name: failed-javadocs-options-file + path: build/tmp/javadoc/javadoc.options diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index a5724fbe..4f3a2d4a 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,6 +11,8 @@ jobs: container: image: jcxldn/openjdk-alpine:11-jdk steps: + - name: Install git + run: apk add --no-cache git - uses: actions/checkout@v2 - name: Echo branch name run: echo ${GITHUB_REF##*/} diff --git a/build.gradle b/build.gradle index 2901d0f7..56037b0a 100644 --- a/build.gradle +++ b/build.gradle @@ -4,6 +4,8 @@ plugins { id 'jacoco' id "io.freefair.lombok" version "5.3.0" id "com.github.hierynomus.license" version "0.15.0" + // Nemerosa Versioning Plugin for the build info + id "net.nemerosa.versioning" version "2.8.2" } jacoco { @@ -95,6 +97,38 @@ compileTestJava.options.encoding = "UTF-8" javadoc.options.encoding = "UTF-8" +// Build Info +// Set the timestamp format +def dataBuildTimestamp = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" + +// Import the filter +import org.apache.tools.ant.filters.ReplaceTokens + +// Define the map containing the tokens we want to replace +def tokensMap = [ + BUILDINFO_VERSION: project.version, + BUILDINFO_DATEFORMAT: dataBuildTimestamp, + BUILDINFO_TIMESTAMP: new java.text.SimpleDateFormat(dataBuildTimestamp).format(new Date()), + BUILDINFO_COMMIT: versioning.info.commit, + BUILDINFO_BRANCH: versioning.info.branch, + BUILDINFO_ISDIRTY: versioning.info.dirty.toString() + ] + +// Create task to replace the tokens with their actual values +// NOTE: At the moment this replaces tokens *globally* (format eg. @BUILDINFO_COMMIT@ in source code) +task processSourceTokens(type: Sync) { + from sourceSets.main.java + into 'build/processed/src/main/java' + filter(ReplaceTokens, tokens: tokensMap) + + // Pretty print the build info + println("\n----- StickyAPI Build Info -----\n") + tokensMap.each { println "${String.format("%1\$-" + 10 + "s", it.key.replace("BUILDINFO_", "").toLowerCase())}\t${it.value}" } +} +// Use the filter task as the input for compileJava +compileJava.source = processSourceTokens.outputs + + tasks.publish.dependsOn build, sources diff --git a/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java b/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java index fe91da45..2f246a8b 100644 --- a/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java +++ b/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java @@ -4,6 +4,9 @@ */ package com.dumbdogdiner.stickyapi; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.logging.Logger; @@ -16,7 +19,6 @@ * code-dupe-annihilating code for DDD plugins. * * @author DumbDogDiner (dumbdogdiner.com) - * @version 2.0.0 */ public class StickyAPI { @Getter @@ -25,4 +27,83 @@ public class StickyAPI { @Getter @Setter private static ExecutorService pool = Executors.newCachedThreadPool(); + + // Build Info Start + + /** + * Get the current version of API. + * + * @since TBA + * @return {@link String} version + * + */ + @Getter + private static final String version = "@BUILDINFO_VERSION"; + + // Getter not required + private static final String dateFormat = "@BUILDINFO_DATEFORMAT@"; + + // Custom Getter (see below) + private static final String timestamp = "@BUILDINFO_TIMESTAMP@"; + + /** + * Get a string with the latest commit (id) at API's build-time. + * + * @since TBA + * @return {@link String} commit id + */ + @Getter + private static final String commit = "@BUILDINFO_COMMIT@"; + + /** + * Get a string with the current branch at API's build-time. + * + * @since TBA + * @return {@link String} branch name + */ + @Getter + private static final String branch = "@BUILDINFO_BRANCH@"; + + // Custom Getter (see below) + private static final String isDirty = "@BUILDINFO_ISDIRTY@"; + + + /** + * Get a Date object showing the current time at API's build-time. + * + * @since TBA + * @return {@link Date} date + */ + public static Date getTimestamp() { + SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); + try { + Date date = formatter.parse(timestamp); + return date; + } catch (ParseException e) { + e.printStackTrace(); + return null; + } + } + + /** + * Get a string with the latest commit sha at API's build-time. + * + * @since TBA + * @return {@link String} sha + */ + public static String getSha() { + return commit.substring(0, 7); + } + + /** + * Get a boolean showing if the working tree was dirty at API's build-time. + *

+ * If the working directory was dirty, this will return true, meaning there were modified tracked files and staged changes at build-time. + * + * @since TBA + * @return {@link Boolean} isDirty + */ + public static Boolean getIsDirty() { + return Boolean.parseBoolean(isDirty); + } } diff --git a/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java b/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java new file mode 100644 index 00000000..5778f503 --- /dev/null +++ b/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2020-2021 DumbDogDiner . All rights reserved. + * Licensed under the MIT license, see LICENSE for more information... + */ +package com.dumbdogdiner.stickyapi; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.jupiter.api.Test; + +public class StickyAPITest { + + @Test + public void testGetVersion() { + String version = StickyAPI.getVersion(); + + assertNotNull(version); + assertFalse(version.length() == 0); + } + + @Test + public void testGetTimestamp() { + assertNotNull(StickyAPI.getTimestamp()); + } + + @Test + public void testGetCommit() { + assertEquals(40, StickyAPI.getCommit().length()); + } + + @Test + public void testGetSha() { + String sha = StickyAPI.getSha(); + + assertEquals(7, sha.length()); + assertEquals(StickyAPI.getCommit().substring(0, 7), sha); + } + + @Test + public void testGetBranch() { + String branch = StickyAPI.getBranch(); + + assertNotNull(branch); + assertTrue(branch.length() != 0); + } + + @Test + // This test is kinda useless? + public void testGetIsDirty() { + assertTrue(StickyAPI.getIsDirty() instanceof Boolean); + } +}