Skip to content

Commit 19d7396

Browse files
AdmStorm 2.0: @admstorm-marker (#171)
1 parent 384dda8 commit 19d7396

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1432
-169
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# AdmStorm Changelog
22

3+
## [Unreleased]
4+
5+
## 2.0.0 — 11.05.2023
6+
7+
Second stable version
8+
9+
### Added
10+
11+
- `@admstorm-marker` - allowing you to see the values directly in the IDE
12+
313
## 1.3.5 — 07.03.2023
414

515
### Fixed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ plugins {
1313
// gradle-changelog-plugin - read more: https://github.com/JetBrains/gradle-changelog-plugin
1414
id("org.jetbrains.changelog") version "2.0.0"
1515
// kotlinx serialization - read more: https://github.com/Kotlin/kotlinx.serialization
16-
kotlin("plugin.serialization") version "1.5.31"
16+
kotlin("plugin.serialization") version "1.6.21"
1717
// detekt linter - read more: https://github.com/detekt/detekt
1818
id("io.gitlab.arturbosch.detekt") version "1.22.0"
1919
// diktat linter - read more: https://github.com/saveourtool/diktat

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pluginName=admstorm
55
pluginRepositoryUrl = https://github.com/VKCOM/admstorm
66

77
# SemVer format -> https://semver.org
8-
pluginVersion=1.3.5
8+
pluginVersion=2.0.0
99

1010
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
1111
pluginSinceBuild=223

src/main/kotlin/com/vk/admstorm/AdmService.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.openapi.project.Project
1010
import com.intellij.psi.PsiManager
1111
import com.vk.admstorm.utils.MyPathUtils
1212
import com.vk.admstorm.utils.MyUtils
13+
import com.vk.admstorm.utils.extensions.unquote
1314

1415
@Service
1516
class AdmService(private var myProject: Project) {
@@ -55,7 +56,7 @@ class AdmService(private var myProject: Project) {
5556
var containsGlobal = false
5657
composerPsiFile.accept(object : JsonRecursiveElementVisitor() {
5758
override fun visitStringLiteral(lit: JsonStringLiteral) {
58-
containsGlobal = containsGlobal || lit.text.contains("vk/global")
59+
containsGlobal = containsGlobal || lit.text.unquote() == "vk/global"
5960
}
6061
})
6162

src/main/kotlin/com/vk/admstorm/AdmStormStartupActivity.kt

+3
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ class AdmStormStartupActivity : StartupActivity {
232232

233233
override fun runActivity(project: Project) {
234234
setupLogger(project)
235+
initPlugin(project)
236+
}
235237

238+
private fun initPlugin(project: Project) {
236239
if (!project.pluginEnabled()) {
237240
// We don't connect if this is not a vkcom project
238241
return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.vk.admstorm.admscript
2+
3+
import com.google.common.cache.Cache
4+
import com.google.common.cache.CacheBuilder
5+
import com.intellij.openapi.diagnostic.logger
6+
import com.intellij.openapi.project.Project
7+
import com.vk.admstorm.CommandRunner
8+
import com.vk.admstorm.admscript.utils.DataResponse
9+
import com.vk.admstorm.admscript.utils.DataResponseSerializer
10+
import com.vk.admstorm.env.Env
11+
import kotlinx.serialization.KSerializer
12+
import kotlinx.serialization.json.Json
13+
import java.util.concurrent.TimeUnit
14+
15+
abstract class AdmScript<TModel>(private val project: Project) : IService<TModel> {
16+
companion object {
17+
private val LOG = logger<AdmScript<Any>>()
18+
19+
private val resultCache: Cache<String, String> = CacheBuilder.newBuilder()
20+
.weakKeys()
21+
.expireAfterWrite(1, TimeUnit.MINUTES)
22+
.build()
23+
24+
}
25+
26+
abstract val methodName: String
27+
28+
protected fun <TModel : Any> execCommand(keyName: String, serializer: KSerializer<TModel>): DataResponse<TModel> {
29+
val workingDir = "${Env.data.projectRoot}/${Env.data.phpSourceFolder}"
30+
val admScriptName = Env.data.admScriptName
31+
32+
val baseCommand = "php ${workingDir}/${admScriptName}"
33+
val command = "$baseCommand --method $methodName --key $keyName"
34+
35+
var isFromCache = true
36+
val cacheValue = resultCache.getIfPresent(keyName)
37+
val stdout = if (cacheValue == null) {
38+
val output = CommandRunner.runRemotely(
39+
project,
40+
command,
41+
command,
42+
workingDir,
43+
5_000,
44+
)
45+
46+
if (output.exitCode == CommandRunner.FAILED_CODE) {
47+
return DataResponse(errorMessage = "An error occurred while sending data")
48+
}
49+
50+
if (output.exitCode == CommandRunner.TIMEOUT_CODE) {
51+
return DataResponse(errorMessage = "Timeout occurred. Try again.")
52+
}
53+
54+
val stdout = output.stdout
55+
if (output.exitCode != 0) {
56+
return DataResponse(errorMessage = stdout)
57+
}
58+
59+
isFromCache = false
60+
stdout
61+
} else {
62+
cacheValue
63+
}
64+
65+
return try {
66+
val response = Json.decodeFromString(DataResponseSerializer(serializer), stdout)
67+
if (response.errorMessage == null && !isFromCache) {
68+
resultCache.put(keyName, stdout)
69+
}
70+
71+
response
72+
} catch (e: Exception) {
73+
LOG.warn("Error when decode the response:", e)
74+
return DataResponse(errorMessage = "Error when decode the response")
75+
}
76+
}
77+
78+
abstract override fun execCommand(keyName: String): DataResponse<TModel>
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.vk.admstorm.admscript
2+
3+
import com.vk.admstorm.admscript.utils.DataResponse
4+
5+
interface IService<TModel> {
6+
fun execCommand(keyName: String): DataResponse<TModel>?
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.vk.admstorm.admscript.utils
2+
3+
data class DataResponse<out TModel>(
4+
val value: TModel? = null,
5+
val errorMessage: String? = null,
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.vk.admstorm.admscript.utils
2+
3+
import kotlinx.serialization.KSerializer
4+
import kotlinx.serialization.SerializationException
5+
import kotlinx.serialization.Serializer
6+
import kotlinx.serialization.builtins.nullable
7+
import kotlinx.serialization.descriptors.SerialDescriptor
8+
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
9+
import kotlinx.serialization.descriptors.nullable
10+
import kotlinx.serialization.encoding.CompositeDecoder
11+
import kotlinx.serialization.encoding.Decoder
12+
import kotlinx.serialization.encoding.Encoder
13+
import kotlinx.serialization.serializer
14+
15+
@Serializer(forClass = DataResponse::class)
16+
class DataResponseSerializer<TModel : Any>(private val dataSerializer: KSerializer<TModel>) : KSerializer<DataResponse<TModel>> {
17+
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("DataResponseSerializer") {
18+
val dataDescriptor = dataSerializer.descriptor.nullable
19+
element("value", dataDescriptor)
20+
element("error_message", dataSerializer.descriptor.nullable)
21+
}
22+
23+
override fun deserialize(decoder: Decoder): DataResponse<TModel> {
24+
val inp = decoder.beginStructure(descriptor)
25+
var value: TModel? = null
26+
var error: String? = null
27+
loop@ while (true) {
28+
when (val i = inp.decodeElementIndex(descriptor)) {
29+
CompositeDecoder.DECODE_DONE -> break@loop
30+
0 -> value = inp.decodeNullableSerializableElement(descriptor, i, dataSerializer.nullable)
31+
1 -> error = inp.decodeNullableSerializableElement(descriptor, i, serializer())
32+
else -> throw SerializationException("Unknown index $i")
33+
}
34+
}
35+
inp.endStructure(descriptor)
36+
return DataResponse(value, error)
37+
}
38+
39+
override fun serialize(encoder: Encoder, value: DataResponse<TModel>) {
40+
encoder.beginStructure(descriptor).apply {
41+
encodeNullableSerializableElement(descriptor, 0, dataSerializer, value.value)
42+
encodeNullableSerializableElement(descriptor, 1, serializer<String?>(), value.errorMessage)
43+
endStructure(descriptor)
44+
}
45+
}
46+
}

src/main/kotlin/com/vk/admstorm/env/Env.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.vk.admstorm.env
22

33
import com.intellij.openapi.diagnostic.logger
44
import com.intellij.openapi.project.Project
5-
import com.intellij.util.application
65
import com.vk.admstorm.CommandRunner
76
import com.vk.admstorm.configuration.kphp.KphpRunType
87
import com.vk.admstorm.configuration.phplinter.PhpLinterCheckers
@@ -33,6 +32,7 @@ data class KphpCommand(
3332
@Serializable
3433
data class Service(
3534
val name: String,
35+
val key: String,
3636
val url: String,
3737
)
3838

@@ -63,6 +63,7 @@ data class EnvConfig(
6363
var testDomainSite: String = "",
6464
var serviceDomain: String = "",
6565
var kphpCommands: List<KphpCommand> = listOf(),
66+
var admScriptName: String = "",
6667
var services: List<Service> = listOf(),
6768
)
6869

@@ -164,7 +165,7 @@ object Env {
164165
}
165166

166167
myIsResolved = true
167-
application.messageBus.syncPublisher(EnvListener.TOPIC).onResolve()
168+
project.messageBus.syncPublisher(EnvListener.TOPIC).onResolve()
168169
}
169170

170171
private fun setPhpLinterCheckers(project: Project) {

src/main/kotlin/com/vk/admstorm/highlight/DebugLogLineMarkerProvider.kt

-127
This file was deleted.

0 commit comments

Comments
 (0)