core/src/commonMain/kotlin/licensee/LicenseeLicenseInfoRepository.kt
changeset 29 b6b62004b1e7
parent 28 19cf2e8a0627
child 34 ce299aacc068
equal deleted inserted replaced
28:19cf2e8a0627 29:b6b62004b1e7
    22 package com.geekorum.aboutoss.core.licensee
    22 package com.geekorum.aboutoss.core.licensee
    23 
    23 
    24 import kotlinx.coroutines.CoroutineDispatcher
    24 import kotlinx.coroutines.CoroutineDispatcher
    25 import kotlinx.coroutines.withContext
    25 import kotlinx.coroutines.withContext
    26 import kotlinx.serialization.ExperimentalSerializationApi
    26 import kotlinx.serialization.ExperimentalSerializationApi
    27 import kotlinx.serialization.Serializable
       
    28 import kotlinx.serialization.json.Json
       
    29 import okio.Source
    27 import okio.Source
    30 import okio.buffer
       
    31 
    28 
    32 class LicenseeLicenseInfoRepository(
    29 class LicenseeLicenseInfoRepository(
    33     private val produceInput: suspend () -> Source,
    30     private val produceInput: suspend () -> Source,
    34     private val mainCoroutineDispatcher: CoroutineDispatcher,
    31     private val mainCoroutineDispatcher: CoroutineDispatcher,
    35     private val ioCoroutineDispatcher: CoroutineDispatcher,
    32     private val ioCoroutineDispatcher: CoroutineDispatcher,
    60             licensesInfo = licenses
    57             licensesInfo = licenses
    61         }
    58         }
    62     }
    59     }
    63 }
    60 }
    64 
    61 
    65 
       
    66 private class LicenseeParser(
       
    67     input: Source
       
    68 ): AutoCloseable {
       
    69     private val buffered = input.buffer()
       
    70 
       
    71     fun readLicensee(): Map<String, String> {
       
    72         val json = Json {
       
    73             ignoreUnknownKeys = true
       
    74         }
       
    75         val items: List<LicenseItem> = json.decodeFromString(buffered.readUtf8())
       
    76 
       
    77         return items.associate {
       
    78             val name = it.name ?: "${it.groupId}:${it.artifactId}"
       
    79             val license = it.spdxLicenses.firstNotNullOfOrNull {
       
    80                 "${it.name}\n\n${it.url}"
       
    81             } ?: it.unknownLicenses.firstNotNullOf {
       
    82                 "${it.name}\n\n${it.url}"
       
    83             }
       
    84             name to license
       
    85         }
       
    86     }
       
    87 
       
    88     override fun close() {
       
    89         buffered.close()
       
    90     }
       
    91 }
       
    92 
       
    93 
       
    94 @Serializable
       
    95 private data class LicenseItem(
       
    96     val groupId: String,
       
    97     val artifactId: String,
       
    98     val version: String,
       
    99     val spdxLicenses: List<SpdxLicense> = emptyList(),
       
   100     val unknownLicenses: List<UnknownLicense> = emptyList(),
       
   101     val name: String? = null,
       
   102     val scm: Scm? = null,
       
   103 )
       
   104 
       
   105 @Serializable
       
   106 private data class SpdxLicense(
       
   107     val identifier: String,
       
   108     val name: String,
       
   109     val url: String,
       
   110 )
       
   111 
       
   112 @Serializable
       
   113 private data class UnknownLicense(
       
   114     val name: String,
       
   115     val url: String
       
   116 )
       
   117 
       
   118 @Serializable
       
   119 private data class Scm(
       
   120     val url: String,
       
   121 )