Skip to content

Commit 97d1c08

Browse files
committed
refactor(scanner): Introduce a ScannerMatcherCriteria interface
Replace the `ScannerMatcherConfig` data class with a `ScannerMatcherCriteria` interface which is implemented by the scanners' config classes to directly retrieve the matcher criteria. This avoids some code duplication in the scanner implementations. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 187bd6e commit 97d1c08

File tree

11 files changed

+98
-101
lines changed

11 files changed

+98
-101
lines changed

plugins/scanners/askalono/src/main/kotlin/Askalono.kt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import org.ossreviewtoolkit.scanner.LocalPathScannerWrapper
3838
import org.ossreviewtoolkit.scanner.ScanContext
3939
import org.ossreviewtoolkit.scanner.ScanException
4040
import org.ossreviewtoolkit.scanner.ScannerMatcher
41-
import org.ossreviewtoolkit.scanner.ScannerMatcherConfig
4241
import org.ossreviewtoolkit.scanner.ScannerWrapperFactory
4342
import org.ossreviewtoolkit.utils.common.CommandLineTool
4443
import org.ossreviewtoolkit.utils.common.Os
@@ -66,18 +65,7 @@ class Askalono(
6665
config: AskalonoConfig
6766
) : LocalPathScannerWrapper() {
6867
override val configuration = ""
69-
70-
override val matcher by lazy {
71-
ScannerMatcher.create(
72-
details,
73-
ScannerMatcherConfig(
74-
config.regScannerName,
75-
config.minVersion,
76-
config.maxVersion,
77-
config.configuration
78-
)
79-
)
80-
}
68+
override val matcher by lazy { ScannerMatcher.create(details, config) }
8169

8270
override val version by lazy {
8371
require(AskalonoCommand.isInPath()) {

plugins/scanners/askalono/src/main/kotlin/AskalonoConfig.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,29 @@
2020
package org.ossreviewtoolkit.plugins.scanners.askalono
2121

2222
import org.ossreviewtoolkit.plugins.api.OrtPluginOption
23+
import org.ossreviewtoolkit.scanner.ScannerMatcherCriteria
2324

2425
data class AskalonoConfig(
2526
/**
2627
* A regular expression to match the scanner name when looking up scan results in the storage.
2728
*/
28-
val regScannerName: String?,
29+
override val regScannerName: String?,
2930

3031
/**
3132
* The minimum version of stored scan results to use.
3233
*/
33-
val minVersion: String?,
34+
override val minVersion: String?,
3435

3536
/**
3637
* The maximum version of stored scan results to use.
3738
*/
38-
val maxVersion: String?,
39+
override val maxVersion: String?,
3940

4041
/**
4142
* The configuration to use for the scanner. Only scan results with the same configuration are used when looking up
4243
* scan results in the storage.
4344
*/
44-
val configuration: String?,
45+
override val configuration: String?,
4546

4647
/**
4748
* Whether to read scan results from the storage.
@@ -54,4 +55,4 @@ data class AskalonoConfig(
5455
*/
5556
@OrtPluginOption(defaultValue = "true")
5657
val writeToStorage: Boolean
57-
)
58+
) : ScannerMatcherCriteria

plugins/scanners/boyterlc/src/main/kotlin/BoyterLc.kt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import org.ossreviewtoolkit.scanner.LocalPathScannerWrapper
3737
import org.ossreviewtoolkit.scanner.ScanContext
3838
import org.ossreviewtoolkit.scanner.ScanException
3939
import org.ossreviewtoolkit.scanner.ScannerMatcher
40-
import org.ossreviewtoolkit.scanner.ScannerMatcherConfig
4140
import org.ossreviewtoolkit.scanner.ScannerWrapperFactory
4241
import org.ossreviewtoolkit.utils.common.CommandLineTool
4342
import org.ossreviewtoolkit.utils.common.Os
@@ -72,18 +71,7 @@ class BoyterLc(
7271
}
7372

7473
override val configuration = CONFIGURATION_OPTIONS.joinToString(" ")
75-
76-
override val matcher by lazy {
77-
ScannerMatcher.create(
78-
details,
79-
ScannerMatcherConfig(
80-
config.regScannerName,
81-
config.minVersion,
82-
config.maxVersion,
83-
config.configuration
84-
)
85-
)
86-
}
74+
override val matcher by lazy { ScannerMatcher.create(details, config) }
8775

8876
override val version by lazy {
8977
require(BoyterLcCommand.isInPath()) {

plugins/scanners/boyterlc/src/main/kotlin/BoyterLcConfig.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,29 @@
2020
package org.ossreviewtoolkit.plugins.scanners.boyterlc
2121

2222
import org.ossreviewtoolkit.plugins.api.OrtPluginOption
23+
import org.ossreviewtoolkit.scanner.ScannerMatcherCriteria
2324

2425
data class BoyterLcConfig(
2526
/**
2627
* A regular expression to match the scanner name when looking up scan results in the storage.
2728
*/
28-
val regScannerName: String?,
29+
override val regScannerName: String?,
2930

3031
/**
3132
* The minimum version of stored scan results to use.
3233
*/
33-
val minVersion: String?,
34+
override val minVersion: String?,
3435

3536
/**
3637
* The maximum version of stored scan results to use.
3738
*/
38-
val maxVersion: String?,
39+
override val maxVersion: String?,
3940

4041
/**
4142
* The configuration to use for the scanner. Only scan results with the same configuration are used when looking up
4243
* scan results in the storage.
4344
*/
44-
val configuration: String?,
45+
override val configuration: String?,
4546

4647
/**
4748
* Whether to read scan results from the storage.
@@ -54,4 +55,4 @@ data class BoyterLcConfig(
5455
*/
5556
@OrtPluginOption(defaultValue = "true")
5657
val writeToStorage: Boolean
57-
)
58+
) : ScannerMatcherCriteria

plugins/scanners/licensee/src/main/kotlin/Licensee.kt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import org.ossreviewtoolkit.scanner.LocalPathScannerWrapper
4444
import org.ossreviewtoolkit.scanner.ScanContext
4545
import org.ossreviewtoolkit.scanner.ScanException
4646
import org.ossreviewtoolkit.scanner.ScannerMatcher
47-
import org.ossreviewtoolkit.scanner.ScannerMatcherConfig
4847
import org.ossreviewtoolkit.scanner.ScannerWrapperFactory
4948
import org.ossreviewtoolkit.utils.common.CommandLineTool
5049
import org.ossreviewtoolkit.utils.common.Os
@@ -75,18 +74,7 @@ class Licensee(
7574
}
7675

7776
override val configuration = CONFIGURATION_OPTIONS.joinToString(" ")
78-
79-
override val matcher by lazy {
80-
ScannerMatcher.create(
81-
details,
82-
ScannerMatcherConfig(
83-
config.regScannerName,
84-
config.minVersion,
85-
config.maxVersion,
86-
config.configuration
87-
)
88-
)
89-
}
77+
override val matcher by lazy { ScannerMatcher.create(details, config) }
9078

9179
override val version by lazy {
9280
require(LicenseeCommand.isInPath()) {

plugins/scanners/licensee/src/main/kotlin/LicenseeConfig.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,29 @@
2020
package org.ossreviewtoolkit.plugins.scanners.licensee
2121

2222
import org.ossreviewtoolkit.plugins.api.OrtPluginOption
23+
import org.ossreviewtoolkit.scanner.ScannerMatcherCriteria
2324

2425
data class LicenseeConfig(
2526
/**
2627
* A regular expression to match the scanner name when looking up scan results in the storage.
2728
*/
28-
val regScannerName: String?,
29+
override val regScannerName: String?,
2930

3031
/**
3132
* The minimum version of stored scan results to use.
3233
*/
33-
val minVersion: String?,
34+
override val minVersion: String?,
3435

3536
/**
3637
* The maximum version of stored scan results to use.
3738
*/
38-
val maxVersion: String?,
39+
override val maxVersion: String?,
3940

4041
/**
4142
* The configuration to use for the scanner. Only scan results with the same configuration are used when looking up
4243
* scan results in the storage.
4344
*/
44-
val configuration: String?,
45+
override val configuration: String?,
4546

4647
/**
4748
* Whether to read scan results from the storage.
@@ -54,4 +55,4 @@ data class LicenseeConfig(
5455
*/
5556
@OrtPluginOption(defaultValue = "true")
5657
val writeToStorage: Boolean
57-
)
58+
) : ScannerMatcherCriteria

plugins/scanners/scancode/src/main/kotlin/ScanCode.kt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import org.ossreviewtoolkit.plugins.api.PluginDescriptor
3333
import org.ossreviewtoolkit.scanner.LocalPathScannerWrapper
3434
import org.ossreviewtoolkit.scanner.ScanContext
3535
import org.ossreviewtoolkit.scanner.ScannerMatcher
36-
import org.ossreviewtoolkit.scanner.ScannerMatcherConfig
3736
import org.ossreviewtoolkit.scanner.ScannerWrapperFactory
3837
import org.ossreviewtoolkit.utils.common.CommandLineTool
3938
import org.ossreviewtoolkit.utils.common.Os
@@ -115,17 +114,7 @@ class ScanCode(
115114
}.joinToString(" ")
116115
}
117116

118-
override val matcher by lazy {
119-
ScannerMatcher.create(
120-
details,
121-
ScannerMatcherConfig(
122-
config.regScannerName,
123-
config.minVersion,
124-
config.maxVersion,
125-
config.configuration
126-
)
127-
)
128-
}
117+
override val matcher by lazy { ScannerMatcher.create(details, config) }
129118

130119
override val version by lazy {
131120
require(ScanCodeCommand.isInPath()) {

plugins/scanners/scancode/src/main/kotlin/ScanCodeConfig.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package org.ossreviewtoolkit.plugins.scanners.scancode
2222
import org.ossreviewtoolkit.model.ScannerDetails
2323
import org.ossreviewtoolkit.plugins.api.OrtPluginOption
2424
import org.ossreviewtoolkit.scanner.ScanStorage
25+
import org.ossreviewtoolkit.scanner.ScannerMatcherCriteria
2526

2627
data class ScanCodeConfig(
2728
/**
@@ -53,23 +54,23 @@ data class ScanCodeConfig(
5354
/**
5455
* A regular expression to match the scanner name when looking up scan results in the storage.
5556
*/
56-
val regScannerName: String?,
57+
override val regScannerName: String?,
5758

5859
/**
5960
* The minimum version of stored scan results to use.
6061
*/
61-
val minVersion: String?,
62+
override val minVersion: String?,
6263

6364
/**
6465
* The maximum version of stored scan results to use.
6566
*/
66-
val maxVersion: String?,
67+
override val maxVersion: String?,
6768

6869
/**
6970
* The configuration to use for the scanner. Only scan results with the same configuration are used when looking up
7071
* scan results in the storage.
7172
*/
72-
val configuration: String?,
73+
override val configuration: String?,
7374

7475
/**
7576
* Whether to read scan results from the storage.
@@ -82,4 +83,4 @@ data class ScanCodeConfig(
8283
*/
8384
@OrtPluginOption(defaultValue = "true")
8485
val writeToStorage: Boolean
85-
)
86+
) : ScannerMatcherCriteria

scanner/src/main/kotlin/ScannerMatcher.kt

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ data class ScannerMatcher(
6363
/**
6464
* Return a [ScannerMatcher] instance that is to be used when looking up existing scan results from a
6565
* [ScanStorageReader]. By default, the properties of this instance are initialized to match the scanner
66-
* [details]. These defaults can be overridden by the provided [config].
66+
* [details]. These defaults can be overridden by the provided [criteria].
6767
*/
68-
fun create(details: ScannerDetails, config: ScannerMatcherConfig = ScannerMatcherConfig.EMPTY): ScannerMatcher {
68+
fun create(details: ScannerDetails, criteria: ScannerMatcherCriteria? = null): ScannerMatcher {
6969
val scannerVersion = checkNotNull(Semver.coerce(details.version))
70-
val minVersion = Semver.coerce(config.minVersion) ?: scannerVersion
71-
val maxVersion = Semver.coerce(config.maxVersion) ?: minVersion.nextMajor()
72-
val name = config.regScannerName ?: details.name
73-
val configuration = config.configuration ?: details.configuration
70+
val minVersion = Semver.coerce(criteria?.minVersion) ?: scannerVersion
71+
val maxVersion = Semver.coerce(criteria?.maxVersion) ?: minVersion.nextMajor()
72+
val name = criteria?.regScannerName ?: details.name
73+
val configuration = criteria?.configuration ?: details.configuration
7474

7575
return ScannerMatcher(name, minVersion, maxVersion, configuration)
7676
}
@@ -96,17 +96,3 @@ data class ScannerMatcher(
9696
return version in minVersion..<maxVersion && configuration == details.configuration
9797
}
9898
}
99-
100-
/**
101-
* A holder class for the [ScannerMatcher] configuration.
102-
*/
103-
data class ScannerMatcherConfig(
104-
val regScannerName: String? = null,
105-
val minVersion: String? = null,
106-
val maxVersion: String? = null,
107-
val configuration: String? = null
108-
) {
109-
companion object {
110-
val EMPTY = ScannerMatcherConfig(null, null, null, null)
111-
}
112-
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2020 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.scanner
21+
22+
import org.ossreviewtoolkit.model.ScannerDetails
23+
24+
/**
25+
* A holder class for the [ScannerMatcher] criteria. Only non-null properties are taken into account for matching.
26+
*/
27+
interface ScannerMatcherCriteria {
28+
/**
29+
* Criterion to match the scanner name. This string is interpreted as a regular expression. In the most basic
30+
* form, it can be an exact scanner name, but by using features of regular expressions, a more advanced
31+
* matching can be achieved. So it is possible, for instance, to select multiple scanners using an alternative ('|')
32+
* expression or an arbitrary one using a wildcard ('.*').
33+
*/
34+
val regScannerName: String?
35+
36+
/**
37+
* Criterion to match the scanner version, including this minimum version. Results are accepted if they are produced
38+
* by scanners with a version greater than or equal to this version.
39+
*/
40+
val minVersion: String?
41+
42+
/**
43+
* Criterion to match the scanner version, excluding this maximum version. Results are accepted if they are produced
44+
* by scanners with a version less than this version.
45+
*/
46+
val maxVersion: String?
47+
48+
/**
49+
* Criterion to match the [configuration][ScannerDetails.configuration] of the scanner.
50+
*/
51+
val configuration: String?
52+
}

0 commit comments

Comments
 (0)