Skip to content

Commit

Permalink
Add option to build drafts
Browse files Browse the repository at this point in the history
By default, no drafts will be built. Using the Gradle command line option --build-drafts will include drafts in the built site.
  • Loading branch information
erichaagdev committed Mar 31, 2024
1 parent 5716e3b commit 76bef95
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions blog/posts/work-avoidance-with-gradle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ summary: In this article I explore the concept of work avoidance, a way to impro
categories: [ Gradle ]
tags: [ build-optimization ]
date: 2023-01-15T11:35:02-06:00
draft: true
---

<!--more-->
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/dev.erichaag.hugo-site.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ val hugoThemeResolvable = configurations.resolvable("${hugoThemeDeclarable.name}

val buildHugoSite by tasks.registering(HugoBuild::class) {
hugoExecutable = hugoExecutableResolvable
buildDrafts.convention(false)
publicDirectory = layout.buildDirectory.dir("hugo/public")
sourceDirectory.fileProvider(processHugo.map { it.destinationDir })
}
Expand All @@ -58,6 +59,7 @@ val printHugoVersion by tasks.registering(HugoVersion::class) {

val serveHugoSite by tasks.registering(HugoServe::class) {
hugoExecutable = hugoExecutableResolvable
buildDrafts.convention(false)
sourceDirectory.fileProvider(processHugo.map { it.destinationDir })
}

Expand Down
16 changes: 15 additions & 1 deletion buildSrc/src/main/kotlin/dev/erichaag/hugo/HugoBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ package dev.erichaag.hugo

import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.tasks.*
import javax.inject.Inject
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.IgnoreEmptyDirectories
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.process.ExecOperations

@CacheableTask
Expand All @@ -17,6 +26,10 @@ abstract class HugoBuild @Inject constructor(
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val sourceDirectory: DirectoryProperty

@get:Input
@get:Option(option = "build-drafts", description = "Includes content marked as draft.")
abstract val buildDrafts: Property<Boolean>

@get:OutputDirectory
abstract val publicDirectory: DirectoryProperty

Expand All @@ -30,6 +43,7 @@ abstract class HugoBuild @Inject constructor(
args("--destination", publicDirectory.get().asFile.path)
args("--noBuildLock")
args("--minify")
if (buildDrafts.get()) args("--buildDrafts")
}
}
}
8 changes: 8 additions & 0 deletions buildSrc/src/main/kotlin/dev/erichaag/hugo/HugoServe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package dev.erichaag.hugo

import javax.inject.Inject
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.UntrackedTask
import org.gradle.api.tasks.options.Option
import org.gradle.process.ExecOperations

@UntrackedTask(because = "Not worth tracking")
Expand All @@ -15,11 +18,16 @@ abstract class HugoServe @Inject constructor(
@get:InputDirectory
abstract val sourceDirectory: DirectoryProperty

@get:Input
@get:Option(option = "build-drafts", description = "Includes content marked as draft.")
abstract val buildDrafts: Property<Boolean>

@TaskAction
fun action() = hugoExec {
args("serve")
args("--source", sourceDirectory.get().asFile.path)
args("--disableFastRender")
args("--noBuildLock")
if (buildDrafts.get()) args("--buildDrafts")
}
}

0 comments on commit 76bef95

Please sign in to comment.