Skip to content

Commit

Permalink
Include the file name in file read/parse failures (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored Nov 14, 2023
1 parent 7fba1e4 commit d8ff7b6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package app.cash.paraphrase.plugin

import app.cash.paraphrase.plugin.model.ResourceFolder
import java.io.File
import java.io.InputStream
import javax.inject.Inject
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
Expand Down Expand Up @@ -71,7 +73,8 @@ internal abstract class GenerateFormattedResources @Inject constructor() : Defau
// values-es -> [TokenizedResource(name=hello, ..)]
val resourcesByConfiguration = filesByConfiguration
.mapValues { (_, files) ->
files.flatMap(::parseResources).map(::tokenizeResource)
files.flatMap { it.checkedRead(::parseResources) }
.map(::tokenizeResource)
}

// Split the folder map into individual maps keyed on resource name.
Expand All @@ -98,7 +101,7 @@ internal abstract class GenerateFormattedResources @Inject constructor() : Defau
// https://developer.android.com/studio/projects/android-library#PrivateResources suggests this
// is the case. Check AGP source.
val publicResources = (filesByConfiguration[ResourceFolder.Default] ?: emptyList())
.flatMap(::parsePublicResources)
.flatMap { it.checkedRead(::parsePublicResources) }
.toSet()

// Merge each resource's configuration map into final, canonical versions.
Expand All @@ -115,4 +118,12 @@ internal abstract class GenerateFormattedResources @Inject constructor() : Defau

// TODO Fail on errors which make it this far.
}

private fun <T> File.checkedRead(parser: (InputStream) -> T): T {
return try {
inputStream().buffered().run(parser)
} catch (e: Exception) {
throw IllegalArgumentException("Unable to parse $this", e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,10 @@ package app.cash.paraphrase.plugin

import app.cash.paraphrase.plugin.model.PublicResource
import app.cash.paraphrase.plugin.model.ResourceName
import java.io.File
import java.io.InputStream
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.Node

/**
* Parses and returns all of the Android <string> resources declared in the given file.
*
* Ignores all other resources, including <plurals> and <string-array>.
*/
internal fun parsePublicResources(file: File): List<PublicResource> =
file.inputStream().use(::parsePublicResources)

/**
* Parses and returns all of the Android <public> resources declared in the given stream, regardless
* of type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,10 @@ package app.cash.paraphrase.plugin

import app.cash.paraphrase.plugin.model.ResourceName
import app.cash.paraphrase.plugin.model.StringResource
import java.io.File
import java.io.InputStream
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.Node

/**
* Parses and returns all of the Android <string> resources declared in the given file.
*
* Ignores all other resources, including <plurals> and <string-array>.
*/
internal fun parseResources(file: File): List<StringResource> =
file.inputStream().use(::parseResources)

/**
* Parses and returns all of the Android <string> resources declared in the given stream.
*
Expand Down

0 comments on commit d8ff7b6

Please sign in to comment.