Skip to content

Commit

Permalink
Merge branch 'nashorn'
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjerry committed Jan 11, 2024
2 parents 1d10582 + 9e41b9a commit bf6acfd
Show file tree
Hide file tree
Showing 37 changed files with 270 additions and 205 deletions.
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
2. 生成的代码有误?
答: 插件自带的三个脚本通过参照 curl 文档的部分例子进行测试. curl 是一个复杂的工具, 难免有遗漏.
此外, 因为库版本原因, 生成的代码可能包含无效的 API. 对于生成代码问题你只需要修改脚本即可.
3. 为什么生成的代码格式混乱?
3. 为什么生成的代码格式混乱?
答: 请查看插件选项中是否勾选了自动格式化和保持缩进. 如果格式化失败, 请检查 IDEA 是否安装了对应语言的支持插件.
4. 脚本执行慢?
答: 脚本每次修改后执行可能会慢些, 但之后会从编译缓存中执行脚本.
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pluginName=Headers
pluginRepositoryUrl=https://github.com/bitjerry/Headers

# SemVer format -> https://semver.org
pluginVersion=2.0.0
pluginVersion=2.0.1

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild=203
Expand All @@ -14,8 +14,8 @@ pluginUntilBuild=
# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType=IC
platformVersion=2020.3
platformPlugins=PythonCore:203.5981.165, java
#platformPlugins =
#platformPlugins=PythonCore:203.5981.165, java
platformPlugins =

