Skip to content

Commit 513b494

Browse files
committed
修复问题
1 parent d982ec5 commit 513b494

File tree

7 files changed

+60
-6
lines changed

7 files changed

+60
-6
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ jobs:
4949
tag_name: v${{ env.version_name }}
5050
release_name: v${{ env.version_name }}
5151
body: |
52-
新版功能生成dart model
53-
1.add the plugin.translationKey=trn,after generate the translation will end width 'trn'
52+
r.dart文件生成优化,如果已经有定义了另外的名字了,保持这个名字,而不再自动生成新的名字
53+
r.dart file generation optimization, if another name has been defined, retain this name instead of automatically generating a new name
5454
5555
- name: Upload Release Asset
5656
id: upload-release-asset

.idea/JsonBeanGenerator.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group 'org.example'
8-
version '1.9.0-SNAPSHOT'
8+
version '1.9.1-SNAPSHOT'
99

1010

1111
repositories {
6 KB
Binary file not shown.

src/main/.DS_Store

6 KB
Binary file not shown.

src/main/kotlin/com/awesome/plugins/assetgenerate/generator/FlutterAssetGenerator.kt

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ class FlutterAssetGenerator(
2020
private val targetDir: String?
2121
) :
2222
BaseAssetGenerator(mDirectory, ignoreDirs, targetDir) {
23+
24+
// 存储已定义的资源映射关系
25+
private val existingAssetMap = mutableMapOf<String, String>()
26+
2327
override fun generate() {
28+
// 读取现有的资源定义
29+
loadExistingAssets()
30+
2431
val builder = StringBuilder()
2532
val basePath = mDirectory.basePath()
2633
val isChildProject = basePath != mDirectory.project.basePath
@@ -81,17 +88,21 @@ class FlutterAssetGenerator(
8188

8289
mDirectory.files.map {
8390
val assetName = it.virtualFile.name
91+
val assetPath = "$currentDirVariableName${File.separator}$assetName"
92+
8493
if (isFont(it)) {
8594
builder.append(
8695
" static const String ${
8796
it.virtualFile.nameWithoutExtension.clearSymbol().toCamel()
8897
} = '${it.virtualFile.nameWithoutExtension}';\n"
8998
)
9099
} else {
100+
// 使用已存在的名称或生成新的名称
101+
val variableName = existingAssetMap[assetPath]
102+
?: it.virtualFile.nameWithoutExtension.clearSymbol().toCamel()
103+
91104
builder.append(
92-
" static const String ${
93-
it.virtualFile.nameWithoutExtension.clearSymbol().toCamel()
94-
} = '$currentDirVariableName${File.separator}$assetName';\n"
105+
" static const String $variableName = '$assetPath';\n"
95106
)
96107
}
97108
}
@@ -113,4 +124,30 @@ class FlutterAssetGenerator(
113124
private fun isContainFile(mDirectory: PsiDirectory): Boolean {
114125
return mDirectory.files.isNotEmpty() || mDirectory.subdirectories.isNotEmpty()
115126
}
127+
128+
private fun loadExistingAssets() {
129+
val rDartFile = File(getRDartFilePath())
130+
if (!rDartFile.exists()) return
131+
132+
// 匹配资源定义语句
133+
val pattern = "static const String ([a-zA-Z0-9_]+) = '(.*?)'"
134+
val regex = Regex(pattern)
135+
136+
rDartFile.readText().lines().forEach { line ->
137+
regex.find(line)?.let { result ->
138+
val name = result.groupValues[1]
139+
val path = result.groupValues[2]
140+
existingAssetMap[path] = name
141+
}
142+
}
143+
}
144+
145+
private fun getRDartFilePath(): String {
146+
val newTargetDir = if (!targetDir.isNullOrEmpty()) {
147+
targetDir
148+
} else {
149+
"${File.separator}lib${File.separator}res"
150+
}
151+
return "${mDirectory.parent?.virtualFile?.path}$newTargetDir${File.separator}r.dart"
152+
}
116153
}

0 commit comments

Comments
 (0)