Skip to content

Commit 1a4078c

Browse files
author
dengzi
committed
fix can not create file from template
1 parent 62e3aae commit 1a4078c

18 files changed

Lines changed: 85 additions & 128 deletions

resources/META-INF/plugin.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
<idea-plugin>
22
<id>com.dengzii.plugin.template</id>
33
<name>Template Module Generator</name>
4-
<version>1.0</version>
5-
<vendor email="dengzii@foxmail.com" url="https://github.com/dengzii">denzi</vendor>
4+
<version>1.1</version>
5+
<vendor email="dengzii@foxmail.com" url="https://github.com/dengzii">dengzi</vendor>
66

77
<description><![CDATA[
88
<b>Create a diverse directory structure from a highly customizable template</b><br>
99
<br>
10-
The AndroidStudio only a limited number of module categories can be created<br>
1110
Using this plugin, help you create directories and files from customizable template<br>
1211
<br>
12+
<b>Feature</b><br>
13+
1. custom directory structure<br>
14+
2. support placeholders, and replace it when you create module.<br>
15+
3. create file from template<br>
16+
4. support file template variables<br>
17+
<br>
1318
]]></description>
1419

1520
<change-notes><![CDATA[
21+
1.1: feature: support create module template, placeholder, file template
1622
1.0: basically feature, generate module directories from template
1723
]]>
1824
</change-notes>

resources/fileTemplates/Template AndroidManifest.xml.ft

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
package="${PACKAGE_NAME}">
44

55
<application
6-
android:name="${PACKAGE_NAME}.App"
6+
android:name=".${APPLICATION_NAME}"
77
android:allowBackup="false"
88
android:icon="@mipmap/ic_launcher"
99
android:label="@string/app_name"
1010
android:theme="@style/AppTheme">
11-
<activity android:name="${PACKAGE_NAME}.MainActivity">
11+
<activity android:name=".MainActivity">
1212
<intent-filter>
1313
<action android:name="android.intent.action.VIEW" />
1414
<action android:name="android.intent.action.MAIN" />

resources/fileTemplates/Template AndroidManifest.xml.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

resources/fileTemplates/Template App.java.ft

Lines changed: 0 additions & 10 deletions
This file was deleted.

resources/fileTemplates/Template Application.java.ft

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
package ${PACKAGE_NAME};
1+
package ${PACKAGE_NAME}.${FEATURE_NAME}.${MODULE_NAME};
22

3-
import ${BASE_APPLICATION};
4-
5-
public class ${CLASS_NAME} extends BaseApplication{
3+
public class ${APPLICATION_NAME} extends BaseApplication{
64

75
public void onCreate(){
86

resources/fileTemplates/Template Application.java.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

resources/fileTemplates/Template Application.kt.ft

Lines changed: 0 additions & 10 deletions
This file was deleted.

resources/fileTemplates/Template Application.kt.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

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

33
import com.dengzii.plugin.template.model.FileTreeNode
44
import com.dengzii.plugin.template.model.Module
5-
import com.dengzii.plugin.template.template.Template
65
import com.dengzii.plugin.template.utils.Logger
76
import com.google.gson.GsonBuilder
87
import com.intellij.ide.util.PropertiesComponent
@@ -26,7 +25,7 @@ object Config {
2625
private val GSON by lazy { GsonBuilder().setLenient().create() }
2726
private val STORE by lazy { PropertiesComponent.getInstance() }
2827

29-
fun clear() = STORE.unsetValue(KEY_TEMPLATES)
28+
private fun clear() = STORE.unsetValue(KEY_TEMPLATES)
3029

3130
fun loadModuleTemplates(): MutableList<Module> {
3231
val result = mutableListOf<Module>()
@@ -43,7 +42,7 @@ object Config {
4342
return result
4443
}
4544
Logger.i(TAG, "loadModuleTemplates")
46-
arr.forEach {
45+
arr.filter { !it.isNullOrBlank() }.forEach {
4746
try {
4847
val module = GSON.fromJson(it, Module::class.java)
4948
setParent(module.template)
@@ -59,6 +58,10 @@ object Config {
5958
}
6059

6160
fun saveModuleTemplates(templates: List<Module>) {
61+
if (templates.isEmpty()) {
62+
clear()
63+
return
64+
}
6265
val t = mutableListOf<String>()
6366
templates.forEach {
6467
t.add(GSON.toJson(it))

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
3838
return
3939
}
4040
val fileTreeNode = module.template
41-
Logger.d(TAG, fileTreeNode.getPlaceholderInherit().toString())
41+
Logger.d(TAG, "Placeholders : " + fileTreeNode.getPlaceholderInherit().toString())
42+
Logger.d(TAG, "FileTemplates : " + fileTreeNode.getFileTemplateInherit().toString())
4243
fileTreeNode.children.forEach {
4344
createFileTree(it, current)
4445
}
4546
}
4647

4748
private fun createFileTree(treeNode: FileTreeNode, currentDirectory: VirtualFile) {
48-
Logger.i(TAG, "Create ${treeNode.getPath()}")
49+
Logger.d(TAG, "create node $treeNode")
4950
if (treeNode.isDir) {
5051
val dir = kit.createDir(treeNode.getRealName(), currentDirectory)
5152
if (dir == null) {
@@ -54,19 +55,23 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
5455
}
5556
treeNode.children.forEach {
5657
createFileTree(it, dir)
58+
Logger.d(TAG, "create dir ${it.getPath()}")
5759
}
5860
} else {
5961
if (treeNode.hasFileTemplate()) {
6062
val result = kit.createFileFromTemplate(
6163
treeNode.getRealName(),
62-
treeNode.getTemplateName()!!,
64+
treeNode.getTemplateFile()!!,
6365
treeNode.getPlaceholderInherit().orEmpty(),
6466
currentDirectory)
6567
if (result == null) {
66-
Logger.e(TAG, "create file from template failed, file: ${treeNode.getRealName()} template:${treeNode.getTemplateName()}")
68+
Logger.e(TAG, "create file from template failed, file: ${treeNode.getRealName()} template:${treeNode.getTemplateFile()}")
69+
} else {
70+
Logger.d(TAG, "create file from template ${treeNode.getRealName()}")
6771
}
6872
} else {
6973
kit.createFile(treeNode.getRealName(), currentDirectory)
74+
Logger.d(TAG, "create file ${treeNode.getPath()}")
7075
}
7176
}
7277
}

0 commit comments

Comments
 (0)