@@ -4,6 +4,7 @@ import com.dengzii.plugin.template.template.Placeholder
44import com.dengzii.plugin.template.template.replacePlaceholder
55import com.dengzii.plugin.template.utils.Logger
66import java.io.File
7+ import java.util.*
78
89/* *
910 * <pre>
@@ -22,15 +23,24 @@ open class FileTreeNode private constructor() {
2223 }
2324
2425 var isDir = true
25- val children by lazy { mutableListOf<FileTreeNode >() }
26- var placeHolderMap: MutableMap <Placeholder , String >? = null
26+ var children = mutableListOf<FileTreeNode >()
27+ var placeHolderMap: MutableMap <String , String >? = null
2728 get() = field ? : parent?.placeHolderMap
2829
30+ // template for node, higher priority than fileTemplates
31+ var template: String? = null
32+
33+ // template of filename
34+ var fileTemplates: MutableMap <String , String >? = null
35+ get() = field ? : parent?.fileTemplates
36+
2937 // the origin name with original placeholder
3038 private var realName: String = " "
31- private var parent: FileTreeNode ? = null
39+ @Transient
40+ var parent: FileTreeNode ? = null
3241
3342 companion object {
43+
3444 private val TAG = FileTreeNode ::class .java.simpleName
3545
3646 fun root (path : String ): FileTreeNode {
@@ -60,11 +70,30 @@ open class FileTreeNode private constructor() {
6070 return this
6171 }
6272
73+ fun getRealName (): String {
74+ return realName
75+ }
76+
77+ fun fileTemplate (fileName : String , template : String ) {
78+ if (this .fileTemplates == null ) {
79+ this .fileTemplates = mutableMapOf ()
80+ }
81+ fileTemplates!! [fileName] = template
82+ }
83+
84+ fun hasTemplate (): Boolean {
85+ return template != null || fileTemplates?.containsKey(realName) == true
86+ }
87+
88+ fun getTemplateName (): String? {
89+ return template ? : fileTemplates?.get(realName)
90+ }
91+
6392 fun placeholder (placeholder : Placeholder , value : String ) {
6493 if (this .placeHolderMap == null ) {
6594 this .placeHolderMap = kotlin.collections.mutableMapOf ()
6695 }
67- placeHolderMap!! [placeholder] = value
96+ placeHolderMap!! [placeholder.getPlaceholder() ] = value
6897 }
6998
7099 /* *
@@ -112,16 +141,27 @@ open class FileTreeNode private constructor() {
112141 fun include (other : FileTreeNode ) {
113142 if (! isDir) return
114143 other.children.forEach {
115- it.parent = this
116- children.add(it)
144+ val child = it.clone()
145+ child.parent = this
146+ children.add(child)
117147 }
118148 }
119149
120- fun dir (name : String , block : FileTreeNode .() -> Unit = {}) {
150+ fun dir (path : String , block : FileTreeNode .() -> Unit = {}) {
121151 if (! isDir) return
122- val dir = FileTreeNode (this , name, true )
123- children.add(dir)
124- dir(block)
152+ val dirs = path.split(" /" ).filter { it.isNotBlank() }.toMutableList()
153+ createDirs(dirs, this )(block)
154+ }
155+
156+ private fun createDirs (dirs : MutableList <String >, parent : FileTreeNode ): FileTreeNode {
157+ if (dirs.isEmpty()) {
158+ return parent
159+ }
160+ val current = dirs[0 ]
161+ dirs.removeAt(0 )
162+ val dirNode = FileTreeNode (parent, current, true )
163+ parent.children.add(dirNode)
164+ return createDirs(dirs, dirNode)
125165 }
126166
127167 fun file (name : String ) {
@@ -141,18 +181,50 @@ open class FileTreeNode private constructor() {
141181 return parent!! .getPath() + " /" + name
142182 }
143183
144-
145184 fun isRoot (): Boolean {
146185 return this == parent
147186 }
148187
149188 fun getTreeGraph (): String {
150- val strBuilder = StringBuilder ()
151- traversal({ i, dep ->
152- val head = if (i.isRoot()) " ┌" else if (children.last() == i) " └" else " ├"
153- strBuilder.append(" │ " .repeat(dep) + " $head ─" + i.name)
189+ return getNodeGraph().toString()
190+ }
191+
192+ fun clone (): FileTreeNode {
193+ val cl = FileTreeNode (null , name, isDir)
194+ cl.fileTemplates = fileTemplates?.toMutableMap()
195+ cl.placeHolderMap = placeHolderMap?.toMutableMap()
196+ children.forEach {
197+ val child = it.clone()
198+ child.parent = cl
199+ cl.children.add(child)
200+ }
201+ return cl
202+ }
203+
204+ private fun getNodeGraph (head : Stack <String > = Stack (), str : StringBuilder = StringBuilder ()): StringBuilder {
205+
206+ head.forEach {
207+ str.append(it)
208+ }
209+ str.append(when (this ) {
210+ parent?.children?.last() -> " └─"
211+ parent?.children?.first() -> " ├─"
212+ else -> if (parent?.parent != null ) " ├─" else " ┌─"
154213 })
155- return strBuilder.toString()
214+ str.append(name).append(" \n " )
215+
216+ if (! children.isNullOrEmpty()) {
217+ head.push(when {
218+ parent == null -> " "
219+ parent?.children?.last() != this -> " │\t "
220+ else -> " \t "
221+ })
222+ children.forEach {
223+ str.append(it.getNodeGraph(head))
224+ }
225+ head.pop()
226+ }
227+ return str
156228 }
157229
158230 private fun createChild () {
0 commit comments