forked from cytomine/Cytomine-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Events.groovy
281 lines (233 loc) · 10.1 KB
/
_Events.groovy
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//import be.cytomine.ViewPortToBuildXML
import grails.util.Environment
import org.w3c.dom.Document
import org.w3c.dom.Element
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.OutputKeys
import javax.xml.transform.Transformer
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
eventTestPhasesStart = { msg ->
File app = new File("web-app/application.js")
File lib = new File("web-app/lib.js")
app.delete()
lib.delete()
println("========= C O M P I L E == J S ========= ")
ViewPortToBuildXML.process()
def proc = "./scripts/yui-compressor-ant-task/doc/example/deploy.sh".execute()
proc.in.eachLine { line -> println line }
proc = "./scripts/yui-compressor-ant-task/doc/lib/deploy.sh".execute()
proc.in.eachLine { line -> println line }
println("======================================== ")
}
eventGenerateWebXmlEnd = {
println "*************************** eventGenerateWebXmlEnd ***************************"
println Environment.getCurrent() == Environment.PRODUCTION
if (Environment.getCurrent() == Environment.PRODUCTION) {
println("========= C O M P I L E == J S ========= ")
ViewPortToBuildXML.process()
def proc = "./scripts/yui-compressor-ant-task/doc/example/deploy.sh".execute()
def (output, error) = new StringWriter().with { o -> // For the output
new StringWriter().with { e -> // For the error stream
proc.waitForProcessOutput( o, e )
[ o, e ]*.toString() // Return them both
}
}
assert error.length() == 0
output.eachLine { line -> println line }
proc = "./scripts/yui-compressor-ant-task/doc/lib/deploy.sh".execute()
proc.in.eachLine { line -> println line }
println("======================================== ")
}
// new JsondocController().build()
}
eventConfigureTomcat = { tomcat ->
String contextFile = "web-app/WEB-INF/context.xml"
System.getProperties().list(System.out);
//increase max cookie size
tomcat.connector.setAttribute("port",Integer.parseInt(System.getProperty("server.port")))
tomcat.connector.setAttribute("maxHttpHeaderSize",262144)
// if (Environment.getCurrent().name.equals("cluster")) {
// if (new File(contextFile).exists()) {
// println "----- CONFIGURING TOMCAT REALM ----------"
// this.buildSettings = grails.util.BuildSettingsHolder.getSettings()
// def workDir = buildSettings.projectWorkDir
// def tomcatDir = new File("${workDir}/tomcat").absolutePath
// println tomcatDir
// ant.copy(file:"${contextFile}", todir:"${tomcatDir}/conf")
// } else {
// println "----- NO CONTEXT FILE in" + new File(contextFile)
// }
// }
}
/**
* User: lrollus
* Date: 16/03/12
* Extract data from viewport (import cytomine js + lib js) and build 2 xml files with application and lib data.
* Thanks to these two xml files, we ca create application.js and lib.js
* application.js: all js files from cytomine
* lib.js: all js files from lib
*/
class ViewPortToBuildXML {
/**
* Create lib build.xml and app build.xml thanks to viewport info
*/
public static void process() {
//Read viewport
def inputStreamFileSourceFull = new FileInputStream("grails-app/views/layouts/viewport.gsp");
//must skip line 0->11 because not valid xml
String content = convertStreamToString(inputStreamFileSourceFull, 12)
content = content.replace("<jawr:script src='/i18n/messages.js'></jawr:script>", "")
print content
//convert string to xml element
def inputStreamFileSource = new ByteArrayInputStream(content.getBytes("UTF-8"));
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
Element viewPortXML = builder.parse(inputStreamFileSource).documentElement
//Get lib/app src js files from viewports
List<String> libFiles = getLibFilesFromViewPort(viewPortXML);
List<String> appFiles = getAppFilesFromViewPort(viewPortXML);
//Add lines to build.xml template files
String libXML = fillTemplateFile(libFiles, "scripts/yui-compressor-ant-task/doc/lib/build_template.xml");
String appXML = fillTemplateFile(appFiles, "scripts/yui-compressor-ant-task/doc/example/build_template.xml");
//Write files
writeToFile(libXML, "scripts/yui-compressor-ant-task/doc/lib/build.xml")
writeToFile(appXML, "scripts/yui-compressor-ant-task/doc/example/build.xml")
}
/**
* Read template xml file and fill js files path from viewport
* @param lines All js files to include
* @param file Template file
* @return XML string with all js files include template
*/
public static String fillTemplateFile(List<String> lines, String file) {
println "ADD ${lines.size()} in $file"
//read template file
def inputStreamFileSource = new FileInputStream(file);
def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
Document doc = builder.parse(inputStreamFileSource)
def buildXML = doc.documentElement
def build = null
def targetsElem = buildXML.getElementsByTagName("target")
(0..<targetsElem.length).each {
def targetElem = targetsElem.item(it)
def scriptElemSrc = targetElem.attributes.getNamedItem('name').nodeValue.toString()
if (scriptElemSrc.startsWith("build")) build = targetElem;
}
//save it as a new files
def concat = build.getElementsByTagName("concat").item(0)
if (concat.hasChildNodes()) {
while (concat.childNodes.length >= 1) {
concat.removeChild(concat.firstChild);
}
}
//write lines
lines.each {
Element fileset = doc.createElement('fileset')
fileset.setAttribute("dir", "\${src.dir}")
fileset.setAttribute("includes", it)
concat.appendChild(fileset)
}
//save it as xml and return it as string
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
return xmlString
}
/**
* Get all lib js files from viewPort
* @param viewPortXML ViewPortXML
* @return List of js files
*/
public static List<String> getLibFilesFromViewPort(def viewPortXML) {
List<String> libFiles = new ArrayList<String>()
//get root element
def gElem = getGElement(viewPortXML)
//get all script element
def scriptElems = gElem.getElementsByTagName("script")
//browse script elements and get src value if element is lib
(0..<scriptElems.length).each {
def scriptElem = scriptElems.item(it)
if (scriptElem.attributes.getNamedItem('src') != null) {
def scriptElemSrc = scriptElem.attributes.getNamedItem('src').nodeValue.toString()
if (scriptElemSrc.startsWith("lib")) libFiles.add(scriptElemSrc.replace("?version=<g:meta name=\"app.version\"/>",""));
}
}
libFiles.each {
println "lib -> " + it
}
return libFiles
}
/**
* Get all application js files from viewPort
* @param viewPortXML ViewPortXML
* @return List of js files
*/
public static List<String> getAppFilesFromViewPort(def viewPortXML) {
List<String> appFiles = new ArrayList<String>()
//get root element
def gElem = getGElement(viewPortXML)
//get all script element
def scriptElems = gElem.getElementsByTagName("script")
//browse script elements and get src value if element is app
(0..<scriptElems.length).each {
def scriptElem = scriptElems.item(it)
if (scriptElem.attributes.getNamedItem('src') != null) {
def scriptElemSrc = scriptElem.attributes.getNamedItem('src').nodeValue.toString()
if (scriptElemSrc.startsWith("app")) appFiles.add(scriptElemSrc.replace("?version=<g:meta name=\"app.version\"/>",""));
}
}
appFiles.each {
println "app -> " + it
}
return appFiles
}
/**
* Get g element which is the parent of all import files
* @param viewPortXML View port xml
* @return G element
*/
public static def getGElement(def viewPortXML) {
def g = null
viewPortXML.getElementsByTagName("head").item(0).childNodes.each {
if (it.nodeName.equals("g:if") && g == null) g = it
//println it.nodeName
}
return g
}
/**
* Convert stream to string and start at line offset
* @param is Stream
* @param offset Line where the stream start to read
* @return String with stream content
* @throws IOException Error when reading file
*/
public static String convertStreamToString(InputStream is, int offset) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
int i = 0;
while ((line = r.readLine()) != null) {
if (i >= offset) {
total.append(line);
}
i++
}
return total.toString()
}
/**
* Write an xml string to a file
* @param xmlString XML String
* @param filePath Destination file
*/
public static void writeToFile(def xmlString, def filePath) {
println "ADD ${xmlString.size()} in ${new File("$filePath").absolutePath}"
new File("$filePath").withWriter { out ->
out.println xmlString
}
}
}