Skip to content

Commit f7d3f3e

Browse files
committed
Add a Groovy script to generate a book
1 parent 9d12bb7 commit f7d3f3e

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

book.groovy

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
@Grapes(
2+
@Grab(group='org.yaml', module='snakeyaml', version='1.4')
3+
)
4+
import groovy.io.FileType
5+
import org.yaml.snakeyaml.Yaml
6+
7+
Properties gradleProperties = new Properties()
8+
File propertiesFile = new File('gradle.properties')
9+
propertiesFile.withInputStream {
10+
gradleProperties.load(it)
11+
}
12+
13+
def rootRespositoryName = 'grails-guides'
14+
def buildFolder = 'build'
15+
def bookOutputFolder = "${buildFolder}/book"
16+
def bookOutputImagesFolder = "${bookOutputFolder}/images"
17+
def bookOutputSourceFolder = "${bookOutputFolder}/src"
18+
19+
if ( new File(bookOutputFolder).exists() ) {
20+
new File(bookOutputFolder).deleteDir()
21+
}
22+
23+
[buildFolder, bookOutputFolder, bookOutputImagesFolder, bookOutputSourceFolder].each { String folderName ->
24+
def folder = new File(folderName)
25+
if( !folder.exists() ) {
26+
folder.mkdirs()
27+
}
28+
}
29+
def frontCoverImageName = 'front-cover.png'
30+
31+
def sout = new StringBuilder()
32+
serr = new StringBuilder()
33+
Process process = "cp ${frontCoverImageName} ${bookOutputImagesFolder}/${frontCoverImageName}".execute()
34+
process.consumeProcessOutput(sout, serr)
35+
process.waitForOrKill(1000)
36+
37+
new AntBuilder().copy(todir: bookOutputSourceFolder) {
38+
fileset(dir: 'complete', includes: "**")
39+
}
40+
41+
new AntBuilder().copy(todir: bookOutputFolder) {
42+
fileset(dir: 'build/resources/grails-guides-master/src/main/docs', includes: "**")
43+
}
44+
45+
new AntBuilder().copy(todir: bookOutputFolder) {
46+
fileset(dir: 'src/main/docs/guide', includes: "**")
47+
}
48+
49+
def adocList = []
50+
51+
def dir = new File(bookOutputFolder)
52+
dir.eachFileRecurse (FileType.FILES) { file ->
53+
adocList << file
54+
}
55+
56+
adocList.each {
57+
if ( it.path.endsWith('.adoc') ) {
58+
new AntBuilder().replace(file: it.path, token: 'include::{commondir}/', value: 'include::')
59+
}
60+
}
61+
62+
63+
def bookName = "${gradleProperties.githubSlug.replaceAll("${rootRespositoryName}/" as String, '')}.adoc"
64+
65+
66+
def bookFile = new File("${bookOutputFolder}/${bookName}")
67+
bookFile.createNewFile()
68+
bookFile.text = ''
69+
bookFile << "= ${gradleProperties.title}\n"
70+
bookFile << "${gradleProperties.authors}\n"
71+
bookFile << 'v1.0, 2017-01-13\n'
72+
bookFile << ':doctype: book\n'
73+
bookFile << ":producer: ${gradleProperties.bookProducer}\n"
74+
bookFile << ":keywords: ${gradleProperties.bookKeywords}\n"
75+
bookFile << ":copyright: ${gradleProperties.copyright}\n"
76+
bookFile << ":imagesdir: images\n"
77+
bookFile << ":sourcedir: src\n"
78+
bookFile << ":front-cover-image: image:front-cover.png[Front Cover,1050,1600]\n"
79+
bookFile << ":toc:\n"
80+
81+
Yaml yaml = new Yaml()
82+
def data = new File('src/main/docs/guide/toc.yml').text
83+
def map = (Map) yaml.load(data)
84+
85+
for ( String chapterName : map.keySet() ) {
86+
bookFile << "include::${chapterName}.adoc[]\n"
87+
def chapterFile = new File("${bookOutputFolder}/${chapterName}.adoc")
88+
def lines = chapterFile.readLines()
89+
lines = lines.plus(0, "[[${chapterName}.adoc]]\n= ${map[chapterName].title}\n")
90+
chapterFile.text = lines.join('\n')
91+
92+
for ( String subchapterName : map[chapterName].keySet().findAll { it != 'title' } ) {
93+
chapterFile.append("\n\ninclude::${subchapterName}.adoc[]")
94+
def subchapterFile = new File("${bookOutputFolder}/${subchapterName}.adoc")
95+
lines = subchapterFile.readLines()
96+
lines = lines.plus(0, "== ${map[chapterName][subchapterName]}\n")
97+
subchapterFile.text = lines.join('\n')
98+
}
99+
100+
}

front-cover.png

111 KB
Loading

gradle.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ subtitle=Learn how to create your first Grails app
33
authors=Zachary Klein
44
copyright=Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically. All guides are released with an ASLv2 license for the code, and Creative Commons license for the writing.
55
githubSlug=grails-guides/creating-your-first-grails-app
6-
githubBranch=master
6+
githubBranch=master
7+
bookProducer=OCI
8+
bookKeywords=Grails, GSP
9+
bookCopyright=OCI 2017. All Rights reserved

0 commit comments

Comments
 (0)