Save config comments (not all of them apparently)

This commit is contained in:
Norbi Peti 2021-04-06 01:05:58 +02:00
parent d80703ac68
commit 74bce1ecf9
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 19 additions and 21 deletions

View file

@ -1,3 +1,5 @@
import org.bukkit.configuration.file.YamlConfiguration
import java.util.regex.Pattern import java.util.regex.Pattern
import scala.io.Source import scala.io.Source
import scala.util.Using import scala.util.Using
@ -46,49 +48,41 @@ assemblyMergeStrategy in assembly := {
val getConfigComments = TaskKey[Unit]("getConfigComments") val getConfigComments = TaskKey[Unit]("getConfigComments")
getConfigComments := { getConfigComments := {
val sv = (Compile / sources).value val sv = (Compile / sources).value
//val cdataRegex = Pattern.compile("(?:def|val|var) \\w*ConfigData\\w*(?:\\[\\w+])? (\\w+)")
val cdataRegex = Pattern.compile("(?:def|val|var) (\\w+)(?::[^=]+)? = get(?:I)?Config") val cdataRegex = Pattern.compile("(?:def|val|var) (\\w+)(?::[^=]+)? = get(?:I)?Config")
val clRegex = Pattern.compile("class (\\w+) extends (\\w+)") val clRegex = Pattern.compile("class (\\w+) extends (\\w+)")
val config = new YamlConfiguration()
for (file <- sv) { for (file <- sv) {
Using(Source.fromFile(file)) { src => Using(Source.fromFile(file)) { src =>
var pkg: String = null var pkg: String = null
var cl: String = null var clKey: String = null
var comment: String = null var comment: String = null
var justCommented: Boolean = false var justCommented: Boolean = false
var isComponent: Boolean = false
for (line <- src.getLines) { for (line <- src.getLines) {
val clMatcher = clRegex.matcher(line) val clMatcher = clRegex.matcher(line)
if (line.startsWith("package")) { if (line.startsWith("package")) {
pkg = line.substring("package ".length) pkg = line.substring("package ".length)
//println("Found package: " + pkg) } else if (line.contains("class") && pkg != null && clKey == null && clMatcher.find()) { //First occurrence
} else if (line.contains("class") && pkg != null && cl == null && clMatcher.find()) { //First occurrence clKey = if (clMatcher.group(2).contains("Component"))
//cl = line.substring(line.indexOf("class") + "class ".length) "component." + clMatcher.group(1)
cl = clMatcher.group(1) else
isComponent = clMatcher.group(2).contains("Component") "global"
//println("Found class: " + cl) } else if (line.contains("/**") && clKey != null) {
} else if (line.contains("/**") && cl != null) {
comment = "" comment = ""
justCommented = false justCommented = false
//println("Found comment start") } else if (line.contains("*/") && comment != null)
} else if (line.contains("*/") && comment != null) {
justCommented = true justCommented = true
//println("Found comment end") else if (comment != null) {
} else if (comment != null) {
if (justCommented) { if (justCommented) {
//println("Just commented")
//println(s"line: $line")
val matcher = cdataRegex.matcher(line) val matcher = cdataRegex.matcher(line)
if (matcher.find()) if (matcher.find())
println(s"$pkg.$cl.${matcher.group(1)} comment:\n" + comment) config.set(s"$clKey.${matcher.group(1)}", comment.trim)
justCommented = false justCommented = false
comment = null comment = null
} }
else { else comment += line.replaceFirst("^\\s*\\*\\s+", "") + "\n"
comment += line.replaceFirst("^\\s*\\*\\s+", "")
//println("Adding to comment")
}
} }
} }
config.save("configHelp.yml")
}.recover[Unit]({ case t => t.printStackTrace() }) }.recover[Unit]({ case t => t.printStackTrace() })
} }
} }

View file

@ -1,2 +1,6 @@
//lazy val commenter = RootProject(file("../commenter")) //lazy val commenter = RootProject(file("../commenter"))
//lazy val root = (project in file(".")).dependsOn(commenter) //lazy val root = (project in file(".")).dependsOn(commenter)
resolvers += Resolver.mavenLocal
libraryDependencies += "org.spigotmc" % "spigot-api" % "1.12.2-R0.1-SNAPSHOT"