Skip to content

Commit

Permalink
Allow FirelyPlugin to be applied on a library; (#5)
Browse files Browse the repository at this point in the history
Add activateFetched after a successful fetch on InternalFirely
  • Loading branch information
ruimendesM authored Aug 9, 2022
1 parent a97686e commit 6413cbc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class InternalFirely(context: Context, private val config: IFirelyConfig) {


config.allValues()
.associate { it.key to it.default}
.associate { it.key to it.default }
.also { defaults ->
firebaseRemoteConfig.setDefaultsAsync(defaults)
}
Expand All @@ -94,9 +94,9 @@ class InternalFirely(context: Context, private val config: IFirelyConfig) {
updateAllTrackingProperties()
}

fun getString(name: String) : String = firebaseRemoteConfig.getString(name)
fun getString(name: String): String = firebaseRemoteConfig.getString(name)
fun getBoolean(name: String) = firebaseRemoteConfig.getBoolean(name)
fun getDouble(name: String) : Double= firebaseRemoteConfig.getDouble(name)
fun getDouble(name: String): Double = firebaseRemoteConfig.getDouble(name)
fun getLong(name: String): Long = firebaseRemoteConfig.getLong(name)

fun getCurrentKnownValues(): Map<String, String> {
Expand Down Expand Up @@ -148,6 +148,8 @@ class InternalFirely(context: Context, private val config: IFirelyConfig) {
prefs.edit()
.putBoolean(SHARED_PREF_INITIAL_CHECK, true)
.apply()

activateFetched()
} else {
if (Firely.logLevel().debugLogEnabled()) {
Log.d(LOG_TAG, "Fetch Failed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
package com.busbud.android.firely

import com.android.build.gradle.AppExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.api.BaseVariant
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.UnknownDomainObjectException
import java.io.File


Expand All @@ -35,31 +38,39 @@ private const val FILE_NAME = "firely-config.json"
class FirelyPlugin : Plugin<Project> {

override fun apply(project: Project) {
val android = project.extensions.getByType(AppExtension::class.java)
// Add generated source
android.applicationVariants.all { variant ->
val sourceFolder = File(project.buildDir, "/generated/source/firely/${variant.dirName}")
var configFile = File("${project.projectDir.absolutePath}/$FILE_NAME")

if (!configFile.exists()) {
configFile = File("${project.projectDir.absolutePath}/${project.name}/${FILE_NAME}")
try {
project.extensions.getByType(AppExtension::class.java).applicationVariants.all { variant ->
addGeneratedSource(project, variant)
}

if (!configFile.exists()) {
println("Unable to find ${configFile.absolutePath} in your project")
return@all
} catch (exception: UnknownDomainObjectException) {
project.extensions.getByType(LibraryExtension::class.java).libraryVariants.all { variant ->
addGeneratedSource(project, variant)
}
}
}

val firelyTaskProvider = project.tasks.register(
"${TASK_NAME}${variant.name.replaceFirstChar { it.uppercaseChar() }}",
FirelyTask::class.java
) { task ->
task.inputFile = configFile
task.outputDir = sourceFolder
}
private fun addGeneratedSource(project: Project, variant: BaseVariant) {
val sourceFolder = File(project.buildDir, "/generated/source/firely/${variant.dirName}")
var configFile = File("${project.projectDir.absolutePath}/$FILE_NAME")

firelyTaskProvider.get().outputs.upToDateWhen { false }
variant.registerJavaGeneratingTask(firelyTaskProvider, sourceFolder)
if (!configFile.exists()) {
configFile = File("${project.projectDir.absolutePath}/${project.name}/${FILE_NAME}")
}

if (!configFile.exists()) {
println("Unable to find ${configFile.absolutePath} in your project")
return
}

val firelyTaskProvider = project.tasks.register(
"${TASK_NAME}${variant.name.replaceFirstChar { it.uppercaseChar() }}",
FirelyTask::class.java
) { task ->
task.inputFile = configFile
task.outputDir = sourceFolder
}

firelyTaskProvider.get().outputs.upToDateWhen { false }
variant.registerJavaGeneratingTask(firelyTaskProvider, sourceFolder)
}
}

0 comments on commit 6413cbc

Please sign in to comment.