Updated Cli to include new options.
This commit is contained in:
parent
3e276232c3
commit
8fd08f51f1
@ -6,51 +6,63 @@ import com.jessebrault.ssg.buildscript.DefaultBuildScriptConfiguratorFactory
|
|||||||
import com.jessebrault.ssg.util.Diagnostic
|
import com.jessebrault.ssg.util.Diagnostic
|
||||||
import org.apache.logging.log4j.LogManager
|
import org.apache.logging.log4j.LogManager
|
||||||
import org.apache.logging.log4j.Logger
|
import org.apache.logging.log4j.Logger
|
||||||
|
import picocli.CommandLine
|
||||||
|
|
||||||
abstract class AbstractBuildCommand extends AbstractSubCommand {
|
abstract class AbstractBuildCommand extends AbstractSubCommand {
|
||||||
|
|
||||||
private static final Logger logger = LogManager.getLogger(AbstractBuildCommand)
|
private static final Logger logger = LogManager.getLogger(AbstractBuildCommand)
|
||||||
|
|
||||||
protected final Collection<Build> builds = []
|
protected Collection<Build> availableBuilds = []
|
||||||
|
|
||||||
AbstractBuildCommand() {
|
@CommandLine.Option(
|
||||||
// Run build script
|
names = ['-b', '--build'],
|
||||||
if (new File('ssgBuilds.groovy').exists()) {
|
description = 'The name of a build to execute.',
|
||||||
logger.info('found buildScript: ssgBuilds.groovy')
|
paramLabel = 'buildName'
|
||||||
|
)
|
||||||
|
protected Collection<String> requestedBuilds = ['default']
|
||||||
|
|
||||||
|
@CommandLine.Option(
|
||||||
|
names = ['-s', '--script', '--buildScript'],
|
||||||
|
description = 'The build script file to execute.',
|
||||||
|
paramLabel = 'buildScript'
|
||||||
|
)
|
||||||
|
void setBuildFile(File buildScriptFile) {
|
||||||
|
if (buildScriptFile == null) {
|
||||||
|
buildScriptFile = new File('ssgBuilds.groovy')
|
||||||
|
}
|
||||||
|
if (buildScriptFile.exists()) {
|
||||||
|
logger.info('found buildScriptFile: {}', buildScriptFile)
|
||||||
def configuratorFactory = new DefaultBuildScriptConfiguratorFactory()
|
def configuratorFactory = new DefaultBuildScriptConfiguratorFactory()
|
||||||
this.builds.addAll(BuildScriptUtil.runBuildScript('ssgBuilds.groovy') {
|
this.availableBuilds = BuildScriptUtil.runBuildScript(buildScriptFile.path) {
|
||||||
configuratorFactory.get().accept(it)
|
configuratorFactory.get().accept(it)
|
||||||
})
|
}
|
||||||
logger.debug('after running ssgBuilds.groovy, builds: {}', this.builds)
|
logger.debug('after running buildScriptFile {}, builds: {}', buildScriptFile, this.availableBuilds)
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException('ssgBuilds.groovy could not be found')
|
throw new IllegalArgumentException(
|
||||||
|
"buildScriptFile file ${ buildScriptFile } does not exist or could not be found."
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final Integer doBuild() {
|
protected final Integer doBuild(String requestedBuild) {
|
||||||
logger.traceEntry('builds: {}', this.builds)
|
logger.traceEntry('requestedBuild: {}', requestedBuild)
|
||||||
|
|
||||||
def buildTasksConverter = new SimpleBuildTasksConverter()
|
def buildTasksConverter = new SimpleBuildTasksConverter()
|
||||||
|
|
||||||
|
def build = this.availableBuilds.find { it.name == requestedBuild }
|
||||||
|
|
||||||
def hadDiagnostics = false
|
def hadDiagnostics = false
|
||||||
// Do each build
|
def tasksResult = buildTasksConverter.convert(build)
|
||||||
this.builds.each {
|
if (tasksResult.hasDiagnostics()) {
|
||||||
def tasksResult = buildTasksConverter.convert(it)
|
hadDiagnostics = true
|
||||||
if (tasksResult.hasDiagnostics()) {
|
tasksResult.diagnostics.each { logger.error(it.message) }
|
||||||
|
} else {
|
||||||
|
def tasks = tasksResult.get()
|
||||||
|
Collection<Diagnostic> taskDiagnostics = []
|
||||||
|
tasks.each { it.execute(tasks) }
|
||||||
|
if (!taskDiagnostics.isEmpty()) {
|
||||||
hadDiagnostics = true
|
hadDiagnostics = true
|
||||||
tasksResult.diagnostics.each {
|
taskDiagnostics.each { logger.error(it.message) }
|
||||||
logger.error(it.message)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
def tasks = tasksResult.get()
|
|
||||||
Collection<Diagnostic> taskDiagnostics = []
|
|
||||||
tasks.each { it.execute(tasks) }
|
|
||||||
if (!taskDiagnostics.isEmpty()) {
|
|
||||||
hadDiagnostics = true
|
|
||||||
taskDiagnostics.each {
|
|
||||||
logger.error(it.message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,15 @@ final class SsgBuild extends AbstractBuildCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer doSubCommand() {
|
Integer doSubCommand() {
|
||||||
this.doBuild()
|
logger.traceEntry()
|
||||||
|
def result = 0
|
||||||
|
this.requestedBuilds.each {
|
||||||
|
def buildResult = this.doBuild(it)
|
||||||
|
if (buildResult == 1) {
|
||||||
|
result = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.traceExit(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.jessebrault.ssg
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import picocli.CommandLine
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
|
||||||
|
final class StaticSiteGeneratorCliTests {
|
||||||
|
|
||||||
|
private static void cliSmokeScreen(String... args) {
|
||||||
|
assertEquals(0, new CommandLine(StaticSiteGeneratorCli).execute(args))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void helpSmokeScreen() {
|
||||||
|
cliSmokeScreen('--help')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void initHelpSmokeScreen() {
|
||||||
|
cliSmokeScreen('init', '--help')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void buildHelpSmokeScreen() {
|
||||||
|
cliSmokeScreen('build', '--help')
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user