Skip to content

Commit

Permalink
#301 added stream-based normalizer constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jk1 committed May 26, 2024
1 parent 30f36c1 commit 34d4f88
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,12 @@ The normalizer can be enabled via a filter.
import com.github.jk1.license.filter.*
licenseReport {
// LicenseBundleNormalizer also accepts bundle stream as a parameter
filters = [new LicenseBundleNormalizer(bundlePath: "$projectDir/config/license-normalizer-bundle.json")]
}
```

If no bundle-file is specified, a default file is used containing some commons rules. You are encouraged to create your own bundle-file
If no bundle-file is specified, a default file is used containing some common rules. You are encouraged to create your own bundle-file
and contribute back useful rules.

### SPDX support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class LicenseBundleNormalizer implements DependencyFilter {
private static Logger LOGGER = Logging.getLogger(ReportTask.class)

protected String bundlePath
protected InputStream bundleStream
protected boolean createDefaultTransformationRules
protected boolean isInitialized

Expand All @@ -60,6 +61,11 @@ class LicenseBundleNormalizer implements DependencyFilter {
this.createDefaultTransformationRules = createDefaultTransformationRules
}

LicenseBundleNormalizer(InputStream bundleStream, boolean createDefaultTransformationRules) {
this.bundleStream = bundleStream
this.createDefaultTransformationRules = createDefaultTransformationRules
}

synchronized void init() {
if (isInitialized) {
return
Expand All @@ -71,14 +77,10 @@ class LicenseBundleNormalizer implements DependencyFilter {
filterConfig += "createDefaultTransformationRules = $createDefaultTransformationRules\n"

if (bundlePath != null) {
def normalizerText = new File(bundlePath).text
filterConfig += "normalizerText = $normalizerText\n"

LOGGER.debug("Using supplied normalizer bundle from {}: {}", bundlePath, normalizerText)

def config = toConfig(new JsonSlurper().setType(JsonParserType.LAX).parse(normalizerText.chars))

mergeConfigIntoGlobalConfig(config)
applyBundleFrom(new File(bundlePath).text)
}
if (bundleStream != null) {
applyBundleFrom(bundleStream.text)
}

if (createDefaultTransformationRules) {
Expand All @@ -89,6 +91,13 @@ class LicenseBundleNormalizer implements DependencyFilter {
LOGGER.debug("Bundle normalizer initialized (Bundles: ${normalizerConfig.bundles.size()}, Rules: ${normalizerConfig.transformationRules.size()})")
}

private def applyBundleFrom(String text) {
filterConfig += "normalizerText = $text\n"
LOGGER.debug("Using supplied normalizer bundle: {}", text)
def config = toConfig(new JsonSlurper().setType(JsonParserType.LAX).parse(text.chars))
mergeConfigIntoGlobalConfig(config)
}

@Input
String getFilterConfigForCache() { return this.filterConfig }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class LicenseBundleNormalizerSpec extends Specification {
when:
new LicenseBundleNormalizer()
new LicenseBundleNormalizer(bundlePath: null)
new LicenseBundleNormalizer(bundleStream: null)
new LicenseBundleNormalizer(createDefaultTransformationRules: false)
new LicenseBundleNormalizer(bundlePath: null, createDefaultTransformationRules: false)

Expand Down Expand Up @@ -849,6 +850,6 @@ class LicenseBundleNormalizerSpec extends Specification {
}

private LicenseBundleNormalizer newNormalizer(boolean createDefaultTransformationRules = false) {
new LicenseBundleNormalizer(bundlePath: normalizerFile.absolutePath, createDefaultTransformationRules: createDefaultTransformationRules)
new LicenseBundleNormalizer(normalizerFile.absolutePath, createDefaultTransformationRules)
}
}

0 comments on commit 34d4f88

Please sign in to comment.