source: trunk/scripts/UpdateRev.groovy @ 522

Last change on this file since 522 was 522, checked in by gav, 14 years ago

Add UpdateRev.groovy and _Events.groovy scripts to update application.properties and metadata at compile time to svn base revision.

  • Property svn:executable set to *
File size: 4.1 KB
RevLine 
[522]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
52    // Success.
53    return result
54
55} // updateVcsRevision()
56
57/**
58* Get the app.vcsRevision property from properties file.
59* @retuns A map containing revision and lineNumber otherwise an error map.
60*/
61def getAppRevision(propertiesFile) {
62    def result = [:]
63
64    def fail = { Map m ->
65        result.error = [ code: m.code, args: m.args ]
66        return result
67    }
68
69    propertiesFile.eachLine { line, lineNumber ->
70        // app.vcsRevision=$Rev: NUM $
71        if ( line =~ '^app.vcsRevision.' ) {
72            if(line.size() > 23) {
73                result.revision = line[22..-3]
74                result.lineNumber = lineNumber
75            }
76        }
77    }
78
79    if(!result.revision || !result.lineNumber)
80        return fail(code:"app.vcsRevision.not.found")
81
82    // Success.
83    return result
84
85} // getAppRevision()
86
87/**
88* Get the working copy's base revision from SVN.
89* @retuns A map containing revision otherwise an error map.
90*/
91def getSvnRevision() {
92    def result = [:]
93
94    def fail = { Map m ->
95        result.error = [ code: m.code, args: m.args ]
96        return result
97    }
98
99    def wc = new File(basedir)
100        if(!wc.isDirectory())
101            return fail(code:"vcs.working.copy.not.found", args:[basedir])
102
103    // Use svnkit to get the base revision.
104    def clientManager = SVNClientManager.newInstance()
105    def wcClient = clientManager.getWCClient()
106    try {
107        result.revision = wcClient.doInfo(wc, SVNRevision.BASE).getRevision().toString()
108    }
109    catch(org.tmatesoft.svn.core.SVNException e) {
110        fail(code:"vcs.exception", args:[e])
111    }
112
113    // Success.
114    return result
115} // getSvnRevision()
116
117/**
118* Write revision to properties file.
119* @retuns An error map if any errors.
120*/
121def writeVcsRevision(propertiesFile, revision) {
122    def result = [:]
123
124    def fail = { Map m ->
125        result.error = [ code: m.code, args: m.args ]
126        return result
127    }
128
129    def revisionString = 'app.vcsRevision=\\$Rev: '+revision+' \\$'
130    println "Updating application.properties with ${revisionString}"
131
132    def processFileInplace = { file, Closure processText ->
133        def text = file.text
134        file.write(processText(text))
135    }
136
137    processFileInplace(propertiesFile) { text ->
138        text.replaceAll('app.vcsRevision.*', revisionString)
139    }
140
141    // Success.
142    return result
143
144} // writeVcsRevision()
145
146setDefaultTarget(updateVcsRevision)
Note: See TracBrowser for help on using the repository browser.