sleekbyte/tailor

View on GitHub
build.gradle

Summary

Maintainability
Test Coverage
// Plugins {{{
plugins {
    id "com.github.kt3k.coveralls" version "2.6.3"
}
// }}}

// Java Plugin {{{
apply plugin: 'java'

archivesBaseName = 'tailor'
def props = new Properties()
def config = file('src/main/resources/config.properties')
props.load(new FileInputStream(config))
version = props.getProperty('version')

// Add ANTLR output as sourceSet
sourceSets {
    gen {
        java {
            srcDir file('src/gen/java')
        }
    }
}

test {
    // Restrict heap size for the test JVM(s)
    maxHeapSize = "2g"
    // Display useful test events and output on the console
    testLogging {
        events "skipped", "failed", "standardOut", "standardError"
    }
}

// JaCoCo Plugin {{{
apply plugin: 'jacoco'

// Ensure both XML and HTML reports are generated
jacocoTestReport {
    reports {
        xml.enabled = true
        html.enabled = true
    }
}

// Generate a test coverage report
test.finalizedBy jacocoTestReport
jacocoTestReport.dependsOn test
// }}}

compileJava {
    // Enable compilation in a separate daemon process
    options.fork = true
    // Enable incremental compilation
    options.incremental = true
}

// Ensure main JAR includes ANTLR output
jar {
    from sourceSets.gen.output
}

// Ensure ANTLR output is generated before compilation
compileGenJava.dependsOn 'generateGrammarSource'
// }}}

// ANTLR Plugin {{{
// Generate ANTLR output based on Swift grammar
apply plugin: 'antlr'

generateGrammarSource {
    outputDirectory = file('src/gen/java')
}
// }}}

// Application Plugin {{{
// Add run task
apply plugin: 'application'

applicationName = 'tailor'
mainClassName = 'com.sleekbyte.tailor.Tailor'

// Allow arguments: `gradle run -Pargs="args"`
run {
    if (project.hasProperty('args')) {
        args project.args.split('\\s+')
    }
}

// Exclude unnecessary compile/runtime dependencies from distribution
distributions.main.contents {
    exclude(
        'antlr4-4.5.3.jar',
        'com/',
    )
}
// }}}

// Code Style Plugins {{{
// Use [Checkstyle](http://checkstyle.sourceforge.net) Plugin
apply plugin: 'checkstyle'

checkstyle {
    toolVersion = "6.7"
    // Exclude generated code from Checkstyle checks
    sourceSets -= sourceSets.gen
}

// Fail the build if checkstyleMain or checkstyleTest have warnings
task verifyNoCheckstyleWarnings << {
    def checkstyleMainWarningsFile = 'build/reports/checkstyle/main.xml'
    def checkstyleTestWarningsFile = 'build/reports/checkstyle/test.xml'
    File mainWarningsFile = file(checkstyleMainWarningsFile)
    File testWarningsFile = file(checkstyleTestWarningsFile)
    if (mainWarningsFile.exists() && mainWarningsFile.text.contains("<error ")) {
        throw new GradleException("There were Checkstyle warnings! For more info check $mainWarningsFile")
    }
    if (testWarningsFile.exists() && testWarningsFile.text.contains("<error ")) {
        throw new GradleException("There were Checkstyle warnings! For more info check $testWarningsFile")
    }
}

checkstyleMain {
    configFile = file('config/checkstyle/google_checks.xml')
    // Always execute verifyNoCheckstyleWarnings task after this task
    finalizedBy verifyNoCheckstyleWarnings
}

checkstyleTest {
    configFile = file('config/checkstyle/google_test_checks.xml')
    // Always execute verifyNoCheckstyleWarnings task after this task
    finalizedBy verifyNoCheckstyleWarnings
}

// Use [FindBugs](http://findbugs.sourceforge.net/) Plugin
apply plugin: 'findbugs'

findbugs {
    // Exclude generated code from FindBugs checks
    sourceSets -= sourceSets.gen
}

tasks.withType(FindBugs) {
    reports {
        // Use HTML reports instead of XML
        xml.enabled = false
        html.enabled = true
    }
}

