Skip to content

Commit

Permalink
The version is now calculated internally.
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Apr 15, 2019
1 parent 909b03e commit 6455662
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 198 deletions.
53 changes: 0 additions & 53 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 18 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class A {

### Default Template

The [default template](https://github.com/ethauvin/semver/blob/master/src/main/resources/semver.mustache ) implements the following static fields:
The [default template](https://github.com/ethauvin/semver/blob/master/src/main/resources/semver.mustache ) implements the following static variables:

Field | Description | Example
:------------------|:---------------------------------|:-----------------
Expand All @@ -71,13 +71,6 @@ Field | Description | Example
`BUILDMETA_PREFIX` | The metadata prefix. | `+`
`SEPARATOR` | The version separator. | `.`

And the following methods/functions:

Method | Description | Example
:------------------------|:----------------------------------------------------------|:--------
`preReleaseWithPrefix()` | Returns the pre-release with a prefix, `-` by default. | `-alpha`
`buildMetaWithPrefix()` | Returns the build metadata with a prefix, `+` by default. | `+001`

### Custom Template

A very simple custom template might look something like:
Expand All @@ -91,29 +84,27 @@ import java.util.Date;
public final class {{className}} {
public final static String PROJECT = "{{project}}";
public final static Date DATE = new Date({{epoch}}L);
public final static int MAJOR = {{major}};
public final static int MINOR = {{minor}};
public final static int PATCH = {{patch}};
public final static String PRERELEASE = "{{preRelease}}";
public final static String BUILDMETA = "{{buildMeta}}";
public final static String VERSION = "{{semver}}";
}
```

The mustache variables automatically filled in by the processor are:

Variable | Description | Type
:----------------------|:----------------------------|:--------
`{{packageName}}` | The package name. | `String`
`{{className}}` | The class name. | `String`
`{{project}}` | The project name. | `String`
`{{epoch}}` | The build epoch/unix time. | `long`
`{{major}}` | The major version. | `int`
`{{minor}}` | The minor version. | `int`
`{{patch}}` | The patch version. | `int`
`{{preRelease}}` | The pre-release version. | `String`
`{{preReleasePrefix}}` | The pre-release prefix. | `String`
`{{buildMeta}}` | The build metadata version. | `String`
`{{buildMetaPrefix}}` | The metadata prefix. | `String`
`{{separator}}` | The version separator. | `String`
Variable | Description | Type
:-----------------------------|:----------------------------|:--------
`{{packageName}}` | The package name. | `String`
`{{className}}` | The class name. | `String`
`{{project}}` | The project name. | `String`
`{{epoch}}` | The build epoch/unix time. | `long`
`{{major}}` | The major version. | `int`
`{{minor}}` | The minor version. | `int`
`{{patch}}` | The patch version. | `int`
`{{preRelease}}` | The pre-release version. | `String`
`{{preReleasePrefix}}` | The pre-release prefix. | `String`
`{{buildMeta}}` | The build metadata version. | `String`
`{{buildMetaPrefix}}` | The metadata prefix. | `String`
`{{separator}}` | The version separator. | `String`
`{{semver}}` or `{{version}}` | The full semantic version. | `String`

Please also look at this [example](https://github.com/ethauvin/mobibot/blob/master/version.mustache) using [`java.time`](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html)

Expand Down
10 changes: 1 addition & 9 deletions examples/java/example.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ public final class {{className}} {
public final static int PATCH = {{patch}};
public final static String PRERELEASE = "{{preRelease}}";
public final static String BUILDMETA = "{{buildMeta}}";

/**
* The full semantic version string.
*/
public final static String VERSION = Integer.toString(MAJOR) + '.'
+ Integer.toString(MINOR) + '.'
+ Integer.toString(PATCH)
+ ((!PRERELEASE.isEmpty()) ? "-" + PRERELEASE : "")
+ ((!BUILDMETA.isEmpty()) ? "+" + BUILDMETA : "");
public final static String VERSION = "{{version}}";

/**
* Disables the default constructor.
Expand Down
7 changes: 1 addition & 6 deletions examples/kotlin/example.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ object {{className}} {
val PRERELEASE = "{{preRelease}}"
@JvmField
val BUILDMETA = "{{buildMeta}}"

/**
* The full semantic version string.
*/
@JvmField
val VERSION = ("$MAJOR.$MINOR.$PATCH" + if (PRERELEASE.isNotEmpty()) "-$PRERELEASE" else ""
+ if (BUILDMETA.isNotEmpty()) "+$BUILDMETA" else "")
val VERSION = "{{version}}"
}
20 changes: 14 additions & 6 deletions src/main/java/net/thauvin/erik/semver/VersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public String getBuildMetaPrefix() {
*
* @param buildMetaPrefix The meta-data prefix.
*/
public void setBuildMetaPrefix(String buildMetaPrefix) {
public void setBuildMetaPrefix(final String buildMetaPrefix) {
this.buildMetaPrefix = buildMetaPrefix;
}

Expand All @@ -140,7 +140,7 @@ public String getClassName() {
*
* @param className The new class name.
*/
public void setClassName(String className) {
public void setClassName(final String className) {
this.className = className;
}

Expand Down Expand Up @@ -203,7 +203,7 @@ public String getPackageName() {
*
* @param packageName The new package name.
*/
public void setPackageName(String packageName) {
public void setPackageName(final String packageName) {
this.packageName = packageName;
}

Expand Down Expand Up @@ -257,7 +257,7 @@ public String getPreReleasePrefix() {
*
* @param preReleasePrefix The new pre-release prefix.
*/
public void setPreReleasePrefix(String preReleasePrefix) {
public void setPreReleasePrefix(final String preReleasePrefix) {
this.preReleasePrefix = preReleasePrefix;
}

Expand All @@ -279,6 +279,14 @@ public void setProject(final String project) {
this.project = project;
}


/**
* Sames as {@link #getVersion()}
*/
public String getSemver() {
return getVersion();
}

/**
* Returns the version separator.
*
Expand All @@ -289,11 +297,11 @@ public String getSeparator() {
}

/**
* Setsthe version separtor.
* Sets the version separator.
*
* @param separator The new version separator.
*/
public void setSeparator(String separator) {
public void setSeparator(final String separator) {
this.separator = separator;
}

Expand Down
17 changes: 9 additions & 8 deletions src/main/java/net/thauvin/erik/semver/package.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Semantic Version Annotation Processor</TITLE>
</HEAD>
<BODY>
An annotation processor that automatically generates a <code>GeneratedVersion</code> class containing the semantic version (major, minor, patch, etc.) that is read from a Properties file or defined in the annotation.
<html lang="en">
<head>
<title>Semantic Version Annotation Processor</title>
</head>
<body>
An annotation processor that automatically generates a <code>GeneratedVersion</code> class containing the semantic
version (major, minor, patch, etc.) that is read from a Properties file or defined in the annotation.

@since 1.0
</BODY>
</HTML>
</body>
</html>
38 changes: 1 addition & 37 deletions src/main/resources/semver-kt.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,6 @@ object {{className}} {
val BUILDMEATA_PREFIX = "{{buildMetaPrefix}}"
@JvmField
val SEPARATOR = "{{separator}}"

/**
* The full semantic version string.
*/
@JvmField
val VERSION = "$MAJOR$SEPARATOR$MINOR$SEPARATOR$PATCH" + preReleaseWithPrefix() + buildMetaWithPrefix()

/**
* Returns the pre-release version with prefix.
*
* @param prefix The prefix, defaults to [PRERELEASE_PREFIX].
* @return The pre-release version, if any.
*/
@JvmStatic
@JvmOverloads
fun preReleaseWithPrefix(prefix: String = PRERELEASE_PREFIX): String {
return if (PRERELEASE.isNotEmpty()) {
"$prefix$PRERELEASE"
} else {
PRERELEASE
}
}

/**
* Returns the build metadata with prefix.
*
* @param prefix The prefix, defaults to [BUILDMEATA_PREFIX].
* @return The build metadata, if any.
*/
@JvmStatic
@JvmOverloads
fun buildMetaWithPrefix(prefix: String = BUILDMEATA_PREFIX): String {
return if (BUILDMETA.isNotEmpty()) {
"$prefix$BUILDMETA"
} else {
BUILDMETA
}
}
val VERSION = "{{version}}"
}
53 changes: 1 addition & 52 deletions src/main/resources/semver.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,63 +22,12 @@ public final class {{className}} {
public final static String BUILDMETA = "{{buildMeta}}";
public final static String BUILDMETA_PREFIX = "{{buildMetaPrefix}}";
public final static String SEPARATOR = "{{separator}}";

/**
* The full semantic version string.
*/
public final static String VERSION = Integer.toString(MAJOR) + SEPARATOR + Integer.toString(MINOR) + SEPARATOR
+ Integer.toString(PATCH) + preReleaseWithPrefix() + buildMetaWithPrefix();
public final static String VERSION = "{{version}}";

/**
* Disables the default constructor.
*/
private {{className}}() {
throw new UnsupportedOperationException("Illegal constructor call.");
}

/**
* Returns the build metadata with {@value #BUILDMETA_PREFIX} prefix.
*
* @return The build metadata, if any.
*/
public static String buildMetaWithPrefix() {
return buildMetaWithPrefix(BUILDMETA_PREFIX);
}

/**
* Returns the build metadata.
*
* @param prefix Prefix to prepend.
* @return The build metadata, if any.
*/
public static String buildMetaWithPrefix(final String prefix) {
if (BUILDMETA.length() > 0) {
return prefix + BUILDMETA;
} else {
return BUILDMETA;
}
}

/**
* Returns the pre-release version with {@value #PRERELEASE_PREFIX} prefix.
*
* @return The pre-release version, if any.
*/
public static String preReleaseWithPrefix() {
return preReleaseWithPrefix(PRERELEASE_PREFIX);
}

/**
* Returns the pre-release version.
*
* @param prefix The prefix to prepend.
* @return The pre-release version, if any.
*/
public static String preReleaseWithPrefix(final String prefix) {
if (PRERELEASE.length() > 0) {
return prefix + PRERELEASE;
} else {
return PRERELEASE;
}
}
}
8 changes: 8 additions & 0 deletions src/test/java/net/thauvin/erik/semver/VersionInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public void testGetVersion() {
versionInfo.setSeparator("-");

Assert.assertEquals(versionInfo.getVersion(), "3-2-1+001", "getVersion(3-2-1+001)");

Assert.assertEquals(versionInfo.getVersion(), versionInfo.getSemver(),
"getVersion(3-2-1+001) = getSemver(3-2-1+001) ");
}

@Test
Expand Down Expand Up @@ -122,6 +125,8 @@ public void testSetGet() {

Assert.assertEquals(versionInfo.getVersion(), "1.2.3-alpha+001", "getVersion(1.2.3-alpha+001)");

Assert.assertEquals(versionInfo.getVersion(), versionInfo.getSemver(), "getVersion() = getSemver()");

versionInfo.setBuildMetaPrefix("");

Assert.assertEquals(versionInfo.getBuildMetaPrefix(), "", "getBuildMetaPrefix( )");
Expand Down Expand Up @@ -188,6 +193,9 @@ public void testVersionInfo() {
+ version.buildMeta(),
"getVersion(version)");

Assert.assertEquals(versionInfo.getVersion(), versionInfo.getSemver(),
"getVersion(version) = getSemver(version)");

Assert.assertEquals(versionInfo.getProject(), version.project(), "getProject(project)");

Assert.assertEquals(versionInfo.getClassName(), version.className(), "getClassName(className)");
Expand Down

0 comments on commit 6455662

Please sign in to comment.