@@ -18,7 +18,6 @@ import com.intellij.openapi.project.modules
18
18
import com.intellij.openapi.projectRoots.JavaSdkVersion
19
19
import com.intellij.openapi.roots.ProjectRootManager
20
20
import com.intellij.openapi.util.Disposer
21
- import com.intellij.openapi.vfs.VfsUtil
22
21
import com.intellij.openapi.vfs.VirtualFile
23
22
import com.intellij.openapi.wm.ToolWindowManager
24
23
import kotlinx.coroutines.Job
@@ -44,6 +43,7 @@ import software.aws.toolkits.jetbrains.services.codemodernizer.model.CodeModerni
44
43
import software.aws.toolkits.jetbrains.services.codemodernizer.model.CustomerSelection
45
44
import software.aws.toolkits.jetbrains.services.codemodernizer.model.InvalidTelemetryReason
46
45
import software.aws.toolkits.jetbrains.services.codemodernizer.model.JobId
46
+ import software.aws.toolkits.jetbrains.services.codemodernizer.model.MAVEN_CONFIGURATION_FILE_NAME
47
47
import software.aws.toolkits.jetbrains.services.codemodernizer.model.ValidationResult
48
48
import software.aws.toolkits.jetbrains.services.codemodernizer.panels.managers.CodeModernizerBottomWindowPanelManager
49
49
import software.aws.toolkits.jetbrains.services.codemodernizer.state.CodeModernizerSessionState
@@ -69,6 +69,7 @@ import software.aws.toolkits.telemetry.CodeTransformPreValidationError
69
69
import software.aws.toolkits.telemetry.CodeTransformStartSrcComponents
70
70
import software.aws.toolkits.telemetry.CodetransformTelemetry
71
71
import software.aws.toolkits.telemetry.Result
72
+ import java.io.File
72
73
import java.time.Instant
73
74
import java.util.concurrent.atomic.AtomicBoolean
74
75
import javax.swing.Icon
@@ -88,7 +89,7 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
88
89
Disposer .register(contentManager, it)
89
90
}
90
91
}
91
- private val supportedBuildFileNames = listOf (" pom.xml " )
92
+ private val supportedBuildFileNames = listOf (MAVEN_CONFIGURATION_FILE_NAME )
92
93
private val supportedJavaMappings = mapOf (
93
94
JavaSdkVersion .JDK_1_8 to setOf (JavaSdkVersion .JDK_17 ),
94
95
JavaSdkVersion .JDK_11 to setOf (JavaSdkVersion .JDK_17 ),
@@ -152,9 +153,9 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
152
153
)
153
154
)
154
155
}
155
- val valid = getSupportedBuildFilesInProject().isNotEmpty ()
156
- return if (valid ) {
157
- ValidationResult (true )
156
+ val validatedBuildFiles = getSupportedBuildFilesInProject()
157
+ return if (validatedBuildFiles.isNotEmpty() ) {
158
+ ValidationResult (true , validatedBuildFiles = validatedBuildFiles )
158
159
} else {
159
160
ValidationResult (
160
161
false ,
@@ -208,7 +209,7 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
208
209
val validationResult = validate(project)
209
210
runInEdt {
210
211
if (validationResult.valid) {
211
- runModernize()
212
+ runModernize(validationResult.validatedBuildFiles) ? : isModernizationInProgress.set( false )
212
213
} else {
213
214
warnUnsupportedProject(validationResult.invalidReason)
214
215
isModernizationInProgress.set(false )
@@ -245,9 +246,9 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
245
246
}
246
247
}
247
248
248
- fun runModernize (): Job ? {
249
+ fun runModernize (validatedBuildFiles : List < VirtualFile > ): Job ? {
249
250
initStopParameters()
250
- val customerSelection = getCustomerSelection() ? : return null
251
+ val customerSelection = getCustomerSelection(validatedBuildFiles ) ? : return null
251
252
CodetransformTelemetry .jobStartedCompleteFromPopupDialog(
252
253
codeTransformJavaSourceVersionsAllowed = CodeTransformJavaSourceVersionsAllowed .from(customerSelection.sourceJavaVersion.name),
253
254
codeTransformJavaTargetVersionsAllowed = CodeTransformJavaTargetVersionsAllowed .from(customerSelection.targetJavaVersion.name),
@@ -267,9 +268,9 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
267
268
CodeModernizerSessionState .getInstance(project).currentJobStopTime = Instant .MIN
268
269
}
269
270
270
- fun getCustomerSelection (): CustomerSelection ? = PreCodeTransformUserDialog (
271
+ fun getCustomerSelection (validatedBuildFiles : List < VirtualFile > ): CustomerSelection ? = PreCodeTransformUserDialog (
271
272
project,
272
- getSupportedBuildFilesInProject() ,
273
+ validatedBuildFiles ,
273
274
supportedJavaMappings,
274
275
).create()
275
276
@@ -638,6 +639,29 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
638
639
managerState.flags.putAll(state.flags)
639
640
}
640
641
642
+ private fun findBuildFiles (sourceFolder : File ): List <File > {
643
+ /* *
644
+ * For every dir, check if any supported build files (pom.xml etc) exists.
645
+ * If found store it and stop further recursion.
646
+ */
647
+ val buildFiles = mutableListOf<File >()
648
+ sourceFolder.walkTopDown()
649
+ .maxDepth(5 )
650
+ .onEnter { currentDir ->
651
+ supportedBuildFileNames.forEach {
652
+ val maybeSupportedFile = currentDir.resolve(MAVEN_CONFIGURATION_FILE_NAME )
653
+ if (maybeSupportedFile.exists()) {
654
+ buildFiles.add(maybeSupportedFile)
655
+ return @onEnter false
656
+ }
657
+ }
658
+ return @onEnter true
659
+ }.forEach {
660
+ // noop, collects the sequence
661
+ }
662
+ return buildFiles
663
+ }
664
+
641
665
private fun getSupportedBuildFilesInProject (): List <VirtualFile > {
642
666
/* *
643
667
* Strategy:
@@ -648,15 +672,14 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
648
672
val probableProjectRoot = project.basePath?.toVirtualFile() // May point to only one intellij module (the first opened one)
649
673
val probableContentRoots = projectRootManager.contentRoots.toMutableSet() // May not point to the topmost folder of modules
650
674
probableContentRoots.add(probableProjectRoot) // dedupe
651
- val detectedBuildFiles = probableContentRoots.filterNotNull().flatMap { root ->
652
- VfsUtil .collectChildrenRecursively(root).filter { it.name in supportedBuildFileNames }
675
+ val topLevelRoots = filterOnlyParentFiles(probableContentRoots)
676
+ val detectedBuildFiles = topLevelRoots.flatMap { root ->
677
+ findBuildFiles(root.toNioPath().toFile()).mapNotNull { it.path.toVirtualFile() }
653
678
}
654
679
655
- val topLevelBuildFiles = filterOnlyParentFiles(detectedBuildFiles)
656
-
657
680
val supportedModules = getSupportedModulesInProject().toSet()
658
681
val validProjectJdk = project.getSupportedJavaMappingsForProject(supportedJavaMappings).isNotEmpty()
659
- return topLevelBuildFiles .filter {
682
+ return detectedBuildFiles .filter {
660
683
val moduleOfFile = runReadAction { projectRootManager.fileIndex.getModuleForFile(it) }
661
684
return @filter (moduleOfFile in supportedModules) || (moduleOfFile == null && validProjectJdk)
662
685
}
0 commit comments