Skip to content

Commit

Permalink
Move Gradle classes to gradle package
Browse files Browse the repository at this point in the history
and improve type safety of plugin implemenentation
  • Loading branch information
ascheman committed Sep 18, 2024
1 parent 9307f2e commit 833c6d9
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion htmlSanityCheck-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ gradlePlugin {
plugins {
htmlSanityCheck {
id = 'org.aim42.htmlSanityCheck'
implementationClass = 'org.aim42.htmlsanitycheck.HtmlSanityCheckPlugin'
implementationClass = 'org.aim42.htmlsanitycheck.gradle.HtmlSanityCheckPlugin'
displayName = 'Gradle HtmlSanityCheck Plugin'
description = project.description
tags = ["checks", "quality", "documentation", "html", "asciidoctor", "test"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
// see end-of-file for license information

package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle

import groovy.transform.TypeChecked
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.language.base.plugins.LifecycleBasePlugin

@TypeChecked
// tag::gradle-plugin-implementation[]
class HtmlSanityCheckPlugin implements Plugin<Project> {

final static String HTML_SANITY_CHECK = "htmlSanityCheck"

void apply(Project project) {

project.task( HTML_SANITY_CHECK,
type: HtmlSanityCheckTask,
description: "performs semantic checks on html files",
group: LifecycleBasePlugin.VERIFICATION_GROUP)

project.tasks.register( HTML_SANITY_CHECK, HtmlSanityCheckTask.class)
}
}
// end::gradle-plugin-implementation[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle

import groovy.transform.TypeChecked
import groovy.transform.TypeCheckingMode
import org.aim42.htmlsanitycheck.AllChecksRunner
import org.aim42.htmlsanitycheck.Configuration
import org.aim42.htmlsanitycheck.check.AllCheckers
import org.aim42.htmlsanitycheck.check.Checker
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
Expand All @@ -10,6 +15,7 @@ import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.language.base.plugins.LifecycleBasePlugin

// see end-of-file for license information

Expand All @@ -21,6 +27,7 @@ import org.gradle.api.tasks.TaskAction
*
* @author Gernot Starke
*/
@TypeChecked
class HtmlSanityCheckTask extends DefaultTask {

// we support checking several named files
Expand Down Expand Up @@ -74,7 +81,7 @@ class HtmlSanityCheckTask extends DefaultTask {
Set<Integer> httpSuccessCodes

@Input
List<Class> checkerClasses = AllCheckers.CHECKER_CLASSES
List<Class<? extends Checker>> checkerClasses = AllCheckers.CHECKER_CLASSES

// private stuff
// **************************************************************************
Expand All @@ -87,6 +94,8 @@ class HtmlSanityCheckTask extends DefaultTask {
* by setting outputs.upToDateWhen to false.
*/
HtmlSanityCheckTask() {
description = "performs semantic checks on html files"
group = LifecycleBasePlugin.VERIFICATION_GROUP
// Never consider this task up-to-date.
// thx https://github.com/stevesaliman/gradle-cobertura-plugin/commit/d61191f7d5f4e8e89abcd5f3839a210985526648
outputs.upToDateWhen { false }
Expand All @@ -96,6 +105,7 @@ class HtmlSanityCheckTask extends DefaultTask {
junitResultsDir = new File(project.DEFAULT_BUILD_DIR_NAME, '/test-results/htmlSanityCheck/')
}

@TypeChecked(TypeCheckingMode.SKIP)
void setSourceDir(File sourceDir) {
this.sourceDir = sourceDir
if (sourceDocuments == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle

import org.junit.Rule
import org.junit.rules.TemporaryFolder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle


import org.gradle.api.Project
import org.gradle.language.base.plugins.LifecycleBasePlugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle


import org.gradle.testkit.runner.GradleRunner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle

import org.aim42.htmlsanitycheck.MisconfigurationException
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/docs/arc42/chapters/chap-08-gradle-plugin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ we implement a lean wrapper as described in the Gradle user guide.

[source, groovy]
----
include::{gradle-plugin-source-path}/htmlsanitycheck/HtmlSanityCheckPlugin.groovy[tag=gradle-plugin-implementation]
include::{gradle-plugin-source-path}/htmlsanitycheck/gradle/HtmlSanityCheckPlugin.groovy[tag=gradle-plugin-implementation]
----

==== Directory Structure and Required Files
Expand Down
14 changes: 8 additions & 6 deletions src/docs/development/development-intro.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ TBD
| | | | |-org
| | | | | |-aim42
| | | | | | |-htmlsanitycheck
| | | | | | | | ...
| | | | | | | |-HtmlSanityCheckPlugin.groovy // <1>
| | | | | | | |-HtmlSanityCheckTask.groovy
| | | | | | | |-gradle
| | | | | | | | | ...
| | | | | | | | |-HtmlSanityCheckPlugin.groovy // <1>
| | | | | | | | |-HtmlSanityCheckTask.groovy
| | | | |-resources
| | | | | |-META-INF // <2>
| | | | | | |-gradle-plugins
Expand All @@ -63,15 +64,16 @@ TBD
| | | | |-org
| | | | | |-aim42
| | | | | | |-htmlsanitycheck
| | | | | | | | ...
| | | | | | | |-HtmlSanityCheckPluginTest
| | | | | | | |-gradle
| | | | | | | | | ...
| | | | | | | | |-HtmlSanityCheckPluginSpec
|
----
<1> the actual plugin code, a xyzPlugin and xyzTask groovy files
<2> Gradle expects plugin properties in META-INF
<3> Property file containing the name of the actual implementation class:

implementation-class=org.aim42.htmlsanitycheck.HtmlSanityCheckPlugin
implementation-class=org.aim42.htmlsanitycheck.gradle.HtmlSanityCheckPlugin

==== Notes for IntelliJ Users
If you work with IntelliJ, you should configure your run- and test configurations
Expand Down

0 comments on commit 833c6d9

Please sign in to comment.