// Use [PMD](http://pmd.sourceforge.net/) Plugin
apply plugin: 'pmd'

pmd {
    // Exclude generated code from PMD checks
    sourceSets -= sourceSets.gen
    // Disable default ruleSets
    ruleSets = []
    // Use custom ruleSet file
    ruleSetFiles = files('config/pmd/tailorRuleSet.xml')
}

tasks.withType(Pmd) {
    reports {
        // Use HTML reports instead of XML
        xml.enabled = false
        html.enabled = true
    }
}
// }}}

// RubyGems {{{
task gems(type: Exec) {
    inputs.file file('src/dist/gems/Gemfile')
    outputs.dir file('src/dist/gems/vendor/')
    workingDir 'src/dist/gems'
    executable 'sh'
    args '-c', 'bundle package --no-install'
}
// Ensure gems are packaged into distribution
distZip.dependsOn 'gems'
distTar.dependsOn 'gems'
installDist.dependsOn 'gems'
// }}}

// Update Version Number {{{
task setVersion {
    doFirst {
        println "Enter new version number:"
        def writer = new FileWriter(config)
        version = "${System.in.newReader().readLine()}"
        try {
            props.setProperty("version", "${version}")
                props.store(writer, null)
                writer.flush()
        } finally {
            writer.close()
        }
        updateInstallScripts.execute()
    }
}

task updateInstallScripts(type: Exec) {
    doFirst {
        commandLine "sed", "-i", "", "s/[0-9]\\{1,\\}\\.[0-9]\\{1,\\}\\.[0-9]\\{1,\\}/${version}/g", "script/install.sh", "script/install.ps1", "script/uninstall.ps1"
    }
}
// }}}

// Generate man page {{{
task manpage(type: Exec) {
    inputs.files files('man/Gemfile', 'man/tailor.1.ronn')
    outputs.files files('man/vendor/', 'src/dist/tailor.1')
    workingDir 'man'
    executable 'sh'
    args '-c', "bundle install --path vendor/bundle" +
        " && bundle exec ronn --manual='Tailor Manual' --organization='Sleekbyte' --roff tailor.1.ronn" +
        " && mv tailor.1 ../src/dist/"
}
// Ensure man page is packaged into distribution
distZip.dependsOn 'manpage'
distTar.dependsOn 'manpage'
installDist.dependsOn 'manpage'
// }}}

// Install Globally {{{
task extractGlobally(type: Sync) {
    from zipTree("${buildDir}/distributions/tailor-${version}.zip")
    into '/usr/local/tailor'
}

task installMan(type: Exec) {
    commandLine "ln", "-fs", "/usr/local/tailor/tailor-${version}/tailor.1", "/usr/local/share/man/man1/tailor.1"
}

task install(type: Exec) {
    commandLine "ln", "-fs", "/usr/local/tailor/tailor-${version}/bin/tailor", "/usr/local/bin/tailor"
}

extractGlobally.dependsOn distZip
installMan.dependsOn extractGlobally
install.dependsOn installMan
// }}}

// Ensure ANTLR output, generated man page, and Ruby gems are deleted via clean
clean {
    delete file('src/gen/')
    delete files('src/dist/tailor.1')
    delete files('src/dist/gems/.bundle/', 'src/dist/gems/Gemfile.lock', 'src/dist/gems/vendor/')
}

repositories {
    jcenter()
}

dependencies {
    antlr 'org.antlr:antlr4:4.6'
    genCompileOnly 'org.antlr:antlr4-runtime:4.6'
    // Ensure ANTLR output is compiled before main sourceSet
    compile sourceSets.gen.output
    compile 'commons-cli:commons-cli:1.4'
    compile 'org.antlr:antlr4-runtime:4.6'
    compile 'org.fusesource.jansi:jansi:1.15'
    compile 'org.yaml:snakeyaml:1.18'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.github.spullara.mustache.java:compiler:0.9.4'
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-all:1.3'
    testCompile 'org.mockito:mockito-core:2.7.18'
    testCompile 'com.github.stefanbirkner:system-rules:1.16.1'
}