Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Escape plugin names in plugins.sbt and generated source files #95

Merged
merged 2 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions project/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import scala.xml.PrettyPrinter
* @param name the name of the plugin
*/
class Plugin(val pluginSourceDirectoryName: String, val name: String) {
val normalizedName: String = Plugin.toPluginPathName(name)
val pluginDirectoryPath: String = s"$pluginSourceDirectoryName/$normalizedName"
val normalizedName: String = Plugin.toNormalizedName(name)
val pluginDirectoryPath: String = s"$pluginSourceDirectoryName/${normalizedName.toLowerCase}"

/**
* Creates the plugin folder inside of a plugin source directory
Expand Down Expand Up @@ -147,7 +147,20 @@ object Plugin {
pluginSourceFolder.exists() && pluginSourceFolder.isDirectory
}

private def toPluginPathName(name: String) = name.replaceAll("[ -]", "").toLowerCase
private def toNormalizedName(name: String): String = {
if (name.isEmpty) {
return ""
}

val firstChar = name(0)

if (!Character.isJavaIdentifierStart(firstChar)) {
toNormalizedName(name.substring(1))
} else {
val rest = name.substring(1)
firstChar + rest.filter(Character.isJavaIdentifierPart)
}
}

private def containsPluginXMLFile(directory: File): Boolean = {
new File(s"$directory/src/main/resources/plugin.xml").exists()
Expand Down
10 changes: 5 additions & 5 deletions project/SbtFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ class SbtFile(var name: String, var version: String, var plugins: List[Plugin],
val sbtContent = new StringBuilder("// GENERATED FILE USING THE CHAT OVERFLOW PLUGIN FRAMEWORK\n")

if (name != "") {
sbtContent append "\nname := \"%s\"".format(name)
sbtContent append "\nname := \"%s\"".format(name.replaceAll("\\", ""))
}

if (version != "") {
sbtContent append "\nversion := \"%s\"".format(version)
sbtContent append "\nversion := \"%s\"".format(version.replaceAll("\\", ""))
}

if (plugins.nonEmpty) {
for (plugin <- plugins) {
var pluginLine = "\nlazy val %s = (project in file(\"%s\"))".format(plugin.normalizedName, plugin.pluginDirectoryPath)
var pluginLine = "\nlazy val `%s` = (project in file(\"%s\"))".format(plugin.normalizedName, plugin.pluginDirectoryPath)

if (apiProjectPath != "") {
pluginLine += ".dependsOn(apiProject)"
Expand All @@ -79,8 +79,8 @@ class SbtFile(var name: String, var version: String, var plugins: List[Plugin],
}

if (defineRoot) {
var rootLine = "\n\nlazy val root = (project in file(\".\")).aggregate(apiProject,%s)"
.format(plugins.map(_.normalizedName).mkString(", "))
var rootLine = "\n\nlazy val root = (project in file(\".\")).aggregate(apiProject, %s)"
.format(plugins.map(p => s"`${p.normalizedName}`").mkString(", "))

if (apiProjectPath != "") {
rootLine += ".dependsOn(apiProject)"
Expand Down