-
-
Save uehaj/11244046 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.codehaus.groovy.tools.groovydoc.GroovyDocTool | |
import org.codehaus.groovy.groovydoc.GroovyRootDoc | |
import static groovy.io.FileType.* | |
import org.codehaus.groovy.tools.groovydoc.* | |
includeTargets << grailsScript("Init") | |
/** | |
* Generate i18n messages.properties from the domain class comments. | |
* support version: grails-1.3.0 or above | |
* usage: grails generate-i18n-labels ja | |
*/ | |
target(main: "Generate i18n messages.properties from the domain class comments") { | |
depends(parseArguments) | |
String lang = argsMap['params']?argsMap['params'][0]:'ja' | |
File domainDir = new File('grails-app','domain') | |
List srcFiles =[] | |
String domainSrcPath = domainDir.absolutePath+'/' | |
domainDir.eachFileRecurse FILES,{ srcFiles<<(it.absolutePath - domainSrcPath) } | |
srcFiles = srcFiles.grep{it==~/^.*\.groovy$/} | |
def docTool = new GroovyDocTool([domainSrcPath] as String[]) | |
docTool.add(srcFiles) | |
GroovyRootDoc root = docTool.getRootDoc() | |
def classes = root.classes() | |
def excludeProp = ['constraints','hasMany','belongsTo','mapping','hasOne', 'mappedBy', 'transients'] | |
def generateI18nPropLabelFor = {SimpleGroovyClassDoc d,writer-> | |
def comment = d.getRawCommentText() | |
def classLabel = comment.find(/@label(.*)/){it[1].trim()}?:d.firstSentenceCommentText() | |
classLabel = classLabel.replaceAll("\n.*", "") | |
def className = d.name() | |
def propName = className[0].toLowerCase() + className[1..-1] | |
def labelKey = "${propName}.label" | |
writer << "\n\n${propName}.label=${classLabel}\n" | |
d.properties().each{p-> | |
if(!excludeProp.contains(p.name())) { | |
writer << "${propName}.${p.name()}.label=${p.commentText().trim()?:p.name()}\n" | |
} | |
} | |
} | |
String fileName = "${basedir}/grails-app/i18n/fieldlabel${lang.length()>0?'_'+lang:''}.properties" | |
String backupFileName = fileName + ".bak" | |
def backupFile = new File(backupFileName) | |
if (backupFile.exists() && !new File(fileName).renameTo(new File(backupFileName))) { | |
println "failed to create backup file \"$backupFileName\", but continue on" | |
if (backupFile.exists()) { | |
backupFile.delete() | |
} | |
if (!new File(fileName).renameTo(new File(backupFileName))) { | |
println "failed to create backup file \"$backupFileName\", abort" | |
System.exit(1) | |
} | |
} | |
new File(fileName).withWriter { writer -> | |
println "write to $fileName." | |
classes.each { | |
generateI18nPropLabelFor(it, writer) | |
} | |
} | |
} | |
setDefaultTarget(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment