Skip to content

Commit

Permalink
feat(mock): init for run mock serivce
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 14, 2024
1 parent 6613b8d commit ec41dfb
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ project(":toolsets:plantuml") {
}
}

project(":toolsets:wiremock") {
project(":toolsets:mock") {
intellij {
version.set(prop("ideaVersion"))
plugins.set(ideaPlugins + prop("wireMockPlugin"))
Expand Down Expand Up @@ -359,6 +359,7 @@ project(":plugin") {
implementation(project(":toolsets:terminal"))
implementation(project(":toolsets:sonarqube"))
implementation(project(":toolsets:database"))
implementation(project(":toolsets:mock"))
}

// Collects all jars produced by compilation of project modules and merges them into singe one.
Expand Down
3 changes: 2 additions & 1 deletion plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
<module name="com.phodal.shire.httpclient"/>
<module name="com.phodal.shire.terminal"/>
<module name="com.phodal.shire.database"/>

<module name="com.phodal.shirelang.git"/>

<module name="com.phodal.shire.mock"/>

<!-- experimental features -->
<module name="com.phodal.shire.sonarqube"/>
</content>
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ include(
"toolsets:sonarqube",
"toolsets:plantuml",
"toolsets:database",
"toolsets:wiremock",
"toolsets:mock",
)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.phodal.shire.wiremock.provider

import com.intellij.execution.RunManager
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.json.psi.JsonFile
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiManager
import com.phodal.shirecore.provider.function.ToolchainFunctionProvider
import com.phodal.shirecore.runner.ConfigurationRunner

enum class WiremockFunction(val funName: String) {
Mock("mock")
;

companion object {
fun fromString(value: String): WiremockFunction? {
return values().firstOrNull { it.funName == value }
}
}
}


class WiremockFunctionProvider : ToolchainFunctionProvider, ConfigurationRunner {
override fun isApplicable(project: Project, funcName: String): Boolean {
return WiremockFunction.values().any { it.funName == funcName }
}

override fun execute(project: Project, funcName: String, args: List<Any>, allVariables: Map<String, Any?>): Any {
val wiremockFunction = WiremockFunction.fromString(funcName)
?: throw IllegalArgumentException("Shire[Wiremock]: Invalid Wiremock function name")

return when (wiremockFunction) {
WiremockFunction.Mock -> {
if (args.isEmpty()) {
return "ShireError[Wiremock]: No args found"
}

val mockFilepath = args.first()
val mockFile = project.baseDir.findFileByRelativePath(mockFilepath.toString())
?: throw IllegalArgumentException("ShireError[Wiremock]: No file found")

val jsonFile = PsiManager.getInstance(project).findFile(mockFile) as? JsonFile
?: throw IllegalArgumentException("ShireError[Wiremock]: No file found")

runMock(project, jsonFile)
}
}
}

private fun runMock(project: Project, mockFile: JsonFile): Any {
val configurationSettings =
ConfigurationContext(mockFile).configurationsFromContext?.firstOrNull()?.configurationSettings
?: return "Please install Wiremock plugin"

val runManager = RunManager.getInstance(project)
// val configure = runManager.allConfigurationsList.firstOrNull() ?: return "Please install Wiremock plugin"
runManager.selectedConfiguration = configurationSettings

configurationSettings.isActivateToolWindowBeforeRun = true

val runContext = createRunContext()
executeRunConfigures(project, configurationSettings, runContext, null, null)

return "ShireError[Wiremock]: No file found"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<idea-plugin package="com.phodal.shire.wiremock">
<idea-plugin package="com.phodal.shire.mock">
<!--suppress PluginXmlValidity -->
<dependencies>
<plugin id="com.intellij.wiremock"/>
Expand Down

This file was deleted.

0 comments on commit ec41dfb

Please sign in to comment.