Skip to content

Commit

Permalink
ok manque boutons
Browse files Browse the repository at this point in the history
  • Loading branch information
pkernevez committed Oct 28, 2023
1 parent d85c646 commit cc0c0f5
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.kleis.lcaac.plugin.actions

import ch.kleis.lcaac.plugin.actions.tasks.EventTaskLogger
import ch.kleis.lcaac.plugin.actions.tasks.ModelGenerationTask
import ch.kleis.lcaac.plugin.language.psi.LcaFile
import ch.kleis.lcaac.plugin.psi.LcaProcess
Expand All @@ -24,7 +25,11 @@ class ModelGenerationWithDataAction(
val file = e.getData(LangDataKeys.PSI_FILE) as LcaFile? ?: return
val containingDirectory = file.containingDirectory ?: return

val task = ModelGenerationTask(project, process, processName, file, containingDirectory)
val task = ModelGenerationTask(
project, process, processName, file,
containingDirectory,
logger = EventTaskLogger(project)
)
ProgressManager.getInstance().run(task)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package ch.kleis.lcaac.plugin.actions

import ch.kleis.lcaac.plugin.actions.tasks.RunTask
import ch.kleis.lcaac.plugin.actions.tasks.TaskLogger
import ch.kleis.lcaac.plugin.actions.tasks.TerminalTaskLogger
import ch.kleis.lcaac.plugin.psi.LcaRun
import com.intellij.execution.process.ProcessHandler
import com.intellij.icons.AllIcons
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.terminal.TerminalExecutionConsole
import com.intellij.ui.content.ContentFactory
import java.io.OutputStream

class RunRunAction(
private val run: LcaRun,
Expand All @@ -17,9 +25,46 @@ class RunRunAction(
) {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return

val task = RunTask(project, run, runnerName)
val logger = createLogPanel(project)
val task = RunTask(project, run, logger, runnerName)

ProgressManager.getInstance().run(task)
}

private fun createLogPanel(project: Project): TaskLogger {
val toolWindow = ToolWindowManager.getInstance(project).getToolWindow("LCA Run")
?: throw Error("Unable to find Run Logger panel")
val handler = object : ProcessHandler() {
override fun destroyProcessImpl() {
TODO("Not yet implemented")
}

override fun detachProcessImpl() {
TODO("Not yet implemented")
}

override fun detachIsDefault(): Boolean {
TODO("Not yet implemented")
}

override fun getProcessInput(): OutputStream? {
TODO("Not yet implemented")
}

}
val termContent = TerminalExecutionConsole(project, handler)
// val testResultsContent = TestResultsWindow(results).getContent()
val content = ContentFactory.getInstance().createContent(
termContent.terminalWidget,
"Run $runnerName",
false,
)
toolWindow.contentManager.removeAllContents(true)
toolWindow.contentManager.addContent(content)
toolWindow.contentManager.setSelectedContent(content)
toolWindow.show()
return TerminalTaskLogger(termContent)
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import ch.kleis.lcaac.plugin.language.loader.LcaFileCollector
import ch.kleis.lcaac.plugin.language.loader.LcaLoader
import ch.kleis.lcaac.plugin.language.psi.LcaFile
import ch.kleis.lcaac.plugin.psi.LcaProcess
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.diagnostic.Logger
Expand All @@ -31,6 +29,7 @@ class ModelGenerationTask(
private val processName: String,
private val file: LcaFile,
private val containingDirectory: PsiDirectory,
private val logger: TaskLogger,
private val csvFile: String = "$processName.csv"
) : Task.Backgroundable(project, "Run with ${processName}.csv") {
companion object {
Expand Down Expand Up @@ -72,42 +71,31 @@ class ModelGenerationTask(
ApplicationManager.getApplication().invokeAndWait { ->

// runWriteAction {
val vfile = VfsUtil.findFile(
val vFile = VfsUtil.findFile(
outputPath, true
)
VirtualFileManager.getInstance().syncRefresh()
vfile?.refresh(false, false)
vFile?.refresh(false, false)
LOG.info("Now refresh")
// }
val dumbSrv = DumbService.getInstance(project)
//ApplicationManager.getApplication().getService(DumbService::class.java)
dumbSrv.completeJustSubmittedTasks()

}
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, message, NotificationType.INFORMATION)
.notify(project)
logger.info(title, message)
} catch (e: EvaluatorException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
logger.info(title, e.message ?: "unknown error")
LOG.warn("Unable to process computation", e)
} catch (e: NoSuchElementException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
logger.info(title, e.message ?: "unknown error")
LOG.warn("Unable to process computation", e)
} catch (e: FileNotFoundException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
logger.info(title, e.message ?: "unknown error")
LOG.warn("Unable to process computation, file not found", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import ch.kleis.lcaac.plugin.ui.toolwindow.contribution_analysis.tables.ImpactAs
import ch.kleis.lcaac.plugin.ui.toolwindow.contribution_analysis.tables.InventoryTableModel
import ch.kleis.lcaac.plugin.ui.toolwindow.contribution_analysis.tables.SupplyTableModel
import ch.kleis.lcaac.plugin.ui.toolwindow.shared.SaveTableModelTask
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.progress.ProgressIndicator
Expand All @@ -27,14 +25,16 @@ import kotlin.io.path.Path
typealias Contrib = ContributionAnalysis<BasicNumber, BasicMatrix>

class RunTask(
project: Project,
private val project: Project,
private val run: LcaRun,
runnerName: String,
private val logger: TaskLogger,
runnerName: String
) : Task.Backgroundable(project, runnerName) {
companion object {
private val LOG = Logger.getInstance(RunTask::class.java)
}

// private val logger = TerminalTaskLogger()
override fun run(indicator: ProgressIndicator) {
val size = run.runnableList.size
run.runnableList.forEachIndexed { index, element ->
Expand All @@ -51,32 +51,22 @@ class RunTask(
private fun generate(generate: LcaGenerate, indicator: ProgressIndicator) {
var task: ModelGenerationTask? = null
ApplicationManager.getApplication().runReadAction {
// var file: PsiFile? = null
// var process: LcaProcess? = null
// var containingDirectory: PsiDirectory? = null
val process = generate.getProcessRef().reference.resolve() as LcaProcess
val file = process.containingFile as LcaFile?
val containingDirectory = file?.containingDirectory

// process.let {
containingDirectory?.let {
task = ModelGenerationTask(
project = project,
process = process,
processName = process.name,
file = run.containingFile as LcaFile,
containingDirectory = it
containingDirectory = it,
logger = logger
)
// }
}
}
task?.run(indicator)
// ApplicationManager.getApplication().invokeAndWait { ->
//// runWriteAction {
// val dumbSrv = DumbService.getInstance(project)
// //ApplicationManager.getApplication().getService(DumbService::class.java)
// dumbSrv.completeJustSubmittedTasks()
// }
}

private fun assess(assess: LcaAssess, indicator: ProgressIndicator) {
Expand Down Expand Up @@ -133,14 +123,15 @@ class RunTask(
)
output.forEach { (file, model) ->
val path = Path("${project.basePath}/out/${processName}-$file.csv")
val task = SaveTableModelTask(project, model, path.toFile())
val task = SaveTableModelTask(project, model, path.toFile(), logger)
ProgressManager.getInstance().run(task)
VirtualFileManager.getInstance().refreshAndFindFileByNioPath(path)
LOG.info("File $path generated")
}
}

override fun onSuccess() {
logger.info("Run ${run.runRef.name}", "Success")
// val toolWindow = ToolWindowManager.getInstance(project).getToolWindow("LCA Tests") ?: return
// val testResultsContent = TestResultsWindow(results).getContent()
// val content = ContentFactory.getInstance().createContent(
Expand All @@ -154,12 +145,9 @@ class RunTask(
// toolWindow.show()
}


override fun onThrowable(e: Throwable) {
val title = "Error"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
logger.error("Run ${run.runRef.name} finish with Errors", e.message ?: "unknown error")
LOG.warn("Unable to process computation", e)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ch.kleis.lcaac.plugin.actions.tasks

import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.project.Project
import com.intellij.terminal.TerminalExecutionConsole

interface TaskLogger {
fun info(title: String, text: String)
fun error(title: String, text: String)
}

class TerminalTaskLogger(private val term: TerminalExecutionConsole) : TaskLogger {
override fun info(title: String, html: String) {
val text = html.replace("<br>", "\n ")
term.print("$title: $text\n", ConsoleViewContentType.LOG_INFO_OUTPUT)
}

override fun error(title: String, text: String) {
term.print("$title: $text\n", ConsoleViewContentType.LOG_ERROR_OUTPUT)
}
}

class EventTaskLogger(private val project: Project) : TaskLogger {
override fun info(title: String, text: String) {
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, text, NotificationType.INFORMATION)
.notify(project)
}

override fun error(title: String, text: String) {
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, text, NotificationType.ERROR)
.notify(project)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.kleis.lcaac.plugin.ui.toolwindow

import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory

class LcaRunToolWindowFactory : ToolWindowFactory {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.kleis.lcaac.plugin.ui.toolwindow.shared

import ch.kleis.lcaac.plugin.actions.tasks.EventTaskLogger
import com.intellij.icons.AllIcons
import com.intellij.openapi.fileChooser.FileChooserFactory
import com.intellij.openapi.fileChooser.FileSaverDescriptor
Expand Down Expand Up @@ -63,8 +64,8 @@ class CopyPastableTablePane(
button.addActionListener {
val descriptor = FileSaverDescriptor("Save as CSV", "Save data as CSV file")
val saveFileDialog = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project)
val vf = saveFileDialog.save(project.projectFile, defaultFilename) ?: return@addActionListener
val task = SaveTableModelTask(project, model, vf.file)
val vf = saveFileDialog.save(project.projectFile, defaultFilename) ?: return@addActionListener
val task = SaveTableModelTask(project, model, vf.file, EventTaskLogger(project))
ProgressManager.getInstance().run(task)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ch.kleis.lcaac.plugin.ui.toolwindow.shared

import ch.kleis.lcaac.plugin.MyBundle
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import ch.kleis.lcaac.plugin.actions.tasks.TaskLogger
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
Expand All @@ -20,6 +19,7 @@ class SaveTableModelTask(
private val project: Project,
private val model: TableModel,
private val file: File,
private val logger: TaskLogger
) : Task.Backgroundable(project, "Saving data") {
companion object {
private val LOG = Logger.getInstance(SaveTableModelTask::class.java)
Expand All @@ -39,23 +39,17 @@ class SaveTableModelTask(
}
}
val duration = (System.currentTimeMillis() - start) / 1000
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(
MyBundle.message(
"lca.dialog.export.finished.success",
duration,
path
), NotificationType.INFORMATION
logger.info(
"Save Model", MyBundle.message(
"lca.dialog.export.finished.success",
duration,
path
)
.notify(project)
)
VirtualFileManager.getInstance().refreshAndFindFileByNioPath(path)
} catch (e: Exception) {
val title = "Error while saving results to file"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
logger.error(title, e.message ?: "unknown error")
LOG.warn("Unable to process computation", e)
}
}
Expand Down
5 changes: 5 additions & 0 deletions plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
anchor="bottom"
factoryClass="ch.kleis.lcaac.plugin.ui.toolwindow.LcaTestsToolWindowFactory"
canCloseContents="true"/>
<toolWindow id="LCA Run"
icon="AllIcons.General.Modified"
anchor="bottom"
factoryClass="ch.kleis.lcaac.plugin.ui.toolwindow.LcaRunToolWindowFactory"
canCloseContents="true"/>

<runLineMarkerContributor
language="LCA"
Expand Down

0 comments on commit cc0c0f5

Please sign in to comment.