Skip to content

Commit

Permalink
Fetch the project information from neptune
Browse files Browse the repository at this point in the history
  • Loading branch information
waleedyaseen committed Aug 29, 2024
1 parent b6c340f commit fd5bff8
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import com.intellij.openapi.components.service
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.JavaSdk
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.wm.ToolWindow
import io.runescript.plugin.ide.execution.createNeptuneJvmCommand
import java.io.File
import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicInteger
Expand Down Expand Up @@ -78,16 +78,6 @@ class RsBuildInstance(
}

private fun createCommandLine(): GeneralCommandLine {
val homeDirectory = neptuneHome
val parameters = mutableListOf<String>()
parameters += "-classpath"
parameters += "$homeDirectory${File.separator}lib${File.separator}*"
parameters += "me.filby.neptune.clientscript.compiler.ClientScriptCompilerApplicationKt"
val path = JavaSdk.getInstance().getVMExecutablePath(javaSdk)
return GeneralCommandLine()
.withExePath(path)
.withParameters(parameters)
.withWorkDirectory(workDirectory)
.withEnvironment(System.getenv())
return createNeptuneJvmCommand(javaSdk, neptuneHome, workDirectory)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.runescript.plugin.ide.execution

import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.openapi.projectRoots.JavaSdk
import com.intellij.openapi.projectRoots.Sdk
import java.io.File

fun createNeptuneJvmCommand(
javaSdk: Sdk,
neptuneHome: File,
workDirectory: String
) = createNeptuneJvmCommand(JavaSdk.getInstance().getVMExecutablePath(javaSdk), neptuneHome, workDirectory)

fun createNeptuneJvmCommand(
vmExecutablePath: String,
neptuneHome: File,
workDirectory: String
) = GeneralCommandLine()
.withExePath(vmExecutablePath)
.withParameters(createNeptuneJvmArgs(neptuneHome.toString()))
.withWorkDirectory(workDirectory)
.withEnvironment(System.getenv())

private fun createNeptuneJvmArgs(
homeDirectory: String
): MutableList<String> {
val args = mutableListOf<String>()
args += "-classpath"
args += "$homeDirectory${File.separator}lib${File.separator}*"
args += "me.filby.neptune.clientscript.compiler.ClientScriptCompilerApplicationKt"
return args
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package io.runescript.plugin.ide.neptune
import com.intellij.openapi.externalSystem.model.settings.ExternalSystemExecutionSettings

class NeptuneExecutionSettings(
val jreHome: String,
val jvmExecutablePath: String,
val neptuneSdkHome: String,
) : ExternalSystemExecutionSettings()
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.options.Configurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.JavaSdk
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.startup.StartupActivity
import com.intellij.openapi.util.Pair
import com.intellij.util.Function
Expand Down Expand Up @@ -40,11 +42,14 @@ class NeptuneManager :
return Function { pair ->
val project = pair.first
val systemSettings = project.service<NeptuneSettings>()
val settings = NeptuneExecutionSettings(
val javaSdk = ProjectJdkTable.getInstance()
.findJdk(systemSettings.launcherJre)
?: error("Java SDK not found")
val jvmExecutablePath = JavaSdk.getInstance().getVMExecutablePath(javaSdk)
NeptuneExecutionSettings(
jvmExecutablePath,
systemSettings.neptuneHome,
systemSettings.neptuneHome
)
settings
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.runescript.plugin.ide.neptune

import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import com.intellij.openapi.externalSystem.importing.ProjectResolverPolicy
import com.intellij.openapi.externalSystem.model.DataNode
import com.intellij.openapi.externalSystem.model.ProjectKeys
Expand All @@ -10,6 +12,7 @@ import com.intellij.openapi.externalSystem.model.project.ProjectData
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener
import com.intellij.openapi.externalSystem.service.project.ExternalSystemProjectResolver
import io.runescript.plugin.ide.execution.createNeptuneJvmCommand
import io.runescript.plugin.ide.projectWizard.RsModuleType
import org.slf4j.LoggerFactory
import java.io.File
Expand All @@ -26,24 +29,14 @@ class NeptuneProjectResolver : ExternalSystemProjectResolver<NeptuneExecutionSet
resolverPolicy: ProjectResolverPolicy?,

Check warning on line 29 in src/main/kotlin/io/runescript/plugin/ide/neptune/NeptuneProjectResolver.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.openapi.externalSystem.importing.ProjectResolverPolicy' is marked unstable with @ApiStatus.Experimental
listener: ExternalSystemTaskNotificationListener
): DataNode<ProjectData>? {

if (settings == null) {
check(false) { "NeptuneExecutionSettings is null" }
return null
}

log.info("JVM Arguments: ${settings.jvmArguments.toTypedArray().contentToString()}")
log.info("Environment Variables: ${settings.env.toMap().entries.joinToString(", ") { "${it.key}=${it.value}" }}")
log.info("Execution Name: ${settings.arguments}")
check(settings != null) { "Neptune settings must not be null" }

val projectRoot = File(projectPath)
check(projectRoot.isDirectory)

val neptuneData = extractNeptuneProjectMetadata(projectRoot)
val neptuneData = extractNeptuneProjectMetadata(settings, projectRoot) ?: return null
val projectName = neptuneData.name

log.info("Extracted project name: {}", projectName)

val projectData = ProjectData(
Neptune.SYSTEM_ID,
projectName,
Expand All @@ -53,11 +46,16 @@ class NeptuneProjectResolver : ExternalSystemProjectResolver<NeptuneExecutionSet
val projectNode = DataNode(ProjectKeys.PROJECT, projectData, null)
val moduleNode = projectNode.createModuleNode(projectName, projectRoot.absolutePath)

val outputPath = neptuneData.writers?.binary?.outputPath
var excludedPaths = neptuneData.excludePaths
if (outputPath != null) {
excludedPaths = excludedPaths + outputPath
}
moduleNode.createContentRootNode(
projectRoot.absolutePath,
listOf("src"),
listOf("symbols"),
listOf("pack")
neptuneData.sourcePaths,
neptuneData.symbolPaths,
excludedPaths,
)

return projectNode
Expand Down Expand Up @@ -102,20 +100,52 @@ class NeptuneProjectResolver : ExternalSystemProjectResolver<NeptuneExecutionSet
return false
}

private fun extractNeptuneProjectMetadata(projectRoot: File): NeptuneProjectData {
private fun extractNeptuneProjectMetadata(
settings: NeptuneExecutionSettings,
projectRoot: File
): NeptuneProjectData? {
val neptuneFile = projectRoot.resolve("neptune.toml")
check(neptuneFile.exists())

val fileText = neptuneFile.readText()
val commandLine = createNeptuneJvmCommand(
settings.jvmExecutablePath,
File(settings.neptuneSdkHome),
projectRoot.absolutePath
)
commandLine.addParameter("--config-path")
commandLine.addParameter(neptuneFile.absolutePath)

commandLine.addParameter("--print")

val regex = Regex("name\\s*=\\s*['\"](.*)['\"]")
val match = regex.find(fileText)
val projectName = match?.groupValues?.get(1) ?: projectRoot.name
// Turn off logging.
commandLine.addParameter("--log-level")
commandLine.addParameter("off")

return NeptuneProjectData(projectName)
val process = commandLine.createProcess()
val output = process.inputStream.bufferedReader().readText()
try {
return Gson().fromJson(output, NeptuneProjectData::class.java)
} catch (e: Throwable) {
log.error("Failed to parse Neptune project data", e)
return null
}
}

private data class NeptuneProjectData(
val name: String
@SerializedName("name")
val name: String,
@SerializedName("sourcePaths")
val sourcePaths: List<String>,
@SerializedName("symbolPaths")
val symbolPaths: List<String>,
@SerializedName("libraryPaths")
val libraryPaths: List<String>,
@SerializedName("excludePaths")
val excludePaths: List<String>,
@SerializedName("writers")
val writers: ClientScriptWriterConfig?,
)
data class ClientScriptWriterConfig(val binary: BinaryFileWriterConfig? = null)
data class BinaryFileWriterConfig(val outputPath: String)

}

0 comments on commit fd5bff8

Please sign in to comment.