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

Commit 3e944ca

Browse files
authored
Merge pull request #95 from daniel0611/fix/94-escape-plugin-names
Escape plugin names in plugins.sbt and generated source files
2 parents 42926c7 + 0c9c7bf commit 3e944ca

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

project/Plugin.scala

+16-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import scala.xml.PrettyPrinter
1212
* @param name the name of the plugin
1313
*/
1414
class Plugin(val pluginSourceDirectoryName: String, val name: String) {
15-
val normalizedName: String = Plugin.toPluginPathName(name)
16-
val pluginDirectoryPath: String = s"$pluginSourceDirectoryName/$normalizedName"
15+
val normalizedName: String = Plugin.toNormalizedName(name)
16+
val pluginDirectoryPath: String = s"$pluginSourceDirectoryName/${normalizedName.toLowerCase}"
1717

1818
/**
1919
* Creates the plugin folder inside of a plugin source directory
@@ -147,7 +147,20 @@ object Plugin {
147147
pluginSourceFolder.exists() && pluginSourceFolder.isDirectory
148148
}
149149

150-
private def toPluginPathName(name: String) = name.replaceAll("[ -]", "").toLowerCase
150+
private def toNormalizedName(name: String): String = {
151+
if (name.isEmpty) {
152+
return ""
153+
}
154+
155+
val firstChar = name(0)
156+
157+
if (!Character.isJavaIdentifierStart(firstChar)) {
158+
toNormalizedName(name.substring(1))
159+
} else {
160+
val rest = name.substring(1)
161+
firstChar + rest.filter(Character.isJavaIdentifierPart)
162+
}
163+
}
151164

152165
private def containsPluginXMLFile(directory: File): Boolean = {
153166
new File(s"$directory/src/main/resources/plugin.xml").exists()

project/SbtFile.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ class SbtFile(var name: String, var version: String, var plugins: List[Plugin],
5555
val sbtContent = new StringBuilder("// GENERATED FILE USING THE CHAT OVERFLOW PLUGIN FRAMEWORK\n")
5656

5757
if (name != "") {
58-
sbtContent append "\nname := \"%s\"".format(name)
58+
sbtContent append "\nname := \"%s\"".format(name.replaceAll("\\", ""))
5959
}
6060

6161
if (version != "") {
62-
sbtContent append "\nversion := \"%s\"".format(version)
62+
sbtContent append "\nversion := \"%s\"".format(version.replaceAll("\\", ""))
6363
}
6464

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

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

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

8585
if (apiProjectPath != "") {
8686
rootLine += ".dependsOn(apiProject)"

0 commit comments

Comments
 (0)