-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.gradle
More file actions
162 lines (132 loc) · 4.91 KB
/
build.gradle
File metadata and controls
162 lines (132 loc) · 4.91 KB
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
157
158
159
160
161
162
import com.typesafe.config.*;
apply plugin: 'base'
apply plugin: 'js'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath('com.typesafe:config:1.2.0')
classpath('com.eriwen:gradle-js-plugin:1.9.0')
}
}
version = buildVersion
group = "com.github.dev9com.jsonconfig"
description = "dev9 configuration compiler"
ext {
artifactId = "config-json"
environments = ['default', 'dev', 'qa', 'stage', 'prod']
bundles = fileTree(dir: 'src/main/bundles', include: '*.conf').files*.name*.replace('.conf', '')
workOutputFile = file("${buildDir}/work/work.conf")
resolvePartial = ConfigResolveOptions.defaults().setAllowUnresolved(true)
}
defaultTasks 'assemble'
tasks.assemble.dependsOn 'bundle'
task bundle (dependsOn: 'generate', type: Tar) {
description = "Creates the final tar.gz archive of the generated config files"
compression = Compression.GZIP
into "${artifactId}-${version}"
from("${buildDir}") {
exclude "distributions"
exclude "work"
}
}
task generate {
environments.each {
dependsOn "gen_${it}"
}
}
environments.each { env ->
// Generate a master task to generate all files for one env
task "gen_${env}"(dependsOn: ["gen_${env}_full", "gen_${env}_bundles"]) {
}
// Generate the full config for this environment
task "gen_${env}_full"(dependsOn: 'mergeConfigFiles') {
ext.outputFile = file("${buildDir}/${env}/config.json")
inputs.file(workOutputFile)
outputs.file(outputFile)
doLast {
def config = ConfigFactory.parseFile(workOutputFile)
def envConfig = config.hasPath(env) ? config.getConfig(env) : ConfigFactory.empty()
envConfig = envConfig.withFallback(config.getConfig('default')).resolve(resolvePartial).resolveWith(config, resolvePartial)
outputFile.write(pretty(envConfig))
}
}
// This bundles task adds dependencies on all the individual bundle tasks
task "gen_${env}_bundles" {
bundles.each {
dependsOn "gen_${env}_bundle_${it}"
}
}
bundles.each { bundle ->
task "gen_${env}_bundle_${bundle}"(dependsOn: "gen_${env}_full") {
ext.bundleFile = file("src/main/bundles/${bundle}.conf")
ext.inputFile = file("${buildDir}/${env}/config.json")
ext.outputFile = file("${buildDir}/${env}/${bundle}.json")
inputs.files(bundleFile, inputFile)
outputs.file(outputFile)
doLast {
def fullConfig = ConfigFactory.parseFile(inputFile)
def bundleConfig = ConfigFactory.parseFile(bundleFile)
def bundleOutput = ConfigFactory.empty();
if ( bundleConfig.hasPath('includes') )
{
bundleConfig.getStringList('includes').each { include ->
bundleOutput = bundleOutput.withFallback(fullConfig.getObject(include).atPath(include))
}
}
else
{
bundleOutput = bundleOutput.withFallback(fullConfig)
}
if ( bundleConfig.hasPath('remove') )
{
bundleConfig.getStringList('remove').each { remove ->
if ( bundleOutput.hasPath(remove) )
bundleOutput = bundleOutput.withoutPath(remove)
}
}
outputFile.write(pretty(bundleOutput.resolve(resolvePartial)))
}
}
}
}
task createWorkingDirectories {
environments.each {
outputs.file(file("${buildDir}/${it}"))
}
doLast {
environments.each {
file("${buildDir}/${it}").mkdirs()
}
}
}
task mergeConfigFiles(dependsOn: 'createWorkingDirectories', type: com.eriwen.gradle.js.tasks.CombineJsTask) {
source = fileTree(dir: 'src/main/conf', include: '*.conf')
dest = workOutputFile
doLast {
workOutputFile.append("\ndefault {\n configVersion: ${version}\n}\n\n")
}
}
jshint {
// Depends on our generation task
tasks.jshint.dependsOn 'generate'
// This plugin has a weird naming issue (http://forums.gradle.org/gradle/topics/gradle_js_plugin)
// Prefixing config with tasks.jshint seems to fix it.
tasks.jshint.source = fileTree(dir: buildDir, include: "**/*.json")
tasks.jshint.dest = file("${buildDir}/jshint.out")
tasks.jshint.outputToStdOut = true
tasks.jshint.ignoreExitCode = false
jshint.options = [expr: "true", unused: "true"]
}
def pretty(Config config) {
config.root().render(ConfigRenderOptions.concise().setFormatted(true))
}
def prettyPrint(Config config) {
println pretty(config)
}
// This task gets the gradlew scripts and the jar file for us.
// Use this to upgrade gradle versions.
task createWrapper(type: Wrapper) {
gradleVersion = '1.10'
}