|
| 1 | +package ru.hh.plugins.geminio |
| 2 | + |
| 3 | +import com.intellij.openapi.actionSystem.ActionManager |
| 4 | +import com.intellij.openapi.actionSystem.AnAction |
| 5 | +import com.intellij.openapi.actionSystem.DefaultActionGroup |
| 6 | +import com.intellij.openapi.project.Project |
| 7 | +import ru.hh.plugins.extensions.toUnderlines |
| 8 | +import ru.hh.plugins.geminio.GeminioConstants.GEMINIO_TEMPLATE_CONFIG_FILE_NAME |
| 9 | +import ru.hh.plugins.geminio.actions.RescanTemplatesAction |
| 10 | +import ru.hh.plugins.geminio.actions.SetupGeminioConfigAction |
| 11 | +import ru.hh.plugins.geminio.actions.module_template.ExecuteGeminioModuleTemplateAction |
| 12 | +import ru.hh.plugins.geminio.actions.template.ExecuteGeminioTemplateAction |
| 13 | +import ru.hh.plugins.geminio.config.GeminioPluginConfig |
| 14 | +import ru.hh.plugins.geminio.config.editor.GeminioPluginSettings |
| 15 | +import ru.hh.plugins.geminio.services.balloonError |
| 16 | +import java.io.File |
| 17 | + |
| 18 | +internal class ActionsHelper { |
| 19 | + |
| 20 | + private companion object { |
| 21 | + const val BASE_ID = "ru.hh.plugins.geminio.actions." |
| 22 | + const val NEW_GROUP_ID_SUFFIX = "NewGroup." |
| 23 | + const val GENERATE_GROUP_ID_SUFFIX = "GenerateGroup." |
| 24 | + |
| 25 | + const val LOG_DIVIDER = "============" |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + fun createGeminioActions(project: Project) { |
| 30 | + val pluginConfig = GeminioPluginSettings.getConfig(project) |
| 31 | + val pathToConfig = pluginConfig.configFilePath |
| 32 | + val pathToTemplates = project.basePath + pluginConfig.templatesRootDirPath |
| 33 | + val pathToModulesTemplates = project.basePath + pluginConfig.modulesTemplatesRootDirPath |
| 34 | + |
| 35 | + println("\tpathToConfig: $pathToConfig") |
| 36 | + println("\tpathToTemplates: $pathToTemplates") |
| 37 | + println("\tpathToModulesTemplates: $pathToModulesTemplates") |
| 38 | + println(LOG_DIVIDER) |
| 39 | + println(LOG_DIVIDER) |
| 40 | + |
| 41 | + resetGeminioActions(project) |
| 42 | + |
| 43 | + if (project.isConfigNotValid(pathToConfig, pathToTemplates, pathToModulesTemplates)) { |
| 44 | + val error = "Geminio's config is not valid (may be not configured at all) -> no need to create actions" |
| 45 | + project.balloonError(message = error, action = SetupGeminioConfigAction()) |
| 46 | + println("\t$error") |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + createActionsForTemplates( |
| 51 | + project = project, |
| 52 | + pluginConfig = pluginConfig, |
| 53 | + rootDirPath = pathToTemplates, |
| 54 | + isModulesTemplates = false, |
| 55 | + ) |
| 56 | + |
| 57 | + createActionsForTemplates( |
| 58 | + project = project, |
| 59 | + pluginConfig = pluginConfig, |
| 60 | + rootDirPath = pathToModulesTemplates, |
| 61 | + isModulesTemplates = true, |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + fun resetGeminioActions(project: Project) { |
| 66 | + val actionManager = ActionManager.getInstance() |
| 67 | + val pluginConfig = GeminioPluginSettings.getConfig(project) |
| 68 | + actionManager.removeAllActionsFromGroups(pluginConfig, isModulesTemplates = true) |
| 69 | + actionManager.removeAllActionsFromGroups(pluginConfig, isModulesTemplates = false) |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + private fun createActionsForTemplates( |
| 74 | + project: Project, |
| 75 | + pluginConfig: GeminioPluginConfig, |
| 76 | + rootDirPath: String, |
| 77 | + isModulesTemplates: Boolean, |
| 78 | + ) { |
| 79 | + val actionManager = ActionManager.getInstance() |
| 80 | + val bundle = getTemplateActionsBundle(pluginConfig, isModulesTemplates) |
| 81 | + val hhNewGroup = actionManager.getAction(bundle.templatesNewGroupId) as DefaultActionGroup |
| 82 | + hhNewGroup.templatePresentation.text = bundle.templatesNewGroupName |
| 83 | + |
| 84 | + val hhGenerateGroup = actionManager.getAction(bundle.templatesGenerateGroupId) as DefaultActionGroup |
| 85 | + val rootDirectory = File(rootDirPath) |
| 86 | + |
| 87 | + if (rootDirectory.exists().not() || rootDirectory.isDirectory.not()) { |
| 88 | + println("Templates directory doesn't exists [path: $rootDirPath, isModulesTemplates: $isModulesTemplates]") |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + println("\tTemplates directory exists [path: $rootDirPath, isModulesTemplates: $isModulesTemplates]") |
| 93 | + val templatesDirs = rootDirectory.getSubfolderNamesWithRecipes() |
| 94 | + |
| 95 | + println("\tTemplates count: ${templatesDirs.size}") |
| 96 | + println(LOG_DIVIDER) |
| 97 | + |
| 98 | + actionManager.addTemplatesActions( |
| 99 | + projectName = project.name, |
| 100 | + templatesDirs = templatesDirs, |
| 101 | + rootDirPath = rootDirPath, |
| 102 | + isModulesTemplates = isModulesTemplates, |
| 103 | + groups = Groups(hhNewGroup = hhNewGroup, hhGenerateGroup = hhGenerateGroup), |
| 104 | + ) |
| 105 | + |
| 106 | + actionManager.addRescanActions( |
| 107 | + projectName = project.name, |
| 108 | + groups = Groups(hhNewGroup = hhNewGroup, hhGenerateGroup = hhGenerateGroup), |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + private fun ActionManager.removeAllActionsFromGroups( |
| 113 | + pluginConfig: GeminioPluginConfig, |
| 114 | + isModulesTemplates: Boolean, |
| 115 | + ) { |
| 116 | + val bundle = getTemplateActionsBundle(pluginConfig, isModulesTemplates) |
| 117 | + val hhNewGroup = getAction(bundle.templatesNewGroupId) as DefaultActionGroup |
| 118 | + val hhGenerateGroup = getAction(bundle.templatesGenerateGroupId) as DefaultActionGroup |
| 119 | + |
| 120 | + hhNewGroup.removeAll() |
| 121 | + hhGenerateGroup.removeAll() |
| 122 | + } |
| 123 | + |
| 124 | + private fun createActionForTemplate( |
| 125 | + templatesRootDirPath: String, |
| 126 | + templateDirName: String, |
| 127 | + isModulesTemplates: Boolean, |
| 128 | + actionManager: ActionManager, |
| 129 | + actionId: String, |
| 130 | + ): AnAction { |
| 131 | + val action = when (isModulesTemplates) { |
| 132 | + true -> { |
| 133 | + ExecuteGeminioModuleTemplateAction( |
| 134 | + actionText = templateDirName, |
| 135 | + actionDescription = "Action for executing '$templateDirName'", |
| 136 | + geminioRecipePath = getGeminioRecipeFilePath(templatesRootDirPath, templateDirName), |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + false -> { |
| 141 | + ExecuteGeminioTemplateAction( |
| 142 | + actionText = templateDirName, |
| 143 | + actionDescription = "Action for executing '$templateDirName'", |
| 144 | + geminioRecipePath = getGeminioRecipeFilePath(templatesRootDirPath, templateDirName), |
| 145 | + ) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + with(actionManager) { |
| 150 | + getActionOrStub(actionId) |
| 151 | + ?.also { replaceAction(actionId, action) } |
| 152 | + ?: registerAction(actionId, action) |
| 153 | + } |
| 154 | + |
| 155 | + return action |
| 156 | + } |
| 157 | + |
| 158 | + private fun getGeminioRecipeFilePath(templatesRootDirPath: String, templateDirName: String): String { |
| 159 | + return "$templatesRootDirPath/$templateDirName/${GeminioConstants.GEMINIO_TEMPLATE_CONFIG_FILE_NAME}" |
| 160 | + } |
| 161 | + |
| 162 | + private fun getTemplateActionsBundle( |
| 163 | + pluginConfig: GeminioPluginConfig, |
| 164 | + isModulesTemplates: Boolean, |
| 165 | + ): TemplateActionsBundle = when (isModulesTemplates) { |
| 166 | + true -> { |
| 167 | + TemplateActionsBundle( |
| 168 | + templatesNewGroupId = GeminioConstants.HH_MODULES_TEMPLATES_NEW_GROUP_ID, |
| 169 | + templatesGenerateGroupId = GeminioConstants.HH_MODULES_TEMPLATES_GENERATE_GROUP_ID, |
| 170 | + templatesNewGroupName = pluginConfig.groupsNames.forNewModulesGroup, |
| 171 | + ) |
| 172 | + } |
| 173 | + |
| 174 | + false -> { |
| 175 | + TemplateActionsBundle( |
| 176 | + templatesNewGroupId = GeminioConstants.HH_TEMPLATES_NEW_GROUP_ID, |
| 177 | + templatesGenerateGroupId = GeminioConstants.HH_TEMPLATES_GENERATE_GROUP_ID, |
| 178 | + templatesNewGroupName = pluginConfig.groupsNames.forNewGroup, |
| 179 | + ) |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private fun ActionManager.addTemplatesActions( |
| 184 | + projectName: String, |
| 185 | + templatesDirs: List<String>, |
| 186 | + rootDirPath: String, |
| 187 | + isModulesTemplates: Boolean, |
| 188 | + groups: Groups, |
| 189 | + ) { |
| 190 | + templatesDirs.forEach { templateName -> |
| 191 | + createActionForTemplate( |
| 192 | + templatesRootDirPath = rootDirPath, |
| 193 | + templateDirName = templateName, |
| 194 | + isModulesTemplates = isModulesTemplates, |
| 195 | + actionManager = this, |
| 196 | + actionId = BASE_ID + NEW_GROUP_ID_SUFFIX + "$projectName." + templateName.toUnderlines(), |
| 197 | + ).also(groups.hhNewGroup::add) |
| 198 | + |
| 199 | + createActionForTemplate( |
| 200 | + templatesRootDirPath = rootDirPath, |
| 201 | + templateDirName = templateName, |
| 202 | + isModulesTemplates = isModulesTemplates, |
| 203 | + actionManager = this, |
| 204 | + actionId = BASE_ID + GENERATE_GROUP_ID_SUFFIX + "$projectName." + templateName.toUnderlines(), |
| 205 | + ).also(groups.hhGenerateGroup::add) |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + private fun ActionManager.addRescanActions( |
| 210 | + projectName: String, |
| 211 | + groups: Groups, |
| 212 | + ) { |
| 213 | + val actionId = "$BASE_ID$projectName.RescanActionId" |
| 214 | + val rescanTemplatesAction = RescanTemplatesAction() |
| 215 | + |
| 216 | + getActionOrStub(actionId) |
| 217 | + ?.also { replaceAction(actionId, rescanTemplatesAction) } |
| 218 | + ?: registerAction(actionId, rescanTemplatesAction) |
| 219 | + |
| 220 | + groups.hhNewGroup.add(rescanTemplatesAction) |
| 221 | + groups.hhGenerateGroup.add(rescanTemplatesAction) |
| 222 | + } |
| 223 | + |
| 224 | + private fun File.getSubfolderNamesWithRecipes(): List<String> { |
| 225 | + return listFiles { file, _ -> file.isDirectory } |
| 226 | + ?.filter { file -> |
| 227 | + file.listFiles { _, name -> name == GEMINIO_TEMPLATE_CONFIG_FILE_NAME }.isNullOrEmpty().not() |
| 228 | + } |
| 229 | + ?.map { it.name } |
| 230 | + ?: emptyList() |
| 231 | + } |
| 232 | + |
| 233 | + private fun Project.isConfigNotValid( |
| 234 | + pathToConfig: String, |
| 235 | + pathToTemplates: String, |
| 236 | + pathToModulesTemplates: String, |
| 237 | + ): Boolean { |
| 238 | + return pathToConfig.isBlank() || pathToTemplates == basePath || pathToModulesTemplates == basePath |
| 239 | + } |
| 240 | + |
| 241 | + |
| 242 | + private data class TemplateActionsBundle( |
| 243 | + val templatesNewGroupId: String, |
| 244 | + val templatesGenerateGroupId: String, |
| 245 | + val templatesNewGroupName: String, |
| 246 | + ) |
| 247 | + |
| 248 | + private data class Groups( |
| 249 | + val hhNewGroup: DefaultActionGroup, |
| 250 | + val hhGenerateGroup: DefaultActionGroup, |
| 251 | + ) |
| 252 | + |
| 253 | +} |
0 commit comments