Skip to content

Commit

Permalink
Merge pull request #9 from Koboo/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Koboo authored Feb 5, 2023
2 parents 3dfd19b + 6da11d3 commit 03cc654
Show file tree
Hide file tree
Showing 104 changed files with 2,179 additions and 339 deletions.
55 changes: 19 additions & 36 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import java.util.stream.Collectors

plugins {
id('java')
id('maven-publish')
id('com.github.johnrengelman.shadow') version('7.1.2')
id('com.github.breadmoirai.github-release') version('2.4.1')
id('com.github.johnrengelman.shadow') version('5.2.0')
}

group("$projectGroup")
Expand All @@ -15,9 +12,14 @@ repositories {
}

dependencies {
// MongoDB
implementation("org.mongodb:mongodb-driver-sync:$mongoDriverVersion")
testImplementation("org.mongodb:mongodb-driver-sync:$mongoDriverVersion")

// JetBrains annotations
implementation("org.jetbrains:annotations:$jetbrainsAnnotationsVersion")
testImplementation("org.jetbrains:annotations:$jetbrainsAnnotationsVersion")

// Lombok
compileOnly("org.projectlombok:lombok:$lombokVersion")
annotationProcessor("org.projectlombok:lombok:$lombokVersion")
Expand All @@ -35,17 +37,24 @@ test {
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
withJavadocJar()
withSourcesJar()
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType(JavaCompile).configureEach {
options.fork = true
options.encoding = 'UTF-8'
}

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
archiveClassifier.set('sources')
}
task javadocJar(type: Jar) {
from javadoc
archiveClassifier.set('javadoc')
}

publishing {
repositories {
maven {
Expand All @@ -59,38 +68,12 @@ publishing {
publications {
maven(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
}
}
}

githubRelease {
token System.getenv('GITHUB_TOKEN')
generateReleaseNotes = true
draft = true

releaseAssets jar.destinationDir.listFiles()

body { """\
## Links to $version
* [Documentation](https://koboo.gitbook.com/en2do)
* [Maven](https://reposilite.koboo.eu/#/releases/eu/koboo/en2do/$version/)
* [JavaDocs](https://reposilite.koboo.eu/javadoc/releases/eu/koboo/en2do/$version/)
* [Jenkins](https://jenkins.koboo.eu/job/en2do/job/Build%20and%20Publish%20(main)/)
${
changelog().call()
.readLines()
.stream()
.limit(10)
.map { "- $it" }
.collect(Collectors.joining('\n', '## Changelog\n', ''))
}
- And more..
""" }
}

project.tasks.shadowJar.finalizedBy(project.tasks.javadocJar)
project.tasks.shadowJar.finalizedBy(project.tasks.sourcesJar)
project.tasks.publish.dependsOn(project.tasks.shadowJar)
project.tasks.githubRelease.dependsOn(project.tasks.shadowJar)
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
### Project properties ###
projectGroup=eu.koboo
projectVersion=2.2.0
projectVersion=2.3.0-SNAPSHOT
#
### Dependency versions ###
lombokVersion=1.18.24
mongoDriverVersion=4.8.2
jupiterVersion=5.9.2
slf4jVersion=2.0.6
jetbrainsAnnotationsVersion=24.0.0
#
### Gradle properties ###
#
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
129 changes: 112 additions & 17 deletions src/main/java/eu/koboo/en2do/Credentials.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package eu.koboo.en2do;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -11,34 +18,63 @@
/**
* This object is used to simplify creating credentials to the mongodb server.
* See documentation: <a href="https://koboo.gitbook.io/en2do/get-started/create-the-mongomanager">...</a>
*
* @param connectString The connection string to the mongodb database server
* @param database The database, which should be used
*/
@SuppressWarnings("unused")
public record Credentials(String connectString, String database) {
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@Getter
public class Credentials {

/**
* Empty representation of the credentials object
*/
private static final Credentials EMPTY = new Credentials(null, null);
/**
* The default key of the connection string.
*/
private static final String CONNECT_KEY = "en2do.connectstring";
/**
* The default key of the database.
*/
private static final String DATABASE_KEY = "en2do.database";

/**
* The default name of the credentials file.
*/
private static final String DEFAULT_CREDENTIAL_FILE = "credentials.properties";

private static Credentials fromStreamProperties(InputStream inputStream) {
/**
* Utility method for reading credentials from an input stream.
*
* @param inputStream The input stream, which should be read.
* @return The new created credentials object.
*/
private static @NotNull Credentials fromStreamProperties(@NotNull InputStream inputStream) {
try {
Properties properties = new Properties();
properties.load(inputStream);
return new Credentials(properties.getProperty(CONNECT_KEY), properties.getProperty(DATABASE_KEY));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Error while loading credentials");
}
return null;
}

public static Credentials fromResource() {
/**
* Automatically reading credentials from the default resourcePath, which is
* "{applicationJar}/credentials.properties"
*
* @return The new created credentials object.
*/
public static @Nullable Credentials fromResource() {
return fromResource("/" + DEFAULT_CREDENTIAL_FILE);
}

public static Credentials fromResource(String resourcePath) {
/**
* Automatically reading credentials from a resource file from given resourcePath.
*
* @param resourcePath The resource path with the containing credentials.
* @return The new created credentials object.
*/
public static @Nullable Credentials fromResource(@Nullable String resourcePath) {
if (resourcePath == null) {
throw new RuntimeException("Couldn't read resource from null path!");
}
Expand All @@ -48,17 +84,32 @@ public static Credentials fromResource(String resourcePath) {
return null;
}
try (InputStream inputStream = managerClass.getResourceAsStream(resourcePath)) {
if (inputStream == null) {
throw new RuntimeException("Couldn't create a stream from the resource in the path \"" + resourcePath + "\"!");
}
return fromStreamProperties(inputStream);
} catch (IOException e) {
throw new RuntimeException("Couldn't read resource from path \"" + resourcePath + "\": ", e);
}
}

public static Credentials fromFile() {
/**
* Automatically reading credentials from the default filePath, which is
* "{applicationDirectory}/credentials.properties"
*
* @return The new created credentials object.
*/
public static @Nullable Credentials fromFile() {
return fromFile(DEFAULT_CREDENTIAL_FILE);
}

public static Credentials fromFile(String filePath) {
/**
* Automatically reading credentials from a file from given filePath.
*
* @param filePath The file path with the containing credentials.
* @return The new created credentials object.
*/
public static @Nullable Credentials fromFile(@Nullable String filePath) {
if (filePath == null) {
throw new RuntimeException("Couldn't read file from null path!");
}
Expand All @@ -73,24 +124,68 @@ public static Credentials fromFile(String filePath) {
}
}

public static Credentials fromSystemProperties() {
/**
* Automatically reading credentials from the system properties.
*
* @return The new created credentials object.
*/
public static @NotNull Credentials fromSystemProperties() {
return fromSystemProperties(CONNECT_KEY, DATABASE_KEY);
}

public static Credentials fromSystemProperties(String propertyConnectKey, String propertyDatabaseKey) {
/**
* Automatically reading credentials from the system properties,
* using custom keys for the connectString and database
*
* @param propertyConnectKey The property key for the connection string
* @param propertyDatabaseKey The property key for the database
* @return The new created credentials object.
*/
public static @NotNull Credentials fromSystemProperties(@NotNull String propertyConnectKey, @NotNull String propertyDatabaseKey) {
return new Credentials(System.getProperty(propertyConnectKey), System.getProperty(propertyDatabaseKey));
}

public static Credentials fromSystemEnvVars() {
/**
* Automatically reading credentials from the system environmental variables.
*
* @return The new created credentials object.
*/
public static @NotNull Credentials fromSystemEnvVars() {
return fromSystemEnvVars(CONNECT_KEY.toUpperCase(Locale.ROOT).replaceFirst("\\.", "_"),
DATABASE_KEY.toUpperCase(Locale.ROOT).replaceFirst("\\.", "_"));
}

public static Credentials fromSystemEnvVars(String envVarConnectKey, String envVarDatabaseKey) {
/**
* Automatically reading credentials from the system environmental variables.
* using custom keys for the connectString and database
*
* @param envVarConnectKey The environmental variable key for the connection string
* @param envVarDatabaseKey The environmental variable key for the database
* @return The new created credentials object.
*/
public static @NotNull Credentials fromSystemEnvVars(@NotNull String envVarConnectKey, @NotNull String envVarDatabaseKey) {
return new Credentials(System.getenv(envVarConnectKey), System.getenv(envVarDatabaseKey));
}

public static Credentials of(String connectString, String database) {
/**
* Create a new credentials object by passing the two values directly.
*
* @param connectString The connection string to the mongodb server.
* @param database The database which should be used.
* @return A new created credentials object.
*/
public static @NotNull Credentials of(@Nullable String connectString, @Nullable String database) {
return new Credentials(connectString, database);
}

/**
* The connection string to the mongodb database server
*/
@Nullable
String connectString;
/**
* The database, which should be used
*/
@Nullable
String database;
}
Loading

0 comments on commit 03cc654

Please sign in to comment.