Skip to content

Commit 8f04540

Browse files
authored
Merge pull request #292 from domaframework/fix/generate-sql-file-path-from-source-set
Enable SQL File Generation in Multi-Module Projects
2 parents cb8f87f + 89de62c commit 8f04540

File tree

14 files changed

+208
-209
lines changed

14 files changed

+208
-209
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/CommonPathParameter.kt

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.domaframework.doma.intellij.common
1717

18+
import com.intellij.compiler.CompilerConfiguration
1819
import com.intellij.openapi.module.Module
20+
import com.intellij.openapi.project.Project
1921
import com.intellij.openapi.roots.ModuleRootManager
2022
import com.intellij.openapi.vfs.VirtualFile
2123
import org.jetbrains.jps.model.java.JavaResourceRootType
@@ -32,14 +34,12 @@ object CommonPathParameterUtil {
3234
/**
3335
* Holds directory information for a module.
3436
*
35-
* @property moduleBasePath The base path of the module.
3637
* @property moduleSourceDirectories List of source directories.
3738
* @property moduleResourceDirectories List of resource directories.
3839
* @property moduleTestSourceDirectories List of test source directories.
3940
* @property moduleTestResourceDirectories List of test resource directories.
4041
*/
4142
data class ModulePaths(
42-
val moduleBasePath: VirtualFile?,
4343
val moduleSourceDirectories: List<VirtualFile>,
4444
val moduleResourceDirectories: List<VirtualFile>,
4545
val moduleTestSourceDirectories: List<VirtualFile>,
@@ -51,12 +51,30 @@ object CommonPathParameterUtil {
5151

5252
/**
5353
* Returns the directory information for the specified module (uses cache if available).
54-
* If the module's directory structure has changed, call [refreshModulePaths] to update the cache.
5554
*
5655
* @param module The module to retrieve directory information for.
5756
* @return The cached or newly computed ModulePaths.
5857
*/
59-
fun getModulePaths(module: Module): ModulePaths = modulePathCache[module] ?: refreshModulePaths(module)
58+
fun getModulePaths(module: Module): ModulePaths? = modulePathCache[module]
59+
60+
/**
61+
* Checks if a given path is a generated directory based on annotation processor settings.
62+
*
63+
* @param module The module to check.
64+
* @param path The path to check.
65+
* @return True if the path is a generated directory, false otherwise.
66+
*/
67+
private fun isGeneratedDirectory(
68+
module: Module,
69+
path: String,
70+
): Boolean {
71+
val project: Project = module.project
72+
val compilerConfiguration = CompilerConfiguration.getInstance(project).getAnnotationProcessingConfiguration(module)
73+
val annotationProcessingConfiguration = compilerConfiguration.getGeneratedSourcesDirectoryName(false)
74+
75+
// Check if the path matches any of the generated source directories
76+
return path.contains("/build/$annotationProcessingConfiguration/")
77+
}
6078

6179
/**
6280
* Refreshes the directory information for the specified module and updates the cache.
@@ -65,38 +83,53 @@ object CommonPathParameterUtil {
6583
* @param module The module to refresh.
6684
* @return The updated ModulePaths.
6785
*/
68-
fun refreshModulePaths(module: Module): ModulePaths {
69-
var basePath: VirtualFile? = null
86+
fun refreshModulePaths(module: Module) {
7087
val sourceDirs = mutableListOf<VirtualFile>()
7188
val resourceDirs = mutableListOf<VirtualFile>()
7289
val testSourceDirs = mutableListOf<VirtualFile>()
7390
val testResourceDirs = mutableListOf<VirtualFile>()
7491

7592
val moduleManager = ModuleRootManager.getInstance(module)
76-
moduleManager.contentEntries.firstOrNull()?.let { entry ->
77-
basePath = entry.file
78-
entry.sourceFolders.forEach { folder ->
79-
val file = folder.file
80-
if (file != null) {
81-
when (folder.rootType) {
82-
JavaSourceRootType.SOURCE -> sourceDirs.add(file)
83-
JavaSourceRootType.TEST_SOURCE -> testSourceDirs.add(file)
84-
JavaResourceRootType.RESOURCE -> resourceDirs.add(file)
85-
JavaResourceRootType.TEST_RESOURCE -> testResourceDirs.add(file)
93+
moduleManager.contentEntries.forEach { entry ->
94+
val entryFile = entry.file
95+
if (entryFile != null && !isGeneratedDirectory(module, entryFile.path)) {
96+
entry.sourceFolders.forEach { folder ->
97+
val file = folder.file
98+
if (file != null) {
99+
when (folder.rootType) {
100+
JavaSourceRootType.SOURCE ->
101+
if (!sourceDirs.contains(file)) {
102+
sourceDirs.add(file)
103+
}
104+
105+
JavaSourceRootType.TEST_SOURCE ->
106+
if (!testSourceDirs.contains(file)) {
107+
testSourceDirs.add(file)
108+
}
109+
110+
JavaResourceRootType.RESOURCE ->
111+
if (!resourceDirs.contains(file)) {
112+
resourceDirs.add(file)
113+
}
114+
115+
JavaResourceRootType.TEST_RESOURCE ->
116+
if (!testResourceDirs.contains(file)) {
117+
testResourceDirs.add(file)
118+
}
119+
}
86120
}
87121
}
88122
}
89123
}
124+
90125
val paths =
91126
ModulePaths(
92-
basePath,
93127
sourceDirs,
94128
resourceDirs,
95129
testSourceDirs,
96130
testResourceDirs,
97131
)
98132
modulePathCache[module] = paths
99-
return paths
100133
}
101134

102135
/**
@@ -110,7 +143,7 @@ object CommonPathParameterUtil {
110143
module: Module,
111144
file: VirtualFile,
112145
): Boolean {
113-
val paths = getModulePaths(module)
146+
val paths = getModulePaths(module) ?: return false
114147
if (paths.moduleTestSourceDirectories.any { file.path.contains(it.path) }) return true
115148
if (paths.moduleTestResourceDirectories.any { file.path.contains(it.path) }) return true
116149
return false
@@ -127,30 +160,14 @@ object CommonPathParameterUtil {
127160
fun getResources(
128161
module: Module,
129162
file: VirtualFile,
130-
): List<VirtualFile> =
131-
if (isTest(module, file)) {
132-
getModulePaths(module).moduleTestResourceDirectories
133-
} else {
134-
getModulePaths(module).moduleResourceDirectories
135-
}
136-
137-
/**
138-
* Returns the source directories for the given file in the specified module.
139-
* If the file is in a test directory, test source directories are returned.
140-
*
141-
* @param module The module to check.
142-
* @param file The file to check.
143-
* @return List of source directories.
144-
*/
145-
fun getSources(
146-
module: Module,
147-
file: VirtualFile,
148-
): List<VirtualFile> =
149-
if (isTest(module, file)) {
150-
getModulePaths(module).moduleTestSourceDirectories
163+
): List<VirtualFile> {
164+
val modulePaths = getModulePaths(module) ?: return emptyList()
165+
return if (isTest(module, file)) {
166+
modulePaths.moduleTestResourceDirectories
151167
} else {
152-
getModulePaths(module).moduleSourceDirectories
168+
modulePaths.moduleResourceDirectories
153169
}
170+
}
154171

155172
/**
156173
* Clears the module directory cache. Call this if the module structure changes.

src/main/kotlin/org/domaframework/doma/intellij/common/config/DomaCompileConfigUtil.kt

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616
package org.domaframework.doma.intellij.common.config
1717

18-
import com.intellij.openapi.project.Project
19-
import com.intellij.openapi.vfs.VirtualFile
20-
import org.domaframework.doma.intellij.common.CommonPathParameterUtil
18+
import com.intellij.openapi.module.Module
19+
import org.domaframework.doma.intellij.extension.getResourcesFile
2120
import java.util.concurrent.ConcurrentHashMap
2221

2322
object DomaCompileConfigUtil {
@@ -30,40 +29,34 @@ object DomaCompileConfigUtil {
3029

3130
/**
3231
* Get the value of the specified key from doma.compile.config
33-
* @param project active project
34-
* @param resourcePaths the path to the resource directories
32+
* @param module the module to retrieve the configuration from
33+
* @param isTest true if the configuration is for test sources, false for main sources
3534
* @param key the key to retrieve the value for
3635
* @return the value associated with the key, or null if not found
3736
*/
3837
fun getConfigValue(
39-
project: Project,
40-
resourcePaths: List<VirtualFile>,
38+
module: Module,
39+
isTest: Boolean,
4140
key: String,
4241
): String? {
43-
resourcePaths.forEach { resourcePath ->
44-
if (resourcePath.isValid) {
45-
val configVFile = resourcePath.findChild("doma.compile.config")
46-
val cacheKey = "${project.basePath}/${resourcePath.path}/doma.compile.config"
47-
val lastModified = configVFile?.timeStamp ?: 0L
48-
val cached = configCache[cacheKey]
42+
val settingFileName = "doma.compile.config"
43+
val settingFile = module.getResourcesFile(settingFileName, isTest)
44+
val cacheKey = "${settingFile?.path}/$settingFileName"
45+
val lastModified = settingFile?.timeStamp ?: 0L
46+
val cached = configCache[cacheKey]
4947

50-
val props =
51-
if (cached == null || cached.second != lastModified) {
52-
val loadedProps =
53-
configVFile?.inputStream?.use { input ->
54-
java.util.Properties().apply { load(input) }
55-
} ?: java.util.Properties()
56-
configCache[cacheKey] = loadedProps to lastModified
57-
loadedProps
58-
} else {
59-
cached.first
60-
}
61-
val propProperty = props.getProperty(key)
62-
if (propProperty != null) return propProperty
48+
val props =
49+
if (cached == null || cached.second != lastModified) {
50+
val loadedProps =
51+
settingFile?.inputStream?.use { input ->
52+
java.util.Properties().apply { load(input) }
53+
} ?: java.util.Properties()
54+
configCache[cacheKey] = loadedProps to lastModified
55+
loadedProps
6356
} else {
64-
CommonPathParameterUtil.clearCache()
57+
cached.first
6558
}
66-
}
67-
return null
59+
val propProperty = props.getProperty(key)
60+
return propProperty
6861
}
6962
}

0 commit comments

Comments
 (0)