Skip to content
Merged
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
46 changes: 46 additions & 0 deletions .github/workflows/test-all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main, develop ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Make Gradle wrapper executable
run: chmod +x gradlew

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 17

- name: Cache Gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*','**/gradle-wrapper.properties') }}
restore-keys: ${{ runner.os }}-gradle-

- name: Build & test
run: ./gradlew clean test testAll --no-daemon

- name: Upload test reports
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results
path: |
**/build/test-results/**/*.xml
**/build/reports/tests/**
45 changes: 44 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id 'com.adarshr.test-logger' version '4.0.0'
}

allprojects {
group = 'fr.traqueur.commands'
version = property('version')
Expand All @@ -6,10 +10,49 @@ allprojects {
plugin 'java-library'
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs += ['-nowarn']
}

repositories {
mavenCentral()
maven {
url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
}
}
}
}

subprojects {
if (!project.name.contains('test-plugin')) {

apply plugin: 'com.adarshr.test-logger'

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
testImplementation 'org.mockito:mockito-core:5.3.1'
}
test {
useJUnitPlatform()
jvmArgs += ['-XX:+EnableDynamicAgentLoading']
reports {
html.required.set(true)
junitXml.required.set(true)
}

testLogging {
events("passed", "skipped", "failed")
exceptionFormat "full"
}
}
}
}

tasks.register("testAll") {
group = "verification"
description = "Execute tous les tests des sous-projets qui ont une tâche 'test'"

// Déclare la dépendance vers chaque tâche 'test' de chaque sous-projet
dependsOn subprojects
.findAll { it.tasks.findByName('test') != null }
.collect { it.tasks.named('test') }
}
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ java {

def generatedResourcesDir = "$buildDir/generated-resources"

task generateCommandsProperties {
tasks.register('generateCommandsProperties') {
doLast {
def outputDir = file("$generatedResourcesDir")
outputDir.mkdirs()
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/fr/traqueur/commands/api/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ public <T> Optional<T> getAs(String argument, Class<T> typeRef) {
throw new NoGoodTypeArgumentException();
}
} catch (NoGoodTypeArgumentException e) {
logger.error("The argument " + argument + " is not the good type.");
return Optional.empty();
}

Expand Down
11 changes: 9 additions & 2 deletions core/src/main/java/fr/traqueur/commands/api/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ public String generateDefaultUsage(CommandPlatform<T,S> platform, S sender, Stri
StringBuilder usage = new StringBuilder();
usage.append("/");
Arrays.stream(label.split("\\.")).forEach(s -> usage.append(s).append(" "));
//remove the last space
if(this.args.isEmpty() && this.optionalArgs.isEmpty()) {
usage.deleteCharAt(usage.length() - 1);
}

StringBuilder firstArg = new StringBuilder();
this.getSubcommands()
Expand All @@ -511,8 +515,11 @@ public String generateDefaultUsage(CommandPlatform<T,S> platform, S sender, Stri
}

usage.append(this.getArgs().stream().map(argument -> "<" + argument.arg() + ">").collect(Collectors.joining(" ")));
usage.append(" ");
usage.append(this.getOptinalArgs().stream().map(argument -> "[" + argument.arg() + "]").collect(Collectors.joining(" ")));
if (!this.getOptinalArgs().isEmpty()) {
usage.append(" ");
usage.append(this.getOptinalArgs().stream().map(argument -> "[" + argument.arg() + "]").collect(Collectors.joining(" ")));
}

return usage.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class ArgsWithInfiniteArgumentException extends Exception {
* @param optional if the argument is optional
*/
public ArgsWithInfiniteArgumentException(boolean optional) {
super((optional ? "Optional arguments" : "Arguments") + "cannot follow infinite arguments.");
super((optional ? "Optional arguments" : "Arguments") + " cannot follow infinite arguments.");
}
}
32 changes: 26 additions & 6 deletions core/src/main/java/fr/traqueur/commands/api/updater/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Properties;
Expand All @@ -13,6 +14,26 @@
*/
public class Updater {

private static final String VERSION_PROPERTY_FILE = "commands.properties";
private static URL URL_LATEST_RELEASE;
private static Logger LOGGER = Logger.getLogger("CommandsAPI");

static {
try {
URL_LATEST_RELEASE = URI.create("https://api.github.com/repos/Traqueur-dev/CommandsAPI/releases/latest").toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

public static void setUrlLatestRelease(URL URL_LATEST_RELEASE) {
Updater.URL_LATEST_RELEASE = URL_LATEST_RELEASE;
}

public static void setLogger(Logger LOGGER) {
Updater.LOGGER = LOGGER;
}

/**
* Private constructor to prevent instantiation
*/
Expand All @@ -23,7 +44,7 @@ private Updater() {}
*/
public static void checkUpdates() {
if(!Updater.isUpToDate()) {
Logger.getLogger("CommandsAPI").warning("The framework is not up to date, the latest version is " + Updater.fetchLatestVersion());
LOGGER.warning("The framework is not up to date, the latest version is " + Updater.fetchLatestVersion());
}
}

Expand All @@ -34,7 +55,7 @@ public static void checkUpdates() {
public static String getVersion() {
Properties prop = new Properties();
try {
prop.load(Updater.class.getClassLoader().getResourceAsStream("commands.properties"));
prop.load(Updater.class.getClassLoader().getResourceAsStream(VERSION_PROPERTY_FILE));
return prop.getProperty("version");
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -60,8 +81,7 @@ public static boolean isUpToDate() {
*/
public static String fetchLatestVersion() {
try {
URL url = URI.create("https://api.github.com/repos/Traqueur-dev/CommandsAPI/releases/latest").toURL();
String responseString = getString(url);
String responseString = getString();
int tagNameIndex = responseString.indexOf("\"tag_name\"");
int start = responseString.indexOf('\"', tagNameIndex + 10) + 1;
int end = responseString.indexOf('\"', start);
Expand All @@ -75,8 +95,8 @@ public static String fetchLatestVersion() {
* Get the latest version of the plugin
* @return The latest version of the plugin
*/
private static String getString(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
private static String getString() throws IOException {
HttpURLConnection connection = (HttpURLConnection) Updater.URL_LATEST_RELEASE.openConnection();
connection.setRequestMethod("GET");

StringBuilder response = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public BooleanArgument() {

@Override
public Boolean apply(String s) {
if(s == null || s.isEmpty()) {
return null;
}
if(s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) {
return Boolean.parseBoolean(s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public DoubleArgument() {}
*/
@Override
public Double apply(String input) {
if (input == null || input.isEmpty()) {
return null;
}
try {
return Double.valueOf(input);
} catch (NumberFormatException e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public EnumArgument(Class<T> clazz) {

@Override
public Enum<T> apply(String s) {
if (s == null || s.isEmpty()) {
return null;
}
try {
return Enum.valueOf(clazz, s);
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public IntegerArgument() {}
*/
@Override
public Integer apply(String input) {
if (input == null || input.isEmpty()) {
return null;
}
try {
return Integer.valueOf(input);
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public LongArgument() {}
*/
@Override
public Long apply(String input) {
if (input == null || input.isEmpty()) {
return null;
}
try {
return Long.valueOf(input);
} catch (NumberFormatException e) {
Expand Down
Loading