-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdcc3a0
commit bf2fe2e
Showing
13 changed files
with
825 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/Configuration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.freeletics.khonshu.deeplinks.plugin | ||
|
||
import com.freeletics.khonshu.navigation.deeplinks.DeepLinkHandler | ||
import com.freeletics.khonshu.navigation.deeplinks.replacePlaceholders | ||
import com.freeletics.khonshu.navigation.internal.InternalNavigationApi | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
public data class Configuration( | ||
val prefixes: List<Prefix> = emptyList(), | ||
val placeholders: List<Placeholder> = emptyList(), | ||
val deepLinks: Map<String, DeepLink>, | ||
) | ||
|
||
@Serializable | ||
public data class DeepLink( | ||
val patterns: List<Pattern>, | ||
val prefixes: List<Prefix> = emptyList(), // use global if empty | ||
val placeholders: List<Placeholder> = emptyList(), // use global if empty or if key not found | ||
val exampleQueries: List<String> = emptyList(), | ||
) | ||
|
||
@Serializable | ||
@JvmInline | ||
public value class Pattern( | ||
public val value: String, | ||
) { | ||
init { | ||
// for validation purposes | ||
DeepLinkHandler.Pattern(value) | ||
} | ||
|
||
@OptIn(InternalNavigationApi::class) | ||
public fun replacePlaceholders(replacement: (String) -> String): String { | ||
return DeepLinkHandler.Pattern(value).replacePlaceholders(replacement) | ||
} | ||
} | ||
|
||
@Serializable | ||
public data class Prefix( | ||
val scheme: String, | ||
val host: String, | ||
val autoVerified: Boolean, | ||
) { | ||
init { | ||
// for validation purposes | ||
DeepLinkHandler.Prefix("$scheme://$host") | ||
} | ||
} | ||
|
||
@Serializable | ||
public data class Placeholder( | ||
val key: String, | ||
val exampleValues: List<String>, | ||
) |
23 changes: 0 additions & 23 deletions
23
deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/Deeplinks.kt
This file was deleted.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
.../src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/DeeplinksManifestConfigurator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.freeletics.khonshu.deeplinks.plugin | ||
|
||
import com.freeletics.khonshu.navigation.deeplinks.replacePlaceholders | ||
import com.freeletics.khonshu.navigation.internal.InternalNavigationApi | ||
import java.io.File | ||
import net.peanuuutz.tomlkt.Toml | ||
|
||
private const val PLACEHOLDER = "<!-- DEEPLINK INTENT FILTERS -->" | ||
|
||
internal fun configure( | ||
configurationFile: File, | ||
inputManifestFile: File, | ||
outputManifestFile: File, | ||
) { | ||
val manifest = inputManifestFile.readLines().toMutableList() | ||
val placeholderIndex = manifest.indexOfFirst { it.contains(PLACEHOLDER) } | ||
check(placeholderIndex >= 0) { | ||
"Did not find $PLACEHOLDER in given manifest ${inputManifestFile.absolutePath}" | ||
} | ||
|
||
val configuration = Toml.decodeFromString(Configuration.serializer(), configurationFile.readText()) | ||
val indentation = manifest[placeholderIndex].takeWhile { it == ' ' } | ||
manifest[placeholderIndex] = intentFiltersFromConfig(configuration, indentation) | ||
|
||
outputManifestFile.writeText(manifest.joinToString(separator = "\n")) | ||
} | ||
|
||
private fun intentFiltersFromConfig(configuration: Configuration, indentation: String): String { | ||
val builder = IntentFilterBuilder(indentation) | ||
|
||
val deepLinksWithGlobalPrefixes = configuration.deepLinks.values.filter { it.prefixes.isEmpty() } | ||
if (deepLinksWithGlobalPrefixes.isNotEmpty()) { | ||
check(configuration.prefixes.isNotEmpty()) { | ||
"Configuration contains deep links without a prefix but has no global prefixes" | ||
} | ||
|
||
configuration.prefixes.forEach { prefix -> | ||
builder.appendIntentFilter(prefix, deepLinksWithGlobalPrefixes) | ||
} | ||
} | ||
|
||
configuration.deepLinks.values.filter { it.prefixes.isNotEmpty() }.forEach { | ||
it.prefixes.forEach { prefix -> | ||
builder.appendIntentFilter(prefix, listOf(it)) | ||
} | ||
} | ||
|
||
return builder.toString() | ||
} | ||
|
||
private fun IntentFilterBuilder.appendIntentFilter(prefix: Prefix, deepLinks: List<DeepLink>) { | ||
start(prefix.autoVerified) | ||
appendAction("android.intent.action.VIEW") | ||
appendCategory("android.intent.category.DEFAULT") | ||
appendCategory("android.intent.category.BROWSABLE") | ||
|
||
deepLinks.forEach { deepLink -> | ||
deepLink.patterns.forEach { pattern -> | ||
appendData(prefix.scheme, prefix.host, pattern) | ||
} | ||
} | ||
end() | ||
} | ||
|
||
private class IntentFilterBuilder( | ||
private val indentation: String, | ||
) { | ||
private val builder = StringBuilder() | ||
|
||
fun start(autoVerify: Boolean) { | ||
builder.appendLine("$indentation<intent-filter android:autoVerify=\"${autoVerify}\">") | ||
} | ||
|
||
fun appendAction(action: String) { | ||
builder.appendLine("$indentation <action android:name=\"${action}\" />") | ||
} | ||
|
||
fun appendCategory(category: String) { | ||
builder.appendLine("$indentation <category android:name=\"${category}\" />") | ||
} | ||
|
||
@OptIn(InternalNavigationApi::class) | ||
fun appendData(scheme: String, host: String, pattern: Pattern) { | ||
val pathPattern = pattern.replacePlaceholders { ".*" } | ||
builder.appendLine("$indentation <data") | ||
builder.appendLine("$indentation android:scheme=\"${scheme}\"") | ||
builder.appendLine("$indentation android:host=\"${host}\"") | ||
builder.appendLine("$indentation android:pathPattern=\"/${pathPattern}\"") | ||
builder.appendLine("$indentation />") | ||
} | ||
|
||
fun end() { | ||
builder.appendLine("$indentation</intent-filter>") | ||
} | ||
|
||
override fun toString(): String { | ||
return builder.toString().trimEnd() | ||
} | ||
} |
Oops, something went wrong.