|
| 1 | +<?xml version="1.0"?> |
| 2 | + <!-- |
| 3 | + * Copyright (c) 2012, FinancialForce.com, inc |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without modification, |
| 7 | + * are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * - Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * - Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * - Neither the name of the FinancialForce.com, inc nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software without |
| 16 | + * specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 21 | + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +--> |
| 27 | +<project name="ExecAnon"> |
| 28 | + |
| 29 | + <!-- Download from http://sourceforge.net/projects/ant-contrib/files/ant-contrib/1.0b3/ --> |
| 30 | + <taskdef |
| 31 | + resource="net/sf/antcontrib/antlib.xml" |
| 32 | + classpath="${basedir}/lib/ant-contrib-1.0b3.jar"/> |
| 33 | + |
| 34 | + <!-- Download from https://code.google.com/p/missing-link/ --> |
| 35 | + <taskdef |
| 36 | + name="http" |
| 37 | + classname="org.missinglink.ant.task.http.HttpClientTask" |
| 38 | + classpath="${basedir}/lib/ml-ant-http-1.1.3.jar"/> |
| 39 | + |
| 40 | + <!-- Download from http://www.oopsconsultancy.com/software/xmltask/ --> |
| 41 | + <taskdef |
| 42 | + name="xmltask" |
| 43 | + classname="com.oopsconsultancy.xmltask.ant.XmlTask" |
| 44 | + classpath="${basedir}/lib/xmltask.jar"/> |
| 45 | + |
| 46 | + <target name="ExecAnon"> |
| 47 | + <executeApex username="${sf.username}" password="${sf.password}">${what}</executeApex> |
| 48 | + </target> |
| 49 | + |
| 50 | + <!-- Provides access to the Salesforce Tooling REST API ExecuteAnnoynmous resource --> |
| 51 | + <macrodef name="executeApex" description="Provides access to the Salesforce Tooling REST API ExecuteAnnoynmous resource"> |
| 52 | + <attribute name="username" description="Salesforce user name."/> |
| 53 | + <attribute name="password" description="Salesforce password."/> |
| 54 | + <attribute name="resultprefix" description="Property name prefix used for properties containing response data" default="executeAnonymousResponse"/> |
| 55 | + <attribute name="failonerror" description="If the execute fails then fail the Ant script" default="true"/> |
| 56 | + <text name="apexcode"/> |
| 57 | + <sequential> |
| 58 | + <!-- Login --> |
| 59 | + <login username="@{username}" password="@{password}" serverurl="serverUrl" sessionId="sessionId"/> |
| 60 | + <!-- Extract host/instance name from the serverUrl returned from the login response --> |
| 61 | + <propertyregex property="host" |
| 62 | + input="${serverUrl}" |
| 63 | + regexp="^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$" |
| 64 | + select="\3" |
| 65 | + casesensitive="false" /> |
| 66 | + <!-- Execute Apex via Tooling API /executeAnonymous resource --> |
| 67 | + <http url="https://${host}/services/data/v30.0/tooling/executeAnonymous" method="GET" entityProperty="executeAnonymousResponse" statusProperty="loginResponseStatus" printrequestheaders="false" printresponseheaders="false"> |
| 68 | + <headers> |
| 69 | + <header name="Authorization" value="Bearer ${sessionId}"/> |
| 70 | + </headers> |
| 71 | + <query> |
| 72 | + <parameter name="anonymousBody" value="@{apexcode}"/> |
| 73 | + </query> |
| 74 | + </http> |
| 75 | + <!-- Parse JSON response and set properites --> |
| 76 | + <script language="javascript"> |
| 77 | + var response = eval('('+project.getProperty('executeAnonymousResponse')+')'); |
| 78 | + for(field in response) |
| 79 | + project.setProperty('@{resultprefix}.' + field, response[field]); |
| 80 | + </script> |
| 81 | + <!-- Fail on error?--> |
| 82 | + <if> |
| 83 | + <and> |
| 84 | + <equals arg1="@{failonerror}" arg2="true"/> |
| 85 | + <equals arg1="${@{resultprefix}.success}" arg2="false"/> |
| 86 | + </and> |
| 87 | + <then> |
| 88 | + <if> |
| 89 | + <equals arg1="${@{resultprefix}.compiled}" arg2="false"/> |
| 90 | + <then> |
| 91 | + <fail message="${@{resultprefix}.line}:${@{resultprefix}.column} ${@{resultprefix}.compileProblem}"/> |
| 92 | + </then> |
| 93 | + <else> |
| 94 | + <fail message="${@{resultprefix}.exceptionMessage} ${@{resultprefix}.exceptionStackTrace}"/> |
| 95 | + </else> |
| 96 | + </if> |
| 97 | + </then> |
| 98 | + </if> |
| 99 | + </sequential> |
| 100 | + </macrodef> |
| 101 | + |
| 102 | + <!-- Login into Salesforce and return the session Id and serverUrl --> |
| 103 | + <macrodef name="login"> |
| 104 | + <attribute name="username" description="Salesforce user name."/> |
| 105 | + <attribute name="password" description="Salesforce password."/> |
| 106 | + <attribute name="serverurl" description="Server Url property."/> |
| 107 | + <attribute name="sessionId" description="Session Id property."/> |
| 108 | + <sequential> |
| 109 | + <!-- Obtain Session Id via Login SOAP service --> |
| 110 | + <http url="https://login.salesforce.com/services/Soap/c/30.0" method="POST" failonunexpected="false" entityProperty="loginResponse" statusProperty="loginResponseStatus"> |
| 111 | + <headers> |
| 112 | + <header name="Content-Type" value="text/xml"/> |
| 113 | + <header name="SOAPAction" value="login"/> |
| 114 | + </headers> |
| 115 | + <entity> |
| 116 | + <![CDATA[ |
| 117 | + <env:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'> |
| 118 | + <env:Body> |
| 119 | + <sf:login xmlns:sf='urn:enterprise.soap.sforce.com'> |
| 120 | + <sf:username>@{username}</sf:username> |
| 121 | + <sf:password>@{password}</sf:password> |
| 122 | + </sf:login> |
| 123 | + </env:Body> |
| 124 | + </env:Envelope> |
| 125 | + ]]> |
| 126 | + </entity> |
| 127 | + </http> |
| 128 | + <!-- Parse response --> |
| 129 | + <xmltask destbuffer="loginResponseBuffer"> |
| 130 | + <insert path="/">${loginResponse}</insert> |
| 131 | + </xmltask> |
| 132 | + <if> |
| 133 | + <!-- Success? --> |
| 134 | + <equals arg1="${loginResponseStatus}" arg2="200"/> |
| 135 | + <then> |
| 136 | + <!-- Parse sessionId and serverUrl --> |
| 137 | + <xmltask sourcebuffer="loginResponseBuffer" failWithoutMatch="true"> |
| 138 | + <copy path="/*[local-name()='Envelope']/*[local-name()='Body']/:loginResponse/:result/:sessionId/text()" property="@{sessionId}"/> |
| 139 | + <copy path="/*[local-name()='Envelope']/*[local-name()='Body']/:loginResponse/:result/:serverUrl/text()" property="@{serverUrl}"/> |
| 140 | + </xmltask> |
| 141 | + </then> |
| 142 | + <else> |
| 143 | + <!-- Parse login error message and fail build --> |
| 144 | + <xmltask sourcebuffer="loginResponseBuffer" failWithoutMatch="true"> |
| 145 | + <copy path="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='Fault']/*[local-name()='faultstring']/text()" property="faultString"/> |
| 146 | + </xmltask> |
| 147 | + <fail message="${faultString}"/> |
| 148 | + </else> |
| 149 | + </if> |
| 150 | + </sequential> |
| 151 | + </macrodef> |
| 152 | + |
| 153 | + <target name="ExecAnonScript"> |
| 154 | + <loadfile property="script" srcFile="${what}"/> |
| 155 | + <executeApex username="${sf.username}" password="${sf.password}">${script}</executeApex> |
| 156 | + </target> |
| 157 | + |
| 158 | +</project> |
0 commit comments