@@ -3,16 +3,21 @@ package com.github.shiraji.permissionsdispatcherplugin.actions
33import com.github.shiraji.permissionsdispatcherplugin.config.GeneratePMCodeConfig
44import com.github.shiraji.permissionsdispatcherplugin.data.PdVersion
55import com.github.shiraji.permissionsdispatcherplugin.data.RebuildType
6+ import com.github.shiraji.permissionsdispatcherplugin.extentions.generateVersionNumberFrom
67import com.github.shiraji.permissionsdispatcherplugin.handlers.GeneratePMCodeHandlerJava
78import com.github.shiraji.permissionsdispatcherplugin.handlers.GeneratePMCodeHandlerKt
89import com.github.shiraji.permissionsdispatcherplugin.models.GeneratePMCodeModel
910import com.github.shiraji.permissionsdispatcherplugin.views.GeneratePMCodeDialog
1011import com.intellij.codeInsight.CodeInsightActionHandler
1112import com.intellij.codeInsight.actions.CodeInsightAction
1213import com.intellij.compiler.actions.CompileProjectAction
14+ import com.intellij.notification.Notification
15+ import com.intellij.notification.NotificationType
16+ import com.intellij.notification.Notifications
1317import com.intellij.openapi.actionSystem.AnActionEvent
1418import com.intellij.openapi.actionSystem.CommonDataKeys
1519import com.intellij.psi.PsiJavaFile
20+ import com.intellij.psi.PsiManager
1621import com.intellij.psi.search.FilenameIndex
1722import com.intellij.psi.search.GlobalSearchScope
1823import org.jetbrains.kotlin.psi.KtFile
@@ -52,51 +57,64 @@ class GeneratePMCodeAction : CodeInsightAction() {
5257 val project = e?.getData(CommonDataKeys .PROJECT ) ? : return
5358 isKotlin = e?.getData(CommonDataKeys .PSI_FILE ) is KtFile
5459
55- var pdVersion: PdVersion = PdVersion .UNKNOWN
60+ var pdVersion: PdVersion = PdVersion .NOTFOUND
5661
57- // I think it should not be filter build.gradle. Find all .gradle files.
58- FilenameIndex .getFilesByName(project, " build.gradle" , GlobalSearchScope .projectScope(project)).forEach {
59- if (it is GroovyFile ) {
60- val dependenciesBlock = it.findDescendantOfType<GrMethodCallExpression > {
61- it.invokedExpression.text == " dependencies"
62- }
63-
64- val pdLine = dependenciesBlock?.findDescendantOfType<GrCommandArgumentList > {
65- it.text.contains(" com.github.hotchemi:permissionsdispatcher:" )
66- }
62+ fun updatePdVersion (dependenciesBlock : GrMethodCallExpression ) {
63+ val pdLine = dependenciesBlock.findDescendantOfType<GrCommandArgumentList > {
64+ it.text.contains(" com.github.hotchemi:permissionsdispatcher:" )
65+ }
6766
68- pdLine?.text?.let {
69- text ->
70- // for now, forget about variables...
71- val versionText = text.substring(text.lastIndexOf(" :" ) + 1 ).replace(" \' " , " " ).replace(" \" " , " " )
72- pdVersion = PdVersion .fromText(versionText)
73- return @forEach
74- }
67+ pdLine?.text?.let {
68+ text ->
69+ // for now, forget about variables...
70+ val versionText = text.generateVersionNumberFrom()
71+ pdVersion = PdVersion .fromText(versionText)
7572 }
7673 }
7774
78- val dialog = GeneratePMCodeDialog (project, pdVersion)
79- if (dialog.showAndGet()) {
80- model = GeneratePMCodeModel (project)
81-
82- model.apply {
83- permissions = dialog.selectedPermissions
84- if (dialog.needsPermissionCheckBox.isSelected) needsPermissionMethodName = dialog.needsPermissionTextField.text
85- if (dialog.onShowRationaleCheckBox.isSelected) onShowRationaleMethodName = dialog.onShowRationaleTextField.text
86- if (dialog.onPermissionDeniedCheckBox.isSelected) onPermissionDeniedMethodName = dialog.onPermissionDeniedTextField.text
87- if (dialog.onNeverAskAgainCheckBox.isSelected) onNeverAskAgainMethodName = dialog.onNeverAskAgainTextField.text
88- val maxSdkVersionText = dialog.maxSdkVersionTextField.text
89- if (maxSdkVersionText != null && maxSdkVersionText.isNotBlank()) {
90- maxSdkVersion = maxSdkVersionText.toInt()
75+ fun generatePMCode () {
76+ val dialog = GeneratePMCodeDialog (project, pdVersion)
77+ if (dialog.showAndGet()) {
78+ model = GeneratePMCodeModel (project)
79+
80+ model.apply {
81+ permissions = dialog.selectedPermissions
82+ if (dialog.needsPermissionCheckBox.isSelected) needsPermissionMethodName = dialog.needsPermissionTextField.text
83+ if (dialog.onShowRationaleCheckBox.isSelected) onShowRationaleMethodName = dialog.onShowRationaleTextField.text
84+ if (dialog.onPermissionDeniedCheckBox.isSelected) onPermissionDeniedMethodName = dialog.onPermissionDeniedTextField.text
85+ if (dialog.onNeverAskAgainCheckBox.isSelected) onNeverAskAgainMethodName = dialog.onNeverAskAgainTextField.text
86+ val maxSdkVersionText = dialog.maxSdkVersionTextField.text
87+ if (maxSdkVersionText != null && maxSdkVersionText.isNotBlank()) {
88+ maxSdkVersion = maxSdkVersionText.toInt()
89+ }
9190 }
91+
92+ super .actionPerformed(e)
93+ rebuildAction(e)
9294 }
95+ }
9396
94- super .actionPerformed(e)
95- afterActionPerformed(e)
97+ FilenameIndex .getAllFilesByExt(project, " gradle" , GlobalSearchScope .projectScope(project)).forEach {
98+ val groovyFile = PsiManager .getInstance(project).findFile(it) as ? GroovyFile ? : return @forEach
99+ val dependenciesBlock = groovyFile.findDescendantOfType<GrMethodCallExpression > {
100+ it.invokedExpression.text == " dependencies"
101+ } ? : return @forEach
102+ updatePdVersion(dependenciesBlock)
103+ }
104+
105+ if (pdVersion == PdVersion .NOTFOUND ) {
106+ // no dependencies found for PermissionsDispatcher!
107+ Notifications .Bus .notify(Notification (
108+ " PermissionsManager Plugin" ,
109+ " No PermissionsDispatcher dependency found" ,
110+ " Please add PermissionsDispatcher dependency" ,
111+ NotificationType .WARNING ))
112+ } else {
113+ generatePMCode()
96114 }
97115 }
98116
99- private fun afterActionPerformed (e : AnActionEvent ? ) {
117+ private fun rebuildAction (e : AnActionEvent ? ) {
100118 when (RebuildType .fromId(GeneratePMCodeConfig .rebuildTypeId)) {
101119 RebuildType .ALWAYS -> rebuild(e)
102120 RebuildType .PROMPT -> {
0 commit comments