-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
185 lines (167 loc) · 6.09 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def scriptProperties = [
name: "my-project",
version: "0.0.1",
author: "Your Name",
description: "This is what your script does!",
// target is what Tampermonkey uses to decide which pages to run on
target: "/regex for the targeted site/",
externalDependencies: ["https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"]
]
def cssProperties = [
// colors
'$clouds': '#ecf0f1',
'$darkerClouds': '#DFE3E4',
'$slightlyDarkerClouds': '#A0A4A5',
'$disabled': 'grey',
'$midnight': '#2c3e50',
'$darkerMidnight': '#1F3143',
'$darkestMidnight': '#152739',
'$lightBlue': '#2683f0',
// status colors
'$starting': 'rgba(0, 255, 0, .5)',
'$stopping': 'rgba(255, 0, 0, .5)',
// other
'$pageHeaderHeight': '50px'
]
def getBinFolder = {
def binFolder = new File("bin")
if(!binFolder.exists()) {
binFolder.mkdirs()
}
binFolder
}
def getPlaceholderMatches = { line -> line =~ /\$\{.*\}/ }
def getPlaceholderName = { placeholder -> placeholder.trim()[2..-2]}
def fillProperties = { line, dependencyModifier ->
getPlaceholderMatches(line).each {
def propertyName = getPlaceholderName(it)
def property = scriptProperties[propertyName]
if(propertyName == "content") {
property = file("bin/combined.js").text
} else if(propertyName == "versionForJs") {
property = "var SCRIPT_VERSION = \"${scriptProperties.version}\"\n"
} else if(property == null) {
throw new GradleScriptException("No property found for placeholder: ${it}", null)
}
if(propertyName == "externalDependencies") {
property = dependencyModifier(property)
}
logger.info "replacing ${it} with ${property}"
line = line.replace(it, property)
}
line
}
def replaceFromFile = { line, lineModifier={ text -> text } ->
def lineToWrite = line
def placeholderMatches = getPlaceholderMatches(line)
if(placeholderMatches) {
def placeholder = placeholderMatches[0]
lineToWrite = line.replace(placeholder, lineModifier(new File("${getPlaceholderName(placeholder)}").text))
}
lineToWrite
}
def combineStuff = { fileType, lineModifier={ text -> text } ->
new File(getBinFolder(), "combined.${fileType}").withWriter { writer ->
new File("src/shell/shell.${fileType}").eachLine { line ->
writer << "${replaceFromFile(line, lineModifier)}\n"
}
}
}
task combineHtml {
inputs.dir("src")
outputs.file('bin/combined.html')
doLast {
def html = file("src/shell/shell.html").text
def placeholders = getPlaceholderMatches(html)
while(placeholders) {
placeholders.each { placeholder ->
html = html.replace(placeholder, new File("${getPlaceholderName(placeholder)}").text)
}
placeholders = getPlaceholderMatches(html)
}
html = html.replaceAll(/>\s*</, '><').replaceAll(/\n/, '')
def index = 0
def lineLength = 300
def htmlLines = []
while(index < html.length()) {
def endIndex = Math.min(index + lineLength, html.length())
def nextLine = html.substring(index, endIndex)
if(nextLine.endsWith('\\')) {
// don't end with \' because it breaks the HTML part of the script
endIndex++
nextLine = html.substring(index, endIndex)
}
htmlLines.add(nextLine)
index += (endIndex - index)
}
html = "'${htmlLines.join("'\n+ '")}'"
new File(getBinFolder(), "combined.html").write("var html = ${html}\njQuery('body').removeClass().removeAttr('style').html(html);");
}
}
task combineCss {
inputs.dir("src")
outputs.file('bin/combined.css')
doLast {
def css = []
fileTree("src").include("**/*.css").collect({ cssFile ->
def cssPart = ''
cssFile.eachLine { line ->
(line =~ /\$\w+/).each {
if(cssProperties[it] == null) {
throw new GradleScriptException("No CSS property found for argument ${it}", null)
}
line = line.replace(it, cssProperties[it])
}
// exclude comment lines
if(!(line ==~ /.*\/.*/)) {
cssPart += "${line.trim()} "
}
}
if(!cssPart.isEmpty()) {
if(cssPart.contains("'")) {
throw new GradleScriptException("Cannot have single quotes in CSS.\nLine: ${cssPart}", null)
}
css.push("'${cssPart}'")
}
})
new File(getBinFolder(), 'combined.css').text = css.join(",\n")
}
}
task combineJs {
inputs.dir("src")
outputs.file('bin/combined.js')
dependsOn 'combineHtml'
dependsOn 'combineCss'
doLast {
// TODO: don't add tab for css
// add a tab because it looks better for both scripts
combineStuff("js", { text -> " ${text.split('\n').join('\n ')}"})
}
}
task createDevtoolsScript {
dependsOn 'combineJs'
inputs.dir("src")
doLast {
new File(getBinFolder(), "${scriptProperties.name.toLowerCase()}-devtools.js").withWriter { writer ->
new File("src/templates/devtools.js").eachLine { line ->
writer << fillProperties("${line}\n", { property -> "\"${property.collect { it }.join("\",\"")}\"" })
}
}
}
}
task createTampermonkeyScript {
dependsOn 'combineJs'
inputs.dir("src")
doLast {
new File(getBinFolder(), "${scriptProperties.name}.user.js").withWriter { writer ->
new File("src/templates/tampermonkey.js").eachLine { line ->
writer << fillProperties("${line}\n", { property -> property.collect { "// @require ${it}"}.join("\n") })
}
}
}
}
task clean << {
delete("bin")
}
tasks.create('build').dependsOn 'createTampermonkeyScript','createDevtoolsScript'