Skip to content

Commit

Permalink
Fixing build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Nov 22, 2023
1 parent 7474db6 commit bca108b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 30 deletions.
2 changes: 1 addition & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

dependencies {
implementation(kotlin("reflect"))
compileOnly(kotlin("reflect"))
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ open class Argument<S : CommandSender, T, V>(
}

fun getValue(context: CommandContext<S>): V {
println("getValue")
return getter(context)
return context.getValue(this) ?: getter(context)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package com.mattmx.ktgui.commands.declarative

import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

class CommandContext<S : CommandSender>(
val sender: S,
val args: Array<String>,
)
) {
private lateinit var map: Map<String, Any?>

fun ref(name: String) = map[name]

operator fun <T, V> ReadOnlyProperty<T, V>.getValue(
thisRef: T,
property: KProperty<*>
) : V {
return map[property.name] as V
}

fun <T, V> getValue(argument: Argument<S, T, V>) : V? {
return if (::map.isInitialized) map[argument.id] as V? else null
}

fun clone() = CommandContext(sender, args)

fun withValues(map: List<Pair<String, Any?>>) = apply {
this.map = map.toMap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,22 @@ open class KtCommandBuilder<S : CommandSender>(val name: String) {
return subcommands.firstOrNull { it.name == firstArg || it.aliases.contains(firstArg) }
}

// something i saw on another command dsl, doesn't solve problem
lateinit var contextRef: ContextRef<S>

class ContextRef<S : CommandSender>(
var context: CommandContext<S>
)
private lateinit var context: CommandContext<S>

operator fun <T, V> Argument<S, T, V>.provideDelegate(
thisRef: Any?,
property: KProperty<*>,
): ReadOnlyProperty<T, V> {
id = property.name

println("KtCommandBuilder#provideDelegate")
return ReadOnlyProperty { _, _ -> getter.invoke(contextRef.context) }
return ReadOnlyProperty { thisRef, property -> context.getValue(this) as V }
}

operator fun invoke(context: CommandContext<S>) {
if (!::contextRef.isInitialized)
contextRef = ContextRef(context)
else this.contextRef.context = context

runs.invoke(context)
// save values of args
this.context = context
val values = expectedArguments.map { it.id to it.getValue(context) }
runs.invoke(context.withValues(values))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ fun main() {
runs {
thread {
Thread.sleep(1000)
println("[You -> $username]")
println(ref("username"))
println("[You -> $username] $message")
}
}
}

println(msg.getUsage(true))
msg(CommandContext(CommandSender(), argsOf("MattMX")))
msg(CommandContext(CommandSender(), argsOf("MattMX test")))
msg(CommandContext(CommandSender(), argsOf("GabbySimon")))
}
6 changes: 1 addition & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
plugins {
kotlin("jvm") version "1.7.21"
id("org.ajoberstar.grgit") version "4.1.0"
}

fun getCheckedOutGitCommitHash(): String = grgit.head().abbreviatedId

val commitHash = getCheckedOutGitCommitHash()
val version = "2.0"

rootProject.version = "$version-$commitHash"
rootProject.version = version

subprojects {
apply(plugin = "java")
Expand Down
20 changes: 14 additions & 6 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
id("com.github.johnrengelman.shadow") version "7.0.0"
id("com.github.johnrengelman.shadow") version "7.1.2"
`maven-publish`
id("org.ajoberstar.grgit") version "4.1.0"
}

repositories {
Expand All @@ -14,8 +15,8 @@ repositories {
}

dependencies {
implementation(project(":api"))
implementation("co.pvphub:ProtocolLibDsl:-SNAPSHOT")
shadow(implementation(project(":api"))!!)
shadow(implementation("co.pvphub:ProtocolLibDsl:-SNAPSHOT")!!)
compileOnly("com.comphenix.protocol:ProtocolLib:4.7.0")
}

Expand All @@ -29,9 +30,13 @@ tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}

fun getCheckedOutGitCommitHash(): String = grgit.head().abbreviatedId

val commitHash = getCheckedOutGitCommitHash()

tasks {
withType<ProcessResources> {
val props = "version" to rootProject.version
val props = "version" to "${rootProject.version}-commit-$commitHash"
inputs.properties(props)
filteringCharset = "UTF-8"
filesMatching("plugin.yml") {
Expand All @@ -43,7 +48,10 @@ tasks {
}
}

val compile = tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
archiveBaseName.set("ktgui-plugin-${rootProject.version}")
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
archiveBaseName.set("ktgui-plugin")
archiveClassifier.set("all")
archiveVersion.set(rootProject.version.toString())
exclude("kotlin.*")
mergeServiceFiles()
}

0 comments on commit bca108b

Please sign in to comment.