Skip to content

Commit

Permalink
Add RequireJavaVendor rule. Fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Jan 25, 2021
1 parent a7c4626 commit f774eb4
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2021 The author and/or original authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package enforcer.rules

import groovy.transform.CompileStatic
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.kordamp.gradle.plugin.enforcer.api.EnforcerContext
import org.kordamp.gradle.plugin.enforcer.api.EnforcerPhase
import org.kordamp.gradle.plugin.enforcer.api.EnforcerRuleException

import javax.inject.Inject

import static org.kordamp.gradle.plugin.enforcer.api.EnforcerPhase.AFTER_PROJECT
import static org.kordamp.gradle.plugin.enforcer.api.EnforcerPhase.BEFORE_BUILD

/**
* This rule checks that the Java vendor is allowed.
*
* Adapted from {@code org.apache.maven.plugins.enforcer.RequireJavaVendor}
* Original author: Tim Sijstermans
*
* @author Andres Almiray
* @since 0.9.0
*/
@CompileStatic
class RequireJavaVendor extends AbstractStandardEnforcerRule {
final ListProperty<String> includes
final ListProperty<String> excludes

@Inject
RequireJavaVendor(ObjectFactory objects) throws EnforcerRuleException {
super(objects, [BEFORE_BUILD, AFTER_PROJECT] as EnforcerPhase[])
includes = objects.listProperty(String).convention([])
excludes = objects.listProperty(String).convention([])
}

void include(String str) {
includes.add(str)
}

void exclude(String str) {
excludes.add(str)
}

@Override
protected void doExecute(EnforcerContext context) throws EnforcerRuleException {
String javaVendor = System.getProperty('java.vendor')

if (excludes.get().contains(javaVendor)) {
if (!includes.get().empty) {
if (!includes.get().contains(javaVendor)) {
throw createException(javaVendor)
}
return
}
throw createException(javaVendor)
}
}

private EnforcerRuleException createException(String javaVendor) {
StringBuilder sb = new StringBuilder()

if (message.present) {
sb.append(message.get())
.append(System.lineSeparator())
}
sb.append("Vendor ${javaVendor} is in a list of banned vendors: ${excludes.get()}")

fail(sb.toString())
}
}

1 change: 1 addition & 0 deletions docs/guide/src/docs/asciidoc/rules.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ include::{includedir}/rules/require-files-exist.adoc[]
include::{includedir}/rules/require-files-size.adoc[]
include::{includedir}/rules/require-gradle-property.adoc[]
include::{includedir}/rules/require-gradle-version.adoc[]
include::{includedir}/rules/require-java-vendor.adoc[]
include::{includedir}/rules/require-java-version.adoc[]
include::{includedir}/rules/require-os.adoc[]
include::{includedir}/rules/require-release-deps.adoc[]
Expand Down
53 changes: 53 additions & 0 deletions docs/guide/src/docs/asciidoc/rules/require-java-vendor.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

= RequireJavaVendor

This rule enforces that the Java vendor is allowed.

== Allowed Phases
* BEFORE_BUILD
* AFTER_PROJECT

== Default Phases
* BEFORE_BUILD
* AFTER_PROJECT

== Configuration
[source,groovy]
[subs="+macros"]
----
enforce {
rule(enforcer.rules.RequireJavaVendor) { r ->
r.enabled
r.enforcerLevel
r.message
r.phases
r.excludes
r.includes
}
}
----

== Properties

[%header, cols="<,<,<,^,<4"]
|===
| Name
| Type
| Default
| Required
| Description

| excludes
| ListProperty<String>
| `[ ]`
| {icon_req_n}
| Specify the banned vendors. This should be an exact match of the System Property `java.vendor`.

| includes
| ListProperty<String>
| `[ ]`
| {icon_req_n}
| Specify the allowed vendor names. This should be an exact match of the System Property `java.vendor`.

|===

4 changes: 4 additions & 0 deletions docs/guide/src/docs/asciidoc/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ The following table shows rules available to both plugins
| {icon_req_n}
| {icon_req_y}

| <<RequireJavaVendor>>
| {icon_req_y}
| {icon_req_y}

| <<RequireJavaVersion>>
| {icon_req_y}
| {icon_req_y}
Expand Down

0 comments on commit f774eb4

Please sign in to comment.