The below code is the actual build.gradle file used to build Asynchrone's website.
/*
*
* Version 1.0.0
* 2018-09-06
* Yves Vindevogel (vindevoy)
*
*/
/*
*
* Settings
* Leave this the top section so it's easy to adapt settings
*
*/
def buildDir = "${rootDir}/build/jbake"
/*
*
* Site related tasks
*
*/
// Clean removes all directories that contain files that are not in Git
task clean {
group "JBake"
description "Removes the build and log directory"
doLast {
def directories = ["build", "log", "database", "lib"]
logger.quiet "Cleaning: ${directories}\n"
directories.each {dir ->
logger.quiet "Deleting ${dir}"
project.exec {
workingDir rootDir
commandLine "rm", "-rf", "${rootDir}/${dir}"
}
}
logger.quiet "\nFinished cleaning"
}
}
// Bakes the site using the shell script
task bake {
group "JBake"
description "Bake the website"
doLast {
// No logger here, JBake is logging enough
project.exec {
workingDir rootDir
commandLine "${rootDir}/src/bin/bake-site.sh"
}
logger.quiet "\nFinished baking"
}
}
// Creates the favicon.ico file from the favicon.png file
task favicon {
group "JBake"
description "Create the favicon"
doLast {
logger.quiet "Creating favicon"
project.exec {
workingDir rootDir
commandLine "${rootDir}/src/bin/create-favicon.sh"
}
logger.quiet "Finished creating favicon"
}
}
// Solves problems with br and hr tags
task correct {
group "JBake"
description "Correct problems in HTML"
doLast {
logger.quiet "Correcting HTML problems\n"
logger.info "Correcting build directory"
correctHtmlDir new File("${buildDir}")
logger.quiet "\nFinished correcting"
}
}
// Walks through the baked site and minifies all files
task minify {
group "JBake"
description "Minifies the html and css files"
doLast {
logger.quiet "Minifying jpeg/jpg\n"
minifyJpegDir "${buildDir}/images", rootDir // All jpg/jpeg files
logger.quiet "\nMinifying png\n"
minifyPngDir "${buildDir}/images", rootDir // All png files
logger.quiet "\nMinifying css\n"
minifyCssDir "${buildDir}/css" , rootDir // css directory with own css, not the bootstrap code
logger.quiet "\nMinifying html\n"
minifyHtmlDir "${buildDir}", rootDir // root directory
logger.quiet "\nFinished minifying"
}
}
// Cleans up files in the build directory that we don't need (like the un-minified files)
task postBuild {
group "JBake"
description "Removes unwanted files"
doLast {
logger.quiet "Performing post-build cleanup\n"
def cssDir = new File("${buildDir}/css")
cssDir.eachFile {file ->
if (! file.name.endsWith(".min.css")) {
logger.quiet "Deleting ${file.absolutePath}"
file.delete()
}
}
def imagesDir = new File("${buildDir}/images")
imagesDir.eachFile {file ->
if (file.name.endsWith(".jpg") && file.name.startsWith("full_")) {
logger.quiet "Deleting ${file.absolutePath}"
file.delete()
}
}
imagesDir.eachFile {file ->
if (file.name.endsWith("-fs8.png")) {
logger.quiet "Deleting ${file.absolutePath}"
file.delete()
}
}
imagesDir.eachFile {file ->
if (file.name.startsWith("favicon")) {
logger.quiet "Deleting ${file.absolutePath}"
file.delete()
}
}
def bootstrapCssDir = new File("${buildDir}/vendor/bootstrap/css")
bootstrapCssDir.eachFile {file ->
if (! file.name.endsWith(".min.css")) {
logger.quiet "Deleting ${file.absolutePath}"
file.delete()
}
}
def bootstrapJsDir = new File("${buildDir}/vendor/bootstrap/js")
bootstrapJsDir.eachFile {file ->
if (! file.name.endsWith(".min.js")) {
logger.quiet "Deleting ${file.absolutePath}"
file.delete()
}
}
def jqueryJsDir = new File("${buildDir}/vendor/jquery")
jqueryJsDir.eachFile {file ->
if (! file.name.endsWith(".min.js")) {
logger.quiet "Deleting ${file.absolutePath}"
file.delete()
}
}
logger.quiet "\nFinished post-build cleanup\n"
}
}
/*
*
* Internal Jetty server
*
*/
// Stops processing for 2 seconds in order to have another task finish
// You can specify another number of seconds using -Pseconds=x on the command line
task sleep {
group "JBake"
description "Holds gradle a couple of seconds"
doLast {
def seconds = 2
if (project.properties.find {it.key == "seconds"}) {
seconds = project.properties.get("seconds")
}
logger.quiet "Sleeping ${seconds} seconds"
project.exec {
workingDir rootDir
commandLine "sleep", "${seconds}"
}
logger.quiet "Finished sleeping"
}
}
// Start of the internal Jetty server
task start {
group "JBake"
description "Starts the Jetty server with a baked site"
doLast {
project.exec {
workingDir rootDir
commandLine "${rootDir}/src/bin/start-server.sh"
}
}
}
// Stop of the internal Jetty server
task stop {
group "JBake"
description "Stops the Jetty server"
doLast {
project.exec {
workingDir rootDir
commandLine "${rootDir}/src/bin/stop-server.sh"
}
}
}
// Grouping task to stop and start the server
task restart() {
group "JBake"
description "Restarts the Jetty server"
}
restart.dependsOn stop
restart.dependsOn sleep
restart.dependsOn start
sleep.mustRunAfter stop
start.mustRunAfter sleep
/*
*
* The complete build
*
*/
// Grouping task to create the site from scratch
task site {
group "JBake"
description "Runs the complete sequence to build the site"
}
site.dependsOn stop
site.dependsOn clean
site.dependsOn bake
site.dependsOn correct
site.dependsOn minify
site.dependsOn favicon
site.dependsOn postBuild
clean.mustRunAfter stop
bake.mustRunAfter clean
correct.mustRunAfter bake
favicon.mustRunAfter correct
minify.mustRunAfter favicon
postBuild.mustRunAfter minify
/*
*
* Functions used in the tasks
*
*/
// Minify all html files in a certain directory
def minifyHtmlDir(String dirName, File workdir) {
def dir = new File(dirName)
if (dir.exists()) {
logger.quiet "Minifying ${dir.absolutePath}"
def files = []
dir.eachFile {file ->
if (file.name.endsWith(".html")) {
def info = [
path: file.absolutePath,
size: (int)file.size()
]
files.add(info)
}
}
project.exec {
workingDir workdir
commandLine "html-minifier", "--collapse-whitespace", "--remove-comments", "--keep-closing-slash",
"--input-dir", "${dirName}", "--output-dir", "${dirName}", "--file-ext", "html"
}
files.each {file ->
def current = new File(file.path)
logger.quiet "${file.path} original size ${file.size}, new size: ${current.size()}"
}
}
}
// Minify all css files in a certain directory
def minifyCssDir(String dirName, File workdir) {
def dir = new File(dirName)
if (dir.exists()) {
dir.eachFile { file ->
logger.quiet "Minifying ${file.absolutePath}"
project.exec {
workingDir workdir
commandLine "uglifycss", "--ugly-comments", "--output", "${file.parentFile}/${file.name.substring(0, file.name.length() - 4)}.min.css", "${file.absolutePath}"
}
}
}
}
// Minify all jpeg files in a certain directory
def minifyJpegDir(String dirName, File workdir) {
def dir = new File(dirName)
if (dir.exists()) {
dir.eachFile { file ->
if (file.name.endsWith(".jpeg") || file.name.endsWith(".jpg")) {
// No logger, command logs enough
project.exec {
workingDir workdir
commandLine "jpegoptim", "${file.absolutePath}"
}
}
}
}
}
// Minify all png files in a certain directory
def minifyPngDir(String dirName, File workdir) {
def dir = new File(dirName)
if (dir.exists()) {
dir.eachFile { file ->
if (file.name.endsWith(".png")) {
logger.quiet "Minifying ${file.absolutePath}"
project.exec {
workingDir workdir
commandLine "pngquant", "-f", "${file.absolutePath}", "${file.absolutePath}"
}
}
}
}
}
// Solve XHTML errors in all files in a directory
def correctHtmlDir(File directory) {
directory.eachFile {file ->
if (file.name.endsWith(".html")) {
logger.quiet "Correcting ${file.absolutePath}"
def text = file.text
// Jbake renders line breaks in markdown with <br> but does not close it
text = text.replaceAll("<br>", "<br/>")
text = text.replaceAll("<hr>", "<hr/>")
file.delete()
file << text
}
}
}