Skip to content
Newer
Older
100755 251 lines (210 sloc) 10.7 KB
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
1 /*
2 * Copyright 2004-2005 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /**
18 * Gant script that handles upgrading of a Grails applications
19 *
20 * @author Graeme Rocher
21 * @author Sergey Nebolsin
22 *
23 * @since 0.4
24 */
25
26 import grails.util.Metadata
27
28 includeTargets << grailsScript("_GrailsPlugins")
29
30 target(upgrade: "main upgrade target") {
31
32 depends(createStructure, parseArguments)
33
34 boolean force = argsMap.force || !isInteractive ?: false
35
36 if (appGrailsVersion != grailsVersion) {
37 def gv = appGrailsVersion == null ? "pre-0.5" : appGrailsVersion
38 event("StatusUpdate", ["NOTE: Your application currently expects grails version [$gv], " +
39 "this target will upgrade it to Grails ${grailsVersion}"])
40 }
41
42 if (!force) {
43 ant.input(message: """
44 WARNING: This target will upgrade an older Grails application to ${grailsVersion}.
45 However, tag libraries provided by earlier versions of Grails found in grails-app/taglib will be removed.
46 The target will not, however, delete tag libraries developed by yourself.
47 Are you sure you want to continue?
48 """,
49 validargs: "y,n",
50 addproperty: "grails.upgrade.warning")
51
52 def answer = ant.antProject.properties."grails.upgrade.warning"
53
54 if (answer == "n") exit(0)
55
56 if ((grailsVersion.startsWith("1.0")) &&
57 !(['utf-8', 'us-ascii'].contains(System.getProperty('file.encoding')?.toLowerCase()))) {
58 ant.input(message: """
59 WARNING: This version of Grails requires all source code to be encoded in UTF-8.
60 Your system file encoding indicates that your source code may not be saved in UTF-8.
61 You can re-encode your source code manually after upgrading, but if you have used any
62 non-ASCII chars in your source or GSPs your application may not operate correctly until
63 you re-encode the files as UTF-8.
64
65 Are you sure you want to upgrade your project now?
66 """,
67 validargs: "y,n",
68 addproperty: "grails.src.encoding.warning")
69 answer = ant.antProject.properties."grails.src.encoding.warning"
70 if (answer == "n") exit(0)
71 }
72 }
73
74 clean()
75
76 def coreTaglibs = new File("${basedir}/plugins/core")
77
78 ant.delete(dir: "${coreTaglibs}", failonerror: false)
79
80
81 ant.sequential {
82 def testDir = "${basedir}/grails-tests"
83 if (new File("${testDir}/CVS").exists()) {
84 println """
85 WARNING: Your Grails tests directory '${testDir}' is currently under CVS control so the upgrade script cannot
86 move it to the new location of '${basedir}/test/integration'. Please move the directory using the relevant CVS commands."""
87 }
88 else if (new File("${testDir}/.svn").exists()) {
89 println """
90 WARNING: Your Grails tests directory '${testDir}' is currently under SVN control so the upgrade script cannot
91 move it to the new location of '${basedir}/test/integration'. Please move the directory using the relevant SVN commands."""
92 }
93 else {
94 if (new File(testDir).exists()) {
95 move(todir: "${basedir}/test/integration") {
96 fileset(dir: testDir, includes: "**")
97 }
98 delete(dir: testDir)
99 }
100
101 }
102 delete(dir: "${basedir}/tmp", failonerror: false)
103
104 // Unpack the shared files into a temporary directory, and then
105 // copy over the IDE files.
106 def tmpDir = new File("${basedir}/tmp-upgrade")
107 grailsUnpack(dest: tmpDir.path, src: "grails-shared-files.jar")
108 copy(todir: "${basedir}") {
c1d2eb7 @graemerocher fix for GRAILS-3157, build.xml and other files no longer overwritten …
graemerocher authored Apr 30, 2009
109 fileset(dir: tmpDir.path, includes:"*") {
110 present(present:"srconly", targetdir:basedir)
111 }
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
112 }
113 delete(dir: tmpDir.path)
114 launderIDESupportFiles()
115
116 copy(todir: "${basedir}/web-app") {
117 fileset(dir: "${grailsHome}/src/war") {
118 include(name: "**/**")
119 exclude(name: "WEB-INF/**")
120 present(present: "srconly", targetdir: "${basedir}/web-app")
121 }
122 }
123 copy(file: "${grailsHome}/src/war/WEB-INF/sitemesh.xml",
124 tofile: "${basedir}/web-app/WEB-INF/sitemesh.xml", overwrite: true)
125 copy(file: "${grailsHome}/src/war/WEB-INF/applicationContext.xml",
126 tofile: "${basedir}/web-app/WEB-INF/applicationContext.xml", overwrite: true)
127
128 if (!isPluginProject) {
129 // Install application-only files if needed, exact "one file only" matches
130 ['Config.groovy'].each() {template ->
131 if (!new File(baseFile, '/grails-app/conf').listFiles().find { it.name.equals(template) }) {
132 copy(tofile: "${basedir}/grails-app/conf/${template}") {
133 fileset(file: "${grailsHome}/src/grails/grails-app/conf/${template}") {
134 present(present: "srconly", targetdir: "${basedir}/grails-app/conf")
135 }
136 }
137 }
138 }
139
140 // Install application-only files if needed, with suffix matching
141 ['DataSource.groovy'].each() {template ->
142 if (!new File(baseFile, '/grails-app/conf').listFiles().find { it.name.startsWith(template) }) {
143 copy(tofile: "${basedir}/grails-app/conf/${template}") {
144 fileset(file: "${grailsHome}/src/grails/grails-app/conf/${template}") {
145 present(present: "srconly", targetdir: "${basedir}/grails-app/conf")
146 }
147 }
148 }
149 }
150 }
151
152 // Both applications and plugins can have UrlMappings, but only install default if there is nothing already
153 ['UrlMappings.groovy'].each() {template ->
154 if (!new File(baseFile, '/grails-app/conf').listFiles().find { it.name.endsWith(template) }) {
155 copy(tofile: "${basedir}/grails-app/conf/${template}") {
156 fileset(file: "${grailsHome}/src/grails/grails-app/conf/${template}") {
157 present(present: "srconly", targetdir: "${basedir}/grails-app/conf")
158 }
159 }
160 }
161 }
162
163 // if Config.groovy exists and it does not contain values for
164 // grails.views.default.codec or grails.views.gsp.encoding then
165 // add reasonable defaults for them
166 def configFile = new File(baseFile, '/grails-app/conf/Config.groovy')
167 if (configFile.exists()) {
168 def configSlurper = new ConfigSlurper()
169 def configObject = configSlurper.parse(configFile.toURI().toURL())
170 def defaultCodec = configObject.grails.views.default.codec
171 def gspEncoding = configObject.grails.views.gsp.encoding
172
173 if (!defaultCodec || !gspEncoding) {
174 configFile.withWriterAppend {
175 it.writeLine '\n// The following properties have been added by the Upgrade process...'
176 if (!defaultCodec) it.writeLine 'grails.views.default.codec="none" // none, html, base64'
177 if (!gspEncoding) it.writeLine 'grails.views.gsp.encoding="UTF-8"'
178 }
179 }
180 }
181
182 if (new File("${basedir}/spring").exists()) {
183 move(file: "${basedir}/spring", todir: "${basedir}/grails-app/conf")
184 }
185 if (new File("${basedir}/hibernate").exists()) {
186 move(file: "${basedir}/hibernate", todir: "${basedir}/grails-app/conf")
187 }
188
189 def appKey = baseName.replaceAll(/\s/, '.').toLowerCase()
190
191 replace(dir: "${basedir}/web-app/WEB-INF", includes: "**/*.*",
192 token: "@grails.project.key@", value: "${appKey}")
193
194
195 copy(todir: "${basedir}/web-app/WEB-INF/tld", overwrite: true) {
196 fileset(dir: "${grailsHome}/src/war/WEB-INF/tld/${servletVersion}")
197 fileset(dir: "${grailsHome}/src/war/WEB-INF/tld", includes: "spring.tld")
198 fileset(dir: "${grailsHome}/src/war/WEB-INF/tld", includes: "grails.tld")
199 }
200 touch(file: "${basedir}/grails-app/i18n/messages.properties")
201
202 replaceregexp(match: "^.*GRAILS_HOME.*\$", replace: "", flags: "gm") {
203 fileset(dir: "${basedir}", includes: ".classpath")
204 }
205 replace(dir: "${basedir}",
206 includes: ".classpath",
207 token: "</classpath>",
208 value: "<classpathentry kind=\"var\" path=\"GRAILS_HOME/ant/lib/ant.jar\"/>\n${eclipseClasspathLibs()}${eclipseClasspathGrailsJars()}\n</classpath>")
209 replaceregexp(match: "^\\s*", replace: "", flags: "gm") {
210 fileset(dir: "${basedir}", includes: ".classpath")
211 }
212
213 }
214
215 // Add the app name and Grails version to the metadata.
216 updateMetadata("app.name": "$grailsAppName", "app.grails.version": "$grailsVersion")
217
218 // proceed plugin-specific upgrade logic contained in 'scripts/_Upgrade.groovy' under plugin's root
881a417 @pledbrook This ostensibly fixes a problem with the _GrailsWar script which was
pledbrook authored Sep 23, 2009
219 def plugins = pluginSettings.pluginBaseDirectories
3e17890 1.1 branch is now trunk
graeme authored Mar 13, 2009
220 if (plugins) {
221 for (pluginDir in plugins) {
222 def f = new File(pluginDir)
223 if (f.isDirectory() && f.name != 'core') {
224 // fix for Windows-style path with backslashes
225
226 def pluginBase = "${basedir}/plugins/${f.name}".toString().replaceAll("\\\\", "/")
227 // proceed _Upgrade.groovy plugin script if exists
228 def upgradeScript = new File("${pluginBase}/scripts/_Upgrade.groovy")
229 if (upgradeScript.exists()) {
230 event("StatusUpdate", ["Executing ${f.name} plugin upgrade script"])
231 // instrumenting plugin scripts adding 'pluginBasedir' variable
232 def instrumentedUpgradeScript = "def pluginBasedir = '${pluginBase}'\n" + upgradeScript.text
233 // we are using text form of script here to prevent Gant caching
234 includeTargets << instrumentedUpgradeScript
235 }
236 }
237 }
238
239 }
240
241 installDefaultPluginSet()
242
243 event("StatusUpdate", ["Please make sure you view the README for important information about changes to your source code."])
244
245 event("StatusFinal", ["Project upgraded"])
246 }
247
248 target("default": "Upgrades a Grails application from a previous version of Grails") {
249 depends(upgrade)
250 }
Something went wrong with that request. Please try again.