@@ -22,15 +22,23 @@ open class FileTreeNode private constructor() {
2222 }
2323
2424 var isDir = true
25- val children by lazy { mutableListOf <FileTreeNode >() }
26- var placeHolderMap: MutableMap <Placeholder , String >? = null
25+ val children by lazy { mutableSetOf <FileTreeNode >() }
26+ var placeHolderMap: MutableMap <String , String >? = null
2727 get() = field ? : parent?.placeHolderMap
2828
29+ // template for node, higher priority than fileTemplates
30+ var template: String? = null
31+
32+ // template of filename
33+ var fileTemplates: MutableMap <String , String >? = null
34+ get() = field ? : parent?.fileTemplates
35+
2936 // the origin name with original placeholder
3037 private var realName: String = " "
3138 private var parent: FileTreeNode ? = null
3239
3340 companion object {
41+
3442 private val TAG = FileTreeNode ::class .java.simpleName
3543
3644 fun root (path : String ): FileTreeNode {
@@ -60,11 +68,30 @@ open class FileTreeNode private constructor() {
6068 return this
6169 }
6270
71+ fun getParent (): FileTreeNode ? {
72+ return parent
73+ }
74+
75+ fun fileTemplate (fileName : String , template : String ) {
76+ if (this .fileTemplates == null ) {
77+ this .fileTemplates = mutableMapOf ()
78+ }
79+ fileTemplates!! [fileName] = template
80+ }
81+
82+ fun hasTemplate (): Boolean {
83+ return template != null || fileTemplates?.containsKey(realName) == true
84+ }
85+
86+ fun getTemplateName (): String? {
87+ return template ? : fileTemplates?.get(realName)
88+ }
89+
6390 fun placeholder (placeholder : Placeholder , value : String ) {
6491 if (this .placeHolderMap == null ) {
6592 this .placeHolderMap = kotlin.collections.mutableMapOf ()
6693 }
67- placeHolderMap!! [placeholder] = value
94+ placeHolderMap!! [placeholder.getPlaceholder() ] = value
6895 }
6996
7097 /* *
@@ -112,16 +139,27 @@ open class FileTreeNode private constructor() {
112139 fun include (other : FileTreeNode ) {
113140 if (! isDir) return
114141 other.children.forEach {
115- it.parent = this
116- children.add(it)
142+ val child = it.clone()
143+ child.parent = this
144+ children.add(child)
117145 }
118146 }
119147
120- fun dir (name : String , block : FileTreeNode .() -> Unit = {}) {
148+ fun dir (path : String , block : FileTreeNode .() -> Unit = {}) {
121149 if (! isDir) return
122- val dir = FileTreeNode (this , name, true )
123- children.add(dir)
124- dir(block)
150+ val dirs = path.split(" /" ).filter { it.isNotBlank() }.toMutableList()
151+ createDirs(dirs, this )(block)
152+ }
153+
154+ private fun createDirs (dirs : MutableList <String >, parent : FileTreeNode ): FileTreeNode {
155+ if (dirs.isEmpty()) {
156+ return parent
157+ }
158+ val current = dirs[0 ]
159+ dirs.removeAt(0 )
160+ val dirNode = FileTreeNode (parent, current, true )
161+ parent.children.add(dirNode)
162+ return createDirs(dirs, dirNode)
125163 }
126164
127165 fun file (name : String ) {
@@ -148,13 +186,45 @@ open class FileTreeNode private constructor() {
148186
149187 fun getTreeGraph (): String {
150188 val strBuilder = StringBuilder ()
189+ var line = 0x1
151190 traversal({ i, dep ->
152191 val head = if (i.isRoot()) " ┌" else if (children.last() == i) " └" else " ├"
153- strBuilder.append(" │ " .repeat(dep) + " $head ─" + i.name)
192+ strBuilder.append(" │ " .repeat(dep) + " $head ─" + i.name).append( " \n " )
154193 })
155194 return strBuilder.toString()
156195 }
157196
197+ fun clone (): FileTreeNode {
198+ val cl = FileTreeNode (null , name, isDir)
199+ cl.fileTemplates = fileTemplates?.toMutableMap()
200+ cl.placeHolderMap = placeHolderMap?.toMutableMap()
201+ children.forEach {
202+ val child = it.clone()
203+ child.parent = cl
204+ cl.children.add(child)
205+ }
206+ return cl
207+ }
208+
209+
210+ private fun getNodeGraph (head : StringBuilder = StringBuilder ()): StringBuilder {
211+ val graph = StringBuilder (head)
212+
213+ graph.append(when (this ) {
214+ parent?.children?.last() -> " └─"
215+ parent?.children?.first() -> " ├─"
216+ else -> " ┌─"
217+ })
218+
219+ graph.append(name).append(" \n " )
220+ if (isDir && ! children.isNullOrEmpty()) {
221+ for (i in 0 until children.size) {
222+
223+ }
224+ }
225+ return graph
226+ }
227+
158228 private fun createChild () {
159229 children.forEach {
160230 val file = File (it.getPath())
0 commit comments