Skip to content

Commit

Permalink
Merge pull request #7 from LikeTheSalad/release/2.2.0
Browse files Browse the repository at this point in the history
Release/2.2.2
  • Loading branch information
LikeTheSalad authored Jan 7, 2024
2 parents 2adebfa + a0f94f6 commit 5eae3d2
Show file tree
Hide file tree
Showing 24 changed files with 69 additions and 257 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

Version 2.2.2
---

* Removing gradle from android-resource-collector.

Version 2.2.0
---

Expand Down
11 changes: 0 additions & 11 deletions android-resource-collector/build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
plugins {
alias(libs.plugins.java.library)
id 'kotlin-kapt'
}

dependencies {
api project(':resource-collector')
implementation project(':android-resource-api')
implementation libs.androidCompatApi
embedded libs.dagger
kapt libs.dagger.compiler
testImplementation libs.unitTesting
testImplementation gradleApi()
}

shadowExtension {
relocate("dagger", "${group}.resourcecollector.dagger")
}

libConventions {
setJavaVersion("11")
}

kapt {
correctErrorTypes = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import org.w3c.dom.NodeList
import org.xml.sax.InputSource
import java.io.File
import java.io.StringReader
import javax.inject.Inject
import javax.inject.Singleton
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
Expand All @@ -20,6 +18,24 @@ class AndroidXmlResDocument(private val document: Document) {

companion object {
private const val XML_RESOURCES_TAG = "resources"
private val documentBuilderFactory by lazy {
DocumentBuilderFactory.newInstance().apply { isNamespaceAware = true }
}

fun fromFile(xmlFile: File): AndroidXmlResDocument {
val dBuilder = documentBuilderFactory.newDocumentBuilder()
val xmlInput = InputSource(StringReader(xmlFile.readText()))
return AndroidXmlResDocument(
dBuilder.parse(xmlInput)
)
}

fun createNewDocument(): AndroidXmlResDocument {
val document = documentBuilderFactory.newDocumentBuilder().newDocument().apply {
xmlStandalone = true
}
return AndroidXmlResDocument(document)
}
}

private val resources: Element by lazy { getOrCreateResources() }
Expand Down Expand Up @@ -64,22 +80,4 @@ class AndroidXmlResDocument(private val document: Document) {
return resources
}

@Singleton
class Factory @Inject constructor(private val documentBuilderFactory: DocumentBuilderFactory) {

fun fromFile(xmlFile: File): AndroidXmlResDocument {
val dBuilder = documentBuilderFactory.newDocumentBuilder()
val xmlInput = InputSource(StringReader(xmlFile.readText()))
return AndroidXmlResDocument(
dBuilder.parse(xmlInput)
)
}

fun createNewDocument(): AndroidXmlResDocument {
val document = documentBuilderFactory.newDocumentBuilder().newDocument().apply {
xmlStandalone = true
}
return AndroidXmlResDocument(document)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package com.likethesalad.tools.resource.collector.android.data.resdir

import com.likethesalad.tools.agpcompat.api.bridges.AndroidExtension
import com.likethesalad.tools.resource.api.android.environment.Variant
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ResDirFinder @Inject constructor(private val androidExtension: AndroidExtension) {
object ResDirFinder {

fun findResDirs(variant: Variant): List<ResDir> {
fun findResDirs(androidExtension: AndroidExtension, variant: Variant): List<ResDir> {
return androidExtension.getVariantSrcDirs(variant.name)
.filter { it.exists() }
.map { ResDir(variant, it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ package com.likethesalad.tools.resource.collector.android.data.valuedir
import com.likethesalad.tools.resource.api.android.environment.Language
import com.likethesalad.tools.resource.collector.android.data.resdir.ResDir
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ValueDirFinder @Inject constructor() {
object ValueDirFinder {

companion object {
private val VALUES_FOLDER_NAME_REGEX = Regex("values(-([a-z]{2}(-r[A-Z]{2})*))*")
}
private val VALUES_FOLDER_NAME_REGEX = Regex("values(-([a-z]{2}(-r[A-Z]{2})*))*")

fun findValueDirs(resDir: ResDir): List<ValueDir> {
val valueFolders = resDir.dir.listFiles { _, name ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ package com.likethesalad.tools.resource.collector.android.data.xml

import com.likethesalad.tools.resource.collector.android.data.valuedir.ValueDir
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class XmlFileFinder @Inject constructor() {
object XmlFileFinder {

companion object {
private val XML_FORMAT = Regex(".+\\.[xX][mM][lL]\$")
}
private val XML_FORMAT = Regex(".+\\.[xX][mM][lL]\$")

fun findXmlFiles(valueDir: ValueDir): List<File> {
return valueDir.dir.listFiles { _, name ->
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@ package com.likethesalad.tools.resource.collector.android.source
import com.likethesalad.tools.resource.api.Resource
import com.likethesalad.tools.resource.collector.android.data.AndroidXmlResDocument
import com.likethesalad.tools.resource.collector.source.base.FileResourceSource
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import java.io.File

class AndroidXmlResourceSource @AssistedInject constructor(
@Assisted private val xmlFile: File,
@Assisted private val scope: Resource.Scope,
private val documentFactory: AndroidXmlResDocument.Factory
class AndroidXmlResourceSource(
private val xmlFile: File,
private val scope: Resource.Scope,
) : FileResourceSource(xmlFile) {

@AssistedFactory
interface Factory {
fun create(xmlFile: File, scope: Resource.Scope): AndroidXmlResourceSource
}

val document: AndroidXmlResDocument by lazy {
documentFactory.fromFile(xmlFile)
AndroidXmlResDocument.fromFile(xmlFile)
}

override fun getScope(): Resource.Scope = scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@ import com.likethesalad.tools.resource.collector.android.data.xml.XmlFileFinder
import com.likethesalad.tools.resource.collector.android.source.AndroidXmlResourceSource
import com.likethesalad.tools.resource.collector.source.ResourceSource
import com.likethesalad.tools.resource.collector.source.ResourceSourceProvider
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import java.io.File

class ResDirResourceSourceProvider @AssistedInject constructor(
@Assisted private val resDir: ResDir,
private val valueDirFinder: ValueDirFinder,
private val xmlFileFinder: XmlFileFinder,
private val sourceFactory: AndroidXmlResourceSource.Factory
) : ResourceSourceProvider() {

@AssistedFactory
interface Factory {
fun create(resDir: ResDir): ResDirResourceSourceProvider
}
class ResDirResourceSourceProvider(private val resDir: ResDir) : ResourceSourceProvider() {

override fun doGetSources(): List<ResourceSource> {
val sources = mutableListOf<ResourceSource>()
val valueDirs = valueDirFinder.findValueDirs(resDir)
val valueDirs = ValueDirFinder.findValueDirs(resDir)

for (valueDir in valueDirs) {
sources.addAll(extractSourcesFromValueDir(valueDir))
Expand All @@ -38,7 +25,7 @@ class ResDirResourceSourceProvider @AssistedInject constructor(

private fun extractSourcesFromValueDir(valueDir: ValueDir): Collection<ResourceSource> {
val sources = mutableListOf<ResourceSource>()
val xmlFiles = xmlFileFinder.findXmlFiles(valueDir)
val xmlFiles = XmlFileFinder.findXmlFiles(valueDir)
val scope = AndroidResourceScope(valueDir.resDir.variant, valueDir.language)

for (file in xmlFiles) {
Expand All @@ -51,5 +38,5 @@ class ResDirResourceSourceProvider @AssistedInject constructor(
private fun createAndroidResourceSource(
file: File,
scope: AndroidResourceScope
): AndroidXmlResourceSource = sourceFactory.create(file, scope)
): AndroidXmlResourceSource = AndroidXmlResourceSource(file, scope)
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package com.likethesalad.tools.resource.collector.android.source.providers

import com.likethesalad.tools.agpcompat.api.bridges.AndroidExtension
import com.likethesalad.tools.resource.api.android.environment.Variant
import com.likethesalad.tools.resource.collector.android.data.resdir.ResDir
import com.likethesalad.tools.resource.collector.android.data.resdir.ResDirFinder
import com.likethesalad.tools.resource.collector.android.data.variant.VariantTree
import com.likethesalad.tools.resource.collector.source.ResourceSource
import com.likethesalad.tools.resource.collector.source.ResourceSourceProvider
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject

class VariantTreeResourceSourceProvider @AssistedInject constructor(
@Assisted private val variantTree: VariantTree,
@Assisted private val resDirFinder: ResDirFinder,
private val resDirResourceSourceProviderFactory: ResDirResourceSourceProvider.Factory
) : ResourceSourceProvider() {

@AssistedFactory
interface Factory {
fun create(variantTree: VariantTree, resDirFinder: ResDirFinder): VariantTreeResourceSourceProvider
}
class VariantTreeResourceSourceProvider(
private val androidExtension: AndroidExtension,
private val variantTree: VariantTree
) : ResourceSourceProvider() {

private val lazySources: List<ResourceSource> by lazy {
val sources = mutableListOf<ResourceSource>()
Expand All @@ -44,7 +36,7 @@ class VariantTreeResourceSourceProvider @AssistedInject constructor(

private fun extractSourcesFromVariant(variant: Variant): List<ResourceSource> {
val sources = mutableListOf<ResourceSource>()
val resDirs = resDirFinder.findResDirs(variant)
val resDirs = ResDirFinder.findResDirs(androidExtension, variant)

for (resDir in resDirs) {
sources.addAll(extractSourcesFromResDir(resDir))
Expand All @@ -54,6 +46,6 @@ class VariantTreeResourceSourceProvider @AssistedInject constructor(
}

private fun extractSourcesFromResDir(resDir: ResDir): List<ResourceSource> {
return resDirResourceSourceProviderFactory.create(resDir).getSources()
return ResDirResourceSourceProvider(resDir).getSources()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.likethesalad.tools.resource.api.android.environment.Variant
import com.likethesalad.tools.testing.BaseMockable
import io.mockk.every
import io.mockk.impl.annotations.MockK
import org.junit.Before
import org.junit.Test
import java.io.File

Expand All @@ -17,18 +16,13 @@ class ResDirFinderTest : BaseMockable() {

private lateinit var resDirFinder: ResDirFinder

@Before
fun setUp() {
resDirFinder = ResDirFinder(androidExtensionHelper)
}

@Test
fun `Get res dirs from variant`() {
val variant = Variant.Default
val dirs = setOf(createExistingFileMock(), createExistingFileMock(), createExistingFileMock())
prepareExtensionHelperWithDirs(variant, dirs)

val result = resDirFinder.findResDirs(variant)
val result = ResDirFinder.findResDirs(androidExtensionHelper, variant)

Truth.assertThat(result).containsExactlyElementsIn(dirsToResDir(variant, dirs))
}
Expand All @@ -41,7 +35,7 @@ class ResDirFinderTest : BaseMockable() {
val dirs = existingDirs + nonExistingDirs
prepareExtensionHelperWithDirs(variant, dirs)

val result = resDirFinder.findResDirs(variant)
val result = ResDirFinder.findResDirs(androidExtensionHelper, variant)

Truth.assertThat(result).containsExactlyElementsIn(dirsToResDir(variant, existingDirs))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import org.junit.Test

class ValueDirFinderTest {

private val finder = ValueDirFinder()

@Test
fun `Find values dirs`() {
val resFolder = getResourceFile("res")
val resDir = ResDir(Variant.Default, resFolder)

val result = finder.findValueDirs(resDir)
val result = ValueDirFinder.findValueDirs(resDir)

Truth.assertThat(result).containsExactly(
ValueDir(resDir, getResourceFile("res/values"), Language.Default),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import java.io.File

class XmlFileFinderTest {

private val xmlFileFinder = XmlFileFinder()

@Test
fun `Find xml files`() {
val files = findXmlFilesFromFolder(getResourceFile("res/values"))
Expand Down Expand Up @@ -41,6 +39,6 @@ class XmlFileFinderTest {
val resDir = mockk<ResDir>()
val valuesDir = ValueDir(resDir, folder, Language.Default)

return xmlFileFinder.findXmlFiles(valuesDir)
return XmlFileFinder.findXmlFiles(valuesDir)
}
}
Loading

0 comments on commit 5eae3d2

Please sign in to comment.