Added simple watch mode (no-op).
This commit is contained in:
parent
106952874b
commit
101dd0b8da
@ -20,6 +20,8 @@ import org.apache.logging.log4j.Logger
|
|||||||
import org.apache.logging.log4j.core.LoggerContext
|
import org.apache.logging.log4j.core.LoggerContext
|
||||||
import picocli.CommandLine
|
import picocli.CommandLine
|
||||||
|
|
||||||
|
import java.nio.file.FileSystems
|
||||||
|
import java.nio.file.StandardWatchEventKinds
|
||||||
import java.util.concurrent.Callable
|
import java.util.concurrent.Callable
|
||||||
|
|
||||||
@CommandLine.Command(
|
@CommandLine.Command(
|
||||||
@ -32,6 +34,10 @@ class StaticSiteGeneratorCli implements Callable<Integer> {
|
|||||||
|
|
||||||
private static final Logger logger = LogManager.getLogger(StaticSiteGeneratorCli)
|
private static final Logger logger = LogManager.getLogger(StaticSiteGeneratorCli)
|
||||||
|
|
||||||
|
static void main(String[] args) {
|
||||||
|
new CommandLine(StaticSiteGeneratorCli)
|
||||||
|
}
|
||||||
|
|
||||||
static class LogLevel {
|
static class LogLevel {
|
||||||
|
|
||||||
@CommandLine.Option(names = ['--info'], description = 'Log at INFO level.')
|
@CommandLine.Option(names = ['--info'], description = 'Log at INFO level.')
|
||||||
@ -45,13 +51,12 @@ class StaticSiteGeneratorCli implements Callable<Integer> {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void main(String[] args) {
|
|
||||||
System.exit(new CommandLine(StaticSiteGeneratorCli).execute(args))
|
|
||||||
}
|
|
||||||
|
|
||||||
@CommandLine.ArgGroup(exclusive = true, heading = 'Log Level')
|
@CommandLine.ArgGroup(exclusive = true, heading = 'Log Level')
|
||||||
LogLevel logLevel
|
LogLevel logLevel
|
||||||
|
|
||||||
|
@CommandLine.Option(names = ['-w', '--watch'], description = 'Run in watch mode.')
|
||||||
|
boolean watch
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer call() {
|
Integer call() {
|
||||||
logger.traceEntry()
|
logger.traceEntry()
|
||||||
@ -110,6 +115,16 @@ class StaticSiteGeneratorCli implements Callable<Integer> {
|
|||||||
// Get ssg object
|
// Get ssg object
|
||||||
def ssg = new SimpleStaticSiteGenerator()
|
def ssg = new SimpleStaticSiteGenerator()
|
||||||
|
|
||||||
|
if (this.watch) {
|
||||||
|
watch(builds, ssg)
|
||||||
|
} else {
|
||||||
|
generate(builds, ssg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer generate(Collection<Build> builds, StaticSiteGenerator ssg) {
|
||||||
|
logger.traceEntry('builds: {}, ssg: {}', builds, ssg)
|
||||||
|
|
||||||
def hadDiagnostics = false
|
def hadDiagnostics = false
|
||||||
// Do each build
|
// Do each build
|
||||||
builds.each {
|
builds.each {
|
||||||
@ -131,4 +146,55 @@ class StaticSiteGeneratorCli implements Callable<Integer> {
|
|||||||
logger.traceExit(hadDiagnostics ? 1 : 0)
|
logger.traceExit(hadDiagnostics ? 1 : 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Integer watch(Collection<Build> builds, StaticSiteGenerator ssg) {
|
||||||
|
logger.traceEntry('builds: {}, ssg: {}', builds, ssg)
|
||||||
|
|
||||||
|
Collection<WatchableProvider> watchableProviders = []
|
||||||
|
|
||||||
|
builds.each {
|
||||||
|
it.config.textProviders.each {
|
||||||
|
if (it instanceof WatchableProvider) {
|
||||||
|
watchableProviders << it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
it.config.templatesProviders.each {
|
||||||
|
if (it instanceof WatchableProvider) {
|
||||||
|
watchableProviders << it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
it.config.partsProviders.each {
|
||||||
|
if (it instanceof WatchableProvider) {
|
||||||
|
watchableProviders << it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
it.config.specialPagesProviders.each {
|
||||||
|
if (it instanceof WatchableProvider) {
|
||||||
|
watchableProviders << it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def watchService = FileSystems.getDefault().newWatchService()
|
||||||
|
|
||||||
|
watchableProviders.each {
|
||||||
|
it.watchableDir.toPath().register(
|
||||||
|
watchService,
|
||||||
|
StandardWatchEventKinds.ENTRY_CREATE,
|
||||||
|
StandardWatchEventKinds.ENTRY_DELETE,
|
||||||
|
StandardWatchEventKinds.ENTRY_MODIFY
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
//noinspection GroovyInfiniteLoopStatement
|
||||||
|
while (true) {
|
||||||
|
def watchKey = watchService.take()
|
||||||
|
watchKey.pollEvents().each {
|
||||||
|
logger.debug('watchEvent: {}', it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//noinspection GroovyUnreachableStatement
|
||||||
|
logger.traceExit(0)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.jessebrault.ssg
|
||||||
|
|
||||||
|
trait WatchableProvider {
|
||||||
|
File watchableDir
|
||||||
|
}
|
@ -2,23 +2,28 @@ package com.jessebrault.ssg.text
|
|||||||
|
|
||||||
import com.jessebrault.ssg.util.FileNameHandler
|
import com.jessebrault.ssg.util.FileNameHandler
|
||||||
import com.jessebrault.ssg.util.RelativePathHandler
|
import com.jessebrault.ssg.util.RelativePathHandler
|
||||||
|
import com.jessebrault.ssg.WatchableProvider
|
||||||
import groovy.io.FileType
|
import groovy.io.FileType
|
||||||
import groovy.transform.EqualsAndHashCode
|
import groovy.transform.EqualsAndHashCode
|
||||||
import groovy.transform.NullCheck
|
import groovy.transform.NullCheck
|
||||||
import groovy.transform.TupleConstructor
|
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
@TupleConstructor(includeFields = true, defaults = false)
|
|
||||||
@NullCheck
|
@NullCheck
|
||||||
@EqualsAndHashCode(includeFields = true)
|
@EqualsAndHashCode(includeFields = true)
|
||||||
class TextFileTextsProvider implements TextsProvider {
|
class TextFileTextsProvider implements TextsProvider, WatchableProvider {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(TextFileTextsProvider)
|
private static final Logger logger = LoggerFactory.getLogger(TextFileTextsProvider)
|
||||||
|
|
||||||
private final Collection<TextType> textTypes
|
private final Collection<TextType> textTypes
|
||||||
private final File textsDir
|
private final File textsDir
|
||||||
|
|
||||||
|
TextFileTextsProvider(Collection<TextType> textTypes, File textsDir) {
|
||||||
|
this.textTypes = textTypes
|
||||||
|
this.textsDir = textsDir
|
||||||
|
this.watchableDir = this.textsDir
|
||||||
|
}
|
||||||
|
|
||||||
private TextType getTextType(File file) {
|
private TextType getTextType(File file) {
|
||||||
this.textTypes.find {
|
this.textTypes.find {
|
||||||
it.ids.contains(new FileNameHandler(file).getExtension())
|
it.ids.contains(new FileNameHandler(file).getExtension())
|
||||||
|
Loading…
Reference in New Issue
Block a user