Skip to content

Commit 80d3559

Browse files
author
dengzi
committed
add template
1 parent 93917f3 commit 80d3559

14 files changed

Lines changed: 239 additions & 56 deletions

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<actions>
3131
<!-- Add your actions here -->
3232
<action id="com.dengzii.plugin.template.gen" class="com.dengzii.plugin.template.CreateModuleAction"
33-
text="Template Module Generator"
33+
text="Create Module From Template"
3434
description="Create module from template">
3535
<add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
3636
</action>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ${PACKAGE_NAME};
2+
3+
import ${BASE_APPLICATION};
4+
5+
public class ${CLASS_NAME} extends BaseApplication{
6+
7+
public void onCreate(){
8+
9+
}
10+
}

resources/fileTemplates/Template Application.java.ft

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package ${PACKAGE_NAME};
22

33
import ${BASE_APPLICATION};
44

5-
public class ${APPLICATION_NAME} extends BaseApplication{
5+
public class ${CLASS_NAME} extends BaseApplication{
66

77
public void onCreate(){
88

resources/fileTemplates/Template Application.kt.ft

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package ${PACKAGE_NAME}
22

33
import ${BASE_APPLICATION}
44

5-
public class App extends BaseApplication{
5+
class ${CLASS_NAME}:BaseApplication(){
66

7-
public void onCreate(){
7+
fun onCreate(){
88

99
}
1010
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies{
2+
3+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object Config {
1818

1919
private const val KEY_TEMPLATES = "KEY_TEMPLATES"
2020

21-
val DEFAULT_TEMPLATE = listOf(
21+
val DEFAULT_MODULE_TEMPLATE = listOf(
2222
ModuleConfig.create(AucTemplate.MODULE, "feature", "com.example.feature", "Java", "Auc Feature Module"),
2323
ModuleConfig.create(AucTemplate.APP, "app", "com.example.feature", "Java", "Auc App Module"),
2424
ModuleConfig.create(AucTemplate.PKG, "pkg", "com.example.feature", "Java", "Auc Pkg Module"),
@@ -30,7 +30,7 @@ object Config {
3030
}
3131

3232
fun loadTemplates(): List<ModuleConfig> {
33-
return DEFAULT_TEMPLATE
33+
return DEFAULT_MODULE_TEMPLATE
3434
}
3535

3636
fun saveTemplates() {

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,30 @@ class FileWriteCommand(private var kit: PluginKit, private var moduleConfig: Mod
4444
}
4545
}
4646

47-
private fun createFileTree(treeNode: FileTreeNode, current: VirtualFile?) {
48-
if (current == null) {
49-
Logger.e(TAG, "The parent of ${treeNode.getPath()} is null")
50-
return
51-
}
47+
private fun createFileTree(treeNode: FileTreeNode, currentDirectory: VirtualFile) {
5248
Logger.i(TAG, "Create ${treeNode.getPath()}")
5349
if (treeNode.isDir) {
54-
kit.createDir(treeNode.name, current)
50+
val dir = kit.createDir(treeNode.name, currentDirectory)
51+
if (dir == null) {
52+
Logger.e(TAG, "create directory failure: ${treeNode.name}")
53+
return
54+
}
5555
treeNode.children.forEach {
56-
createFileTree(it, current.findChild(treeNode.name))
56+
createFileTree(it, dir)
5757
}
5858
} else {
59-
kit.createFile(treeNode.name, current)
59+
if (treeNode.hasTemplate()) {
60+
val result = kit.createFileFromTemplate(
61+
treeNode.name,
62+
treeNode.getTemplateName()!!,
63+
treeNode.placeHolderMap.orEmpty(),
64+
currentDirectory)
65+
if (result == null) {
66+
Logger.e(TAG, "create file from template failed, file: ${treeNode.name} template:${treeNode.getTemplateName()}")
67+
}
68+
} else {
69+
kit.createFile(treeNode.name, currentDirectory)
70+
}
6071
}
6172
}
6273
}

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

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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())

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ val Node.gitignore: FileNode get() = fileNode(".gitignore")
4242
val Node.build: FileNode get() = fileNode("build")
4343
val Node.app_name: FileNode get() = fileNode(Placeholder.APPLICATION_NAME.getPlaceholder())
4444

45-
val FileNode.gradle: Unit get() = nodeSuffix(".gradle")
46-
val FileNode.java: Unit get() = nodeSuffix(".java")
47-
val FileNode.pro: Unit get() = nodeSuffix(".pro")
48-
val FileNode.xml: Unit get() = nodeSuffix(".xml")
49-
val FileNode.kt: Unit get() = nodeSuffix(".kt")
45+
val FileNode.gradle: FileNode get() = nodeSuffix(".gradle")
46+
val FileNode.java: FileNode get() = nodeSuffix(".java")
47+
val FileNode.pro: FileNode get() = nodeSuffix(".pro")
48+
val FileNode.xml: FileNode get() = nodeSuffix(".xml")
49+
val FileNode.kt: FileNode get() = nodeSuffix(".kt")
5050

5151
private fun Node.dirNode(name: String): Node {
5252
val dirNode = Node(this, name, true)
@@ -60,6 +60,7 @@ private fun Node.fileNode(name: String): FileNode {
6060
return node
6161
}
6262

63-
private fun FileNode.nodeSuffix(suffix: String) {
63+
private fun FileNode.nodeSuffix(suffix: String): FileNode {
6464
this.name = this.name + suffix
65+
return this
6566
}

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

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

3+
import com.dengzii.plugin.template.model.FileTreeNode
4+
35
/**
46
* <pre>
57
* author : dengzi
@@ -8,6 +10,27 @@ package com.dengzii.plugin.template.template
810
* time : 2019/1/1
911
* desc :
1012
</pre> */
13+
fun main() {
14+
val temp = AucFrame {
15+
dir("src/main/") {
16+
dir("java") {
17+
dir("com/example"){
18+
dir("foo"){
19+
file("bar.txt")
20+
}
21+
dir("app/c")
22+
}
23+
}
24+
dir("res") {
25+
dir("img/png"){
26+
27+
}
28+
}
29+
}
30+
build.gradle
31+
}
32+
println(temp.getTreeGraph())
33+
}
1134

1235
object AucTemplate {
1336

@@ -19,9 +42,32 @@ object AucTemplate {
1942
}
2043
}
2144

22-
val APP = AucFrame {
45+
private val aucFileTemplates: () -> MutableMap<String, String> = {
46+
mutableMapOf(
47+
Pair("AndroidManifest.xml", "Template AndroidManifest.xml"),
48+
Pair("Application.java", "Template App.java"),
49+
Pair("build.gradle", "Template build.gradle"),
50+
Pair("${Placeholder.APPLICATION_NAME.getPlaceholder()}.java", "Template App.java")
51+
)
52+
}
53+
54+
private val BASE = AucFrame {
55+
56+
fileTemplates = aucFileTemplates()
57+
placeholder(Placeholder.PACKAGE_NAME, "com.example")
2358
placeholder(Placeholder.MODULE_NAME, "app")
59+
placeholder(Placeholder.APPLICATION_NAME, "App")
60+
61+
gitignore
62+
build.gradle
63+
proguard_rules.pro
64+
}
65+
66+
val APP = (BASE.clone()) {
67+
fileTemplates = aucFileTemplates()
2468
placeholder(Placeholder.PACKAGE_NAME, "com.example")
69+
placeholder(Placeholder.MODULE_NAME, "app")
70+
2571
app {
2672
src {
2773
main {
@@ -39,15 +85,11 @@ object AucTemplate {
3985
}
4086
test {}
4187
}
42-
gitignore
43-
build.gradle
44-
proguard_rules.pro
4588
}
4689
}
4790

48-
val PKG = AucFrame {
91+
val PKG = (BASE.clone()) {
4992
placeholder(Placeholder.MODULE_NAME, "pkg")
50-
placeholder(Placeholder.PACKAGE_NAME, "com.example")
5193
pkg {
5294
src {
5395
main {
@@ -66,28 +108,29 @@ object AucTemplate {
66108
AndroidManifest.xml
67109
}
68110
}
69-
gitignore
70-
build.gradle
71-
proguard_rules.pro
72111
}
73112
}
74113

75114
val EXPORT = AucFrame {
76115
placeholder(Placeholder.MODULE_NAME, "export")
77-
placeholder(Placeholder.PACKAGE_NAME, "com.example")
78116
export {
79-
src { main {
80-
java { pkg_name { module_name { export {
117+
src {
118+
main {
119+
java {
120+
pkg_name {
121+
module_name {
122+
export {
81123
dir("api") {
82124
file("\${MODULE_NAME}Api.java")
83125
}
84126
dir("bean")
85-
} } } }
127+
}
128+
}
129+
}
130+
}
86131
AndroidManifest.xml
87-
} }
88-
gitignore
89-
build.gradle
90-
proguard_rules.pro
132+
}
133+
}
91134
}
92135
}
93136

0 commit comments

Comments
 (0)