Changeset 531


Ignore:
Timestamp:
May 14, 2010, 12:13:05 PM (14 years ago)
Author:
gav
Message:

Refactor UpadateRev script to improve handling when not running in a working copy.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/AppCoreController.groovy

    r521 r531  
    4848            if(applicationVcsRevision.size() > 7)  { // Svn's $Rev: NUM $
    4949                applicationVcsRevision = applicationVcsRevision[6..-3]
    50                 applicationString += " (r" + applicationVcsRevision + ")"
     50                applicationString += " (rev " + applicationVcsRevision + ")"
    5151            }
    5252            else
  • trunk/scripts/_UpdateRev.groovy

    r526 r531  
    44
    55/**
    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.
     6* Check and update the app.vcsRevision property in application.properties file and metadata.
     7* May be run directly by `grails update-rev` and normally run by _Events.groovy and eventCompileStart.
     8* The revision in the properties file is checked against the version control system (VCS) revision
     9* and updated if required.
     10* The VCS revision is written to the properties file so that it is available in the compiled war.
     11* The compile is intentionally allowed to go ahead if a handled exception occurs.
     12* VCS currently supported: subversion.
    813*/
    9 target(updateVcsRevision: "Update the app.vcsRevision property in application.properties and metadata.") {
     14target(updateVcsRevision: "Check and update the app.vcsRevision property in application.properties file and metadata.") {
    1015
    1116    def result = [:]
    1217
     18    // Properties file.
     19    def url = basedir + "/application.properties"
     20    def propertiesFile = new File(url)
     21
    1322    def fail = { Map m ->
     23        updateInMemoryMetadata('Unknown')
     24        def writeResult = writePropertiesFileRevision(propertiesFile, 'Unknown')
     25        if(writeResult.error) {
     26            m.code= writeResult.error.code
     27            m.args= writeResult.error.args
     28        }
     29
    1430        result.error = [ code: m.code, args: m.args ]
    1531        println "Error: UpdateRev script: " + result.error
     
    1733    }
    1834
    19     def url = basedir + "/application.properties"
     35    // Get propertiesFile revision.
     36    def properitesFileResult = getPropertiesFileRevision(propertiesFile)
     37    if(properitesFileResult.error)
     38        return fail(code: properitesFileResult.error.code, args: properitesFileResult.error.args)
    2039
    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)
     40    // Get VCS revision.
     41    def vcsResult = getVcsRevision()
     42    if(vcsResult.error)
     43        return fail(code: vcsResult.error.code, args: vcsResult.error.args)
    3244
    3345    // Compare and update.
    34     if(appRevision.revision != svnRevision.revision) {
     46    if(properitesFileResult.revision != vcsResult.revision) {
    3547
    36         println "app.vcsRevision = "+appRevision.revision +', SVN Revision = '+svnRevision.revision
     48        println "app.vcsRevision = "+properitesFileResult.revision +', VCS Revision = '+vcsResult.revision
    3749
    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         }
     50        updateInMemoryMetadata(vcsResult.revision)
    4451
    4552        // Update application.properties file.
    46         def writeResult = writeVcsRevision(propertiesFile, svnRevision.revision)
     53        def writeResult = writePropertiesFileRevision(propertiesFile, vcsResult.revision)
    4754        if(writeResult.error)
    4855            return fail(code: writeResult.error.code, args: writeResult.error.args)
     
    5057    } // if(rev != rev)
    5158    else {
    52         println "VCS Revisions match: app.vcsRevision = "+appRevision.revision +', SVN Revision = '+svnRevision.revision +'.'
     59        println "VCS Revisions match: app.vcsRevision = ${properitesFileResult.revision}, VCS Revision = ${vcsResult.revision}."
    5360    }
    5461
     
    6269* @retuns A map containing revision and lineNumber otherwise an error map.
    6370*/
    64 def getAppRevision(propertiesFile) {
     71def getPropertiesFileRevision(propertiesFile) {
    6572    def result = [:]
    6673
     
    6976        return result
    7077    }
     78
     79    if(!propertiesFile.isFile())
     80        return fail(code:"application.properties.file.not.found", args:[propertiesFile.getAbsoluteFile()])
    7181
    7282    propertiesFile.eachLine { line, lineNumber ->
     
    92102* @retuns A map containing revision otherwise an error map.
    93103*/
    94 def getSvnRevision() {
     104def getVcsRevision() {
    95105    def result = [:]
    96106
     
    116126    // Success.
    117127    return result
    118 } // getSvnRevision()
     128} // getVcsRevision()
     129
     130/**
     131* Update the in memory metadata if already loaded.
     132* Available vars: binding.variables.each { println it.key }
     133*/
     134def updateInMemoryMetadata(revision) {
     135    if(binding.variables.containsKey('metadata')) {
     136        def metadata = binding.variables['metadata']
     137        metadata['app.vcsRevision'] = '$Rev: '+revision+' $'
     138    }
     139} // updateInMemoryMetadata()
    119140
    120141/**
     
    122143* @retuns An error map if any errors.
    123144*/
    124 def writeVcsRevision(propertiesFile, revision) {
     145def writePropertiesFileRevision(propertiesFile, revision) {
    125146    def result = [:]
    126147
     
    130151    }
    131152
     153    if(!propertiesFile.isFile())
     154        return fail(code:"application.properties.file.not.found", args:[propertiesFile.getAbsoluteFile()])
     155
    132156    def revisionString = 'app.vcsRevision=\\$Rev: '+revision+' \\$'
    133     println "Updating application.properties with: ${revisionString}"
     157    println "Updating application.properties file with: ${revisionString}"
    134158
    135159    def processFileInplace = { file, Closure processText ->
     
    145169    return result
    146170
    147 } // writeVcsRevision()
     171} // writePropertiesFileRevision()
Note: See TracChangeset for help on using the changeset viewer.