Skip to content

Commit

Permalink
J2D支持Json注释
Browse files Browse the repository at this point in the history
  • Loading branch information
YangLang116 committed Dec 17, 2024
1 parent 6b4281b commit 20f0fcc
Show file tree
Hide file tree
Showing 11 changed files with 2,266 additions and 67 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [未完待续]

## 4.2.5 - 2024-12-17

- `Code`: 支持J2D保留原始json
- `Base`: 修复R文件字段异常

## 4.2.4 - 2024-11-25

- `Base`: Bug修复
Expand Down
4 changes: 2 additions & 2 deletions config/iflutter-version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "4.2.4",
"title": "iFlutter 4.2.4 available",
"version": "4.2.5",
"title": "iFlutter 4.2.5 available",
"subtitle": "",
"content": "Faster 🚀 Stronger 💪",
"detailBtnText": "Details",
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pluginName=iFlutter
pluginVersion=4.2.4
pluginVersion=4.2.5
gradleVersion=17
pluginSinceBuild=223
platformType=IC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.OrderJSONObject;
import template.PluginTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class J2DHandler {

Expand All @@ -28,8 +28,8 @@ public J2DHandler(@NotNull Project project) {
@NotNull
public String genCode(@NotNull String className, @NotNull String jsonData, boolean keepComment) throws JSONException {
List<ClassEntity> classList = new ArrayList<>();
JSONObject orderObj = JsonUtils.createOrderObj(jsonData);
createAndSaveClass(className, orderObj, keepComment, classList);
JSONObject jsonObject = new JSONObject(jsonData);
createAndSaveClass(className, jsonObject, keepComment, classList);

StringBuilder fileContentBuilder = new StringBuilder();
for (int i = classList.size() - 1; i >= 0; i--) {
Expand All @@ -39,24 +39,19 @@ public String genCode(@NotNull String className, @NotNull String jsonData, boole
return fileContentBuilder.toString();
}

private void createAndSaveClass(@NotNull String className,
@NotNull JSONObject jsonObject,
boolean keepComment,
@NotNull List<ClassEntity> classList) {
List<J2DFieldDescriptor> fieldList = parseFieldList(jsonObject, classList, keepComment);
String comment = getComment(jsonObject, keepComment);
private void createAndSaveClass(@NotNull String className, @NotNull JSONObject jsonObj, boolean keepComment, @NotNull List<ClassEntity> classList) {
List<J2DFieldDescriptor> fieldList = parseFieldList(jsonObj, keepComment, classList);
String comment = getComment(jsonObj, keepComment);
String content = PluginTemplate.getJ2DContent(project, className, comment, fieldList);
classList.add(new ClassEntity(className, content));
}

@NotNull
private List<J2DFieldDescriptor> parseFieldList(@NotNull JSONObject jsonObject,
@NotNull List<ClassEntity> classList,
boolean keepComment) {
private List<J2DFieldDescriptor> parseFieldList(@NotNull JSONObject rawObj, boolean keepComment, @NotNull List<ClassEntity> classList) {
OrderJSONObject orderObj = JsonUtils.toOrderJsonObject(rawObj);
List<J2DFieldDescriptor> fieldList = new ArrayList<>();
Set<String> fieldSet = jsonObject.keySet();
for (String fieldName : fieldSet) {
Object fieldValue = jsonObject.get(fieldName);
for (String fieldName : orderObj.keySet()) {
Object fieldValue = orderObj.get(fieldName);
J2DFieldDescriptor field = parseField(fieldName, fieldValue, keepComment, classList, name -> ClassUtils.getClassName(name) + "Entity");
if (field == null) continue;
fieldList.add(field);
Expand All @@ -65,10 +60,7 @@ private List<J2DFieldDescriptor> parseFieldList(@NotNull JSONObject jsonObject,
}

@Nullable
private J2DFieldDescriptor parseField(@NotNull String key, @NotNull Object value,
boolean keepComment,
@NotNull List<ClassEntity> classList,
@NotNull ClassNameFactory classNameFactory) {
private J2DFieldDescriptor parseField(@NotNull String key, @NotNull Object value, boolean keepComment, @NotNull List<ClassEntity> classList, @NotNull ClassNameFactory classNameFactory) {
if (value instanceof String) {
return J2DFieldDescriptor.prime(key, "String");
} else if (value instanceof Boolean) {
Expand All @@ -90,9 +82,7 @@ private J2DFieldDescriptor parseField(@NotNull String key, @NotNull Object value
return null;
}

private String getClassName(@NotNull String name,
@NotNull ClassNameFactory factory,
@NotNull List<ClassEntity> classList) {
private String getClassName(@NotNull String name, @NotNull ClassNameFactory factory, @NotNull List<ClassEntity> classList) {
int index = 1;
String candidate = factory.create(name);
while (true) {
Expand All @@ -110,22 +100,22 @@ private String getClassName(@NotNull String name,
}

@Nullable
private String getComment(@NotNull JSONObject obj, boolean keepComment) {
private String getComment(@NotNull JSONObject rawJson, boolean keepComment) {
if (!keepComment) {
return null;
}
JSONObject tempObj = new JSONObject();
for (String key : obj.keySet()) {
Object value = obj.get(key);
OrderJSONObject orderJson = new OrderJSONObject();
for (String key : JsonUtils.getOrderKeySet(rawJson)) {
Object value = rawJson.get(key);
if (value instanceof JSONObject) {
continue;
}
if (value instanceof JSONArray && !((JSONArray) value).isEmpty() && ((JSONArray) value).get(0) instanceof JSONObject) {
continue;
}
tempObj.put(key, value);
orderJson.put(key, value);
}
String[] lines = tempObj.toString(4).split("\n");
String[] lines = orderJson.toString(4).split("\n");
StringBuilder commentSb = new StringBuilder();
for (String line : lines) {
commentSb.append("// ").append(line).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private Box createClassNameBox() {
@NotNull
private Box createJsonDataBox() {
Box jsonContainer = Box.createVerticalBox();
jsonContainer.setBorder(JBUI.Borders.empty(10, 0, 5, 0));
jsonContainer.setBorder(JBUI.Borders.empty(10, 0));
JLabel jsonLabel = new JLabel("Json Data: ");
jsonLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
jsonContainer.add(jsonLabel);
Expand All @@ -97,7 +97,7 @@ private Box createJsonDataBox() {
private Box createBottomBar() {
Box btnBox = Box.createHorizontalBox();
btnBox.add(Box.createHorizontalGlue());
btnBox.add(keepCommentBox = new JBCheckBox("Keep Comments"));
btnBox.add(keepCommentBox = new JBCheckBox("Keep Comments", true));
btnBox.add(Box.createHorizontalStrut(10));
btnBox.add(createBtn("Format", this::toFormat));
btnBox.add(Box.createHorizontalStrut(10));
Expand Down Expand Up @@ -147,8 +147,9 @@ private void genCode(@Nullable String className, @Nullable String jsonData) {
ToastUtils.make(project, MessageType.ERROR, "dart bean is exist");
return;
}
boolean keepComment = keepCommentBox.isSelected();
try {
String code = handler.genCode(className, jsonData, keepCommentBox.isSelected());
String code = handler.genCode(className, jsonData, keepComment);
genDartFile(fileName, code);
close(OK_EXIT_CODE);
} catch (Exception e) {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/xtu/plugin/flutter/base/utils/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ public static String splashName(@NotNull String name) {
StringBuilder resultSb = new StringBuilder();
for (int i = 0, j = name.length(); i < j; i++) {
char c = name.charAt(i);
if (i != 0 && Character.isUpperCase(c)
&& i + 1 < j && Character.isLowerCase(name.charAt(i + 1))) {
if (i != 0 && Character.isUpperCase(c) && i + 1 < j && Character.isLowerCase(name.charAt(i + 1))) {
resultSb.append("_");
}
resultSb.append(Character.toLowerCase(c));
}
return resultSb.toString();
}

public static String getClassName(String str) {
public static String getClassName(@NotNull String str) {
String name = toCamelCase(str);
if (name == null) return null;
return name.substring(0, 1).toUpperCase() + name.substring(1);
}

public static String toCamelCase(String name) {
if (name == null) return null;
public static String toCamelCase(@NotNull String name) {
int len = name.length();
StringBuilder sb = new StringBuilder();
boolean needUpCase = false;
Expand All @@ -43,5 +40,10 @@ public static String toCamelCase(String name) {
}
return sb.toString();
}

@NotNull
public static String getFieldName(@NotNull String name) {
return name.toUpperCase().replace("-", "_").replace(" ", "_");
}
}

27 changes: 17 additions & 10 deletions src/main/java/com/xtu/plugin/flutter/base/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.OrderJSONObject;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class JsonUtils {

Expand All @@ -13,19 +16,23 @@ public static String formatJson(@NotNull String jsonData) {
JSONArray jsonArray = new JSONArray(jsonData);
return jsonArray.toString(4);
} else {
JSONObject orderObj = createOrderObj(jsonData);
JSONObject rawObj = new JSONObject(jsonData);
OrderJSONObject orderObj = toOrderJsonObject(rawObj);
return orderObj.toString(4);
}
}

public static JSONObject createOrderObj(@NotNull String jsonData) {
JSONObject rawObj = new JSONObject(jsonData);
List<String> keys = new ArrayList<String>(rawObj.keySet());
Collections.sort(keys);
Map<String, Object> pairs = new LinkedHashMap<>();
for (String key : keys) {
pairs.put(key, rawObj.get(key));
public static OrderJSONObject toOrderJsonObject(@NotNull JSONObject rawObj) {
OrderJSONObject orderObj = new OrderJSONObject();
for (String key : getOrderKeySet(rawObj)) {
orderObj.put(key, rawObj.get(key));
}
return new JSONObject(pairs);
return orderObj;
}

public static List<String> getOrderKeySet(@NotNull JSONObject jsonObj) {
List<String> keySet = new ArrayList<>(jsonObj.keySet());
Collections.sort(keySet);
return keySet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.ArrayList;
import java.util.List;

///字体资源文件"font_res.dart"生成
/// 字体资源文件"font_res.dart"生成

public class DartFontFileGenerator {

Expand Down Expand Up @@ -92,7 +92,7 @@ private static String buildFileContent(@NotNull Project project,

@NotNull
public static String getFontVariant(String fontFamily) {
return fontFamily.toUpperCase().replace("-", "_");
return ClassUtils.getFieldName(fontFamily);
}

public static String getFileName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void generate(@NotNull Project project, @NotNull AssetInfoMetaEntity meta
List<String> usefulFileNameList = new ArrayList<>();
for (Map.Entry<String, List<String>> entry : assetCategory.entrySet()) {
String dirName = entry.getKey();
List<String> assetNames = entry.getValue();
String fileName = generateFile(project, meta, resVirtualDirectory, dirName, assetNames);
List<String> assetPathList = entry.getValue();
String fileName = generateFile(project, meta, resVirtualDirectory, dirName, assetPathList);
usefulFileNameList.add(fileName);
}
//删除无用文件
Expand Down Expand Up @@ -95,10 +95,10 @@ private String generateFile(@NotNull Project project,
@NotNull AssetInfoMetaEntity meta,
@NotNull VirtualFile rDirectory,
@NotNull String assetDirName,
@NotNull List<String> assetFileNames) {
@NotNull List<String> assetPathList) {
String className = getClassName(assetDirName);
LogUtils.info("DartRFileGenerator Class: " + className);
String content = buildFileContent(project, meta, className, assetFileNames);
String content = buildFileContent(project, meta, className, assetPathList);
String fileName = assetDirName.toLowerCase() + "_res.dart";
DartUtils.createDartFile(project, rDirectory, fileName, content);
return fileName;
Expand All @@ -108,15 +108,15 @@ private String generateFile(@NotNull Project project,
private String buildFileContent(@NotNull Project project,
@NotNull AssetInfoMetaEntity meta,
@NotNull String className,
@NotNull List<String> assetFileNames) {
@NotNull List<String> assetPathList) {
String resPrefix = AssetUtils.getResPrefix(project, meta.projectName);
List<ResFileTemplateData.Field> fieldList = new ArrayList<>();
fieldList.add(new ResFileTemplateData.Field("PLUGIN_NAME", meta.projectName));
fieldList.add(new ResFileTemplateData.Field("PLUGIN_VERSION", meta.projectVersion));
for (String assetFileName : assetFileNames) {
if (ignoreAsset(project, assetFileName)) continue;
String variantName = getResName(assetFileName);
fieldList.add(new ResFileTemplateData.Field(variantName, resPrefix + assetFileName));
for (String assetPath : assetPathList) {
if (ignoreAsset(project, assetPath)) continue;
String variantName = getResName(assetPath);
fieldList.add(new ResFileTemplateData.Field(variantName, resPrefix + assetPath));
}
ResFileTemplateData data = new ResFileTemplateData(className, fieldList);
return PluginTemplate.getResFileContent(data);
Expand Down Expand Up @@ -144,12 +144,13 @@ public static String getClassName(String assetDirName) {
}

@NotNull
public static String getResName(String assetFileName) {
int startIndex = assetFileName.lastIndexOf("/") + 1;
int endIndex = assetFileName.lastIndexOf(".");
public static String getResName(String assetPath) {
int startIndex = assetPath.lastIndexOf("/") + 1;
int endIndex = assetPath.lastIndexOf(".");
if (endIndex < startIndex) {
endIndex = assetFileName.length();
endIndex = assetPath.length();
}
return assetFileName.substring(startIndex, endIndex).toUpperCase().replace("-", "_");
String assetName = assetPath.substring(startIndex, endIndex);
return ClassUtils.getFieldName(assetName);
}
}
Loading

0 comments on commit 20f0fcc

Please sign in to comment.