Skip to content

Commit

Permalink
Allow a custom templateUrl to be specified for bakeInit
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Oct 7, 2018
1 parent 6841e1c commit 1a22cfc
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 19 deletions.
11 changes: 10 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ gradle bakeInit
The default template is set to `groovy`. You may change this setting by updating the `template` property of the `jbake`
configuration. Accepted values are `freemarker`, `groovy`, `groovy-mte`, thymeleaf`, `jade`.

Alternatively you may define a value for `templateUrl` that points to a custom template, for example

[source,groovy]
----
bakeInit {
templateUrl = 'http://server.acme.com/path/to/template.zip'
}
----

=== Previewing

You can preview your content by running the following command:
Expand All @@ -63,7 +72,7 @@ port by adding this to your build file.
[source,groovy]
----
bakePreview {
port = '8080'
port = '8090'
}
----

Expand Down
1 change: 1 addition & 0 deletions src/main/groovy/org/jbake/gradle/JBakeExtension.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class JBakeExtension {
String srcDirName = 'src/jbake'
String destDirName = 'jbake'
String template = 'groovy'
String templateUrl
boolean clearCache = false
Map<String, Object> configuration = [:]
boolean includeDefaultRepositories = true
Expand Down
4 changes: 3 additions & 1 deletion src/main/groovy/org/jbake/gradle/JBakeInitProxy.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ interface JBakeInitProxy {

void setConfig(Object config)

void init(String type, File outputDir)
void initFromTemplateUrl(String url, File outputDir)

void initFromTemplate(String type, File outputDir)
}
15 changes: 12 additions & 3 deletions src/main/groovy/org/jbake/gradle/JBakeInitTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.jbake.gradle


import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.Configuration
import org.gradle.api.tasks.Input
Expand All @@ -27,7 +26,8 @@ import org.jbake.gradle.impl.JBakeInitProxyImpl
import java.lang.reflect.Constructor

class JBakeInitTask extends DefaultTask {
@Input String template
@Input @Optional String template
@Input @Optional String templateUrl
@OutputDirectory File outputDir
@Input Map<String, Object> configuration = [:]

Expand All @@ -44,10 +44,19 @@ class JBakeInitTask extends DefaultTask {

@TaskAction
void init() {
String _template = getTemplate()
String _templateUrl = getTemplateUrl()

if(!_template && _templateUrl) {
throw new IllegalStateException("You must define a value for either 'template' or 'templateUrl")
}

createJBakeInit()
init.prepare()
mergeConfiguration()
init.init(getTemplate(), getOutputDir())

_templateUrl ? init.initFromTemplateUrl(_templateUrl, getOutputDir()) :
init.initFromTemplate(_template, getOutputDir())
}

private JBakeInitProxy createJBakeInit() {
Expand Down
1 change: 1 addition & 0 deletions src/main/groovy/org/jbake/gradle/JBakePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class JBakePlugin implements Plugin<Project> {
project.task('bakeInit', type: JBakeInitTask, description: 'Setup a jbake project') {
classpath = configuration
conventionMapping.template = { project.jbake.template }
conventionMapping.templateUrl = { project.jbake.templateUrl }
conventionMapping.outputDir = { project.file("${project.jbake.srcDirName}") }
conventionMapping.configuration = { project.jbake.configuration }
}
Expand Down
35 changes: 22 additions & 13 deletions src/main/groovy/org/jbake/gradle/impl/Init.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ class Init {
* Performs checks on output folder before extracting template file
*
* @param outputFolder Target directory for extracting template file
* @param templateLocationFolder Source location for template file
* @param templateType Type of the template to be used
* @throws Exception if required folder structure can't be achieved without content overwriting
*/
void run(File outputFolder, String templateType) throws Exception {
void run(File outputFolder, String templateType) {
String templateFileName = "jbake-example-project-$templateType"
URL url = "https://github.com/jbake-org/${templateFileName}/archive/master.zip".toURL()
run(outputFolder, url)
}

/**
* Performs checks on output folder before extracting template file
*
* @param outputFolder Target directory for extracting template file
* @param templateUrl URL of the template to be used
*/
void run(File outputFolder, URL url) {
if (!outputFolder.canWrite()) {
throw new IllegalStateException("Output folder is not writeable!")
}
Expand Down Expand Up @@ -46,17 +56,16 @@ class Init {
outputFolder.getAbsolutePath()))
}

String templateFileName = "jbake-example-project-$templateType"
URL url = "https://github.com/jbake-org/${templateFileName}/archive/master.zip".toURL()
File tmpJbake = File.createTempDir()
File templateFile = new File(tmpJbake, templateFileName)
templateFile.withOutputStream { it.write(url.bytes) }
if (!templateFile.exists()) {
throw new IllegalStateException("Cannot find example project file: " + templateFile.getPath())
File tmpZipFile = File.createTempFile('jbake-template-', '')
tmpZipFile.withOutputStream { it.write(url.bytes) }
File tmpOutput = File.createTempDir('jbake-extracted-', '')
extract(new FileInputStream(tmpZipFile), tmpOutput)

if (tmpOutput.listFiles().size()) {
tmpOutput.listFiles()[0].renameTo(outputFolder)
} else {
tmpOutput.renameTo(outputFolder)
}
File tmpOutput = File.createTempDir('extracted-', '-' + UUID.randomUUID().toString())
extract(new FileInputStream(templateFile), tmpOutput)
tmpOutput.listFiles({it.name.startsWith(templateFileName)} as FileFilter)*.renameTo(outputFolder)
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/main/groovy/org/jbake/gradle/impl/JBakeInitProxyImpl.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class JBakeInitProxyImpl implements JBakeInitProxy {
}

@Override
void init(String type, File outputDir) {
void initFromTemplate(String type, File outputDir) {
try {
init.run(outputDir, type)
println("Base folder structure successfully created.")
Expand All @@ -42,4 +42,15 @@ class JBakeInitProxyImpl implements JBakeInitProxy {
throw new IllegalStateException(msg, e)
}
}

@Override
void initFromTemplateUrl(String templateUrl, File outputDir) {
try {
init.run(outputDir, templateUrl.toURL())
println("Base folder structure successfully created.")
} catch (final Exception e) {
final String msg = "Failed to initialise structure: " + e.getMessage()
throw new IllegalStateException(msg, e)
}
}
}

0 comments on commit 1a22cfc

Please sign in to comment.