Skip to content

Commit

Permalink
Start splitting modules into separate projects for organizational pur…
Browse files Browse the repository at this point in the history
…poses
  • Loading branch information
Matyrobbrt committed Aug 28, 2024
1 parent 929f5e6 commit e97d956
Show file tree
Hide file tree
Showing 26 changed files with 466 additions and 157 deletions.
71 changes: 33 additions & 38 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import net.neoforged.gradleutils.PomUtilsExtension
import java.nio.file.Files

plugins {
id 'java'
id 'java-library'
id 'maven-publish'
id 'net.neoforged.gradleutils' version '3.0.0-alpha.13'
id 'com.github.johnrengelman.shadow' version '7.+'
Expand Down Expand Up @@ -60,22 +60,6 @@ java {
withSourcesJar()
}

repositories {
mavenCentral()
maven {
name 'm2-dv8tion'
url 'https://m2.dv8tion.net/releases'
}
maven {
name 'jda-chewtils'
url 'https://m2.chew.pro/releases'
}
maven {
name 'jitpack'
url 'https://jitpack.io'
}
}

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs.add('--enable-preview')
Expand All @@ -89,22 +73,27 @@ javadoc {

evaluationDependsOn(':config')

configurations {
library.extendsFrom(implementation)
module
runtimeClasspath.extendsFrom(module)
}

dependencies {
implementation project(':config')

implementation group: 'com.github.matyrobbrt', name: 'JDA-Chewtils', version: "${project.jda_chewtils_version}"
implementation group: 'net.dv8tion', name: 'JDA', version: "${project.jda_version}"
api group: 'com.github.matyrobbrt', name: 'JDA-Chewtils', version: "${project.jda_chewtils_version}"
api group: 'net.dv8tion', name: 'JDA', version: "${project.jda_version}"
implementation group: 'com.google.guava', name: 'guava', version: "${project.guava_version}"
implementation group: 'com.google.code.gson', name: 'gson', version: "${project.gson_version}"
implementation group: 'ch.qos.logback', name: 'logback-classic', version: "${project.logback_version}"

implementation group: 'org.flywaydb', name: 'flyway-core', version: project.flyway_version
implementation group: 'org.jdbi', name: 'jdbi3-core', version: project.jdbi_version
implementation group: 'org.jdbi', name: 'jdbi3-sqlobject', version: project.jdbi_version
implementation libs.bundles.jdbi
implementation group: 'org.xerial', name: 'sqlite-jdbc', version: project.sqlite_version

// YML parsing and generation
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: project.jackson_version
implementation libs.bundles.jackson

// GitHub API interaction library
implementation group: 'org.kohsuke', name: 'github-api', version: project.ghapi_version
Expand Down Expand Up @@ -144,25 +133,30 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:$junit_version")
testImplementation("org.junit.jupiter:junit-jupiter-engine:$junit_version")
testImplementation('org.assertj:assertj-core:3.25.1')

project(':modules').subprojects.each {
library(project(path: it.path, configuration: 'library'))
configurations.module.dependencies.add(dependencies.create(it) {
transitive = false
})
}
}

jar {
manifest {
mainAttributes(
'Maven-Artifact': "${project.group}:${archivesBaseName}:${project.version}",
'Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
'Specification-Title': archivesBaseName,
'Specification-Vendor': 'NeoForged',
'Specification-Version': '1',
'Implementation-Title': archivesBaseName,
'Implementation-Version': "${project.version}",
'Implementation-Vendor': 'NeoForged',
'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
'Built-On-Java': "${System.getProperty('java.vm.version')} (${System.getProperty('java.vm.vendor')})",
'Built-On': "${project.jda_version}-${project.jda_chewtils_version}",
'Main-Class': 'net.neoforged.camelot.BotMain'
)
}
manifest.attributes([
'Maven-Artifact': "${project.group}:${archivesBaseName}:${project.version}",
'Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
'Specification-Title': archivesBaseName,
'Specification-Vendor': 'NeoForged',
'Specification-Version': '1',
'Implementation-Title': archivesBaseName,
'Implementation-Version': "${project.version}",
'Implementation-Vendor': 'NeoForged',
'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
'Built-On-Java': "${System.getProperty('java.vm.version')} (${System.getProperty('java.vm.vendor')})",
'Built-On': "${project.jda_version}-${project.jda_chewtils_version}",
'Main-Class': 'net.neoforged.camelot.BotMain'
])
}

tasks.register('outputVersion') {
Expand All @@ -177,6 +171,7 @@ shadowJar {
archiveFile.set(project.file("$buildDir/libs/camelot-all.jar"))
dependsOn(tasks.gitLog)
from(tasks.gitLog.output)
configurations = [project.configurations.library, project.configurations.module]
}

test {
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id 'groovy-gradle-plugin'
}
97 changes: 97 additions & 0 deletions buildSrc/src/main/groovy/camelot-module.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import java.util.stream.Collectors

project.plugins.apply('java-library')
project.plugins.apply('groovy')

var config = project.extensions.create('camelotModule', CamelotModuleConfig)

var library = project.configurations.create('library')
var moduleConfig = project.configurations.create('module')
project.configurations.getByName('api').extendsFrom(library, moduleConfig)
project.configurations.create('configOut')
project.configurations.create('configOutSrc')

sourceSets.create('config')

dependencies {
"configImplementation"(implementation(project(':config')))
"configOut"(compileOnly(sourceSets.config.output))
"configOutSrc"(sourceSets.config.allSource)

compileOnly group: 'com.google.auto.service', name: 'auto-service', version: '1.0.1'
annotationProcessor group: 'com.google.auto.service', name: 'auto-service', version: '1.0.1'

implementation(project(':'))
}

java.toolchain.languageVersion = JavaLanguageVersion.of(21)
java.toolchain.vendor = JvmVendorSpec.GRAAL_VM

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs.add('--enable-preview')
}

tasks.jar {
from(sourceSets.config.output)
}

abstract class CamelotModuleConfig {
@Input
abstract Property<String> getId()
}

tasks.register('createFiles') {
doLast {
var name = config.id.get().split('-')
.toList().stream().map { it.capitalize() }
.collect(Collectors.joining(''))
var modulePkg = config.id.get().replace('-', '')
writeOrMove(project.file("src/main/java/net/neoforged/camelot/module/${modulePkg}/${name}Module.java"), null, """
package net.neoforged.camelot.module.${modulePkg};
import com.google.auto.service.AutoService;
import net.neoforged.camelot.config.module.${name};
import net.neoforged.camelot.module.api.CamelotModule;
@AutoService(CamelotModule.class)
public class ${name}Module extends CamelotModule.Base<${name}> {
public ${name}Module() {
super(${name}.class);
}
@Override
public String id() {
return "${config.id.get()}";
}
}""")
writeOrMove(project.file("src/config/groovy/net/neoforged/camelot/config/module/${name}.groovy"), rootProject.file("config/src/main/groovy/net/neoforged/camelot/config/module/${name}.groovy"), """
package net.neoforged.camelot.config.module
import groovy.transform.CompileStatic
@CompileStatic
class ${name} extends ModuleConfiguration {
}""")
}
}

tasks.register('createDBFolder') {
doLast {
var modulePkg = config.id.get().replace('-', '')
project.file("src/main/resources/net/neoforged/camelot/module/${modulePkg}/db/schema").mkdirs()
}
}

private static void writeOrMove(File file, File source, String txt) {
if (source?.exists()) {
file.delete()
file << source.text
source.delete()
} else {
file.delete()
file.parentFile.mkdirs()
file << txt
}
}
12 changes: 11 additions & 1 deletion config/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ repositories {
mavenCentral()
}

configurations {
configSources
}

java.withSourcesJar()

groovydoc {
use = true
source(configurations.configSources)
}

tasks.register('groovydocJar', Jar) {
Expand All @@ -38,9 +43,14 @@ tasks.register('groovydocJar', Jar) {
dependencies {
api "org.apache.groovy:groovy:${project.groovy_version}"
api "org.apache.groovy:groovy-contracts:${project.groovy_version}"
implementation group: 'org.kohsuke', name: 'github-api', version: project.ghapi_version
api group: 'org.kohsuke', name: 'github-api', version: project.ghapi_version

sampleCompileOnly(sourceSets.main.output)

project(':modules').subprojects.each {
sampleCompileOnly(project(path: it.path, configuration: 'configOut'))
configSources(project(path: it.path, configuration: 'configOutSrc'))
}
}

publishing {
Expand Down
2 changes: 0 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ guava_version=30.1.1-jre
gson_version=2.10.1
logback_version=1.5.6
flyway_version=8.5.13
jdbi_version=3.32.0
sqlite_version=3.40.0.0

jjwt_version=0.10.5
jackson_version=2.13.3
trove4j_version=3.0.3
ghapi_version=1.313
bcpkix_version=1.58
Expand Down
7 changes: 7 additions & 0 deletions modules/file-preview/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id 'camelot-module'
}

camelotModule {
id = 'file-preview'
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.neoforged.camelot.module;
package net.neoforged.camelot.module.filepreview;

import com.google.auto.service.AutoService;
import net.dv8tion.jda.api.JDABuilder;
Expand Down
12 changes: 12 additions & 0 deletions modules/info-channels/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id 'camelot-module'
}

camelotModule {
id = 'info-channels'
}

dependencies {
library(libs.bundles.jdbi)
library(libs.bundles.jackson)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package net.neoforged.camelot.module.infochannels;

import com.google.auto.service.AutoService;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import net.dv8tion.jda.api.JDA;
import net.neoforged.camelot.BotMain;
import net.neoforged.camelot.config.module.InfoChannels;
import net.neoforged.camelot.db.schemas.GithubLocation;
import net.neoforged.camelot.module.BuiltInModule;
import net.neoforged.camelot.module.api.CamelotModule;
import net.neoforged.camelot.module.infochannels.command.InfoChannelCommand;
import net.neoforged.camelot.module.infochannels.command.RuleCommand;
import net.neoforged.camelot.module.infochannels.db.InfoChannel;
import net.neoforged.camelot.module.infochannels.db.InfoChannelsDAO;
import net.neoforged.camelot.module.infochannels.db.RulesDAO;

import java.util.concurrent.TimeUnit;

/**
* Info channels module.
*/
@AutoService(CamelotModule.class)
public class InfoChannelsModule extends CamelotModule.WithDatabase<InfoChannels> {
public InfoChannelsModule() {
super(InfoChannels.class);

accept(BuiltInModule.DB_MIGRATION_CALLBACKS, builder -> builder
.add(BuiltInModule.DatabaseSource.MAIN, 15, stmt -> {
logger.info("Migrating info channels from main.db to info-channels.db");
var infoChannels = stmt.executeQuery("select * from info_channels");
db().useExtension(InfoChannelsDAO.class, db -> {
while (infoChannels.next()) {
db.insert(
new InfoChannel(
infoChannels.getLong(1),
GithubLocation.parse(infoChannels.getString(2)),
infoChannels.getBoolean(3),
infoChannels.getString(4),
InfoChannel.Type.values()[infoChannels.getInt(5)]
)
);
}
});

logger.info("Migrating rules from main.db to info-channels.db");
var rules = stmt.executeQuery("select * from rules");
db().useExtension(RulesDAO.class, db -> {
while (rules.next()) {
db.insert(
rules.getLong(1),
rules.getLong(2),
rules.getInt(3),
rules.getString(4)
);
}
});
}));
}

@Override
public String id() {
return "info-channels";
}

@Override
public boolean shouldLoad() {
return config().getAuth() != null;
}

@Override
public void registerCommands(CommandClientBuilder builder) {
builder.addSlashCommand(new InfoChannelCommand())
.addSlashCommand(RuleCommand.INSTANCE)
.addCommand(RuleCommand.INSTANCE)
.addContextMenu(new InfoChannelCommand.UploadToDiscohookContextMenu());
}

@Override
public void setup(JDA jda) {
jda.addEventListener(InfoChannelCommand.EVENT_LISTENER);

// Update info channels every couple of minutes
BotMain.EXECUTOR.scheduleAtFixedRate(InfoChannelCommand::run, 1, 2, TimeUnit.MINUTES);
}
}
Loading

0 comments on commit e97d956

Please sign in to comment.