|
16 | 16 | import com.intellij.openapi.components.Service; |
17 | 17 | import com.intellij.openapi.components.ServiceManager; |
18 | 18 | import com.intellij.openapi.diagnostic.Logger; |
| 19 | +import com.intellij.openapi.editor.Document; |
19 | 20 | import com.intellij.psi.PsiDocumentManager; |
20 | 21 | import com.intellij.psi.PsiFile; |
21 | 22 | import com.redhat.exhort.api.v4.AnalysisReport; |
22 | 23 | import com.redhat.exhort.api.v4.DependencyReport; |
23 | 24 | import com.redhat.exhort.api.v4.ProviderReport; |
24 | 25 | import com.redhat.exhort.api.v4.Source; |
25 | | -import org.apache.commons.io.FileUtils; |
26 | 26 | import org.jboss.tools.intellij.exhort.ApiService; |
27 | 27 |
|
28 | 28 | import java.io.IOException; |
| 29 | +import java.nio.charset.StandardCharsets; |
29 | 30 | import java.nio.file.Files; |
30 | 31 | import java.nio.file.Path; |
31 | 32 | import java.util.Collections; |
@@ -73,20 +74,9 @@ public static boolean performAnalysis(String packageManager, |
73 | 74 | Set<Dependency> dependencies, |
74 | 75 | PsiFile file) { |
75 | 76 | if (dependenciesModified(filePath, dependencies)) { |
76 | | - Path tempManifest; |
77 | | - Path tempDirectory; |
78 | 77 | ApiService apiService = ServiceManager.getService(ApiService.class); |
79 | | - try { |
80 | | - tempDirectory = Files.createTempDirectory("rhda-idea"); |
81 | | - tempManifest = Files.createFile(Path.of(tempDirectory.toString(),fileName)); |
82 | | - Files.write(tempManifest,PsiDocumentManager.getInstance(file.getProject()).getCachedDocument(file).getText().getBytes()); |
83 | | - } catch (IOException e) { |
84 | | - throw new RuntimeException(e); |
85 | | - } |
86 | | - |
87 | | -// AnalysisReport report = apiService.getComponentAnalysis(packageManager, fileName, filePath); |
88 | | - AnalysisReport report = apiService.getComponentAnalysis(packageManager, fileName, tempManifest.toString()); |
89 | | - deleteTempDir(tempDirectory); |
| 78 | + waitUntilMemoryAndDiskContentSynchronized(file, filePath); |
| 79 | + AnalysisReport report = apiService.getComponentAnalysis(packageManager, fileName, filePath); |
90 | 80 | if (report == null) { |
91 | 81 | throw new RuntimeException("Failed to perform component analysis, result is invalid."); |
92 | 82 | } |
@@ -164,13 +154,43 @@ public static boolean performAnalysis(String packageManager, |
164 | 154 | return false; |
165 | 155 | } |
166 | 156 |
|
167 | | - private static void deleteTempDir(Path tempDirectory) { |
168 | | - try { |
169 | | - FileUtils.deleteDirectory(tempDirectory.toFile()); |
170 | | - } catch (IOException e) { |
171 | | - LOG.warn("Failed to delete temp directory: " + tempDirectory, e); |
| 157 | + private static void waitUntilMemoryAndDiskContentSynchronized(PsiFile file, String filePath) { |
| 158 | + Document document = PsiDocumentManager.getInstance(file.getProject()).getCachedDocument(file); |
| 159 | + if (document == null) { |
| 160 | + LOG.warn("Document is not cached for file: " + file.getName()); |
172 | 161 | } |
173 | | - } |
174 | 162 |
|
| 163 | + Path path = Path.of(filePath); |
| 164 | + long timeoutMillis = 5000; |
| 165 | + long intervalMillis = 200; |
| 166 | + long startTime = System.currentTimeMillis(); |
| 167 | + |
| 168 | + while (System.currentTimeMillis() - startTime < timeoutMillis) { |
| 169 | + document = PsiDocumentManager.getInstance(file.getProject()).getCachedDocument(file); |
| 170 | + String memoryContent = document.getText(); |
| 171 | + String diskContent = ""; |
| 172 | + |
| 173 | + try { |
| 174 | + if (Files.exists(path)) { |
| 175 | + diskContent = Files.readString(path, StandardCharsets.UTF_8); |
| 176 | + } else { |
| 177 | + LOG.debug("Waiting for file to appear: " + filePath); |
| 178 | + } |
| 179 | + } catch (IOException e) { |
| 180 | + LOG.warn("Failed to read file from disk during wait: " + filePath, e); |
| 181 | + } |
| 182 | + |
| 183 | + if (memoryContent.equals(diskContent)) { |
| 184 | + LOG.debug("Memory and disk content match for: " + filePath); |
| 185 | + } |
175 | 186 |
|
| 187 | + try { |
| 188 | + Thread.sleep(intervalMillis); |
| 189 | + } catch (InterruptedException e) { |
| 190 | + Thread.currentThread().interrupt(); |
| 191 | + LOG.warn("Wait interrupted"); |
| 192 | + } |
| 193 | + } |
| 194 | + LOG.warn("Timeout: Memory and disk content did not match within 5 seconds for: " + filePath); |
| 195 | + } |
176 | 196 | } |
0 commit comments