Skip to content

Commit

Permalink
Add version properties to jar and use it for /poseidon (#122)
Browse files Browse the repository at this point in the history
* Add version properties to jar

* Improve build and test

* Remove invalid step 9

* Additional version info

* Remove unnecessary GitHub action fields

* fix typo

* Improve Poseidon version command

* Dynamically get application name for forks

* Update Poseidon name in pom

* Fix version.properties not including all fields
  • Loading branch information
RhysB authored Dec 14, 2024
1 parent 532b908 commit 18ad13f
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 68 deletions.
62 changes: 61 additions & 1 deletion .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,85 @@ jobs:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the repository
- name: Checkout
uses: actions/checkout@v4

# Step 2: Set up JDK 1.8
- name: Set up JDK 1.8
uses: actions/setup-java@v2
with:
distribution: 'temurin'
java-version: 8

# Step 3: Get the version from pom.xml
- name: Get the version from pom.xml
id: get_version
run: echo "PROJECT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_ENV

# Step 4: Generate custom release version
- name: Generate Release Version
id: release_version
run: |
DATE=$(date +'%y%m%d-%H%M')
SHA=$(echo $GITHUB_SHA | cut -c1-7)
RELEASE_VERSION="${PROJECT_VERSION}-${DATE}-${SHA}"
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV
# Step 5: Create version.properties file
- name: Create version.properties file
run: |
mkdir -p src/main/resources
# Application Information
APP_NAME=$(mvn help:evaluate -Dexpression=project.name -q -DforceStdout)
# Core Metadata
echo "version=${{ env.RELEASE_VERSION }}" >> src/main/resources/version.properties
echo "build_timestamp=$(date --utc +'%Y-%m-%dT%H:%M:%SZ')" >> src/main/resources/version.properties
echo "git_branch=${{ github.ref_name }}" >> src/main/resources/version.properties
echo "git_commit=${GITHUB_SHA}" >> src/main/resources/version.properties
echo "app_name=${APP_NAME}" >> src/main/resources/version.properties
echo "release_version=${{ env.RELEASE_VERSION }}" >> src/main/resources/version.properties
echo "maven_version=${{ env.PROJECT_VERSION }}" >> src/main/resources/version.properties
# Build Type
if [[ "${{ github.ref }}" == "refs/heads/master" || "${{ github.ref }}" == "refs/heads/main" ]]; then
BUILD_TYPE="production"
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
BUILD_TYPE="pull_request"
else
BUILD_TYPE="development"
fi
echo "build_type=${BUILD_TYPE}" >> src/main/resources/version.properties
# CI/CD Metadata
echo "workflow_name=${{ github.workflow }}" >> src/main/resources/version.properties
echo "workflow_run_id=${{ github.run_id }}" >> src/main/resources/version.properties
echo "workflow_run_number=${{ github.run_number }}" >> src/main/resources/version.properties
# Team or Author Information
echo "build_author=${{ github.actor }}" >> src/main/resources/version.properties
- name: Set up Maven
uses: stCarolas/[email protected]
with:
maven-version: 3.9.1

- name: build application
# Step 6: Build application
- name: Build Application
shell: bash
run: |
mvn clean install
# Step 7: Run Tests
- name: Run Tests
shell: bash
run: |
mvn test
# Step 8: Upload artifact
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/java21-buld-test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: build-and-test-lts
name: Java-Compatibility
on:
pull_request:
types:
Expand Down
42 changes: 39 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,52 @@ jobs:
- name: Generate Release Version
id: release_version
run: |
DATE=$(date +'%y%m%d')
DATE=$(date +'%y%m%d-%H%M')
SHA=$(echo $GITHUB_SHA | cut -c1-7)
RELEASE_VERSION="${PROJECT_VERSION}-${DATE}-${SHA}"
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV
# Step 6: Build with Maven
# Step 6: Create version.properties file
- name: Create version.properties file
run: |
mkdir -p src/main/resources
# Application Information
APP_NAME=$(mvn help:evaluate -Dexpression=project.name -q -DforceStdout)
# Core Metadata
echo "version=${{ env.RELEASE_VERSION }}" >> src/main/resources/version.properties
echo "build_timestamp=$(date --utc +'%Y-%m-%dT%H:%M:%SZ')" >> src/main/resources/version.properties
echo "git_branch=${{ github.ref_name }}" >> src/main/resources/version.properties
echo "git_commit=${GITHUB_SHA}" >> src/main/resources/version.properties
echo "app_name=${APP_NAME}" >> src/main/resources/version.properties
echo "release_version=${{ env.RELEASE_VERSION }}" >> src/main/resources/version.properties
echo "maven_version=${{ env.PROJECT_VERSION }}" >> src/main/resources/version.properties
# Build Type
if [[ "${{ github.ref }}" == "refs/heads/master" || "${{ github.ref }}" == "refs/heads/main" ]]; then
BUILD_TYPE="production"
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
BUILD_TYPE="pull_request"
else
BUILD_TYPE="development"
fi
echo "build_type=${BUILD_TYPE}" >> src/main/resources/version.properties
# CI/CD Metadata
echo "workflow_name=${{ github.workflow }}" >> src/main/resources/version.properties
echo "workflow_run_id=${{ github.run_id }}" >> src/main/resources/version.properties
echo "workflow_run_number=${{ github.run_number }}" >> src/main/resources/version.properties
# Team or Author Information
echo "build_author=${{ github.actor }}" >> src/main/resources/version.properties
# Step 7: Build with Maven
- name: Build with Maven
run: mvn clean package

# Step 7: Create GitHub Release
# Step 8: Create GitHub Release
- name: Create GitHub Release
if: ${{ !endsWith(env.PROJECT_VERSION, '-SNAPSHOT') }}
uses: softprops/action-gh-release@v1
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>project-poseidon</artifactId>
<packaging>jar</packaging>
<version>1.1.8</version>
<name>Project-Poseidon</name>
<name>Project Poseidon</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<api.version>unknown</api.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.legacyminecraft.poseidon.commands;

import com.projectposeidon.api.PoseidonUUID;
import com.projectposeidon.api.UUIDType;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;
import java.util.UUID;

public class PoseidonCommand extends Command {

private final Properties versionProperties = new Properties();

public PoseidonCommand(String name) {
super(name);
this.description = "Show data regarding the server's version of Project Poseidon";
this.usageMessage = "/poseidon";
this.setAliases(Arrays.asList("projectposeidon"));
loadVersionProperties();
}

private void loadVersionProperties() {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("version.properties")) {
if (inputStream != null) {
versionProperties.load(inputStream);
}
} catch (IOException e) {
Bukkit.getLogger().warning("Failed to load version.properties: " + e.getMessage());
}
}

@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (args.length == 0) {
String appName = versionProperties.getProperty("app_name", "Unknown");
String releaseVersion = versionProperties.getProperty("release_version", "Unknown");
String mavenVersion = versionProperties.getProperty("maven_version", "Unknown");
String buildTimestamp = versionProperties.getProperty("build_timestamp", "Unknown");
String gitCommit = versionProperties.getProperty("git_commit", "Unknown");
String buildType = versionProperties.getProperty("build_type", "Unknown");

// Shorten the git commit hash to 7 characters
if (gitCommit.length() > 7) {
gitCommit = gitCommit.substring(0, 7);
}

if ("Unknown".equals(releaseVersion)) {
sender.sendMessage(ChatColor.RED + "Warning: version.properties not found. This is a local or unconfigured build.");
} else {
sender.sendMessage(ChatColor.GRAY + "This server is running " + ChatColor.AQUA + appName + ChatColor.GRAY + ":");
sender.sendMessage(ChatColor.GRAY + " - Version: " + ChatColor.YELLOW + releaseVersion);
sender.sendMessage(ChatColor.GRAY + " - Built at: " + ChatColor.YELLOW + buildTimestamp);
sender.sendMessage(ChatColor.GRAY + " - Git SHA: " + ChatColor.YELLOW + gitCommit);

if ("production".equalsIgnoreCase(buildType)) {
sender.sendMessage(ChatColor.GREEN + "This is a release build.");
} else if ("pull_request".equalsIgnoreCase(buildType)) {
sender.sendMessage(ChatColor.BLUE + "This is a pull request build.");
} else {
sender.sendMessage(ChatColor.GRAY + "This is a development build.");
}
}
} else if (args.length == 1) {
if (args[0].equalsIgnoreCase("uuid")) {
sender.sendMessage(ChatColor.GRAY + "Please specify a user /poseidon uuid (username)");
} else {
sender.sendMessage(ChatColor.GRAY + "Unknown sub command.");
}
} else {
if (!args[0].equalsIgnoreCase("uuid")) {
sender.sendMessage(ChatColor.GRAY + "Unknown sub command.");
} else {
UUID uuid = PoseidonUUID.getPlayerUUIDFromCache(args[1], true);
if (uuid == null) {
uuid = PoseidonUUID.getPlayerUUIDFromCache(args[1], false);
}

if (uuid == null) {
sender.sendMessage(ChatColor.GRAY + "Unable to locate the UUID of the player called: " + ChatColor.WHITE + args[1] + ChatColor.GRAY + ". Please remember usernames are cap sensitive");
} else {
sender.sendMessage(ChatColor.GRAY + "Username: " + args[1]);
sender.sendMessage(ChatColor.GRAY + "UUID: " + uuid.toString());
UUIDType uuidType = PoseidonUUID.getPlayerUUIDCacheStatus(args[1]);
if (uuidType.equals(UUIDType.ONLINE)) {
sender.sendMessage(ChatColor.GRAY + "UUID Type: " + ChatColor.GREEN + "Online");
} else if (uuidType.equals(UUIDType.OFFLINE)) {
sender.sendMessage(ChatColor.GRAY + "UUID Type: " + ChatColor.RED + "Offline");
} else {
sender.sendMessage(ChatColor.GRAY + "UUID Type: " + ChatColor.DARK_RED + "UNKNOWN");
}
}
}
}

return true;

}

}
1 change: 1 addition & 0 deletions src/main/java/org/bukkit/command/SimpleCommandMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bukkit.command;

import com.legacyminecraft.poseidon.PoseidonConfig;
import com.legacyminecraft.poseidon.commands.PoseidonCommand;
import com.legacyminecraft.poseidon.commands.TPSCommand;
import org.bukkit.Server;
import org.bukkit.command.defaults.*;
Expand Down
62 changes: 0 additions & 62 deletions src/main/java/org/bukkit/command/defaults/PoseidonCommand.java

This file was deleted.

0 comments on commit 18ad13f

Please sign in to comment.