source: trunk/scripts/_UpdateRev.groovy @ 529

Last change on this file since 529 was 526, checked in by gav, 15 years ago

Refactor UpadateRev script to fix classNotFound exception and make script re-usable.

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1import org.tmatesoft.svn.core.wc.*
2
3//includeTargets << grailsScript("Init")
4
5/**
6* Compare and update the app.vcsRevision property in application.properties and metadata.
7* May be run directly by "grails update-rev" or from _Events.groovy by eventCompileStart.
8*/
9target(updateVcsRevision: "Update the app.vcsRevision property in application.properties and metadata.") {
10
11    def result = [:]
12
13    def fail = { Map m ->
14        result.error = [ code: m.code, args: m.args ]
15        println "Error: UpdateRev script: " + result.error
16        return result
17    }
18
19    def url = basedir + "/application.properties"
20
21    def propertiesFile = new File(url)
22        if(!propertiesFile.isFile())
23            return fail(code:"application.properties.file.not.found", args:[url])
24
25    def appRevision = getAppRevision(propertiesFile)
26    if(appRevision.error)
27        return fail(code: appRevision.error.code, args: appRevision.error.args)
28
29    def svnRevision = getSvnRevision()
30    if(svnRevision.error)
31        return fail(code: svnRevision.error.code, args: svnRevision.error.args)
32
33    // Compare and update.
34    if(appRevision.revision != svnRevision.revision) {
35
36        println "app.vcsRevision = "+appRevision.revision +', SVN Revision = '+svnRevision.revision
37
38        // Update metadata if already loaded.
39        if(binding.variables.containsKey('metadata')) {
40            //binding.variables.each { println it.key } // print available.
41            def metadata = binding.variables['metadata']
42            metadata['app.vcsRevision'] = '$Rev: '+svnRevision.revision+' $'
43        }
44
45        // Update application.properties file.
46        def writeResult = writeVcsRevision(propertiesFile, svnRevision.revision)
47        if(writeResult.error)
48            return fail(code: writeResult.error.code, args: writeResult.error.args)
49
50    } // if(rev != rev)
51    else {
52        println "VCS Revisions match: app.vcsRevision = "+appRevision.revision +', SVN Revision = '+svnRevision.revision +'.'
53    }
54
55    // Success.
56    return result
57
58} // updateVcsRevision()
59
60/**
61* Get the app.vcsRevision property from properties file.
62* @retuns A map containing revision and lineNumber otherwise an error map.
63*/
64def getAppRevision(propertiesFile) {
65    def result = [:]
66
67    def fail = { Map m ->
68        result.error = [ code: m.code, args: m.args ]
69        return result
70    }
71
72    propertiesFile.eachLine { line, lineNumber ->
73        // app.vcsRevision=$Rev: NUM $
74        if ( line =~ '^app.vcsRevision.' ) {
75            if(line.size() > 23) {
76                result.revision = line[22..-3]
77                result.lineNumber = lineNumber
78            }
79        }
80    }
81
82    if(!result.revision || !result.lineNumber)
83        return fail(code:"app.vcsRevision.not.found")
84
85    // Success.
86    return result
87
88} // getAppRevision()
89
90/**
91* Get the working copy's base revision from SVN.
92* @retuns A map containing revision otherwise an error map.
93*/
94def getSvnRevision() {
95    def result = [:]
96
97    def fail = { Map m ->
98        result.error = [ code: m.code, args: m.args ]
99        return result
100    }
101
102    def wc = new File(basedir)
103        if(!wc.isDirectory())
104            return fail(code:"vcs.working.copy.not.found", args:[basedir])
105
106    // Use svnkit to get the base revision.
107    def clientManager = SVNClientManager.newInstance()
108    def wcClient = clientManager.getWCClient()
109    try {
110        result.revision = wcClient.doInfo(wc, SVNRevision.BASE).getRevision().toString()
111    }
112    catch(org.tmatesoft.svn.core.SVNException e) {
113        fail(code:"vcs.exception", args:[e])
114    }
115
116    // Success.
117    return result
118} // getSvnRevision()
119
120/**
121* Write revision to properties file.
122* @retuns An error map if any errors.
123*/
124def writeVcsRevision(propertiesFile, revision) {
125    def result = [:]
126
127    def fail = { Map m ->
128        result.error = [ code: m.code, args: m.args ]
129        return result
130    }
131
132    def revisionString = 'app.vcsRevision=\\$Rev: '+revision+' \\$'
133    println "Updating application.properties with: ${revisionString}"
134
135    def processFileInplace = { file, Closure processText ->
136        def text = file.text
137        file.write(processText(text))
138    }
139
140    processFileInplace(propertiesFile) { text ->
141        text.replaceAll('app.vcsRevision.*', revisionString)
142    }
143
144    // Success.
145    return result
146
147} // writeVcsRevision()
Note: See TracBrowser for help on using the repository browser.