Skip to content

Commit 17613ee

Browse files
committed
修复无法从包含路径分隔符的文件名创建多个目录
1 parent 7ff1da7 commit 17613ee

5 files changed

Lines changed: 85 additions & 52 deletions

File tree

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ class FileTreeDsl() : FileTreeNode() {
1111
return this
1212
}
1313

14-
operator fun FileTreeNode.invoke(block: FileTreeNode.() -> Unit){
14+
operator fun FileTreeNode.invoke(block: FileTreeNode.() -> Unit) {
1515
block(this)
1616
}
1717

1818
/**
1919
* create directory nodes from the path
20+
* if contains '/' or '.' in path
2021
*
2122
* @param path The dir path
2223
* @param block The child node domain
@@ -26,20 +27,21 @@ class FileTreeDsl() : FileTreeNode() {
2627
this(block)
2728
return
2829
}
30+
val realPath = getRealName(path)
2931
var dirs = when {
30-
path.contains(".") -> path.split(".")
31-
path.contains("/") -> path.split("/")
32+
realPath.contains(".") -> realPath.split(".")
33+
realPath.contains("/") -> realPath.split("/")
3234
else -> {
33-
val newNode = FileTreeNode(this, path, true)
35+
val newNode = FileTreeNode(this, realPath, true)
3436
if (addChild(newNode)) {
3537
newNode(block)
3638
}
3739
return
3840
}
3941
}
4042
dirs = dirs.filter {
41-
it.isNotBlank()
42-
}.toMutableList()
43+
it.isNotBlank()
44+
}.toMutableList()
4345
val domain = createDirs(dirs, this)
4446
domain.invoke(block)
4547
}
@@ -49,6 +51,38 @@ class FileTreeDsl() : FileTreeNode() {
4951
addChild(FileTreeNode(this, name, false))
5052
}
5153

54+
fun FileTreeNode.placeholder(name: String, value: String) {
55+
if (this.placeholders == null) {
56+
this.placeholders = mutableMapOf()
57+
}
58+
placeholders!![name] = value
59+
}
60+
61+
/**
62+
* merge all children of another node to this.
63+
* all placeholders and file templates of target node
64+
* will be copied to it's each children
65+
*/
66+
fun FileTreeNode.include(other: FileTreeNode, override: Boolean = false) {
67+
if (!isDir) return
68+
other.children.forEach {
69+
val clone = it.clone()
70+
if (other.placeholders != null) {
71+
if (clone.placeholders == null) {
72+
clone.placeholders = mutableMapOf()
73+
}
74+
clone.placeholders?.putAll(other.placeholders!!)
75+
}
76+
if (other.fileTemplates != null) {
77+
if (clone.fileTemplates == null) {
78+
clone.fileTemplates = mutableMapOf()
79+
}
80+
clone.fileTemplates?.putAll(other.fileTemplates!!)
81+
}
82+
addChild(clone, override)
83+
}
84+
}
85+
5286
fun FileTreeNode.fileTemplate(fileName: String, template: String) {
5387
if (this.fileTemplates == null) {
5488
this.fileTemplates = mutableMapOf()

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

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ open class FileTreeNode() {
111111
* get the real name replace with placeholder
112112
*/
113113
fun getRealName(): String {
114-
return name.replacePlaceholder(getPlaceholderInherit(), !isDir)
114+
return getRealName(this.name)
115+
}
116+
117+
fun getRealName(fileName: String = this.name): String {
118+
119+
return fileName.replacePlaceholder(getPlaceholderInherit(), !isDir)
115120
}
116121

117122
fun getFileTemplateInherit(): MutableMap<String, String>? {
@@ -130,21 +135,21 @@ open class FileTreeNode() {
130135
template = name
131136
}
132137

133-
fun placeholder(name: String, value: String) {
134-
if (this.placeholders == null) {
135-
this.placeholders = mutableMapOf()
136-
}
137-
placeholders!![name] = value
138-
}
139-
140-
fun placeholders(placeholders: Map<String, String>) {
138+
fun addPlaceholders(placeholders: Map<String, String>) {
141139
if (this.placeholders == null) {
142140
this.placeholders = mutableMapOf()
143141
}
144142
this.placeholders!!.putAll(placeholders)
145143
}
146144

147-
fun fileTemplates(placeholders: Map<String, String>) {
145+
// fun getAllPlaceholderInName(): Map<String, String> {
146+
// val result = mutableMapOf<String, String>()
147+
// traversal({ fileTreeNode: FileTreeNode, i: Int ->
148+
//
149+
// })
150+
// }
151+
152+
fun addFileTemplates(placeholders: Map<String, String>) {
148153
if (this.fileTemplates == null) {
149154
this.fileTemplates = mutableMapOf()
150155
}
@@ -190,31 +195,6 @@ open class FileTreeNode() {
190195
}
191196
}
192197

193-
/**
194-
* merge all children of another node to this.
195-
* all placeholders and file templates of target node
196-
* will be copied to it's each children
197-
*/
198-
fun include(other: FileTreeNode, override: Boolean = false) {
199-
if (!isDir) return
200-
other.children.forEach {
201-
val clone = it.clone()
202-
if (other.placeholders != null) {
203-
if (clone.placeholders == null) {
204-
clone.placeholders = mutableMapOf()
205-
}
206-
clone.placeholders?.putAll(other.placeholders!!)
207-
}
208-
if (other.fileTemplates != null) {
209-
if (clone.fileTemplates == null) {
210-
clone.fileTemplates = mutableMapOf()
211-
}
212-
clone.fileTemplates?.putAll(other.fileTemplates!!)
213-
}
214-
addChild(clone, override)
215-
}
216-
}
217-
218198
/**
219199
* create directories tree from a list
220200
* the larger the index, the deeper the directory
@@ -257,6 +237,16 @@ open class FileTreeNode() {
257237
return this == parent
258238
}
259239

240+
fun getAllPlaceholders(): Map<String, String> {
241+
val result = mutableMapOf<String, String>()
242+
traversal({ fileTreeNode: FileTreeNode, _: Int ->
243+
if (fileTreeNode.placeholders != null) {
244+
result.putAll(fileTreeNode.placeholders!!)
245+
}
246+
})
247+
return result
248+
}
249+
260250
fun getTreeGraph(): String {
261251
return getNodeGraph().toString()
262252
}
@@ -335,7 +325,7 @@ open class FileTreeNode() {
335325
}
336326
}
337327

338-
protected fun getLabel(): String {
328+
private fun getLabel(): String {
339329
return "${name}_$isDir"
340330
}
341331

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ object AucTemplate {
3030
}
3131

3232
val APP = FileTreeDsl {
33-
fileTemplates(aucFileTemplates())
34-
placeholders(aucPlaceholders())
33+
addFileTemplates(aucFileTemplates())
34+
addPlaceholders(aucPlaceholders())
3535
placeholder("MODULE_NAME", "app")
3636

3737
dir("app") {
@@ -58,8 +58,8 @@ object AucTemplate {
5858
}
5959

6060
val PKG = FileTreeDsl {
61-
fileTemplates(aucFileTemplates())
62-
placeholders(aucPlaceholders())
61+
addFileTemplates(aucFileTemplates())
62+
addPlaceholders(aucPlaceholders())
6363
placeholder("MODULE_NAME", "pkg")
6464
dir("pkg") {
6565
dir("src") {
@@ -84,8 +84,8 @@ object AucTemplate {
8484
}
8585

8686
val EXPORT = FileTreeDsl {
87-
fileTemplates(aucFileTemplates())
88-
placeholders(aucPlaceholders())
87+
addFileTemplates(aucFileTemplates())
88+
addPlaceholders(aucPlaceholders())
8989
placeholder("MODULE_NAME", "export")
9090
dir("export") {
9191
dir("src") {
@@ -110,8 +110,8 @@ object AucTemplate {
110110
}
111111

112112
val MODULE = FileTreeDsl {
113-
fileTemplates(aucFileTemplates())
114-
placeholders(aucPlaceholders())
113+
addFileTemplates(aucFileTemplates())
114+
addPlaceholders(aucPlaceholders())
115115
dir("\${FEATURE_NAME}") {
116116
include(APP)
117117
include(PKG)

test/AucTemplateTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class AucTemplateTest {
77

88
@Test
99
fun aucAppModuleTest() {
10-
println(AucTemplate.APP.getTreeGraph())
10+
val app = AucTemplate.APP
11+
println(app.getTreeGraph())
12+
1113
}
1214
}

test/FileTreeDslTest.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
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
46
import org.junit.Test
5-
import java.util.regex.Pattern
67

78
class FileTreeDslTest {
89

10+
@Test
11+
fun traversalTest() {
12+
13+
}
14+
915
@Test
1016
fun createSimpleFileTreeTest() {
1117
val tree = FileTreeDsl {
@@ -45,6 +51,7 @@ class FileTreeDslTest {
4551
println(tree.getTreeGraph())
4652
}
4753

54+
4855
@Test
4956
fun fileNamePlaceholderTest() {
5057
val tree = FileTreeDsl {

0 commit comments

Comments
 (0)