Skip to content

Commit 2b87c3b

Browse files
committed
fix: file tree resolve order incorrect
1 parent a97ea89 commit 2b87c3b

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/com/dengzii/plugin/template/FileWriteCommand.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
3838
val fileTreeNode = module.template
3939
Logger.d(TAG, "Placeholders : " + fileTreeNode.getPlaceholderInherit().toString())
4040
Logger.d(TAG, "FileTemplates : " + fileTreeNode.getFileTemplateInherit().toString())
41-
fileTreeNode.expandPath()
42-
fileTreeNode.expandPkgName(true)
43-
fileTreeNode.resolveFileTemplate()
4441

4542
var context: VelocityContext? = null
4643
if (module.enableApacheVelocity) {
@@ -53,6 +50,12 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
5350
}
5451
fileTreeNode.context = context
5552

53+
fileTreeNode.resolveTreeFileName()
54+
fileTreeNode.resolveFileTemplate()
55+
56+
fileTreeNode.expandPath()
57+
fileTreeNode.expandPkgName(true)
58+
5659
val failedList = mutableListOf<FileTreeNode>()
5760
fileTreeNode.children.forEach {
5861
failedList.addAll(createFileTree(context, it, current))

src/com/dengzii/plugin/template/model/FileTreeNode.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ open class FileTreeNode() {
227227
}
228228
}
229229

230+
/**
231+
* Resolve all file name in tree node with apache velocity, and set the resolved name to the node.
232+
*
233+
* @param context The velocity context
234+
*/
235+
fun resolveTreeFileName(context: VelocityContext? = getContextInherit()) {
236+
name = getRealNameInternal(context)
237+
traversal({ it, _ ->
238+
it.resolveTreeFileName(context)
239+
})
240+
}
241+
230242
/**
231243
* Resolve all file template file name in tree node.
232244
*/

test/com/dengzii/plugin/template/model/FileTreeNodeTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.dengzii.plugin.template.model
22

33
import junit.framework.TestCase
4+
import org.apache.velocity.VelocityContext
5+
import org.apache.velocity.util.StringUtils
46
import org.junit.Test
57

68
/**
@@ -82,4 +84,36 @@ class FileTreeNodeTest : TestCase() {
8284
m.template.expandPkgName(true)
8385
println(m.template.getTreeGraph())
8486
}
87+
88+
fun testExpandPkg() {
89+
90+
val dsl = FileTreeDsl {
91+
placeholder("FT", "com_demo")
92+
dir("root") {
93+
dir("\${FT.replaceAll(\"[^\\w]\", \".\")}") {
94+
file("a.txt")
95+
}
96+
dir("\${FT.replaceAll(\"[_]\", \".\")}") {
97+
file("a.txt")
98+
}
99+
file("\${FT}.txt")
100+
}
101+
}
102+
val m = Module.create(dsl, "test")
103+
m.packageNameToDir = true
104+
m.packageNameToDir = true
105+
m.enableApacheVelocity = true
106+
107+
m.template.context = VelocityContext().apply {
108+
put("StringUtils", StringUtils::class.java).apply {
109+
put("FT", "com_demo")
110+
}
111+
}
112+
113+
m.template.resolveTreeFileName()
114+
println(m.template.getTreeGraph())
115+
116+
m.template.expandPkgName(true)
117+
println(m.template.getTreeGraph())
118+
}
85119
}

0 commit comments

Comments
 (0)