@@ -9,6 +9,8 @@ import com.intellij.openapi.command.UndoConfirmationPolicy
99import com.intellij.openapi.command.WriteCommandAction
1010import com.intellij.openapi.vfs.VirtualFile
1111import com.intellij.util.ThrowableRunnable
12+ import org.apache.velocity.VelocityContext
13+ import org.apache.velocity.util.StringUtils
1214
1315/* *
1416 * Create file tree.
@@ -21,9 +23,9 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
2123 private val TAG = FileWriteCommand ::class .java.simpleName
2224 fun startAction (kit : PluginKit , module : Module ) {
2325 WriteCommandAction .writeCommandAction(kit.project)
24- .withGlobalUndo()
25- .withUndoConfirmationPolicy(UndoConfirmationPolicy .REQUEST_CONFIRMATION )
26- .run (FileWriteCommand (kit, module))
26+ .withGlobalUndo()
27+ .withUndoConfirmationPolicy(UndoConfirmationPolicy .REQUEST_CONFIRMATION )
28+ .run (FileWriteCommand (kit, module))
2729 }
2830 }
2931
@@ -39,46 +41,64 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
3941 fileTreeNode.expandPath()
4042 fileTreeNode.expandPkgName(true )
4143
44+ var context: VelocityContext ? = null
45+ if (module.enableApacheVelocity) {
46+ context = VelocityContext ().apply {
47+ put(" StringUtils" , StringUtils ::class .java)
48+ fileTreeNode.getPlaceholderInherit()?.forEach { (k, v) ->
49+ put(k, v)
50+ }
51+ }
52+ }
53+
4254 val failedList = mutableListOf<FileTreeNode >()
4355 fileTreeNode.children.forEach {
44- failedList.addAll(createFileTree(it, current))
56+ failedList.addAll(createFileTree(context, it, current))
4557 }
4658 if (failedList.isNotEmpty()) {
4759 val msg = failedList.joinToString(" \n " ) { it.getRealName() }
48- NotificationUtils .showError(msg, " The following file creation failed." )
60+ NotificationUtils .showError(msg, " The following files creation failed." )
4961 }
5062 }
5163
52- private fun createFileTree (treeNode : FileTreeNode , currentDirectory : VirtualFile ): List <FileTreeNode > {
64+ private fun createFileTree (
65+ context : VelocityContext ? ,
66+ treeNode : FileTreeNode ,
67+ currentDirectory : VirtualFile
68+ ): List <FileTreeNode > {
5369 Logger .d(TAG , " create node $treeNode " )
5470 val failedList = mutableListOf<FileTreeNode >()
5571 if (treeNode.isDir) {
5672 Logger .d(TAG , " create dir ${treeNode.getPath()} " )
57- val dir = kit.createDir(treeNode.getRealName(), currentDirectory)
73+ val dir = kit.createDir(treeNode.getRealName(context ), currentDirectory)
5874 if (dir == null ) {
5975 failedList.add(treeNode)
60- Logger .e(TAG , " create directory failure: ${treeNode.getRealName()} " )
76+ Logger .e(TAG , " create directory failure: ${treeNode.getRealName(context )} " )
6177 } else {
6278 treeNode.children.forEach {
63- failedList.addAll(createFileTree(it, dir))
79+ failedList.addAll(createFileTree(context, it, dir))
6480 }
6581 }
6682 } else {
6783 val template = treeNode.getTemplateFile()
6884 if (template?.isNotBlank() == true ) {
6985 val result = kit.createFileFromTemplate(
70- treeNode.getRealName(),
71- template,
72- treeNode.getPlaceholderInherit().orEmpty(),
73- currentDirectory)
86+ treeNode.getRealName(context),
87+ template,
88+ treeNode.getPlaceholderInherit().orEmpty(),
89+ currentDirectory
90+ )
7491 if (result == null || ! result.isValid) {
7592 failedList.add(treeNode)
76- Logger .e(TAG , " create file from template failed, file: ${treeNode.getRealName()} template:$template " )
93+ Logger .e(
94+ TAG ,
95+ " create file from template failed, file: ${treeNode.getRealName(context)} template:$template "
96+ )
7797 } else {
78- Logger .d(TAG , " create file from template ${treeNode.getRealName()} " )
98+ Logger .d(TAG , " create file from template ${treeNode.getRealName(context )} " )
7999 }
80100 } else {
81- if (! kit.createFile(treeNode.getRealName(), currentDirectory)) {
101+ if (! kit.createFile(treeNode.getRealName(context ), currentDirectory)) {
82102 failedList.add(treeNode)
83103 }
84104 Logger .d(TAG , " create file ${treeNode.getPath()} " )
0 commit comments