Skip to content

Commit

Permalink
Use BuildConfig driven component package & go to 0.5.0 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedrickcooke committed Mar 23, 2020
1 parent 7bbb5fb commit 690b7fd
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 16 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ allprojects {
// In your application build.gradle
android {
defaultConfig {
applicationId "your.application.id"
javaCompileOptions {
annotationProcessorOptions {
arguments = [ "exercise.packageName": "your.application.id" ]
// Replace "com.example.app" with the package declared at the top
// of your generated BuildConfig file.
arguments = [ "exercise.buildConfigPackage": "com.example.app" ]
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
"exercise.packageName": "com.juullabs.exercise"
"exercise.buildConfigPackage": "com.juullabs.exercise"
]
}
}
Expand All @@ -28,6 +28,10 @@ android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
debug {
applicationIdSuffix ".debug"
}
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ allprojects {
subprojects {
// group = 'com.juullabs.exercise'
group = 'com.github.juullabs-oss.android-exercise'
version = '0.4.0'
version = '0.5.0'
}

task clean(type: Delete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ internal class ActivityCodeGenerator(
type: TypeElement
) : CodeGenerator(environment, type) {

private val packageName = environment.options().packageName
private val applicationIdCode = "BuildConfig.APPLICATION_ID"

override val exerciseClassName: String = "${typeName}Extras"
override val exerciseSugarName: String = "extras"
override val codeToRetrieveFromInstance: String = "%N.intent?.extras?.get(\"$packageName.%L\")"
private val codeToRetrieveFromResult: String = "%N.get(\"$packageName.%L\")"
override val codeToRetrieveFromInstance: String = "%N.intent?.extras?.get(\"\${$applicationIdCode}.%L\")"
private val codeToRetrieveFromResult: String = "%N.get(\"\${$applicationIdCode}.%L\")"
private val generatedIntentTypeName = ClassName(typePackage, "${typeName}Intent")
private val resultTypeName = ClassName(typePackage, "${typeName}Result")

override fun onBuild(fileSpecBuilder: FileSpec.Builder) {
fileSpecBuilder.addImport(environment.options().buildConfigPackage, "BuildConfig")
if (!typeIsAbstract) {
fileSpecBuilder.addImport("android.app", "Activity")
fileSpecBuilder.addImport("android.content", "ComponentName")
Expand All @@ -51,9 +52,9 @@ internal class ActivityCodeGenerator(
superclass(intentTypeName)
primaryConstructor { addParameters(extras.asParameterSpecs()) }
addInitializerBlock {
add("component = ComponentName(⇥\n%S,\n%S\n⇤)\n", packageName, typeClassName)
add("component = ComponentName(⇥\n%L,\n%S\n⇤)\n", applicationIdCode, typeClassName)
if (extras.isNotEmpty()) {
add("replaceExtras(%L)\n", extras.toBundle(prefix = "$packageName."))
add("replaceExtras(%L)\n", extras.toBundle(applicationIdCode))
}
}
}
Expand All @@ -76,7 +77,7 @@ internal class ActivityCodeGenerator(
addConstructor {
addModifiers(KModifier.INTERNAL)
addParameters(kind.params.asParameterSpecs())
callThisConstructor(kind.params.toBundle(prefix = "$packageName."))
callThisConstructor(kind.params.toBundle(applicationIdCode))
}
for (param in kind.params) {
addExerciseParameter(param, codeToRetrieveFromResult, "data")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import javax.lang.model.element.TypeElement

@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes("com.juullabs.exercise.annotations.Exercise")
@SupportedOptions(OPTION_PACKAGE_NAME)
@SupportedOptions(OPTION_BUILD_CONFIG_PACKAGE)
class ExerciseProcessor : AbstractProcessor() {

private fun TypeElement.isSubtypeOf(supertype: String): Boolean = with(processingEnv) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ internal fun List<Parameter>.toBundle(
prefix: String? = null
): CodeBlock = if (isEmpty()) {
CodeBlock.of("bundleOf()")
} else if (prefix == null) {
CodeBlock.of(
"bundleOf(⇥\n%L\n⇤)",
joinToString(",\n") { "\"${it.name}\" to ${it.name}" }
)
} else {
CodeBlock.of(
"bundleOf(⇥\n%L\n⇤)",
joinToString(",\n") { "\"${prefix ?: ""}${it.name}\" to ${it.name}" }
joinToString(",\n") { "\"\${$prefix}.${it.name}\" to ${it.name}" }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package com.juullabs.exercise.compile
import javax.annotation.processing.ProcessingEnvironment
import javax.tools.Diagnostic

const val OPTION_PACKAGE_NAME = "exercise.packageName"
const val OPTION_BUILD_CONFIG_PACKAGE = "exercise.buildConfigPackage"

fun ProcessingEnvironment.options() = Options(this)

class Options(private val processingEnvironment: ProcessingEnvironment) {
val packageName: String
get() = processingEnvironment.options[OPTION_PACKAGE_NAME] ?: run {
val msg = "Annotation processor argument `exercise.packageName` must be set."
val buildConfigPackage: String
get() = processingEnvironment.options[OPTION_BUILD_CONFIG_PACKAGE] ?: run {
val msg = "Annotation processor argument `$OPTION_BUILD_CONFIG_PACKAGE` must be set."
processingEnvironment.messager.printMessage(Diagnostic.Kind.ERROR, msg)
error(msg)
}
Expand Down

0 comments on commit 690b7fd

Please sign in to comment.