Skip to content

Commit 53914f7

Browse files
committed
Refactor code to reduce verbosity and improve readability in various files
1 parent a750f72 commit 53914f7

8 files changed

Lines changed: 39 additions & 109 deletions

File tree

src/main/kotlin/com/github/cnrture/quickprojectwizard/common/Utils.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,12 @@ object Utils {
303303
return null
304304
}
305305

306-
// Check and add TYPESAFE_PROJECT_ACCESSORS if missing
307306
try {
308307
val content = settingsFile.readText()
309308
if (!content.contains("TYPESAFE_PROJECT_ACCESSORS")) {
310309
val lines = content.lines().toMutableList()
311310
var insertIndex = -1
312311

313-
// Find the line with rootProject.name
314312
for (i in lines.indices) {
315313
if (lines[i].trim().startsWith("rootProject.name")) {
316314
insertIndex = i + 1

src/main/kotlin/com/github/cnrture/quickprojectwizard/common/file/FileWriter.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ class FileWriter() {
128128
resDir.mkdirs()
129129
createdDirs.add(resDir)
130130

131-
val subDirs = listOf(
132-
"drawable",
133-
"values",
134-
)
131+
val subDirs = listOf("drawable", "values")
135132

136133
subDirs.forEach { dirName ->
137134
val dir = Paths.get(resDir.absolutePath, dirName).toFile()

src/main/kotlin/com/github/cnrture/quickprojectwizard/common/file/ImportAnalyzer.kt

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,53 @@ class ImportAnalyzer {
99

1010
fun analyzeSourceDirectory(directory: File): List<String> {
1111
val requiredModules = mutableListOf<String>()
12-
val sourceFiles = findAllSourceFiles(directory)
13-
14-
sourceFiles.forEach { file ->
12+
findAllSourceFiles(directory).forEach { file ->
1513
val imports = extractImports(file)
1614
val modules = mapImportsToModules(imports)
1715
requiredModules.addAll(modules)
1816
}
19-
2017
return requiredModules.distinct()
2118
}
2219

2320
private fun findAllSourceFiles(directory: File): List<File> {
2421
val sourceFiles = mutableListOf<File>()
25-
2622
directory.walkTopDown().forEach { file ->
27-
if (file.isFile && (file.extension == "kt" || file.extension == "java")) {
28-
sourceFiles.add(file)
29-
}
23+
if (file.isFile && (file.extension == "kt" || file.extension == "java")) sourceFiles.add(file)
3024
}
31-
3225
return sourceFiles
3326
}
3427

3528
private fun extractImports(file: File): List<String> {
3629
val imports = mutableListOf<String>()
37-
3830
try {
3931
file.readLines().forEach { line ->
4032
val trimmedLine = line.trim()
4133
if (trimmedLine.startsWith("import ")) {
42-
val importPath = trimmedLine.removePrefix("import ").removeSuffix(";").trim()
43-
imports.add(importPath)
34+
imports.add(trimmedLine.removePrefix("import ").removeSuffix(";").trim())
4435
}
4536
}
4637
} catch (e: Exception) {
4738
e.printStackTrace()
4839
}
49-
5040
return imports
5141
}
5242

5343
private fun mapImportsToModules(imports: List<String>): List<String> {
5444
val modules = mutableListOf<String>()
55-
5645
imports.forEach { importPath ->
5746
modulePackageMapping.forEach { (module, packages) ->
58-
packages.forEach { packagePrefix ->
59-
if (importPath.startsWith(packagePrefix)) {
60-
modules.add(module)
61-
}
62-
}
47+
packages.forEach { if (importPath.startsWith(it)) modules.add(module) }
6348
}
6449
}
65-
6650
return modules.distinct()
6751
}
6852

6953
fun discoverProjectModules(projectRoot: File) {
7054
val moduleMap = mutableMapOf<String, MutableList<String>>()
71-
72-
val gradleFiles = findGradleFiles(projectRoot)
73-
74-
gradleFiles.forEach { gradleFile ->
55+
findGradleFiles(projectRoot).forEach { gradleFile ->
7556
val modulePath = getModulePath(projectRoot, gradleFile.parentFile)
7657
val packageNames = findPackageNames(gradleFile.parentFile)
77-
78-
if (modulePath.isNotEmpty() && packageNames.isNotEmpty()) {
79-
moduleMap[modulePath] = packageNames
80-
}
58+
if (modulePath.isNotEmpty() && packageNames.isNotEmpty()) moduleMap[modulePath] = packageNames
8159
}
8260

8361
modulePackageMapping.clear()
@@ -86,18 +64,15 @@ class ImportAnalyzer {
8664

8765
private fun findGradleFiles(root: File): List<File> {
8866
val gradleFiles = mutableListOf<File>()
89-
9067
root.walkTopDown()
9168
.filter { it.isFile && (it.name == "build.gradle" || it.name == "build.gradle.kts") }
9269
.forEach { gradleFiles.add(it) }
93-
9470
return gradleFiles
9571
}
9672

9773
private fun getModulePath(projectRoot: File, moduleDir: File): String {
9874
val relativePath = moduleDir.relativeTo(projectRoot).path
9975
if (relativePath.isEmpty()) return Constants.EMPTY
100-
10176
return ":${relativePath.replace(File.separator, ":")}"
10277
}
10378

@@ -107,7 +82,6 @@ class ImportAnalyzer {
10782
File(moduleDir, "src/main/java"),
10883
File(moduleDir, "src/main/kotlin")
10984
)
110-
11185
sourceRoots.filter { it.exists() }.forEach { srcRoot ->
11286
srcRoot.walkTopDown()
11387
.filter { it.isDirectory }
@@ -118,13 +92,10 @@ class ImportAnalyzer {
11892

11993
if (hasSourceFiles) {
12094
val packagePath = dir.relativeTo(srcRoot).path.replace(File.separator, ".")
121-
if (packagePath.isNotEmpty()) {
122-
packageNames.add(packagePath)
123-
}
95+
if (packagePath.isNotEmpty()) packageNames.add(packagePath)
12496
}
12597
}
12698
}
127-
12899
return packageNames
129100
}
130101
}

src/main/kotlin/com/github/cnrture/quickprojectwizard/common/file/LibraryDependencyFinder.kt

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
11
package com.github.cnrture.quickprojectwizard.common.file
22

33
import com.github.cnrture.quickprojectwizard.common.Constants
4+
import com.github.cnrture.quickprojectwizard.data.LibraryInfo
5+
import com.github.cnrture.quickprojectwizard.data.PluginInfo
46
import java.io.File
57

68
class LibraryDependencyFinder {
79

8-
data class LibraryInfo(
9-
val alias: String,
10-
val group: String,
11-
val artifact: String,
12-
val versionRef: String? = null,
13-
val version: String? = null,
14-
)
15-
16-
data class PluginInfo(
17-
val alias: String,
18-
val id: String,
19-
val versionRef: String? = null,
20-
val version: String? = null,
21-
)
22-
2310
fun parseLibsVersionsToml(projectRoot: File): List<LibraryInfo> {
2411
val libraries = mutableListOf<LibraryInfo>()
2512

2613
val versionsTomlPath = "gradle/libs.versions.toml"
2714
var versionsToml = File(projectRoot, versionsTomlPath)
2815

29-
if (!versionsToml.exists()) {
30-
versionsToml = File(projectRoot.parentFile, versionsTomlPath)
31-
}
32-
33-
if (!versionsToml.exists()) {
34-
versionsToml = File(projectRoot, "libs.versions.toml")
35-
}
36-
37-
if (!versionsToml.exists()) {
38-
return emptyList()
39-
}
16+
if (!versionsToml.exists()) versionsToml = File(projectRoot.parentFile, versionsTomlPath)
17+
if (!versionsToml.exists()) versionsToml = File(projectRoot, "libs.versions.toml")
18+
if (!versionsToml.exists()) return emptyList()
4019

4120
val content = versionsToml.readText()
4221

@@ -59,7 +38,6 @@ class LibraryDependencyFinder {
5938
val artifact = matchGroups[3]
6039
val versionRef = if (matchGroups.size > 4) matchGroups[4] else Constants.EMPTY
6140
val version = if (matchGroups.size > 5) matchGroups[5] else Constants.EMPTY
62-
6341
libraries.add(LibraryInfo(alias, group, artifact, versionRef.ifEmpty { null }, version.ifEmpty { null }))
6442
}
6543

@@ -71,16 +49,7 @@ class LibraryDependencyFinder {
7149
val artifact = matchGroups[3]
7250
val versionRef = if (matchGroups.size > 4) matchGroups[4] else Constants.EMPTY
7351
val version = if (matchGroups.size > 5) matchGroups[5] else Constants.EMPTY
74-
75-
libraries.add(
76-
LibraryInfo(
77-
alias,
78-
group,
79-
artifact,
80-
versionRef.ifEmpty { null },
81-
version.ifEmpty { null }
82-
)
83-
)
52+
libraries.add(LibraryInfo(alias, group, artifact, versionRef.ifEmpty { null }, version.ifEmpty { null }))
8453
}
8554

8655
return libraries
@@ -92,17 +61,9 @@ class LibraryDependencyFinder {
9261
val versionsTomlPath = "gradle/libs.versions.toml"
9362
var versionsToml = File(projectRoot, versionsTomlPath)
9463

95-
if (!versionsToml.exists()) {
96-
versionsToml = File(projectRoot.parentFile, versionsTomlPath)
97-
}
98-
99-
if (!versionsToml.exists()) {
100-
versionsToml = File(projectRoot, "libs.versions.toml")
101-
}
102-
103-
if (!versionsToml.exists()) {
104-
return emptyList()
105-
}
64+
if (!versionsToml.exists()) versionsToml = File(projectRoot.parentFile, versionsTomlPath)
65+
if (!versionsToml.exists()) versionsToml = File(projectRoot, "libs.versions.toml")
66+
if (!versionsToml.exists()) return emptyList()
10667

10768
val content = versionsToml.readText()
10869

@@ -121,7 +82,6 @@ class LibraryDependencyFinder {
12182
val id = matchGroups[2]
12283
val versionRef = if (matchGroups.size > 3 && matchGroups[3].isNotEmpty()) matchGroups[3] else null
12384
val version = if (matchGroups.size > 4 && matchGroups[4].isNotEmpty()) matchGroups[4] else null
124-
12585
plugins.add(PluginInfo(alias, id, versionRef, version))
12686
}
12787

@@ -130,10 +90,8 @@ class LibraryDependencyFinder {
13090

13191
fun formatLibraryDependencies(libraryAliases: List<String>): String {
13292
if (libraryAliases.isEmpty()) return Constants.EMPTY
133-
13493
val bomLibraries = setOf("compose-bom", "firebase", "firebase-bom")
13594
val annotationProcessors = setOf("room-compiler", "hilt-compiler")
136-
13795
return StringBuilder().apply {
13896
append(" // Library Dependencies\n")
13997
libraryAliases.forEachIndexed { index, alias ->
@@ -151,7 +109,6 @@ class LibraryDependencyFinder {
151109

152110
fun formatPluginDependencies(pluginAliases: List<String>): String {
153111
if (pluginAliases.isEmpty()) return Constants.EMPTY
154-
155112
return StringBuilder().apply {
156113
pluginAliases.forEachIndexed { index, alias ->
157114
append(" alias(libs.plugins.${alias.replace("-", ".")})")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.cnrture.quickprojectwizard.data
2+
3+
data class LibraryInfo(
4+
val alias: String,
5+
val group: String,
6+
val artifact: String,
7+
val versionRef: String? = null,
8+
val version: String? = null,
9+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.cnrture.quickprojectwizard.data
2+
3+
data class PluginInfo(
4+
val alias: String,
5+
val id: String,
6+
val versionRef: String? = null,
7+
val version: String? = null,
8+
)

src/main/kotlin/com/github/cnrture/quickprojectwizard/toolwindow/manager/settings/dialog/ExportSettingsDialog.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ private fun ExportSettingsContent(
7878

7979
Spacer(modifier = Modifier.height(24.dp))
8080

81-
// Directory Selection
8281
Column {
8382
QPWText(
8483
text = "Export Location",
@@ -123,7 +122,6 @@ private fun ExportSettingsContent(
123122

124123
Spacer(modifier = Modifier.height(24.dp))
125124

126-
// File Name Input
127125
Column {
128126
QPWText(
129127
text = "File Name",
@@ -155,7 +153,6 @@ private fun ExportSettingsContent(
155153

156154
Spacer(modifier = Modifier.weight(1f))
157155

158-
// Action Buttons
159156
Row(
160157
modifier = Modifier.fillMaxWidth(),
161158
horizontalArrangement = Arrangement.spacedBy(12.dp, Alignment.End)

src/main/kotlin/com/github/cnrture/quickprojectwizard/toolwindow/template/TemplateWriter.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ class TemplateWriter {
2525
pluginDependencies: String = Constants.EMPTY,
2626
): List<File> {
2727
try {
28-
val gradleTemplate = when (moduleType) {
29-
Constants.ANDROID -> GradleTemplate.getAndroidModuleGradleTemplate(
30-
packageName = packageName,
31-
dependencies = buildDependenciesBlock(dependencies, libraryDependencies),
32-
plugins = pluginDependencies,
33-
)
34-
35-
else -> GradleTemplate.getKotlinModuleGradleTemplate(plugins = pluginDependencies)
28+
val gradleTemplate = if (moduleType == Constants.ANDROID) {
29+
val dependencies = buildDependenciesBlock(dependencies, libraryDependencies)
30+
GradleTemplate.getAndroidModuleGradleTemplate(packageName, dependencies, pluginDependencies)
31+
} else {
32+
GradleTemplate.getKotlinModuleGradleTemplate(plugins = pluginDependencies)
3633
}
3734

3835
val fileName = "build.gradle.kts"
@@ -92,8 +89,4 @@ class TemplateWriter {
9289
}
9390
return emptyList()
9491
}
95-
96-
companion object {
97-
98-
}
9992
}

0 commit comments

Comments
 (0)