Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rules postman.format.after, yapi.format.after #1033

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ plugin_version=2.6.2.212.0
kotlin.code.style=official
kotlin_version=1.8.0
junit_version=5.9.2
itangcent_intellij_version=1.5.5
itangcent_intellij_version=1.5.52-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class FieldsToJson5Action : FieldsToMessageAction("To Json5") {
}

override fun formatMessage(psiClass: PsiClass, type: PsiType?): String {
val obj = psiClassHelper!!.getTypeObject(type, psiClass, JsonOption.ALL)
val obj = psiClassHelper!!.getTypeObject(
psiType = type,
context = psiClass,
option = JsonOption.ALL
)
return ActionContext.getContext()!!.instance(Json5Formatter::class).format(obj)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package com.itangcent.idea.plugin.actions
import com.google.inject.Inject
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiType
import com.itangcent.idea.plugin.format.MessageFormatter
import com.itangcent.idea.plugin.format.SimpleJsonFormatter
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.jvm.PsiClassHelper
import com.itangcent.intellij.jvm.JsonOption
import com.itangcent.intellij.jvm.PsiClassHelper

/**
* @author tangcent
Expand All @@ -22,7 +21,11 @@ class FieldsToJsonAction : FieldsToMessageAction("To Json") {
}

override fun formatMessage(psiClass: PsiClass, type: PsiType?): String {
val obj = psiClassHelper!!.getTypeObject(type, psiClass, JsonOption.READ_GETTER.or(JsonOption.READ_SETTER))
val obj = psiClassHelper!!.getTypeObject(
psiType = type,
context = psiClass,
option = JsonOption.READ_GETTER or JsonOption.READ_SETTER
)
return ActionContext.getContext()!!.instance(SimpleJsonFormatter::class).format(obj)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,70 +205,6 @@ object ClassExportRuleKeys {
StringRuleMode.SINGLE
)

/**
* The pre-request scripts in Postman to execute JavaScript before a request runs.
*
* @see [https://learning.postman.com/docs/writing-scripts/pre-request-scripts]
*/
val POST_PRE_REQUEST: RuleKey<String> = SimpleRuleKey(
"postman.prerequest",
StringRuleMode.MERGE
)

/**
* Add pre-request scripts to entire collections as well as to folders within collections.
* This script will execute before every request in this collection or folder.
*
* @see [https://learning.postman.com/docs/writing-scripts/pre-request-scripts]
*/
val CLASS_POST_PRE_REQUEST: RuleKey<String> = SimpleRuleKey(
"class.postman.prerequest",
StringRuleMode.MERGE
)

/**
* Add pre-request scripts to top collection.
* This script will execute before every request in this collection.
*
* @see [https://learning.postman.com/docs/writing-scripts/pre-request-scripts]
*/
val COLLECTION_POST_PRE_REQUEST: RuleKey<String> = SimpleRuleKey(
"collection.postman.prerequest",
StringRuleMode.MERGE
)

/**
* The test scripts for Postman API requests in JavaScript.
*
* @see [https://learning.postman.com/docs/writing-scripts/test-scripts/]
*/
val POST_TEST: RuleKey<String> = SimpleRuleKey(
"postman.test",
StringRuleMode.MERGE
)

/**
* Add test scripts to entire collections as well as to folders within collections.
* These tests will execute after every request in this collection.
*
* @see [https://learning.postman.com/docs/writing-scripts/test-scripts/]
*/
val CLASS_POST_TEST: RuleKey<String> = SimpleRuleKey(
"class.postman.test",
StringRuleMode.MERGE
)

/**
* Add test scripts to top collection.
* These tests will execute after every request in this collection.
*
* @see [https://learning.postman.com/docs/writing-scripts/test-scripts/]
*/
val COLLECTION_POST_TEST: RuleKey<String> = SimpleRuleKey(
"collection.postman.test",
StringRuleMode.MERGE
)

val AFTER_EXPORT: RuleKey<String> = SimpleRuleKey(
"export.after",
EventRuleMode.IGNORE_ERROR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import com.itangcent.common.logger.traceError
import com.itangcent.common.model.Request
import com.itangcent.common.utils.*
import com.itangcent.idea.plugin.api.ClassApiExporterHelper
import com.itangcent.idea.plugin.rule.SuvRuleContext
import com.itangcent.idea.plugin.rule.setDoc
import com.itangcent.idea.plugin.settings.PostmanExportMode
import com.itangcent.idea.plugin.settings.helper.PostmanSettingsHelper
import com.itangcent.idea.psi.resource
import com.itangcent.idea.utils.FileSaveHelper
import com.itangcent.idea.utils.ModuleHelper
import com.itangcent.intellij.config.rule.RuleComputer
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.logger.Logger

Expand All @@ -38,16 +42,16 @@ class PostmanApiExporter {
private lateinit var postmanFormatter: PostmanFormatter

@Inject
private val moduleHelper: ModuleHelper? = null
private lateinit var moduleHelper: ModuleHelper

@Inject
private val classApiExporterHelper: ClassApiExporterHelper? = null
private lateinit var classApiExporterHelper: ClassApiExporterHelper

companion object : Log()

fun export() {
try {
val requests = classApiExporterHelper!!.export().mapNotNull { it as? Request }
val requests = classApiExporterHelper.export().mapNotNull { it as? Request }
if (requests.isEmpty()) {
logger.info("No api be found to export!")
} else {
Expand Down Expand Up @@ -105,6 +109,7 @@ class PostmanApiExporter {
workspaceId: String?,
) {
val postman = postmanFormatter.parseRequests(requests)

val createdCollection = postmanApiHelper.createCollection(postman, workspaceId)

if (createdCollection.notNullOrEmpty()) {
Expand Down Expand Up @@ -132,7 +137,7 @@ class PostmanApiExporter {
private fun updateRequestsToPostman(requests: List<Request>) {
val moduleGroupedMap = HashMap<String, ArrayList<Request>>()
requests.forEach {
val module = moduleHelper!!.findModule(it.resource!!) ?: "easy-api"
val module = moduleHelper.findModule(it.resource!!) ?: "easy-api"
moduleGroupedMap.safeComputeIfAbsent(module) { ArrayList() }!!
.add(it)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.itangcent.idea.plugin.api.export.postman

import com.itangcent.intellij.config.rule.EventRuleMode
import com.itangcent.intellij.config.rule.RuleKey
import com.itangcent.intellij.config.rule.SimpleRuleKey
import com.itangcent.intellij.config.rule.StringRuleMode

object PostmanExportRuleKeys {

/**
* The pre-request scripts in Postman to execute JavaScript before a request runs.
*
* @see [https://learning.postman.com/docs/writing-scripts/pre-request-scripts]
*/
val POST_PRE_REQUEST: RuleKey<String> = SimpleRuleKey(
"postman.prerequest",
StringRuleMode.MERGE
)

/**
* Add pre-request scripts to entire collections as well as to folders within collections.
* This script will execute before every request in this collection or folder.
*
* @see [https://learning.postman.com/docs/writing-scripts/pre-request-scripts]
*/
val CLASS_POST_PRE_REQUEST: RuleKey<String> = SimpleRuleKey(
"class.postman.prerequest",
StringRuleMode.MERGE
)

/**
* Add pre-request scripts to top collection.
* This script will execute before every request in this collection.
*
* @see [https://learning.postman.com/docs/writing-scripts/pre-request-scripts]
*/
val COLLECTION_POST_PRE_REQUEST: RuleKey<String> = SimpleRuleKey(
"collection.postman.prerequest",
StringRuleMode.MERGE
)

/**
* The test scripts for Postman API requests in JavaScript.
*
* @see [https://learning.postman.com/docs/writing-scripts/test-scripts/]
*/
val POST_TEST: RuleKey<String> = SimpleRuleKey(
"postman.test",
StringRuleMode.MERGE
)

/**
* Add test scripts to entire collections as well as to folders within collections.
* These tests will execute after every request in this collection.
*
* @see [https://learning.postman.com/docs/writing-scripts/test-scripts/]
*/
val CLASS_POST_TEST: RuleKey<String> = SimpleRuleKey(
"class.postman.test",
StringRuleMode.MERGE
)

/**
* Add test scripts to top collection.
* These tests will execute after every request in this collection.
*
* @see [https://learning.postman.com/docs/writing-scripts/test-scripts/]
*/
val COLLECTION_POST_TEST: RuleKey<String> = SimpleRuleKey(
"collection.postman.test",
StringRuleMode.MERGE
)

val AFTER_FORMAT: RuleKey<String> = SimpleRuleKey(
"postman.format.after",
EventRuleMode.THROW_IN_ERROR
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import com.google.inject.Inject
import com.google.inject.Singleton
import com.intellij.psi.PsiClass
import com.itangcent.common.utils.notNullOrBlank
import com.itangcent.idea.plugin.api.export.core.ClassExportRuleKeys
import com.itangcent.idea.plugin.api.export.core.DefaultFormatFolderHelper
import com.itangcent.idea.plugin.api.export.core.Folder
import com.itangcent.intellij.config.rule.RuleComputer
import com.itangcent.intellij.config.rule.computer
import com.itangcent.intellij.jvm.element.ExplicitClass

/**
* 1.support rule:[com.itangcent.idea.plugin.api.export.ClassExportRuleKeys.CLASS_POST_PRE_REQUEST]
* 2.support rule:[com.itangcent.idea.plugin.api.export.ClassExportRuleKeys.CLASS_POST_TEST]
* 1.support rule:[com.itangcent.idea.plugin.api.export.postman.PostmanExportRuleKeys.CLASS_POST_PRE_REQUEST]
* 2.support rule:[com.itangcent.idea.plugin.api.export.postman.PostmanExportRuleKeys.CLASS_POST_TEST]
*
* @see [https://learning.postman.com/docs/writing-scripts/intro-to-scripts/]
*/
Expand All @@ -26,14 +25,14 @@ class PostmanFormatFolderHelper : DefaultFormatFolderHelper() {
override fun resolveFolderOfPsiClass(resource: PsiClass): Folder {
val folder = super.resolveFolderOfPsiClass(resource)

val preRequest = ruleComputer!!.computer(ClassExportRuleKeys.CLASS_POST_PRE_REQUEST, resource)
val preRequest = ruleComputer!!.computer(PostmanExportRuleKeys.CLASS_POST_PRE_REQUEST, resource)
if (preRequest.notNullOrBlank()) {
folder.setExt(ClassExportRuleKeys.POST_PRE_REQUEST.name(), preRequest)
folder.setExt(PostmanExportRuleKeys.POST_PRE_REQUEST.name(), preRequest)
}

val test = ruleComputer.computer(ClassExportRuleKeys.CLASS_POST_TEST, resource)
val test = ruleComputer.computer(PostmanExportRuleKeys.CLASS_POST_TEST, resource)
if (test.notNullOrBlank()) {
folder.setExt(ClassExportRuleKeys.POST_TEST.name(), test)
folder.setExt(PostmanExportRuleKeys.POST_TEST.name(), test)
}

return folder
Expand All @@ -42,14 +41,14 @@ class PostmanFormatFolderHelper : DefaultFormatFolderHelper() {
override fun resolveFolderOfExplicitClass(resource: ExplicitClass): Folder {
val folder = super.resolveFolderOfExplicitClass(resource)

val preRequest = ruleComputer!!.computer(ClassExportRuleKeys.CLASS_POST_PRE_REQUEST, resource)
val preRequest = ruleComputer!!.computer(PostmanExportRuleKeys.CLASS_POST_PRE_REQUEST, resource)
if (preRequest.notNullOrBlank()) {
folder.setExt(ClassExportRuleKeys.POST_PRE_REQUEST.name(), preRequest)
folder.setExt(PostmanExportRuleKeys.POST_PRE_REQUEST.name(), preRequest)
}

val test = ruleComputer.computer(ClassExportRuleKeys.CLASS_POST_TEST, resource)
val test = ruleComputer.computer(PostmanExportRuleKeys.CLASS_POST_TEST, resource)
if (test.notNullOrBlank()) {
folder.setExt(ClassExportRuleKeys.POST_TEST.name(), test)
folder.setExt(PostmanExportRuleKeys.POST_TEST.name(), test)
}

return folder
Expand Down
Loading
Loading