Skip to content

Commit

Permalink
feat: add rules postman.format.after
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent committed Nov 11, 2023
1 parent bd226f6 commit f5227c9
Show file tree
Hide file tree
Showing 24 changed files with 722 additions and 230 deletions.
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

0 comments on commit f5227c9

Please sign in to comment.