Skip to content

Instantly share code, notes, and snippets.

@webdevwilson
Created January 19, 2011 15:56
Show Gist options
  • Save webdevwilson/786348 to your computer and use it in GitHub Desktop.
Save webdevwilson/786348 to your computer and use it in GitHub Desktop.
Groovy code to monitor a directory and copy modified files to a different directory
String.metaClass.div = { f -> delegate + File.separator + f }
def extensions = ['.jpg','.css','.js','.jsp']
def source = new File( args.length > 0 ? args[0] : '.' ).absolutePath
def dest = new File( args.length > 1 ? args[1] : discoverJBoss() ).absolutePath
println "Monitoring ${source}"
println "Any modified files will be copied to ${dest}"
def files = buildFileList(source, extensions)
println "Discovered ${files.size()} files:"
while( true ) {
Thread.sleep(1500)
files.each { filename, oldModified ->
def lastModified = new File(filename).lastModified()
if( lastModified > oldModified ) {
files[filename] = lastModified
// execute build and print results
def path = dest + filename.substring( source.length() )
println "File changed '${filename}' copying to ${path}"
new AntBuilder().copy( file: filename, tofile: path )
}
}
// check for added files
def currentFiles = buildFileList(source, extensions)
currentFiles.each { filename, modified ->
if(!files[filename]) {
println "New file '${filename}' found."
files[filename] = 0
}
}
}
def discoverJBoss() {
def jbossHome = System.getenv('JBOSS_HOME')
if( !jbossHome ) {
throw new Exception('JBOSS_HOME not set')
}
jbossHome = new File( jbossHome ).absolutePath
def jbossTemp = jbossHome / 'server' / 'default' / 'tmp'
def modifiedTime = 0
def newestModified
new File(jbossTemp).listFiles().each {
if( it.directory && new File( it.absolutePath / 'CoreSourceUI.war' ).exists() && it.lastModified() > modifiedTime ) {
modifiedTime = it.lastModified()
newestModified = new File( it.absolutePath / 'CoreSourceUI.war' ).absolutePath
}
}
newestModified
}
def buildFileList(dirname, extensions) {
def extRE = ".+(\\${extensions.join('|\\')})"
def files = [:]
def dir = new File(dirname)
if( dir.directory ) {
dir.listFiles().each {
def path = it.absolutePath
if( !path.matches('.+\\.svn.+') ) {
if( it.directory ) {
files << buildFileList(it.absolutePath, extensions)
} else if( path.matches(extRE) ) {
files[it.absolutePath] = it.lastModified()
}
}
}
}
return files
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment