-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.gradle
156 lines (141 loc) · 6.32 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import net.sourceforge.plantuml.ugraphic.sprite.SpriteGrayLevel
import net.sourceforge.plantuml.ugraphic.sprite.SpriteUtils
import org.apache.batik.transcoder.TranscoderInput
import org.apache.batik.transcoder.TranscoderOutput
import org.apache.batik.transcoder.image.PNGTranscoder
import org.apache.commons.text.CaseUtils
import javax.imageio.ImageIO
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.apache.xmlgraphics', name: 'batik-transcoder', version: '1.11'
classpath group: 'org.apache.xmlgraphics', name: 'batik-codec', version: '1.11'
classpath group: 'org.apache.commons', name: 'commons-text', version: '1.7'
classpath group: 'net.sourceforge.plantuml', name: 'plantuml', version: '1.2019.7'
}
}
plugins {
id "com.cosminpolifronie.gradle.plantuml" version "1.6.0"
}
Map<String, Integer> imageSizes = [
Small: 50,
'' : 100,
Large: 200,
]
File pngDir = new File(project.buildDir, 'png')
File spriteDir = file('dist')
static String svgPatternNameToSpriteBaseName(String svgPatternName) {
CaseUtils.toCamelCase(svgPatternName, true,
'-' as char,
'_' as char,
' ' as char).replaceAll('[-_ ]', '')
}
static Tuple2<String, String> svgNameToGroupAndPatternName(String svgFileName) {
def (unused, group, pattern) = (svgFileName =~ /([\w-]+?)__(.+)\.svg/)[0]
new Tuple2<>(group, pattern)
}
task convertSvgToPng {
description "Converts EIP SVG shapes into PNG files of different sizes"
doLast {
pngDir.mkdirs()
file("enterprise-integration-pattern-shapes-for-gliffy/eip-svg-shapes").
eachFileRecurse(groovy.io.FileType.FILES) { svgFile ->
if (!svgFile.name.endsWith('svg')) {
return
}
imageSizes.each { sizeName, sizeWidth ->
svgFile.withInputStream { svgStream ->
TranscoderInput transcoderInput = new TranscoderInput(svgStream)
String patternName = svgNameToGroupAndPatternName(svgFile.name).second
String spriteName = svgPatternNameToSpriteBaseName(patternName) + sizeName
String pngName = spriteName + '.png'
new File(pngDir, pngName).withOutputStream { pngStream ->
TranscoderOutput transcoderOutput = new TranscoderOutput(pngStream)
PNGTranscoder transcoder = new PNGTranscoder().tap {
addTranscodingHint(PNGTranscoder.KEY_WIDTH, sizeWidth as Float)
addTranscodingHint(PNGTranscoder.KEY_BACKGROUND_COLOR, java.awt.Color.WHITE)
}
transcoder.transcode(transcoderInput, transcoderOutput)
}
}
}
}
}
}
task convertPngToSprite {
description "Converts PNG files to PlantUML sprites"
doLast {
spriteDir.mkdirs()
String pngExtension = '.png'
new File(spriteDir, project.name + '.puml').withPrintWriter { output ->
pngDir.eachFile(groovy.io.FileType.FILES) { pngFile ->
if (!pngFile.name.endsWith(pngExtension)) {
return
}
pngFile.withInputStream { input ->
String spriteName = 'EIP_' + pngFile.name[0..<-pngExtension.size()]
output.println(SpriteUtils.encodeCompressed(
ImageIO.read(input), spriteName, SpriteGrayLevel.GRAY_16))
}
}
}
}
dependsOn convertSvgToPng
}
task generateExample {
description "Generates example diagram with every sprite"
doLast {
Map<String, List<String>> patternCatalog = [:]
project.file("enterprise-integration-pattern-shapes-for-gliffy/eip-svg-shapes").
eachFileRecurse(groovy.io.FileType.FILES) { svgFile ->
if (!svgFile.name.endsWith('svg')) {
return
}
Tuple2<String, String> groupAndPattern = svgNameToGroupAndPatternName(svgFile.name)
patternCatalog[groupAndPattern.first] = patternCatalog[groupAndPattern.first] ?: []
patternCatalog[groupAndPattern.first] << groupAndPattern.second
}
new File(project.buildDir, "all-sprites.puml").withPrintWriter { output ->
output.println """@startuml
!includeurl https://raw.githubusercontent.com/nikolay-martynov/eip-for-plantuml/master/dist/eip-for-plantuml.puml
skinparam defaultTextAlignment center
"""
patternCatalog.keySet().sort().eachWithIndex { group, groupIndex ->
output.println "package \"${group.replaceAll('-', ' ')}\" as g$groupIndex {"
patternCatalog[group].sort().eachWithIndex { patternSvgName, patternIndex ->
String spriteBaseName = svgPatternNameToSpriteBaseName(patternSvgName)
output.println("\trectangle \"<\$EIP_$spriteBaseName>\\n\$EIP_$spriteBaseName\" as g${groupIndex}p$patternIndex")
if (patternIndex > 0) {
output.println "\tg${groupIndex}p${patternIndex - 1} -[hidden]right- g${groupIndex}p$patternIndex"
}
}
output.println "}"
if (groupIndex > 0) {
output.println "g${groupIndex - 1}p0 -[hidden]down- g${groupIndex}p0"
}
}
output.println "@enduml"
}
}
}
plantUml {
render input: 'diagrams/example.puml', output: 'generated-examples/example.svg', withMetadata: false
render input: 'diagrams/sizes.puml', output: 'generated-examples/sizes.svg', withMetadata: false
render input: 'build/all-sprites.puml', output: 'generated-examples/all-sprites.svg', withMetadata: false
}
project.tasks.getByName("plantUml") {
dependsOn generateExample
}
task clean {
description "Cleans output directory"
doLast {
project.buildDir.deleteDir()
}
}
task build {
description "Builds everything"
dependsOn convertPngToSprite, project.tasks.getByName("plantUml")
}
defaultTasks(build.name)