Skip to content

Commit c85bf67

Browse files
authored
Add CLASS_NAME support and fix Pebble template rendering for plugin templates (#1144)
- Add CLASS_NAME token replacement in ZipRecipeExecutor for dynamic class naming in file paths and template content - Add KEY_CLASS_NAME constant to ZipTemplateConstants - Add defaultAppName field to TemplateJson for per-template project name defaults
1 parent cb05a7d commit c85bf67

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

templates-impl/src/main/java/com/itsaky/androidide/templates/impl/zip/ZipJson.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ data class TemplateJson(
1010
val version: String?,
1111
val tooltipTag: String = "",
1212
val defaultSaveLocation: String? = null,
13+
val defaultAppName: String? = null,
1314
val parameters: ParametersJson? = null,
1415
val system: SystemParametersJson? = null
1516
)

templates-impl/src/main/java/com/itsaky/androidide/templates/impl/zip/ZipRecipeExecutor.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class ZipRecipeExecutor(
3737

3838
companion object {
3939
private val log = LoggerFactory.getLogger(ZipRecipeExecutor::class.java)
40+
private val CLASS_NAME_PATTERN = Regex("[^a-zA-Z0-9]")
4041
}
4142

4243
override fun execute(
@@ -73,7 +74,9 @@ class ZipRecipeExecutor(
7374
.syntax(customSyntax)
7475
.build()
7576

76-
val (identifiers, warnings) = metaJson.pebbleParams(data, defModule, params)
77+
val className = data.name.replace(CLASS_NAME_PATTERN, "")
78+
val (baseIdentifiers, warnings) = metaJson.pebbleParams(data, defModule, params)
79+
val identifiers = baseIdentifiers + (KEY_CLASS_NAME to className)
7780
if (warnings.isNotEmpty()) {
7881
warn("Identifier warnings: ${warnings.joinToString(System.lineSeparator())}")
7982
}
@@ -98,6 +101,7 @@ class ZipRecipeExecutor(
98101

99102
val relativePath = normalized.removePrefix("$basePath/")
100103
.replace(packageName.value, defModule.packageName.replace(".", "/"))
104+
.replace(KEY_CLASS_NAME, className)
101105

102106
val outFile = File(projectDir, relativePath.removeSuffix(TEMPLATE_EXTENSION)).canonicalFile
103107

templates-impl/src/main/java/com/itsaky/androidide/templates/impl/zip/ZipTemplateConstants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const val DELIM_COMMENT_CLOSE = "#}"
1717

1818
const val KEY_PACKAGE_NAME = "PACKAGE_NAME"
1919
const val KEY_APP_NAME = "APP_NAME"
20+
const val KEY_CLASS_NAME = "CLASS_NAME"
2021
const val KEY_SAVE_LOCATION = "SAVE_LOCATION"
2122
const val KEY_AGP_VERSION = "AGP_VERSION"
2223
const val KEY_KOTLIN_VERSION = "KOTLIN_VERSION"

templates-impl/src/main/java/com/itsaky/androidide/templates/impl/zip/ZipTemplateReader.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.itsaky.androidide.templates.TextFieldWidget
1313
import com.itsaky.androidide.templates.Widget
1414
import com.itsaky.androidide.templates.base.baseZipProject
1515
import com.itsaky.androidide.templates.booleanParameter
16+
import com.itsaky.androidide.templates.projectNameParameter
1617
import com.itsaky.androidide.templates.stringParameter
1718
import com.itsaky.androidide.utils.FeatureFlags
1819
import org.slf4j.LoggerFactory
@@ -86,6 +87,9 @@ object ZipTemplateReader {
8687
}
8788

8889
val project = baseZipProject(
90+
projectName = projectNameParameter {
91+
metaJson.defaultAppName?.let { default = it }
92+
},
8993
showLanguage = (metaJson.parameters?.optional?.language != null),
9094
showMinSdk = (metaJson.parameters?.optional?.minsdk != null),
9195
showPackageName = (metaJson.parameters?.required?.packageName != null),

0 commit comments

Comments
 (0)