Skip to content

Commit 4373958

Browse files
committed
新增获取占位符列表
1 parent 17613ee commit 4373958

File tree

4 files changed

+64
-30
lines changed

4 files changed

+64
-30
lines changed

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.dengzii.plugin.template.model
22

3+
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
4+
35
class FileTreeDsl() : FileTreeNode() {
46

57
constructor(block: FileTreeDsl.() -> Unit) : this() {
@@ -27,27 +29,37 @@ class FileTreeDsl() : FileTreeNode() {
2729
this(block)
2830
return
2931
}
30-
val realPath = getRealName(path)
31-
var dirs = when {
32-
realPath.contains(".") -> realPath.split(".")
33-
realPath.contains("/") -> realPath.split("/")
34-
else -> {
35-
val newNode = FileTreeNode(this, realPath, true)
36-
if (addChild(newNode)) {
37-
newNode(block)
38-
}
39-
return
40-
}
32+
path.getPlaceholder().ifNotEmpty {
33+
allPlaceholder.addAll(this)
4134
}
42-
dirs = dirs.filter {
43-
it.isNotBlank()
44-
}.toMutableList()
45-
val domain = createDirs(dirs, this)
46-
domain.invoke(block)
35+
// val realPath = getRealName(path)
36+
// var dirs = when {
37+
// realPath.contains(".") -> realPath.split(".")
38+
// realPath.contains("/") -> realPath.split("/")
39+
// else -> {
40+
// val newNode = FileTreeNode(this, realPath, true)
41+
// if (addChild(newNode)) {
42+
// newNode(block)
43+
// }
44+
// return
45+
// }
46+
// }
47+
// dirs = dirs.filter {
48+
// it.isNotBlank()
49+
// }.toMutableList()
50+
val newNode = FileTreeNode(this, path, true)
51+
if (addChild(newNode)) {
52+
newNode.invoke(block)
53+
}
54+
// val domain = createDirs(dirs, this)
55+
// domain.invoke(block)
4756
}
4857

4958
fun FileTreeNode.file(name: String) {
5059
if (!isDir) return
60+
name.getPlaceholder().ifNotEmpty {
61+
allPlaceholder.addAll(this)
62+
}
5163
addChild(FileTreeNode(this, name, false))
5264
}
5365

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package com.dengzii.plugin.template.model
44

55
import com.dengzii.plugin.template.utils.Logger
6+
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
67
import java.io.File
78
import java.util.*
89
import 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()) {

test/AucTemplateTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class AucTemplateTest {
88
@Test
99
fun aucAppModuleTest() {
1010
val app = AucTemplate.APP
11+
app {
12+
placeholder("PACKAGE_NAME", "com.dengzii.plugin")
13+
}
14+
println(app.placeholders)
15+
println(app.getAllPlaceholderInTree())
1116
println(app.getTreeGraph())
1217

1318
}

test/FileTreeDslTest.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
package test
22

33
import com.dengzii.plugin.template.model.FileTreeDsl
4-
import com.dengzii.plugin.template.model.FileTreeNode
5-
import com.dengzii.plugin.template.template.Template
64
import org.junit.Test
75

86
class FileTreeDslTest {
97

10-
@Test
11-
fun traversalTest() {
12-
13-
}
14-
158
@Test
169
fun createSimpleFileTreeTest() {
1710
val tree = FileTreeDsl {

0 commit comments

Comments
 (0)