Create resource tables when initializing project

This commit is contained in:
Akash Yadav
2022-08-19 23:30:45 +05:30
parent fe907be8fe
commit 53cab42b72
9 changed files with 36 additions and 18 deletions
+1
View File
@@ -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')
+1
View File
@@ -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)
}
@@ -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)
}
}
}
+1 -1
View File
@@ -21,7 +21,7 @@ plugins {
}
android {
namespace "com.itsaky.androidide.aaptcompiler"
namespace "com.itsaky.androidide.xml"
compileSdkVersion rootProject.ext.compileSdk
buildToolsVersion rootProject.ext.buildTools
@@ -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
}
/**
@@ -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
}
@@ -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.