-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-copy-on-change.groovy
49 lines (38 loc) · 1.43 KB
/
file-copy-on-change.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor
import org.apache.commons.io.monitor.FileAlterationMonitor
import org.apache.commons.io.monitor.FileAlterationObserver
@Grab("commons-io:commons-io:2.11.0")
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeUnit
if (args.length != 2) {
println "Incorrect usage, example:"
println "groovy file-copy-on-change root/path/to/be/monitored root/path/to/copy/to"
return
}
def monitoredPath = new File(args[0])
def targetPath = new File(args[1])
if (!monitoredPath.exists()) {
println "${monitoredPath.absolutePath} does not exist!"
return
}
if (!targetPath.exists()) {
println "${targetPath.absolutePath} does not exist!"
return
}
println "Starts watching: ${monitoredPath.absolutePath}"
def timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")
char BELL = 7
def observer = new FileAlterationObserver(monitoredPath)
observer.addListener(new FileAlterationListenerAdaptor() {
@Override
void onFileChange(File file) {
def copyPath = new File(targetPath, file.absolutePath - monitoredPath.absolutePath)
copyPath.bytes = file.bytes
println "[${LocalTime.now().format(timeFormatter)}] ${file.absolutePath} --> ${copyPath.absolutePath}"
println BELL
}
})
def monitor = new FileAlterationMonitor(TimeUnit.SECONDS.toMillis(5))
monitor.addObserver(observer)
monitor.start()