Skip to content

Commit

Permalink
Change HtmlSanityCheckTask.sourceDocuments to a org.gradle.api.file.F…
Browse files Browse the repository at this point in the history
…ileCollection to benefit from finding and filtering capabilities of the FileCollection API. (#267)

Fixes #264.
  • Loading branch information
erdi authored and gernotstarke committed Feb 10, 2019
1 parent 5042576 commit 6dc5e59
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 418 deletions.
13 changes: 6 additions & 7 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ This task exposes a few properties as part of its configuration:
[horizontal]
sourceDir:: (mandatory) directory where the html files are located. Type: File. Default: `build/docs`.
sourceDocuments:: (optional) an override to process several source files, which may be a subset of all
files available in [x-]`${sourceDir}`. Type: Set.
Defaults to all files in [x-]`${sourceDir}`.
files available in [x-]`${sourceDir}`. Type: `org.gradle.api.file.FileCollection`.
Defaults to all files in [x-]`${sourceDir}` whose names end with `.html`.

checkingResultsDir:: (optional) directory where the checking results written to.
Defaults to `${buildDir}/report/htmlchecks/`
Expand All @@ -101,9 +101,6 @@ apply plugin: 'org.aim42.htmlSanityCheck'
htmlSanityCheck {
sourceDir = new File( "$buildDir/docs" )
// files to check - in Set-notation
sourceDocuments = [ "one-file.html", "another-file.html", "index.html"]
// where to put results of sanityChecks...
checkingResultsDir = new File( "$buildDir/report/htmlchecks" )
Expand Down Expand Up @@ -188,8 +185,10 @@ htmlSanityCheck {
sourceDir = new File( htmlOutputPath )
// files to check, in Set-notation
sourceDocuments = [ "many-errors.html", "no-errors.html"]
// files to check, specified as a file tree with filtering
sourceDocuments = fileTree(sourceDir) {
include "many-errors.html", "no-errors.html"
}
// where to put results of sanityChecks...
checkingResultsDir = new File( checkingResultsPath )
Expand Down
94 changes: 0 additions & 94 deletions src/main/groovy/org/aim42/filesystem/FileCollector.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,8 @@ import java.util.regex.Pattern

class FileCollector {

// (?i): ignore-case
// htm(l): either htm or html
// at least one character left of the dot
public final static Pattern HTML_FILE_EXTENSION_PATTERN = ~/(?i).+\.htm(l)?$/

public final static Closure<Boolean> INCLUDE_HTML_FILES = { File file ->
// allow only html or htm files
(file.name ==~ HTML_FILE_EXTENSION_PATTERN)
}

public final static String IMAGE_FILE_EXTENSION_PATTERN = ~/(?i).+\.(jpg|jpeg|png|gif|bmp|svg)?$/


public static Boolean isHtmlFile(File file) {
return (isHtmlFile(file.absolutePath)
&& file.isFile())
}

/**
* checks if @param fileName represents a valid html file,
* ignoring the files' content!
* @param fileName
* @return
*/
public static Boolean isHtmlFile(String fileName) {
return (fileName ==~ HTML_FILE_EXTENSION_PATTERN)
}

/**
* checks if @param fileName represents an image file name,
* ignoring the files' content.
Expand All @@ -47,37 +21,6 @@ class FileCollector {
return (fileName ==~ IMAGE_FILE_EXTENSION_PATTERN)
}

/**
* The main use-case for this class: returns all configured html files as Set<File>.
* @param srcDir (mandatory) Directory where the html files are located. Type: File. Default: build/docs
* can be given as File or String
* @param sourceDocs (optional) Defaults to all files in ${srcDir}.
*/
public static Set<File> getHtmlFilesToCheck(File srcDir, Collection<String> sourceDocs) {
// first case: no document names given -> return all html files
// in directory
if ((sourceDocs == null) || (sourceDocs?.empty)) {
return getAllHtmlFilesFromDirectory(srcDir)
} else {
return getAllConfiguredHtmlFiles(srcDir, sourceDocs)
}
}

// if for other types of input parameters, convert and delegate

public static Set<File> getHtmlFilesToCheck(File srcDir, String sourceDoc) {
return getHtmlFilesToCheck(srcDir, [sourceDoc])
}

public static Set<File> getHtmlFilesToCheck(String srcDirName, ArrayList<String> sourceDocs) {
return getHtmlFilesToCheck(new File(srcDirName), sourceDocs)
}

public static Set<File> getHtmlFilesToCheck(String srcDirName, String sourceDocName) {
return getHtmlFilesToCheck(new File(srcDirName), [sourceDocName])
}


/**
* returns all configured image files as Set<File>
*
Expand All @@ -94,25 +37,6 @@ class FileCollector {
}
}

/**
* returns all html files in a given directory.
* (recursively looks in subdirectories)
* @param dir where to look for matching files
* @return all files with appropriate extension
*/
public static Set<File> getAllHtmlFilesFromDirectory(File dir) {
Set<File> files = new HashSet<File>()

// scan only files, not directories
dir.eachFileRecurse(FileType.FILES) { file ->
if (isHtmlFile(file)) {
files.add(file)
}
}

return files
}

/**
* returns all image files in a given directory.
* (recursively looks in subdirectories)
Expand All @@ -132,24 +56,6 @@ class FileCollector {
return files
}

/**
* returns all configured html files from @param srcDocs
* which really exist below @param srcDocs
*/
public static Set<File> getAllConfiguredHtmlFiles(File srcDir, Collection<String> srcDocs) {
Set<File> files = new HashSet<File>()

srcDocs.each { configuredFileName ->
File file = new File(srcDir, configuredFileName)
if (file.exists()) {
files.add(file)
}
}

return files

}

/**
* returns all configured image files from @param srcDocs
* which really exist below @param srcDocs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ class AllChecksRunner {

myConfig = pConfig

this.filesToCheck = FileCollector.getHtmlFilesToCheck(
myConfig.getConfigItemByName(Configuration.ITEM_NAME_sourceDir),
myConfig.getConfigItemByName (Configuration.ITEM_NAME_sourceDocuments)
)

this.filesToCheck = myConfig.getConfigItemByName(Configuration.ITEM_NAME_sourceDocuments)

// TODO: #185 (checker classes shall be detected automatically (aka CheckerFactory)
// CheckerFactory needs the configuration
Expand Down
30 changes: 7 additions & 23 deletions src/main/groovy/org/aim42/htmlsanitycheck/Configuration.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Configuration {
/**
* convenience method for simplified testing
*/
synchronized void addSourceFileConfiguration(File srcDir, Set<String> srcDocs) {
synchronized void addSourceFileConfiguration(File srcDir, Collection<File> srcDocs) {
addConfigurationItem(ITEM_NAME_sourceDir, srcDir)
addConfigurationItem(ITEM_NAME_sourceDocuments, srcDocs)
}
Expand Down Expand Up @@ -232,34 +232,18 @@ class Configuration {
throw new MisconfigurationException("source directory must not be null")
}

// cannot check if both input params are null
if ((srcDir == null) && (srcDocs == null)) {
throw new MisconfigurationException("both sourceDir and sourceDocs were null")
}

// no srcDir was given and empty SrcDocs
if ((!srcDir) && (srcDocs != null)) {
if ((srcDocs?.empty)) {
throw new MisconfigurationException("both sourceDir and sourceDocs must not be empty")
}
}
// non-existing srcDir is absurd too
if ((!srcDir.exists())) {
throw new MisconfigurationException("given sourceDir $srcDir does not exist.")
}

// if srcDir exists but is empty... no good :-(
if ((srcDir.exists())
&& (srcDir.isDirectory())
&& (srcDir.directorySize() == 0)) {
throw new MisconfigurationException("given sourceDir $srcDir is empty")
// cannot check if both input params are null
if (srcDocs == null) {
throw new MisconfigurationException("source documents must not be null")
}

// if srcDir exists but does not contain any html file... no good
if ((srcDir.exists())
&& (srcDir.isDirectory())
&& (FileCollector.getAllHtmlFilesFromDirectory(srcDir).size() == 0)) {
throw new MisconfigurationException("no html file found in $srcDir")
// empty SrcDocs
if (srcDocs.empty) {
throw new MisconfigurationException("source documents must not be empty")
}

Object checksToExecute = getConfigItemByName(Configuration.ITEM_NAME_checksToExecute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package org.aim42.htmlsanitycheck

import org.aim42.htmlsanitycheck.check.AllCheckers
import org.gradle.api.DefaultTask

// see end-of-file for license information
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.*

// see end-of-file for license information

/**
* Entry class for the gradle-plugin.
* Handles parameter-passing from gradle build scripts,
Expand All @@ -19,9 +20,8 @@ class HtmlSanityCheckTask extends DefaultTask {

//
// we support checking several named files
@Optional
@Input
Set<String> sourceDocuments
@InputFiles
FileCollection sourceDocuments

// or all (html) files in a directory
@InputDirectory
Expand Down Expand Up @@ -99,10 +99,18 @@ class HtmlSanityCheckTask extends DefaultTask {

}

/**
* entry point for several html sanity checks
* @author Gernot Starke <[email protected]>
*/
void setSourceDir(File sourceDir) {
this.sourceDir = sourceDir
if (sourceDocuments == null) {
sourceDocuments = project.fileTree(sourceDir)
sourceDocuments.include('**/*.html')
}
}

/**
* entry point for several html sanity checks
* @author Gernot Starke <[email protected]>
*/
@TaskAction
public void sanityCheckHtml() {

Expand Down Expand Up @@ -131,7 +139,7 @@ class HtmlSanityCheckTask extends DefaultTask {
logger.info("allFilesToCheck" + allFilesToCheck.toString(), "")

// create an AllChecksRunner...
def allChecksRunner = new AllChecksRunner( myConfig )
def allChecksRunner = new AllChecksRunner(myConfig)

// ... and perform the actual checks
def allChecks = allChecksRunner.performAllChecks()
Expand Down Expand Up @@ -166,7 +174,7 @@ See ${checkingResultsDir} for a detailed report."""
Configuration tmpConfig = new Configuration()

tmpConfig.with {
addConfigurationItem(Configuration.ITEM_NAME_sourceDocuments, sourceDocuments)
addConfigurationItem(Configuration.ITEM_NAME_sourceDocuments, sourceDocuments.files)
addConfigurationItem(Configuration.ITEM_NAME_sourceDir, sourceDir)
addConfigurationItem(Configuration.ITEM_NAME_checkingResultsDir, checkingResultsDir)
addConfigurationItem(Configuration.ITEM_NAME_junitResultsDir, junitResultsDir)
Expand Down
Loading

0 comments on commit 6dc5e59

Please sign in to comment.