From d1ec7c3fb9fc620fa474dd8cef3ea6b71bb8be39 Mon Sep 17 00:00:00 2001 From: way-zer Date: Mon, 2 Mar 2020 23:23:59 +0800 Subject: [PATCH] :sparkles: Add @file:MavenDepends :lock: Add Support for Java11 :alien: update LibraryManager to v1.3.2 :bento: Add .gitprefix --- .gitprefix | 31 +++ build.gradle | 2 +- gradlew | 188 ------------------ src/main/kotlin/cf/wayzer/SuperItem/Main.kt | 2 +- .../cf/wayzer/SuperItem/features/DataStore.kt | 11 +- .../scripts/CompilationConfiguration.kt | 28 ++- .../wayzer/SuperItem/scripts/ImportClass.kt | 5 +- .../wayzer/SuperItem/scripts/MavenDepends.kt | 11 + .../wayzer/SuperItem/scripts/ScriptLoader.kt | 2 +- .../SuperItem/scripts/ScriptSupporter.kt | 5 +- 10 files changed, 81 insertions(+), 204 deletions(-) create mode 100644 .gitprefix delete mode 100755 gradlew create mode 100644 src/main/kotlin/cf/wayzer/SuperItem/scripts/MavenDepends.kt diff --git a/.gitprefix b/.gitprefix new file mode 100644 index 0000000..02d950b --- /dev/null +++ b/.gitprefix @@ -0,0 +1,31 @@ +//By way_zer, references: +// Angular Conventions +// http://gitmoji.surge.sh/ + +|$| ===Type 类型 +//Follow Angular,May different +:sparkles: NewOrImprove | 新功能或外部改善 +:bug: BugFix | bug修复 +:memo: Documentation | 添加文档 +:art: Style | 自动优化(Lint,Reformat) +:hammer: InteralChange | 内部重构,不外部变化 +:white_check_mark: Test | 测试效果修改 +:bento: Chore | 其他代码无关的杂事 + +|$| ===Reason 原因=== +:alien: DueToApi | 外界导致变更 +:globe_with_meridians: Globalization | 国际化 +:lock: Safe | 安全 +:zap: Proformance | 优化性能 +|$| ===Operate 操作=== +:fire: Remove | 删除xxx +:truck: Move | 移动或重命名 +|$| ===Other 结果,变更,备注=== +:construction: UnderConstruct | 建设中 +:bookmark: NewVersion | 版本号更新 +:boom: BreakChange | 重大修改 +:ok_hand: Closes | 关闭Issue或Pull + +merge { + :twisted_rightwards_arrows: Merge From +} diff --git a/build.gradle b/build.gradle index da9bb71..0e17d01 100644 --- a/build.gradle +++ b/build.gradle @@ -52,7 +52,7 @@ shadowJar { //start dependencies{ - shadow 'cf.wayzer:LibraryManager:1.3' + shadow 'cf.wayzer:LibraryManager:1.3.2' shadow(files("$rootDir/lib/PowerNBT.jar")) compile "org.jetbrains.kotlin:kotlin-script-util:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-scripting-jvm-host:$kotlin_version" diff --git a/gradlew b/gradlew deleted file mode 100755 index b0d6d0a..0000000 --- a/gradlew +++ /dev/null @@ -1,188 +0,0 @@ -#!/usr/bin/env sh - -# -# Copyright 2015 the original author or authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -############################################################################## -## -## Gradle start up script for UN*X -## -############################################################################## - -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null - -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" - -warn () { - echo "$*" -} - -die () { - echo - echo "$*" - echo - exit 1 -} - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; -esac - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi -fi - -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi - -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi - # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" - fi - i=$((i+1)) - done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac -fi - -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=$(save "$@") - -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" - -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - -exec "$JAVACMD" "$@" diff --git a/src/main/kotlin/cf/wayzer/SuperItem/Main.kt b/src/main/kotlin/cf/wayzer/SuperItem/Main.kt index de41fe4..27ac430 100644 --- a/src/main/kotlin/cf/wayzer/SuperItem/Main.kt +++ b/src/main/kotlin/cf/wayzer/SuperItem/Main.kt @@ -37,7 +37,7 @@ class Main : JavaPlugin() { require(Dependency("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")) require(Dependency("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion")) require(Dependency("org.jetbrains:annotations:13.0")) - loadToClasspath() + loadToClassLoader(Main::class.java.classLoader) } } } diff --git a/src/main/kotlin/cf/wayzer/SuperItem/features/DataStore.kt b/src/main/kotlin/cf/wayzer/SuperItem/features/DataStore.kt index 0801b85..9bcf972 100644 --- a/src/main/kotlin/cf/wayzer/SuperItem/features/DataStore.kt +++ b/src/main/kotlin/cf/wayzer/SuperItem/features/DataStore.kt @@ -5,11 +5,10 @@ import cf.wayzer.SuperItem.Main import org.bukkit.metadata.FixedMetadataValue import org.bukkit.metadata.Metadatable -object DataStore { - interface DataStore{ - fun set(o: Metadatable,value: T?) - fun get(o:Metadatable,defaultValue:T):T - } +interface DataStore{ + fun set(o: Metadatable,value: T?) + fun get(o:Metadatable,defaultValue:T):T + class MetaStore(override val defaultData: Nothing) :Feature(),DataStore{ private val key:String get() = "SIMS_${item.name}" @@ -25,4 +24,4 @@ object DataStore { } } //TODO other Store: SQL and File -} \ No newline at end of file +} diff --git a/src/main/kotlin/cf/wayzer/SuperItem/scripts/CompilationConfiguration.kt b/src/main/kotlin/cf/wayzer/SuperItem/scripts/CompilationConfiguration.kt index b96baeb..377ec2c 100644 --- a/src/main/kotlin/cf/wayzer/SuperItem/scripts/CompilationConfiguration.kt +++ b/src/main/kotlin/cf/wayzer/SuperItem/scripts/CompilationConfiguration.kt @@ -1,21 +1,28 @@ package cf.wayzer.SuperItem.scripts import cf.wayzer.SuperItem.Item +import cf.wayzer.libraryManager.Dependency +import cf.wayzer.libraryManager.LibraryManager import org.bukkit.Bukkit import org.bukkit.Material import java.io.File +import java.nio.file.Paths import kotlin.reflect.KClass import kotlin.script.experimental.api.* import kotlin.script.experimental.jvm.JvmScriptCompilationConfigurationBuilder +import kotlin.script.experimental.jvm.dependenciesFromClassloader import kotlin.script.experimental.jvm.jvm import kotlin.script.experimental.jvm.updateClasspath import kotlin.script.experimental.jvm.util.classpathFromClass object CompilationConfiguration: ScriptCompilationConfiguration({ jvm { + dependenciesFromClassloader( + "kotlin-stdlib" + ,classLoader = ScriptLoader::class.java.classLoader) dependenciesFromClass(Bukkit::class, Item::class) } - defaultImports(Item::class, ImportClass::class,Material::class) + defaultImports(Item::class, ImportClass::class,MavenDepends::class,Material::class) defaultImports.append("cf.wayzer.SuperItem.features.*") refineConfiguration{ onAnnotations(ImportClass::class){ context-> @@ -28,6 +35,23 @@ object CompilationConfiguration: ScriptCompilationConfiguration({ } }.asSuccess(diagnostics) } + onAnnotations(MavenDepends::class){ context-> + val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.filter { it.annotationClass== MavenDepends::class }?: listOf() + val dependencies = annotations.map {Dependency((it as MavenDepends).name,it.repo)} + val diagnostics = dependencies.map { ScriptDiagnostic("[Info]MavenDependency: $it",ScriptDiagnostic.Severity.INFO) } + ScriptCompilationConfiguration(context.compilationConfiguration) { + jvm { + LibraryManager(Paths.get("lib")).apply { + addAliYunMirror() + dependencies.forEach { + require(it) + } + loadToClassLoader(javaClass.classLoader) + updateClasspath(loadFiles()) + } + } + }.asSuccess(diagnostics) + } } }) private fun JvmScriptCompilationConfigurationBuilder.dependenciesFromClass(vararg classes: KClass) { @@ -39,4 +63,4 @@ private fun JvmScriptCompilationConfigurationBuilder.dependenciesFromClass(varar url?.toURI()?.schemeSpecificPart?.let { listOf(File(it.removePrefix("file:").split("!")[0])) } }?: emptyList() }.let(this::updateClasspath) -} \ No newline at end of file +} diff --git a/src/main/kotlin/cf/wayzer/SuperItem/scripts/ImportClass.kt b/src/main/kotlin/cf/wayzer/SuperItem/scripts/ImportClass.kt index df6f640..3c52977 100644 --- a/src/main/kotlin/cf/wayzer/SuperItem/scripts/ImportClass.kt +++ b/src/main/kotlin/cf/wayzer/SuperItem/scripts/ImportClass.kt @@ -1,7 +1,8 @@ package cf.wayzer.SuperItem.scripts @Target(AnnotationTarget.FILE) +@Retention(AnnotationRetention.SOURCE) @Repeatable annotation class ImportClass( - val name:String -) \ No newline at end of file + val name:String +) diff --git a/src/main/kotlin/cf/wayzer/SuperItem/scripts/MavenDepends.kt b/src/main/kotlin/cf/wayzer/SuperItem/scripts/MavenDepends.kt new file mode 100644 index 0000000..93c6ed1 --- /dev/null +++ b/src/main/kotlin/cf/wayzer/SuperItem/scripts/MavenDepends.kt @@ -0,0 +1,11 @@ +package cf.wayzer.SuperItem.scripts + +import cf.wayzer.libraryManager.Repository + +@Target(AnnotationTarget.FILE) +@Retention(AnnotationRetention.SOURCE) +@Repeatable +annotation class MavenDepends( + val name: String, + val repo: String = Repository.DEFAULT +) diff --git a/src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptLoader.kt b/src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptLoader.kt index e6d9bf2..f9fc5e6 100644 --- a/src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptLoader.kt +++ b/src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptLoader.kt @@ -50,7 +50,7 @@ class ScriptLoader { private fun load0(f: File):ResultWithDiagnostics{ return BasicJvmScriptingHost(hostConfiguration).eval(f.toScriptSource(),compilationConfiguration, ScriptEvaluationConfiguration { jvm { - baseClassLoader(ScriptSupporter::class.java.classLoader) + baseClassLoader(ScriptLoader::class.java.classLoader) } constructorArgs(f.name.split(".")[0].toUpperCase()) }) diff --git a/src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptSupporter.kt b/src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptSupporter.kt index 02fd432..3013b33 100644 --- a/src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptSupporter.kt +++ b/src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptSupporter.kt @@ -5,7 +5,6 @@ import cf.wayzer.SuperItem.Main import cf.wayzer.libraryManager.Dependency import cf.wayzer.libraryManager.LibraryManager import java.io.File -import java.net.URLClassLoader import java.nio.file.Paths import java.util.logging.Logger @@ -26,8 +25,8 @@ object ScriptSupporter { require(Dependency("org.jetbrains.kotlin:kotlin-scripting-compiler-impl:${Main.kotlinVersion}")) require(Dependency("org.jetbrains.kotlin:kotlin-compiler:${Main.kotlinVersion}")) require(Dependency("org.jetbrains.kotlin:kotlin-script-util:${Main.kotlinVersion}")) - }.loadToClassLoader(Main::class.java.classLoader as URLClassLoader) - loader = ScriptLoader() + }.loadToClassLoader(Main::class.java.classLoader) + loader = Class.forName("cf.wayzer.SuperItem.scripts.ScriptLoader").newInstance() as ScriptLoader loader.logger=logger inited =true }