Skip to content

Commit 3a0b88e

Browse files
committed
ref(FileTreeNode): refactor file tree dsl
1 parent 4e0ee3f commit 3a0b88e

7 files changed

Lines changed: 166 additions & 57 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
5858
Logger.d(TAG, "create dir ${it.getPath()}")
5959
}
6060
} else {
61-
if (treeNode.hasFileTemplate()) {
61+
val template = treeNode.getTemplateFile()
62+
if (template != null) {
6263
val result = kit.createFileFromTemplate(
6364
treeNode.getRealName(),
64-
treeNode.getTemplateFile()!!,
65+
template,
6566
treeNode.getPlaceholderInherit().orEmpty(),
6667
currentDirectory)
6768
if (result == null) {
68-
Logger.e(TAG, "create file from template failed, file: ${treeNode.getRealName()} template:${treeNode.getTemplateFile()}")
69+
Logger.e(TAG, "create file from template failed, file: ${treeNode.getRealName()} template:$template")
6970
} else {
7071
Logger.d(TAG, "create file from template ${treeNode.getRealName()}")
7172
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.dengzii.plugin.template.model
2+
3+
class FileTreeDsl() : FileTreeNode() {
4+
5+
constructor(block: FileTreeDsl.() -> Unit) : this() {
6+
invoke(block)
7+
}
8+
9+
operator fun invoke(block: FileTreeDsl.() -> Unit): FileTreeDsl {
10+
this.block()
11+
return this
12+
}
13+
14+
constructor(parent: FileTreeDsl?, name: String, isDir: Boolean) : this() {
15+
this.name = name
16+
this.parent = parent
17+
this.isDir = isDir
18+
}
19+
20+
/**
21+
* create directory nodes from the path
22+
*
23+
* @param path The dir path
24+
* @param block The child node domain
25+
*/
26+
fun dir(path: String, block: FileTreeDsl.() -> Unit = {}) {
27+
if (!isDir) {
28+
this(block)
29+
return
30+
}
31+
var dirs = when {
32+
path.contains(".") -> path.split(".")
33+
path.contains("/") -> path.split("/")
34+
else -> {
35+
val newNode = FileTreeDsl(this, path, true)
36+
if (addChild(newNode)) {
37+
newNode(block)
38+
}
39+
return
40+
}
41+
}
42+
dirs = dirs
43+
.filter {
44+
it.isNotBlank()
45+
}.toMutableList()
46+
createDirs(dirs, this)(block)
47+
}
48+
49+
fun file(name: String) {
50+
if (!isDir) return
51+
addChild(FileTreeNode(this, name, false))
52+
}
53+
54+
fun fileTemplate(fileName: String, template: String) {
55+
if (this.fileTemplates == null) {
56+
this.fileTemplates = mutableMapOf()
57+
}
58+
fileTemplates!![fileName] = template
59+
}
60+
61+
private fun createDirs(dirs: MutableList<String>, parent: FileTreeDsl): FileTreeNode {
62+
if (dirs.isEmpty()) {
63+
return parent
64+
}
65+
// the first dir
66+
val first = dirs[0]
67+
dirs.removeAt(0)
68+
val firstDir = FileTreeDsl(parent, first, true)
69+
val findChild = getChild(first, true)
70+
if (findChild != null) {
71+
return createDirs(dirs, findChild)
72+
}
73+
addChild(firstDir)
74+
// create child dir
75+
return createDirs(dirs, firstDir)
76+
}
77+
}

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

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.util.*
1414
* time : 2019/1/1
1515
* desc :
1616
</pre> */
17-
open class FileTreeNode private constructor() {
17+
open class FileTreeNode() {
1818

1919
var name: String = ""
2020

@@ -58,21 +58,12 @@ open class FileTreeNode private constructor() {
5858
}
5959
}
6060

61-
constructor(block: FileTreeNode.() -> Unit) : this() {
62-
invoke(block)
63-
}
64-
6561
constructor(parent: FileTreeNode?, name: String, isDir: Boolean) : this() {
6662
this.name = name
6763
this.parent = parent
6864
this.isDir = isDir
6965
}
7066

71-
operator fun invoke(block: FileTreeNode.() -> Unit): FileTreeNode {
72-
this.block()
73-
return this
74-
}
75-
7667
fun removeFromParent(): Boolean {
7768
if (parent != null) {
7869
parent!!.labeledChildren.remove(getLabel())
@@ -110,6 +101,10 @@ open class FileTreeNode private constructor() {
110101
return labeledChildren.containsKey(label)
111102
}
112103

104+
fun getChild(name: String, isDir: Boolean): FileTreeNode? {
105+
return labeledChildren["${name}_$isDir"]
106+
}
107+
113108
/**
114109
* get the real name replace with placeholder
115110
*/
@@ -125,17 +120,6 @@ open class FileTreeNode private constructor() {
125120
return placeholders ?: parent?.getPlaceholderInherit()
126121
}
127122

128-
fun fileTemplate(fileName: String, template: String) {
129-
if (this.fileTemplates == null) {
130-
this.fileTemplates = mutableMapOf()
131-
}
132-
fileTemplates!![fileName] = template
133-
}
134-
135-
fun hasFileTemplate(): Boolean {
136-
return template != null || getFileTemplateInherit()?.containsKey(name) == true
137-
}
138-
139123
fun getTemplateFile(): String? {
140124
return template ?: getFileTemplateInherit()?.get(name)
141125
}
@@ -146,21 +130,21 @@ open class FileTreeNode private constructor() {
146130

147131
fun placeholder(name: String, value: String) {
148132
if (this.placeholders == null) {
149-
this.placeholders = kotlin.collections.mutableMapOf()
133+
this.placeholders = mutableMapOf()
150134
}
151135
placeholders!![name] = value
152136
}
153137

154138
fun placeholders(placeholders: Map<String, String>) {
155139
if (this.placeholders == null) {
156-
this.placeholders = kotlin.collections.mutableMapOf()
140+
this.placeholders = mutableMapOf()
157141
}
158142
this.placeholders!!.putAll(placeholders)
159143
}
160144

161145
fun fileTemplates(placeholders: Map<String, String>) {
162146
if (this.fileTemplates == null) {
163-
this.fileTemplates = kotlin.collections.mutableMapOf()
147+
this.fileTemplates = mutableMapOf()
164148
}
165149
fileTemplates!!.putAll(placeholders)
166150
}
@@ -229,26 +213,14 @@ open class FileTreeNode private constructor() {
229213
}
230214
}
231215

232-
/**
233-
* create directory nodes from the path
234-
*
235-
* @param path The dir path
236-
* @param block The child node domain
237-
*/
238-
fun dir(path: String, block: FileTreeNode.() -> Unit = {}) {
239-
if (!isDir) return
240-
val dirs = path.split("/").filter { it.isNotBlank() }.toMutableList()
241-
createDirs(dirs, this)(block)
242-
}
243-
244216
/**
245217
* create directories tree from a list
246218
* the larger the index, the deeper the directory
247219
*
248220
* @param dirs The dirs list to create tree
249221
* @param parent The parent of current node
250222
*/
251-
private fun createDirs(dirs: MutableList<String>, parent: FileTreeNode): FileTreeNode {
223+
open fun createDirs(dirs: MutableList<String>, parent: FileTreeNode): FileTreeNode {
252224
if (dirs.isEmpty()) {
253225
return parent
254226
}
@@ -259,11 +231,6 @@ open class FileTreeNode private constructor() {
259231
return createDirs(dirs, dirNode)
260232
}
261233

262-
fun file(name: String) {
263-
if (!isDir) return
264-
addChild(FileTreeNode(this, name, false))
265-
}
266-
267234
/**
268235
* get path of current node.
269236
* if the current node is the root node, it will return absolute path,
@@ -318,7 +285,12 @@ open class FileTreeNode private constructor() {
318285
str.append(when (this) {
319286
parent?.children?.last() -> "└─"
320287
parent?.children?.first() -> "├─"
321-
else -> if (parent?.parent != null) "├─" else "┌─"
288+
else -> {
289+
when {
290+
parent != null -> "├─"
291+
else -> ""
292+
}
293+
}
322294
})
323295
str.append(getRealName()).append("\n")
324296

@@ -355,7 +327,7 @@ open class FileTreeNode private constructor() {
355327
}
356328
}
357329

358-
private fun getLabel(): String {
330+
protected fun getLabel(): String {
359331
return "${name}_$isDir"
360332
}
361333

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.dengzii.plugin.template.template
44

5+
import com.dengzii.plugin.template.model.FileTreeDsl
56
import com.dengzii.plugin.template.model.FileTreeNode
67

78
/**
@@ -19,7 +20,7 @@ typealias AucFrame = FileTreeNode
1920

2021
typealias ChildNodeBlock = (parent: Node) -> Unit
2122

22-
typealias Node = FileTreeNode
23+
typealias Node = FileTreeDsl
2324

2425
val Node.test: Node get() = dirNode("test")
2526
val Node.main: Node get() = dirNode("main")

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

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

3+
import com.dengzii.plugin.template.model.FileTreeDsl
34
import com.dengzii.plugin.template.model.FileTreeNode
45

56
/**
@@ -29,7 +30,7 @@ object AucTemplate {
2930
)
3031
}
3132

32-
val APP = FileTreeNode {
33+
val APP = FileTreeDsl {
3334
fileTemplates(aucFileTemplates())
3435
placeholders(aucPlaceholders())
3536
placeholder("MODULE_NAME", "app")
@@ -58,7 +59,7 @@ object AucTemplate {
5859
}
5960
}
6061

61-
val PKG = FileTreeNode {
62+
val PKG = FileTreeDsl {
6263
fileTemplates(aucFileTemplates())
6364
placeholders(aucPlaceholders())
6465
placeholder("MODULE_NAME", "pkg")
@@ -85,7 +86,7 @@ object AucTemplate {
8586
}
8687
}
8788

88-
val EXPORT = FileTreeNode {
89+
val EXPORT = FileTreeDsl {
8990
fileTemplates(aucFileTemplates())
9091
placeholders(aucPlaceholders())
9192
placeholder("MODULE_NAME", "export")
@@ -113,7 +114,7 @@ object AucTemplate {
113114
}
114115
}
115116

116-
val MODULE = FileTreeNode {
117+
val MODULE = FileTreeDsl {
117118
fileTemplates(aucFileTemplates())
118119
placeholders(aucPlaceholders())
119120
feature_name {

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

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

3+
import com.dengzii.plugin.template.model.FileTreeDsl
34
import com.dengzii.plugin.template.model.FileTreeNode
45

56
/**
@@ -13,30 +14,30 @@ import com.dengzii.plugin.template.model.FileTreeNode
1314
*/
1415
object Template {
1516

16-
val ANDROID_RES = FileTreeNode {
17+
val ANDROID_RES = FileTreeDsl {
1718
res {
1819
drawable { }
1920
layout { }
2021
values { }
2122
}
2223
}
23-
val ANDROID_TEST = FileTreeNode {
24+
val ANDROID_TEST = FileTreeDsl {
2425
dir("AndroidTest") {
2526

2627
}
2728
}
2829

29-
val JUNIT_TEST = FileTreeNode {
30+
val JUNIT_TEST = FileTreeDsl {
3031
dir("test") {
3132

3233
}
3334
}
3435

35-
val EMPTY = FileTreeNode {
36+
val EMPTY = FileTreeDsl {
3637
src
3738
}
3839

39-
val ANDROID_APP = FileTreeNode {
40+
val ANDROID_APP = FileTreeDsl {
4041
placeholder("MODULE_NAME", "app")
4142
placeholder("PACKAGE_NAME", "com.example")
4243

0 commit comments

Comments
 (0)