Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v2.2.0.RC1
Fetching contributors…

Cannot retrieve contributors at this time

executable file 236 lines (201 sloc) 10.988 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 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
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Gant script that handles upgrading of a Grails applications
*
* @author Graeme Rocher
* @author Sergey Nebolsin
*
* @since 0.4
*/

import grails.util.Metadata

includeTargets << grailsScript("_GrailsPlugins")

UNMODIFIED_CHECKSUMS = [indexgsp:['e9f4d3450ba02fe92d55f4ae4b53dee8', 'e9f4d3450ba02fe92d55f4ae4b53dee8', '77f5ed5c2fca586a9ff1dc8e7beeb85b', '5313f072b2ed10129a446d5f648d8b41'],
                        errorgsp:['473b673fb3f04a60412ace1b7bc12a8c', '473b673fb3f04a60412ace1b7bc12a8c', '473b673fb3f04a60412ace1b7bc12a8c', '473b673fb3f04a60412ace1b7bc12a8c'],
                        maincss:['612301d27b1d5d6f670cc98905376f59','612301d27b1d5d6f670cc98905376f59', '820b415fc6e53b156b68e5a01fa5677e', '820b415fc6e53b156b68e5a01fa5677e']]

target(upgrade: "main upgrade target") {

    depends(createStructure, parseArguments)

    System.properties.put('runningGrailsUpgrade', 'true')
    boolean force = argsMap.force || !isInteractive ?: false

    if (appGrailsVersion != grailsVersion) {
        def gv = appGrailsVersion == null ? "pre-0.5" : appGrailsVersion
        event("StatusUpdate", ["NOTE: Your application currently expects grails version [$gv], " +
              "this target will upgrade it to Grails ${grailsVersion}"])
    }

    if (!force) {
        ant.input(message: """
WARNING: This target will upgrade an older Grails application to ${grailsVersion}.
Are you sure you want to continue?
""",
                    validargs: "y,n",
                    addproperty: "grails.upgrade.warning")

        def answer = ant.antProject.properties."grails.upgrade.warning"

        if (answer == "n") exit(0)

        if ((grailsVersion.startsWith("1.0")) &&
                !(['utf-8', 'us-ascii'].contains(System.getProperty('file.encoding')?.toLowerCase()))) {
            ant.input(message: """
WARNING: This version of Grails requires all source code to be encoded in UTF-8.
Your system file encoding indicates that your source code may not be saved in UTF-8.
You can re-encode your source code manually after upgrading, but if you have used any
non-ASCII chars in your source or GSPs your application may not operate correctly until
you re-encode the files as UTF-8.

Are you sure you want to upgrade your project now?
""",
                             validargs: "y,n",
                             addproperty: "grails.src.encoding.warning")
            answer = ant.antProject.properties."grails.src.encoding.warning"
            if (answer == "n") exit(0)
        }
    }

    clean()

    def coreTaglibs = new File("${basedir}/plugins/core")

    ant.delete(dir: "${coreTaglibs}", failonerror: false)

    ant.sequential {
        def testDir = "${basedir}/grails-tests"
        if (new File("${testDir}/CVS").exists()) {
            println """
WARNING: Your Grails tests directory '${testDir}' is currently under CVS control so the upgrade script cannot
move it to the new location of '${basedir}/test/integration'. Please move the directory using the relevant CVS commands."""
        }
        else if (new File("${testDir}/.svn").exists()) {
            println """
WARNING: Your Grails tests directory '${testDir}' is currently under SVN control so the upgrade script cannot
move it to the new location of '${basedir}/test/integration'. Please move the directory using the relevant SVN commands."""
        }
        else {
            if (new File(testDir).exists()) {
                move(todir: "${basedir}/test/integration") {
                    fileset(dir: testDir, includes: "**")
                }
                delete(dir: testDir)
            }
        }
        delete(dir: "${basedir}/tmp", failonerror: false)

        if (!new File("$grailsHome/src/war").exists()) {
            if (new File("${grailsHome}/grails-resources").exists()) {
                grailsHome = new File("${grailsHome}/grails-resources")
            }
        }

        copy(todir: "${basedir}/web-app") {
            fileset(dir: "${grailsHome}/src/war") {
                include(name: "**/**")
                exclude(name: "WEB-INF/**")
                present(present: "srconly", targetdir: "${basedir}/web-app")
            }
        }
        copy(file: "${grailsHome}/src/war/WEB-INF/sitemesh.xml",
             tofile: "${basedir}/web-app/WEB-INF/sitemesh.xml", overwrite: true)
        copy(file: "${grailsHome}/src/war/WEB-INF/applicationContext.xml",
             tofile: "${basedir}/web-app/WEB-INF/applicationContext.xml", overwrite: true)

        if (!isPluginProject) {
            // Install application-only files if needed, exact "one file only" matches
            ['Config.groovy'].each() {template ->
                if (!new File(baseFile, '/grails-app/conf').listFiles().find { it.name.equals(template) }) {
                    copyGrailsResource("${basedir}/grails-app/conf/${template}", grailsResource("src/grails/grails-app/conf/${template}"))
                }
            }

            ['BuildConfig.groovy'].each() {template ->
                if (!new File(baseFile, '/grails-app/conf').listFiles().find { it.name.equals(template) }) {
                    copyGrailsResource("${basedir}/grails-app/conf/${template}", grailsResource("src/grails/grails-app/conf/${template}"))
                }
            }

            // Install application-only files if needed, with suffix matching
            ['DataSource.groovy'].each() {template ->
                if (!new File(baseFile, '/grails-app/conf').listFiles().find { it.name.startsWith(template) }) {
                    copyGrailsResource("${basedir}/grails-app/conf/${template}", grailsResource("src/grails/grails-app/conf/${template}"))
                }
            }
        }

        // Both applications and plugins can have UrlMappings, but only install default if there is nothing already
        ['UrlMappings.groovy'].each() {template ->
            if (!new File(baseFile, '/grails-app/conf').listFiles().find { it.name.endsWith(template) }) {
                copyGrailsResource("${basedir}/grails-app/conf/${template}", grailsResource("src/grails/grails-app/conf/${template}"))
            }
        }

        def dsFile = new File(baseFile, "grails-app/conf/DataSource.groovy")
        if (dsFile.exists() && argsMap.'update-data-source') {
            replace file:dsFile, token:"jdbc:hsqldb:mem:devDB", value:"jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
            replace file:dsFile, token:"jdbc:hsqldb:mem:testDb",value: "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
            replace file:dsFile, token:"org.hsqldb.jdbcDriver", value:"org.h2.Driver"
        }
        // if Config.groovy exists and it does not contain values for
        // grails.views.default.codec or grails.views.gsp.encoding then
        // add reasonable defaults for them
        def configFile = new File(baseFile, '/grails-app/conf/Config.groovy')
        if (configFile.exists()) {
            def configText = configFile.text
            configFile.withWriterAppend {
                if (!configText.contains("grails.views.default.codec")) {
                    it.writeLine 'grails.views.default.codec="none" // none, html, base64'
                }
                if (!configText.contains("grails.views.gsp.encoding")) {
                    it.writeLine 'grails.views.gsp.encoding="UTF-8"'
                }
            }
        }

        if (new File("${basedir}/spring").exists()) {
            move(file: "${basedir}/spring", todir: "${basedir}/grails-app/conf")
        }
        if (new File("${basedir}/hibernate").exists()) {
            move(file: "${basedir}/hibernate", todir: "${basedir}/grails-app/conf")
        }

        def appKey = baseName.replaceAll(/\s/, '.').toLowerCase()

        replace(dir: "${basedir}/web-app/WEB-INF", includes: "**/*.*",
                token: "@grails.project.key@", value: "${appKey}")

        copy(todir: "${basedir}/web-app/WEB-INF/tld", overwrite: true) {
            fileset(dir: "${grailsHome}/src/war/WEB-INF/tld/${servletVersion}")
            fileset(dir: "${grailsHome}/src/war/WEB-INF/tld", includes: "spring.tld")
            fileset(dir: "${grailsHome}/src/war/WEB-INF/tld", includes: "grails.tld")
        }
        touch(file: "${basedir}/grails-app/i18n/messages.properties")
    }

    // Add the app name and Grails version to the metadata.
    def newMetadata = ["app.name": "$grailsAppName", "app.grails.version": "$grailsVersion"]
    for (pluginEntry in grailsSettings.defaultPluginMap) {
        def pluginName = pluginEntry.key
        def pluginKey = "plugins.$pluginName".toString()
        if (metadata.containsKey(pluginKey)) {
            newMetadata[pluginKey] = pluginEntry.value
        }
    }
    updateMetadata(metadata, newMetadata)

    // proceed plugin-specific upgrade logic contained in 'scripts/_Upgrade.groovy' under plugin's root
    def plugins = pluginSettings.pluginBaseDirectories
    if (plugins) {
        for (pluginDir in plugins) {
            def f = new File(pluginDir)
            if (f.isDirectory() && f.name != 'core') {
                // fix for Windows-style path with backslashes

                def pluginBase = "${basedir}/plugins/${f.name}".toString().replaceAll("\\\\", "/")
                // proceed _Upgrade.groovy plugin script if exists
                def upgradeScript = new File("${pluginBase}/scripts/_Upgrade.groovy")
                if (upgradeScript.exists()) {
                    event("StatusUpdate", ["Executing ${f.name} plugin upgrade script"])
                    // instrumenting plugin scripts adding 'pluginBasedir' variable
                    def instrumentedUpgradeScript = "def pluginBasedir = '${pluginBase}'\n" + upgradeScript.text
                    // we are using text form of script here to prevent Gant caching
                    includeTargets << instrumentedUpgradeScript
                }
            }
        }
    }

    installDefaultPluginSet()

    event("StatusUpdate", ["Please make sure you view the README for important information about changes to your source code."])

    event("StatusFinal", ["Project upgraded"])
}

target("default": "Upgrades a Grails application from a previous version of Grails") {
    depends(upgrade)
}
Something went wrong with that request. Please try again.