mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
feat(templates): add helper methods to create values resources
This commit is contained in:
+6
-6
@@ -30,8 +30,8 @@ import java.io.File
|
||||
|
||||
class AndroidModuleTemplateBuilder : ModuleTemplateBuilder() {
|
||||
|
||||
val manifestBuilder = AndroidManifestBuilder()
|
||||
val resManager = AndroidModuleResManager()
|
||||
val manifest = AndroidManifestBuilder()
|
||||
val res = AndroidModuleResManager()
|
||||
|
||||
/**
|
||||
* Return the file path to `AndroidManifest.xml`.
|
||||
@@ -45,7 +45,7 @@ class AndroidModuleTemplateBuilder : ModuleTemplateBuilder() {
|
||||
* Configure the properties for `AndroidManifest.xml` file.
|
||||
*/
|
||||
fun manifest(block: AndroidManifestBuilder.() -> Unit) {
|
||||
manifestBuilder.apply(block)
|
||||
manifest.apply(block)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,7 +72,7 @@ class AndroidModuleTemplateBuilder : ModuleTemplateBuilder() {
|
||||
* @param configure Function to configure the resources.
|
||||
*/
|
||||
fun RecipeExecutor.res(configure: AndroidModuleResManager.() -> Unit) {
|
||||
resManager.apply(configure)
|
||||
res.apply(configure)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,7 +100,7 @@ class AndroidModuleTemplateBuilder : ModuleTemplateBuilder() {
|
||||
}
|
||||
|
||||
override fun RecipeExecutor.preConfig() {
|
||||
manifestBuilder.apply {
|
||||
manifest.apply {
|
||||
packageName = data.packageName
|
||||
isLibrary = data.type == AndroidLibrary
|
||||
}
|
||||
@@ -110,7 +110,7 @@ class AndroidModuleTemplateBuilder : ModuleTemplateBuilder() {
|
||||
}
|
||||
|
||||
override fun RecipeExecutor.postConfig() {
|
||||
manifestBuilder.apply {
|
||||
manifest.apply {
|
||||
generate(manifestFile())
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -29,11 +29,13 @@ import com.itsaky.androidide.templates.base.baseAndroidXDependencies
|
||||
* @param copyDefAssets Whether to copy the default Android assets (except `values` directory) to this module.
|
||||
* @param block The module configurator.
|
||||
*/
|
||||
fun ProjectTemplateBuilder.defaultAppModule(name: String = ":app", addAndroidX: Boolean = true,
|
||||
fun ProjectTemplateBuilder.defaultAppModule(name: String = ":app",
|
||||
addAndroidX: Boolean = true,
|
||||
copyDefAssets: Boolean = true,
|
||||
block: AndroidModuleTemplateConfigurator
|
||||
) {
|
||||
check(defModuleTemplate == null) { "Default module has been already configured" }
|
||||
check(
|
||||
defModuleTemplate == null) { "Default module has been already configured" }
|
||||
|
||||
val module = AndroidModuleTemplateBuilder().apply {
|
||||
_name = name
|
||||
|
||||
+9
-9
@@ -31,8 +31,8 @@ import com.itsaky.androidide.templates.RecipeExecutor
|
||||
import com.itsaky.androidide.templates.base.modules.android.ManifestActivity
|
||||
import com.itsaky.androidide.templates.base.modules.android.ManifestIcon
|
||||
import com.itsaky.androidide.xml.permissions.Permission
|
||||
import com.itsaky.androidide.xml.utils.XmlBuilder
|
||||
import java.io.File
|
||||
import com.android.xml.XmlBuilder as XMLBuilder
|
||||
|
||||
/**
|
||||
* Builder for building `AndroidManifest.xml` file for an Android module.
|
||||
@@ -102,12 +102,12 @@ class AndroidManifestBuilder {
|
||||
}
|
||||
|
||||
private fun manifestSrc(): String {
|
||||
return XMLBuilder().apply {
|
||||
return XmlBuilder().apply {
|
||||
buildManifest()
|
||||
}.withXmlDecl()
|
||||
}
|
||||
|
||||
private fun XMLBuilder.buildManifest() {
|
||||
private fun XmlBuilder.buildManifest() {
|
||||
createElement(TAG_MANIFEST) {
|
||||
attr(name = ANDROID_NS_NAME, value = ANDROID_URI, ns = XMLNS)
|
||||
|
||||
@@ -116,7 +116,7 @@ class AndroidManifestBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
private fun XMLBuilder.permissions() {
|
||||
private fun XmlBuilder.permissions() {
|
||||
for (permission in permissions) {
|
||||
createElement(TAG_USES_PERMISSION) {
|
||||
androidAttr("name", permission.constant)
|
||||
@@ -124,7 +124,7 @@ class AndroidManifestBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
private fun XMLBuilder.application() {
|
||||
private fun XmlBuilder.application() {
|
||||
if (isLibrary) {
|
||||
return
|
||||
}
|
||||
@@ -141,7 +141,7 @@ class AndroidManifestBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
private fun XMLBuilder.activities() {
|
||||
private fun XmlBuilder.activities() {
|
||||
for (activity in activities) {
|
||||
createElement(TAG_ACTIVITY) {
|
||||
androidAttr("name", activity.name(packageName))
|
||||
@@ -155,7 +155,7 @@ class AndroidManifestBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
private fun XMLBuilder.intentFilter() {
|
||||
private fun XmlBuilder.intentFilter() {
|
||||
createElement(TAG_INTENT_FILTER) {
|
||||
// action
|
||||
createElement(TAG_ACTION) {
|
||||
@@ -169,11 +169,11 @@ class AndroidManifestBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
private fun XMLBuilder.androidAttr(name: String, value: String) {
|
||||
private fun XmlBuilder.androidAttr(name: String, value: String) {
|
||||
androidAttribute(name, value)
|
||||
}
|
||||
|
||||
private fun XMLBuilder.attr(name: String, value: String, ns: String = "") {
|
||||
private fun XmlBuilder.attr(name: String, value: String, ns: String = "") {
|
||||
attribute(ns, name, value)
|
||||
}
|
||||
|
||||
|
||||
+24
-2
@@ -18,8 +18,9 @@
|
||||
package com.itsaky.androidide.templates.base.util
|
||||
|
||||
import com.android.aaptcompiler.ConfigDescription
|
||||
import com.android.xml.XmlBuilder
|
||||
import com.itsaky.androidide.xml.utils.XmlBuilder
|
||||
import com.itsaky.androidide.templates.SrcSet
|
||||
import com.itsaky.androidide.templates.SrcSet.Main
|
||||
import com.itsaky.androidide.templates.base.AndroidModuleTemplateBuilder
|
||||
import java.io.File
|
||||
|
||||
@@ -55,12 +56,33 @@ class AndroidModuleResManager {
|
||||
var name = type.dirName
|
||||
config.toString().also {
|
||||
if (it != "DEFAULT") {
|
||||
name += it
|
||||
name += "-${it}"
|
||||
}
|
||||
}
|
||||
return File(resDir(srcSet), name).also { it.mkdirs() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new XML values resource file for the given [resource type][type] and
|
||||
* the [configuration][config]. The `<resources>` tag is already appended
|
||||
* to the [XmlBuilder].
|
||||
*
|
||||
* @param name The name of the resource without the `.xml` extension.
|
||||
* @param type The resource type.
|
||||
* @param srcSet The source set.
|
||||
* @param config The configuration for the resource type.
|
||||
*/
|
||||
fun AndroidModuleTemplateBuilder.createValuesResource(name: String,
|
||||
type: ResourceType,
|
||||
srcSet: SrcSet = Main,
|
||||
config: ConfigDescription = ConfigDescription(),
|
||||
configure: XmlBuilder.() -> Unit
|
||||
) {
|
||||
return createXmlResource(name, type, srcSet, config) {
|
||||
createElement("resources", configure)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new XML resource file for the given [resource type][ResourceType] and the [configuration][ConfigDescription].
|
||||
*
|
||||
|
||||
+23
-3
@@ -17,7 +17,7 @@
|
||||
|
||||
package com.itsaky.androidide.templates.base.util
|
||||
|
||||
import com.android.xml.XmlBuilder
|
||||
import com.itsaky.androidide.xml.utils.XmlBuilder
|
||||
|
||||
/**
|
||||
* Creates a new element in the [XmlBuilder] with the given [name].
|
||||
@@ -30,9 +30,29 @@ fun XmlBuilder.createElement(name: String, configure: XmlBuilder.() -> Unit) {
|
||||
endTag(name)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new `<string>` resource entry to this [XmlBuilder].
|
||||
*
|
||||
* @param name The name of the string resource.
|
||||
* @param value The value of the XML resource.
|
||||
*/
|
||||
fun XmlBuilder.addStringRes(name: String, value: String) {
|
||||
createElement("string") {
|
||||
attribute("name", name)
|
||||
text(value)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the generated XML string from the [XmlBuilder] with XML declaration prepended.
|
||||
*/
|
||||
fun XmlBuilder.withXmlDecl() : String {
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n${toString()}"
|
||||
fun XmlBuilder.withXmlDecl(): String {
|
||||
return toString().withXmlDecl()
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepends XML declaration to this string.
|
||||
*/
|
||||
fun String.withXmlDecl(): String {
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n${this}"
|
||||
}
|
||||
Reference in New Issue
Block a user