33package com.dengzii.plugin.template.model
44
55import com.dengzii.plugin.template.utils.Logger
6+ import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
67import java.io.File
78import java.util.*
89import java.util.regex.Pattern
@@ -28,6 +29,8 @@ open class FileTreeNode() {
2829 get() = realChildren
2930
3031 var placeholders: MutableMap <String , String >? = null
32+ // all placeholder in tree node name
33+ val allPlaceholder by lazy { mutableListOf<String >() }
3134
3235 // template for node, higher priority than fileTemplates
3336 private var template: String? = null
@@ -47,6 +50,7 @@ open class FileTreeNode() {
4750
4851 private val TAG = FileTreeNode ::class .java.simpleName
4952 private val sPathSplitPattern = Pattern .compile(" [./]" )
53+ private val sPlaceholderPattern = Pattern .compile(" \\ $\\ {([A-Za-z0-9_\\ -]+)}" )
5054
5155 fun root (path : String ): FileTreeNode {
5256 val root = File (path)
@@ -64,6 +68,9 @@ open class FileTreeNode() {
6468 this .name = name
6569 this .parent = parent
6670 this .isDir = isDir
71+ name.getPlaceholder().ifNotEmpty {
72+ allPlaceholder.addAll(this )
73+ }
6774 }
6875
6976 fun removeFromParent (): Boolean {
@@ -142,12 +149,6 @@ open class FileTreeNode() {
142149 this .placeholders!! .putAll(placeholders)
143150 }
144151
145- // fun getAllPlaceholderInName(): Map<String, String> {
146- // val result = mutableMapOf<String, String>()
147- // traversal({ fileTreeNode: FileTreeNode, i: Int ->
148- //
149- // })
150- // }
151152
152153 fun addFileTemplates (placeholders : Map <String , String >) {
153154 if (this .fileTemplates == null ) {
@@ -237,8 +238,22 @@ open class FileTreeNode() {
237238 return this == parent
238239 }
239240
240- fun getAllPlaceholders (): Map <String , String > {
241+ fun build () {
242+
243+ }
244+
245+ fun getAllPlaceholderInTree (): List <String > {
246+ val result = mutableListOf<String >()
247+ result.addAll(allPlaceholder)
248+ traversal({ fileTreeNode: FileTreeNode , _: Int ->
249+ result.addAll(fileTreeNode.allPlaceholder)
250+ })
251+ return result
252+ }
253+
254+ fun getAllPlaceholdersMap (): Map <String , String > {
241255 val result = mutableMapOf<String , String >()
256+ result.putAll(placeholders.orEmpty())
242257 traversal({ fileTreeNode: FileTreeNode , _: Int ->
243258 if (fileTreeNode.placeholders != null ) {
244259 result.putAll(fileTreeNode.placeholders!! )
@@ -333,6 +348,15 @@ open class FileTreeNode() {
333348 return " FileTreeNode(path='${getPath()} ' isDir=$isDir , fileTemplate=${getTemplateFile()} , children=${children.size} )"
334349 }
335350
351+ protected fun String.getPlaceholder (): List <String > {
352+ val result = mutableListOf<String >()
353+ val nameMatcher = sPlaceholderPattern.matcher(this )
354+ while (nameMatcher.find()) {
355+ result.add(nameMatcher.group(1 ))
356+ }
357+ return result
358+ }
359+
336360 private fun String.replacePlaceholder (placeholders : Map <String , String >? , capitalize : Boolean = false): String {
337361 var after = this
338362 if (placeholders.isNullOrEmpty()) {
0 commit comments