Last active
July 13, 2024 17:01
-
-
Save grtjn/0988970337e9fe3103584fd544d77f2b to your computer and use it in GitHub Desktop.
Gradle runScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def resolveTokens(str) { | |
if (str != null) { | |
Map<String, String> customTokens = mlAppConfig.getCustomTokens(); | |
if (customTokens != null) { | |
for (String key : customTokens.keySet()) { | |
if (key != null) { | |
String value = customTokens.get(key); | |
if (value != null) { | |
str = str.replace(key, value); | |
} | |
} | |
} | |
} | |
} | |
return str | |
} | |
def decodeMultipart(responseEntity) { | |
if (responseEntity.getHeaders().getFirst('Content-Type')) { | |
def boundary = responseEntity.getHeaders().getFirst('Content-Type').replaceAll(/^.*boundary=(.*)$/, '$1') | |
def parts = responseEntity.getBody().replace('\r\n', '\n').replace('\r', '\n').split('--' + boundary) | |
def body = '' | |
parts.each { | |
def splits = it.split('\n\n') | |
if (splits.length > 1) { | |
body = body + splits[1]; | |
} | |
} | |
return body | |
} else { | |
// likely empty response? | |
return responseEntity.getBody() | |
} | |
} | |
def runScript(xquery, jscript) { | |
def port = project.findProperty('port') ?: mlAppServicesPort | |
def url = mlHost + ":" + port + '/v1/eval' | |
logger.lifecycle('Running script ' + (xquery ?: jscript)) | |
try { | |
def manageConfig = getProject().property("mlManageConfig") | |
if (mlAppServicesSimpleSsl == 'true') { | |
// TODO: what if port is not 8000, and https is not enabled? | |
manageConfig.setScheme("https") | |
manageConfig.setConfigureSimpleSsl(true) | |
manageConfig.setPort(port.toInteger()) | |
} else { | |
manageConfig.setScheme("http") | |
manageConfig.setConfigureSimpleSsl(false) | |
manageConfig.setPort(port.toInteger()) | |
} | |
def manageClient = new com.marklogic.mgmt.ManageClient(manageConfig) | |
def code = resolveTokens(new File(xquery ?: jscript).getText('UTF-8')) | |
def body = decodeMultipart(manageClient.postForm("/v1/eval", xquery ? "xquery" : "javascript", code)) | |
logger.debug("Success: ${body}") | |
return body | |
} catch (java.net.ConnectException ex) { | |
logger.warn("Host not reachable: "+ mlHost + ":" + mlAppServicesPort) | |
} catch (Exception ex) { | |
logger.error("Running script failed:" + ex.getMessage()) | |
} | |
} | |
task runScript(group: project.name) { | |
doLast { | |
def xquery = project.findProperty("xquery") ?: '' | |
def jscript = project.findProperty("jscript") ?: '' | |
if (xquery === '' && jscript === '') { | |
logger.error('-' * 30) | |
logger.error('ERR: No script provided. Use -Pxquery=.. or -Pjscript=.. to provide a script, -Poutput= to save results to a file.') | |
} | |
def body = runScript(xquery, jscript) | |
if (body) { | |
def file = project.findProperty("output") ?: '' | |
if (file !== '') { | |
logger.lifecycle("Results written to " + file) | |
new File(file).write(body) | |
} else { | |
print body | |
} | |
} else { | |
logger.lifecycle('Empty response') | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment