-
Notifications
You must be signed in to change notification settings - Fork 6
Properly parse properties in Maven POM #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1a4d6a0
fix: Properly parse properties in Maven POM
bishiboosh 07f97e2
handle convert exception correctly
bishiboosh cf3e92b
fix detekt issue
bishiboosh f100785
fix regex for JS
bishiboosh d0f74f3
Merge branch 'main' into support-maven-properties
bishiboosh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
76 changes: 76 additions & 0 deletions
76
core/src/commonMain/kotlin/com/deezer/caupain/serialization/xml/DynamicTagReader.kt
This file contains hidden or 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,76 @@ | ||
| @file:Suppress("UnusedImport") // Detekt bug | ||
|
|
||
| package com.deezer.caupain.serialization.xml | ||
|
|
||
| import nl.adaptivity.xmlutil.XmlDelegatingReader | ||
| import nl.adaptivity.xmlutil.XmlReader | ||
| import nl.adaptivity.xmlutil.localPart | ||
| import nl.adaptivity.xmlutil.namespaceURI | ||
|
||
| import nl.adaptivity.xmlutil.prefix | ||
|
||
| import nl.adaptivity.xmlutil.serialization.structure.XmlDescriptor | ||
|
|
||
| internal class DynamicTagReader( | ||
| private val idPropertyName: String, | ||
| reader: XmlReader, | ||
| descriptor: XmlDescriptor | ||
| ) : XmlDelegatingReader(reader) { | ||
|
|
||
| private val filterDepth = delegate.depth - reader.depth | ||
|
|
||
| private val elementName = descriptor.tagName | ||
|
|
||
| private val idAttrName = (0 until descriptor.elementsCount) | ||
| .first { descriptor.serialDescriptor.getElementName(it) == idPropertyName } | ||
| .let { descriptor.getElementDescriptor(it) } | ||
| .tagName | ||
|
|
||
| private val idValue = delegate.localName | ||
|
|
||
| override val attributeCount: Int | ||
| get() = if (filterDepth == 0) super.attributeCount + 1 else super.attributeCount | ||
|
|
||
| override fun getAttributeNamespace(index: Int): String = if (filterDepth == 0) { | ||
| if (index == 0) idAttrName.namespaceURI else super.getAttributeNamespace(index - 1) | ||
| } else { | ||
| super.getAttributeNamespace(index) | ||
| } | ||
|
|
||
| override fun getAttributePrefix(index: Int): String = if (filterDepth == 0) { | ||
| if (index == 0) idAttrName.prefix else super.getAttributePrefix(index - 1) | ||
| } else { | ||
| super.getAttributePrefix(index) | ||
| } | ||
|
|
||
| override fun getAttributeLocalName(index: Int): String = if (filterDepth == 0) { | ||
| if (index == 0) idAttrName.localPart else super.getAttributeLocalName(index - 1) | ||
| } else { | ||
| super.getAttributeLocalName(index) | ||
| } | ||
|
|
||
| override fun getAttributeValue(index: Int): String = if (filterDepth == 0) { | ||
| if (index == 0) idValue else super.getAttributeValue(index - 1) | ||
| } else { | ||
| super.getAttributeValue(index) | ||
| } | ||
|
|
||
| @Suppress("UnnecessaryParentheses") // More understandable this way | ||
| override fun getAttributeValue(nsUri: String?, localName: String): String? = | ||
| if ( | ||
| filterDepth == 0 && | ||
| nsUri.orEmpty() == idAttrName.namespaceURI && | ||
| localName == idAttrName.localPart | ||
| ) { | ||
| idValue | ||
| } else { | ||
| super.getAttributeValue(nsUri, localName) | ||
| } | ||
|
|
||
| override val namespaceURI: String | ||
| get() = if (filterDepth == 0) elementName.namespaceURI else super.namespaceURI | ||
|
|
||
| override val localName: String | ||
| get() = if (filterDepth == 0) elementName.localPart else super.localName | ||
|
|
||
| override val prefix: String | ||
| get() = if (filterDepth == 0) elementName.prefix else super.prefix | ||
| } | ||
92 changes: 92 additions & 0 deletions
92
core/src/commonMain/kotlin/com/deezer/caupain/serialization/xml/MavenInfoSerializer.kt
This file contains hidden or 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,92 @@ | ||
| package com.deezer.caupain.serialization.xml | ||
|
|
||
| import com.deezer.caupain.model.maven.Dependency | ||
| import com.deezer.caupain.model.maven.MavenInfo | ||
| import com.deezer.caupain.model.maven.SCMInfos | ||
| import kotlinx.serialization.KSerializer | ||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.descriptors.SerialDescriptor | ||
| import kotlinx.serialization.encoding.Decoder | ||
| import kotlinx.serialization.encoding.Encoder | ||
| import kotlinx.serialization.serializer | ||
| import nl.adaptivity.xmlutil.serialization.XmlChildrenName | ||
| import nl.adaptivity.xmlutil.serialization.XmlElement | ||
| import nl.adaptivity.xmlutil.serialization.XmlSerialName | ||
|
|
||
| @Serializable | ||
| @SerialName("project") | ||
| private data class RawMavenInfo( | ||
| @XmlElement val name: String? = null, | ||
| @XmlElement val url: String? = null, | ||
| @XmlChildrenName("dependency") val dependencies: List<Dependency> = emptyList(), | ||
| @XmlElement @XmlSerialName("scm") val scm: SCMInfos? = null, | ||
| @XmlElement | ||
| @XmlSerialName("properties") | ||
| @Serializable(PropertiesMapSerializer::class) | ||
| val properties: Map<String, String> = emptyMap(), | ||
| ) { | ||
| constructor(mavenInfo: MavenInfo) : this( | ||
| name = mavenInfo.name, | ||
| url = mavenInfo.url, | ||
| dependencies = mavenInfo.dependencies, | ||
| scm = mavenInfo.scm, | ||
| ) | ||
|
|
||
| fun resolved(): MavenInfo { | ||
| return if (properties.isEmpty()) { | ||
| return MavenInfo( | ||
| name = name, | ||
| url = url, | ||
| dependencies = dependencies, | ||
| scm = scm, | ||
| ) | ||
| } else { | ||
| MavenInfo( | ||
| name = name?.resolve(properties), | ||
| url = url?.resolve(properties), | ||
| dependencies = dependencies.map { dependency -> | ||
| Dependency( | ||
| groupId = dependency.groupId.resolve(properties), | ||
| artifactId = dependency.artifactId.resolve(properties), | ||
| version = dependency.version?.resolve(properties), | ||
| ) | ||
| }, | ||
| scm = scm?.let { scmInfos -> | ||
| SCMInfos( | ||
| url = scmInfos.url?.resolve(properties) | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private fun String.resolve(properties: Map<String, String>): String { | ||
| return PLACEHOLDER_REGEX.replace(this) { matchResult -> | ||
| properties[matchResult.groupValues[1]] ?: matchResult.value | ||
| } | ||
| } | ||
|
|
||
| private val PLACEHOLDER_REGEX = "\\$\\{(.+?)\\}".toRegex() | ||
| } | ||
| } | ||
|
|
||
| internal object MavenInfoSerializer : KSerializer<MavenInfo> { | ||
|
|
||
| private val rawSerializer = serializer<RawMavenInfo>() | ||
|
|
||
| override val descriptor: SerialDescriptor | ||
| get() = rawSerializer.descriptor | ||
|
|
||
| override fun serialize( | ||
| encoder: Encoder, | ||
| value: MavenInfo | ||
| ) { | ||
| encoder.encodeSerializableValue(rawSerializer, RawMavenInfo(value)) | ||
| } | ||
|
|
||
| override fun deserialize(decoder: Decoder): MavenInfo { | ||
| return decoder.decodeSerializableValue(rawSerializer).resolved() | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.