# Gradle Releases -> https://github.com/gradle/gradle/releases
gradleVersion = 8.4
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/su/gov/headers/HeadersBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public final class HeadersBundle extends AbstractBundle {

public static final String BUNDLE = "messages.HeadersBundle";

public static final HeadersBundle INSTANCE = new HeadersBundle();
public static final HeadersBundle INSTANCE = new HeadersBundle();

public HeadersBundle() {
super(BUNDLE);
}

public static String message(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object ... params) {
public static String message(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object... params) {
return INSTANCE.getMessage(key, params);
}

Expand Down
57 changes: 41 additions & 16 deletions src/main/java/su/gov/headers/HeadersPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,60 @@
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.notification.*;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.impl.NotificationFullContent;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.Constraints;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import org.jetbrains.annotations.NotNull;
import su.gov.headers.actions.CurlAction;
import su.gov.headers.icons.PluginIcon;
import su.gov.headers.setting.SettingsPersistentState;
import su.gov.headers.transform.TransformScriptModel;

import java.util.List;
import java.util.Objects;

public class HeadersPlugin implements StartupActivity {

private static final Logger LOGGER = Logger.getInstance(HeadersPlugin.class);
public static final String PLUGIN_ID = "su.gov.Header";

public static final IdeaPluginDescriptor descriptor = PluginManagerCore.getPlugin(PluginId.getId(PLUGIN_ID));
public static final PluginId ID = PluginId.getId(PLUGIN_ID);

private static String version;
public @NotNull
static final IdeaPluginDescriptor DESCRIPTOR = Objects.requireNonNull(PluginManagerCore.getPlugin(ID));

private static String name;
private static DefaultActionGroup CURL_ACTION_GROUP;

public static @NotNull String getVersion() {
if (version == null) {
assert descriptor != null;
version = descriptor.getVersion();

public static void registerActions(ActionManager manager, List<TransformScriptModel> models) {
unRegisterActions(manager, models);
LOGGER.debug("Registering " + models + "to group:" + CURL_ACTION_GROUP);
for (int i = models.size() - 1; i >= 0; i--) {
CurlAction action = new CurlAction(models.get(i));
manager.registerAction(action.getId(), action, HeadersPlugin.ID);
CURL_ACTION_GROUP.add(action, Constraints.FIRST);
}
return version;
}

public static @NotNull String getName() {
if (name == null) {
assert descriptor != null;
name = descriptor.getName();
public static void unRegisterActions(ActionManager manager, List<TransformScriptModel> models) {
LOGGER.debug("Unregistering " + models + "to group:" + CURL_ACTION_GROUP);
for (TransformScriptModel model : models) {
AnAction action = manager.getActionOrStub(model.getId());
if (action != null) {
CURL_ACTION_GROUP.remove(action);
manager.unregisterAction(model.getId());
}
}
return name;

}

static class WelcomeNotification extends Notification implements NotificationFullContent {
Expand All @@ -63,9 +84,13 @@ public WelcomeNotification() {

@Override
public void runActivity(@NotNull Project project) {
ActionManager manager = ActionManager.getInstance();
CURL_ACTION_GROUP = (DefaultActionGroup) manager.getAction("Headers.Group.CurlGroup");
SettingsPersistentState state = SettingsPersistentState.getInstance();
if (!getVersion().equals(state.getVersion())) {
state.setVersion(getVersion());
registerActions(manager, state.getTransformModels());
String version = state.getVersion();
if (!DESCRIPTOR.getVersion().equals(version)) {
state.setVersion(DESCRIPTOR.getVersion());
new WelcomeNotification().notify(project);
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/su/gov/headers/actions/CookieAction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
*
* @Time: 2021/12/28 21:38
* @author Mr.lin
* @File: Cookies.java
Expand All @@ -23,18 +22,17 @@ public class CookieAction extends TransformAction {
public String transform(String text) {
LinkedHashMap<String, String> cookieMap = new LinkedHashMap<>();
String[] cookieItems = text.split("\\s*;\\s*");
for (String cookieItem : cookieItems){
for (String cookieItem : cookieItems) {
String[] cookieKV = cookieItem.split("\\s*=\\s*", 2);
if (cookieKV.length != 2){
if (cookieKV.length != 2) {
break;
}
cookieMap.put(cookieKV[0], cookieKV[1]);
}
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(cookieMap);
}
catch (JsonProcessingException e){
} catch (JsonProcessingException e) {
LOGGER.error("Transform request cookie failure", e);
NotificationUtils.error(HeadersBundle.message("error.transform.cookie"), project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @Version V1.0.0
* @Copyright © 2023 by Mr.lin. All rights reserved.
*/
package su.gov.headers.actions.curlAction;
package su.gov.headers.actions;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
Expand All @@ -31,7 +31,6 @@ public CurlAction(TransformScriptModel model) {
super(model);
}


@Override
public String transform(String curlCommand) {
CurlObject curlObject;
Expand Down Expand Up @@ -63,16 +62,15 @@ public String transform(String curlCommand) {
return null;
}

try(Scope scope = Scope.enter()){
try (Scope scope = Scope.enter()) {
script.eval(scope);
String result = scope.call("transform", null, curlObject.getRoot().getObject());
if (result == null) {
NotificationUtils.error(HeadersBundle.message("error.transform.no.transform.function"), project);
return null;
}
return result;
}
catch (Exception e){
} catch (Exception e) {
LOGGER.error("An error occurred while running the script.", e);
NotificationUtils.error(HeadersBundle.message("error.transform.script.runtime"), project);
return null;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/su/gov/headers/actions/FormDataAction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
*
* @Time: 2022/1/10 20:39
* @Author: Mr.lin
* @File: FormData.java
Expand All @@ -24,16 +23,16 @@ public String transform(String text) {
LinkedHashMap<String, String> formDataMap = new LinkedHashMap<>();
ObjectMapper objectMapper = new ObjectMapper();
String[] formDataItems = text.split("\\s*&\\s*");
for (String formDataItem : formDataItems){
for (String formDataItem : formDataItems) {
String[] formDataKV = formDataItem.split("\\s*=\\s*", 2);
if (formDataKV.length != 2){
if (formDataKV.length != 2) {
break;
}
formDataMap.put(formDataKV[0], formDataKV[1]);
}
try{
try {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(formDataMap);
}catch (JsonProcessingException e){
} catch (JsonProcessingException e) {
LOGGER.error("Transform request form data failure", e);
NotificationUtils.error(HeadersBundle.message("error.transform.form.data", e.getMessage()), project);
}
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/su/gov/headers/actions/HeaderAction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
*
* @Time: 2021/12/28 21:38
* @author Mr.lin
* @File: Headers.java
Expand All @@ -20,39 +19,35 @@ public class HeaderAction extends TransformAction {

private static final Logger LOGGER = Logger.getInstance(HeaderAction.class);

private static void parseHeader(String head, HashMap<String, String> headMap){
private static void parseHeader(String head, HashMap<String, String> headMap) {
String key;
String[] valueParts;
String[] headerParts = head.split("\\s", 2);
if ( headerParts.length != 2 ){
if (headerParts.length != 2) {
return;
}
String firstPart = headerParts[0];
String lastPart = headerParts[1];
if (lastPart.isEmpty()){
if (lastPart.isEmpty()) {
return;
}
else if (firstPart.isEmpty()){
} else if (firstPart.isEmpty()) {
parseHeader(lastPart, headMap);
return;
}
if (firstPart.endsWith(":")) {
key = firstPart.substring(0, firstPart.length() - 1);
}
else if (lastPart.startsWith(":")){
} else if (lastPart.startsWith(":")) {
key = firstPart;
lastPart = lastPart.substring(1).trim();
}
else {
} else {
key = firstPart;
}

valueParts = lastPart.split("\\s*?\n\\s*", 2);
if ( valueParts.length == 2 ){
if (valueParts.length == 2) {
headMap.put(key, valueParts[0].trim());
parseHeader(valueParts[1], headMap);
}
else {
} else {
headMap.put(key, lastPart.trim());
}
}
Expand All @@ -64,7 +59,7 @@ public String transform(String text) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(headers);
}catch (JsonProcessingException e){
} catch (JsonProcessingException e) {
LOGGER.error("Transform request headers failure", e);
NotificationUtils.error(HeadersBundle.message("error.transform.header", e.getMessage()), project);
}
Expand Down
35 changes: 0 additions & 35 deletions src/main/java/su/gov/headers/actions/curlAction/CurlGroup.java

This file was deleted.

33 changes: 17 additions & 16 deletions src/main/java/su/gov/headers/curl/CurlObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
*/
package su.gov.headers.curl;

import com.intellij.openapi.diagnostic.Logger;
import su.gov.headers.scripts.objects.JSArrayWarp;
import su.gov.headers.scripts.objects.JSObjectWarp;

public class CurlObject {

private final static Logger LOGGER = Logger.getInstance(CurlObject.class);

private final JSObjectWarp rootObject;

private JSObjectWarp params;
Expand Down Expand Up @@ -64,26 +61,29 @@ public void setUrl(String url) {
}

public JSObjectWarp getParams() {
if (params == null) {
params = JSObjectWarp.newObject();
rootObject.set("params", params.getObject());
if (params != null) {
return params;
}
params = JSObjectWarp.newObject();
rootObject.set("params", params.getObject());
return params;
}

public JSObjectWarp getHeaders() {
if (headers == null) {
headers = JSObjectWarp.newObject();
rootObject.set("headers", headers.getObject());
if (headers != null) {
return headers;
}
headers = JSObjectWarp.newObject();
rootObject.set("headers", headers.getObject());
return headers;
}

public JSObjectWarp getCookies() {
if (cookies == null) {
cookies = JSObjectWarp.newObject();
rootObject.set("cookies", cookies.getObject());
if (cookies != null) {
return cookies;
}
cookies = JSObjectWarp.newObject();
rootObject.set("cookies", cookies.getObject());
return cookies;
}

Expand All @@ -96,11 +96,12 @@ public void setData(String data) {
}

public Form getForm() {
if (form == null) {
JSArrayWarp jsArrayWarp = JSArrayWarp.newObject();
form = new Form(jsArrayWarp);
rootObject.set("form", jsArrayWarp.getObject());
if (form != null) {
return form;
}
JSArrayWarp jsArrayWarp = JSArrayWarp.newObject();
form = new Form(jsArrayWarp);
rootObject.set("form", jsArrayWarp.getObject());
return form;
}

Expand Down
Loading

0 comments on commit bf6acfd

Please sign in to comment.