mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
Create resource tables when initializing project
This commit is contained in:
@@ -133,6 +133,7 @@ dependencies {
|
||||
|
||||
// Local projects here
|
||||
implementation project (':build-tools:aaptcompiler')
|
||||
implementation project (':build-tools:xml-utils')
|
||||
implementation project (':eventbus-android')
|
||||
implementation project (':eventbus-events')
|
||||
implementation project (':build-tools:projects')
|
||||
|
||||
@@ -52,6 +52,7 @@ dependencies {
|
||||
implementation project (path: ':shared')
|
||||
implementation project (path: ':logger')
|
||||
implementation project (path: ':build-tools:javac-services')
|
||||
implementation project (path: ':build-tools:xml-utils')
|
||||
|
||||
implementation common.io
|
||||
implementation google.guava
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*/
|
||||
package com.itsaky.androidide.projects
|
||||
|
||||
import android.content.Context
|
||||
import com.itsaky.androidide.eventbus.events.EventReceiver
|
||||
import com.itsaky.androidide.eventbus.events.file.FileCreationEvent
|
||||
import com.itsaky.androidide.eventbus.events.file.FileDeletionEvent
|
||||
@@ -30,6 +29,7 @@ import com.itsaky.androidide.projects.builder.BuildService
|
||||
import com.itsaky.androidide.projects.util.ProjectTransformer
|
||||
import com.itsaky.androidide.tooling.api.IProject
|
||||
import com.itsaky.androidide.utils.ILogger
|
||||
import com.itsaky.androidide.xml.res.ResourceTableRegistry
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
@@ -56,7 +56,13 @@ object ProjectManager : EventReceiver {
|
||||
this.rootProject!!
|
||||
.subModules
|
||||
.filterIsInstance(ModuleProject::class.java)
|
||||
.forEach(ModuleProject::indexSourcesAndClasspaths)
|
||||
.forEach {
|
||||
it.indexSourcesAndClasspaths()
|
||||
|
||||
if (it is AndroidModule) {
|
||||
it.readResources()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +208,7 @@ object ProjectManager : EventReceiver {
|
||||
if (!isResource(file)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
generateSources(builder)
|
||||
}
|
||||
|
||||
|
||||
+12
@@ -34,6 +34,7 @@ import com.itsaky.androidide.tooling.api.messages.result.SimpleVariantData
|
||||
import com.itsaky.androidide.tooling.api.model.AndroidModule.Companion.FD_INTERMEDIATES
|
||||
import com.itsaky.androidide.tooling.api.model.GradleTask
|
||||
import com.itsaky.androidide.utils.ILogger
|
||||
import com.itsaky.androidide.xml.res.ResourceTableRegistry
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
@@ -232,4 +233,15 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the resource files are creates the [com.android.aaptcompiler.ResourceTable] instances for
|
||||
* the corresponding resource directories.
|
||||
*/
|
||||
fun readResources() {
|
||||
val registry = ResourceTableRegistry.getInstance()
|
||||
for (resourceDirectory in getResourceDirectories()) {
|
||||
registry.forResourceDir(resourceDirectory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ plugins {
|
||||
}
|
||||
|
||||
android {
|
||||
namespace "com.itsaky.androidide.aaptcompiler"
|
||||
namespace "com.itsaky.androidide.xml"
|
||||
compileSdkVersion rootProject.ext.compileSdk
|
||||
buildToolsVersion rootProject.ext.buildTools
|
||||
|
||||
|
||||
+1
-3
@@ -29,9 +29,7 @@ import java.io.File
|
||||
interface ResourceTableRegistry {
|
||||
|
||||
companion object {
|
||||
private val instance = DefaultResourceTableRegistry()
|
||||
|
||||
@JvmStatic fun getInstance(): ResourceTableRegistry = instance
|
||||
@JvmStatic fun getInstance(): ResourceTableRegistry = DefaultResourceTableRegistry
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+3
-4
@@ -34,7 +34,7 @@ import java.util.concurrent.ConcurrentHashMap
|
||||
*
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
internal class DefaultResourceTableRegistry : ResourceTableRegistry {
|
||||
internal object DefaultResourceTableRegistry : ResourceTableRegistry {
|
||||
|
||||
private val log = ILogger.newInstance(javaClass.simpleName)
|
||||
|
||||
@@ -52,6 +52,7 @@ internal class DefaultResourceTableRegistry : ResourceTableRegistry {
|
||||
}
|
||||
|
||||
private fun createTable(resDir: File): ResourceTable? {
|
||||
log.info("Creating resource table for resource directory $resDir")
|
||||
val values = File(resDir, "values")
|
||||
if (!values.exists()) {
|
||||
return null
|
||||
@@ -61,9 +62,7 @@ internal class DefaultResourceTableRegistry : ResourceTableRegistry {
|
||||
val table = ResourceTable()
|
||||
val options =
|
||||
TableExtractorOptions(translatable = true, errorOnPositionalArgs = false, visibility = PUBLIC)
|
||||
values
|
||||
.listFiles { file -> file.isFile && file.extension == "xml" }
|
||||
?.forEach { updateFromFile(it = it, table = table, options = options, logger = logger) }
|
||||
updateFromDirectory(values, table, options, logger)
|
||||
return table
|
||||
}
|
||||
|
||||
|
||||
+8
-7
@@ -14,7 +14,6 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.itsaky.androidide.xml.res
|
||||
|
||||
import android.graphics.Color
|
||||
@@ -26,10 +25,10 @@ import com.android.aaptcompiler.ResourceName
|
||||
import com.android.aaptcompiler.android.ResValue.DataType
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.itsaky.androidide.xml.res.internal.DefaultResourceTableRegistry
|
||||
import java.io.File
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import java.io.File
|
||||
|
||||
/** @author Akash Yadav */
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@@ -45,15 +44,17 @@ class ResourceTableRegistryTest {
|
||||
fun `test with simple framework resource parsing`() {
|
||||
val androidJar = findAndroidJar()
|
||||
val registry = ResourceTableRegistry.getInstance()
|
||||
val resourceTable = registry.forResourceDir(File(androidJar.parentFile, "data/res"))
|
||||
val resDir = File(androidJar.parentFile, "data/res")
|
||||
val resourceTable = registry.forResourceDir(resDir)
|
||||
|
||||
assertThat(resourceTable).isNotNull()
|
||||
|
||||
|
||||
// Should return same instance unless updated
|
||||
assertThat(registry.forResourceDir(resDir)).isEqualTo(resourceTable)
|
||||
|
||||
resourceTable.apply {
|
||||
assertThat(this!!.packages).hasSize(1)
|
||||
this.packages.first().apply {
|
||||
assertThat(this.name).isEqualTo("")
|
||||
}
|
||||
this.packages.first().apply { assertThat(this.name).isEqualTo("") }
|
||||
}
|
||||
|
||||
resourceTable!!.findResource(ResourceName(pck = "", type = STRING, entry = "ok")).apply {
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user