Skip to content

Commit 91b1609

Browse files
committed
refactor(opossum reporter): adjust file structure
Put public method at the top, put caller above callees etc. Signed-off-by: alexzurbonsen <[email protected]>
1 parent 5c8382f commit 91b1609

File tree

1 file changed

+150
-142
lines changed

1 file changed

+150
-142
lines changed

plugins/reporters/opossum/src/main/kotlin/OpossumReporter.kt

Lines changed: 150 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -104,142 +104,35 @@ class OpossumReporter(
104104
override val descriptor: PluginDescriptor = OpossumReporterFactory.descriptor,
105105
private val config: OpossumReporterConfig
106106
) : Reporter {
107-
@Serializable
108-
internal data class OpossumSignal(
109-
@Transient
110-
val uuid: UUID = UUID.randomUUID(),
111-
val source: OpossumSignalSource,
112-
val attributionConfidence: Int = 80,
113-
val packageType: String?,
114-
val packageNamespace: String?,
115-
val packageName: String?,
116-
val packageVersion: String?,
117-
val copyright: String?,
118-
val licenseName: String?,
119-
val url: String?,
120-
val preSelected: Boolean,
121-
val followUp: OpossumFollowUp?,
122-
val excludeFromNotice: Boolean,
123-
val comment: String?
124-
) {
125-
companion object {
126-
fun create(
127-
source: String,
128-
id: Identifier? = null,
129-
url: String? = null,
130-
license: SpdxExpression? = null,
131-
copyright: String? = null,
132-
comment: String? = null,
133-
preSelected: Boolean = false,
134-
followUp: Boolean = false,
135-
excludeFromNotice: Boolean = false
136-
): OpossumSignal {
137-
return OpossumSignal(
138-
source = OpossumSignalSource(name = source),
139-
packageType = id?.getPurlType(),
140-
packageNamespace = id?.namespace,
141-
packageName = id?.name,
142-
packageVersion = id?.version,
143-
copyright = copyright,
144-
licenseName = license?.toString(),
145-
url = url,
146-
preSelected = preSelected,
147-
followUp = OpossumFollowUp.FOLLOW_UP.takeIf { followUp },
148-
excludeFromNotice = excludeFromNotice,
149-
comment = comment,
150-
)
107+
override fun generateReport(input: ReporterInput, outputDir: File): List<Result<File>> {
108+
val reportFileResult = runCatching {
109+
val opossumInput = createOpossumInput(input, config.maxDepth)
110+
111+
outputDir.resolve("report.opossum").also {
112+
writeReport(it, opossumInput)
151113
}
152114
}
153115

154-
fun matches(other: OpossumSignal): Boolean =
155-
source == other.source
156-
&& packageType == other.packageType
157-
&& packageNamespace == other.packageNamespace
158-
&& packageName == other.packageName
159-
&& packageVersion == other.packageVersion
160-
&& copyright == other.copyright
161-
&& licenseName == other.licenseName
162-
&& url == other.url
163-
&& preSelected == other.preSelected
164-
&& comment == other.comment
116+
return listOf(reportFileResult)
165117
}
166118

167-
@Serializable
168-
internal data class OpossumSignalSource(
169-
val name: String,
170-
val documentConfidence: Int = 80,
171-
)
172-
173-
@Serializable
174-
internal enum class OpossumFollowUp {
175-
FOLLOW_UP,
119+
internal fun createOpossumInput(input: ReporterInput, maxDepth: Int = Int.MAX_VALUE): OpossumInput {
120+
return OpossumInputCreator().create(input, maxDepth)
176121
}
177122

178-
@Serializable(with = OpossumResourcesSerializer::class)
179-
internal data class OpossumResources(
180-
val tree: MutableMap<String, OpossumResources> = mutableMapOf()
181-
) {
182-
private fun addResource(pathPieces: List<String>) {
183-
if (pathPieces.isEmpty()) return
184-
185-
val head = pathPieces.first()
186-
val tail = pathPieces.drop(1)
187-
188-
if (head !in tree) tree[head] = OpossumResources()
189-
tree.getValue(head).addResource(tail)
190-
}
191-
192-
fun addResource(path: String) {
193-
val pathPieces = path.split("/").filter { it.isNotEmpty() }
194-
195-
addResource(pathPieces)
196-
}
197-
198-
fun isFile() = tree.isEmpty()
199-
200-
fun isPathAFile(path: String): Boolean {
201-
val pathPieces = path.split("/").filter { it.isNotEmpty() }
202-
203-
return isPathAFile(pathPieces)
204-
}
205-
206-
private fun isPathAFile(pathPieces: List<String>): Boolean {
207-
if (pathPieces.isEmpty()) return isFile()
208-
209-
val head = pathPieces.first()
210-
val tail = pathPieces.drop(1)
211-
212-
return head !in tree || tree.getValue(head).isPathAFile(tail)
123+
private fun writeReport(outputFile: File, opossumInput: OpossumInput) {
124+
val jsonFile = createOrtTempDir().resolve("input.json")
125+
val json = Json {
126+
explicitNulls = false
127+
encodeDefaults = true
213128
}
214129

215-
fun toFileList(): Set<String> =
216-
tree.flatMapTo(mutableSetOf()) { (key, value) ->
217-
value.toFileList().map { resolvePath(key, it, isDirectory = false) }
218-
}.plus("/")
219-
}
130+
jsonFile.writeText(json.encodeToString(OpossumInput.serializer(), opossumInput))
220131

221-
@Serializable
222-
internal data class OpossumFrequentLicense(
223-
val shortName: String,
224-
val fullName: String?,
225-
val defaultText: String?
226-
) : Comparable<OpossumFrequentLicense> {
227-
override fun compareTo(other: OpossumFrequentLicense) =
228-
compareValuesBy(
229-
this,
230-
other,
231-
{ it.shortName },
232-
{ it.fullName },
233-
{ it.defaultText }
234-
)
132+
jsonFile.packZip(outputFile)
133+
jsonFile.delete()
235134
}
236135

237-
@Serializable
238-
internal data class OpossumExternalAttributionSource(
239-
val name: String,
240-
val priority: Int
241-
)
242-
243136
@Serializable
244137
internal data class OpossumInput(
245138
val metadata: OpossumInputMetadata = OpossumInputMetadata(),
@@ -579,33 +472,148 @@ class OpossumReporter(
579472
val fileCreationDate: String = LocalDateTime.now().toString()
580473
)
581474

582-
private fun writeReport(outputFile: File, opossumInput: OpossumInput) {
583-
val jsonFile = createOrtTempDir().resolve("input.json")
584-
val json = Json {
585-
explicitNulls = false
586-
encodeDefaults = true
475+
@Serializable(with = OpossumResourcesSerializer::class)
476+
internal data class OpossumResources(
477+
val tree: MutableMap<String, OpossumResources> = mutableMapOf()
478+
) {
479+
private fun addResource(pathPieces: List<String>) {
480+
if (pathPieces.isEmpty()) {
481+
return
482+
}
483+
484+
val head = pathPieces.first()
485+
val tail = pathPieces.drop(1)
486+
487+
if (head !in tree) {
488+
tree[head] = OpossumResources()
489+
}
490+
491+
tree.getValue(head).addResource(tail)
587492
}
588-
jsonFile.writeText(json.encodeToString(OpossumInput.serializer(), opossumInput))
589493

590-
jsonFile.packZip(outputFile)
591-
jsonFile.delete()
592-
}
494+
fun addResource(path: String) {
495+
val pathPieces = path.split("/").filter { it.isNotEmpty() }
593496

594-
internal fun createOpossumInput(input: ReporterInput, maxDepth: Int = Int.MAX_VALUE): OpossumInput {
595-
return OpossumInputCreator().create(input, maxDepth)
596-
}
497+
addResource(pathPieces)
498+
}
597499

598-
override fun generateReport(input: ReporterInput, outputDir: File): List<Result<File>> {
599-
val reportFileResult = runCatching {
600-
val opossumInput = createOpossumInput(input, config.maxDepth)
500+
fun isFile() = tree.isEmpty()
601501

602-
outputDir.resolve("report.opossum").also {
603-
writeReport(it, opossumInput)
502+
fun isPathAFile(path: String): Boolean {
503+
val pathPieces = path.split("/").filter { it.isNotEmpty() }
504+
505+
return isPathAFile(pathPieces)
506+
}
507+
508+
private fun isPathAFile(pathPieces: List<String>): Boolean {
509+
if (pathPieces.isEmpty()) {
510+
return isFile()
604511
}
512+
513+
val head = pathPieces.first()
514+
val tail = pathPieces.drop(1)
515+
516+
return head !in tree || tree.getValue(head).isPathAFile(tail)
605517
}
606518

607-
return listOf(reportFileResult)
519+
fun toFileList(): Set<String> =
520+
tree.flatMapTo(mutableSetOf()) { (key, value) ->
521+
value.toFileList().map { resolvePath(key, it, isDirectory = false) }
522+
}.plus("/")
608523
}
524+
525+
@Serializable
526+
internal data class OpossumSignal(
527+
@Transient
528+
val uuid: UUID = UUID.randomUUID(),
529+
val source: OpossumSignalSource,
530+
val attributionConfidence: Int = 80,
531+
val packageType: String?,
532+
val packageNamespace: String?,
533+
val packageName: String?,
534+
val packageVersion: String?,
535+
val copyright: String?,
536+
val licenseName: String?,
537+
val url: String?,
538+
val preSelected: Boolean,
539+
val followUp: OpossumFollowUp?,
540+
val excludeFromNotice: Boolean,
541+
val comment: String?
542+
) {
543+
companion object {
544+
fun create(
545+
source: String,
546+
id: Identifier? = null,
547+
url: String? = null,
548+
license: SpdxExpression? = null,
549+
copyright: String? = null,
550+
comment: String? = null,
551+
preSelected: Boolean = false,
552+
followUp: Boolean = false,
553+
excludeFromNotice: Boolean = false
554+
): OpossumSignal {
555+
return OpossumSignal(
556+
source = OpossumSignalSource(name = source),
557+
packageType = id?.getPurlType(),
558+
packageNamespace = id?.namespace,
559+
packageName = id?.name,
560+
packageVersion = id?.version,
561+
copyright = copyright,
562+
licenseName = license?.toString(),
563+
url = url,
564+
preSelected = preSelected,
565+
followUp = OpossumFollowUp.FOLLOW_UP.takeIf { followUp },
566+
excludeFromNotice = excludeFromNotice,
567+
comment = comment
568+
)
569+
}
570+
}
571+
572+
fun matches(other: OpossumSignal): Boolean =
573+
source == other.source
574+
&& packageType == other.packageType
575+
&& packageNamespace == other.packageNamespace
576+
&& packageName == other.packageName
577+
&& packageVersion == other.packageVersion
578+
&& copyright == other.copyright
579+
&& licenseName == other.licenseName
580+
&& url == other.url
581+
&& preSelected == other.preSelected
582+
&& comment == other.comment
583+
}
584+
585+
@Serializable
586+
internal data class OpossumSignalSource(
587+
val name: String,
588+
val documentConfidence: Int = 80
589+
)
590+
591+
@Serializable
592+
internal enum class OpossumFollowUp {
593+
FOLLOW_UP
594+
}
595+
596+
@Serializable
597+
internal data class OpossumFrequentLicense(
598+
val shortName: String,
599+
val fullName: String?,
600+
val defaultText: String?
601+
) : Comparable<OpossumFrequentLicense> {
602+
override fun compareTo(other: OpossumFrequentLicense) =
603+
compareValuesBy(
604+
this,
605+
other,
606+
{ it.shortName },
607+
{ it.fullName },
608+
{ it.defaultText }
609+
)
610+
}
611+
612+
@Serializable
613+
internal data class OpossumExternalAttributionSource(
614+
val name: String,
615+
val priority: Int
616+
)
609617
}
610618

611619
private fun DependencyNode.getDependencies(): List<DependencyNode> =

0 commit comments

Comments
 (0)