Groovy pollozó megoldás

Original date: 2013-09-26

Update: 2016-02-23

Azaz, ha jó eszközeid vannak, akkor mindenre van megoldás.

Adott egy speciális probléma. Van egy olyan pici project, ami maven-t használ és egy speciális bytekód utófeldolgozást igényel. Sajnos a maven féle process-classes nem nagyon támogatott az Eclipse pluginomban (feketepont a maven-nek és az Eclipse-nek is).

Ekkor marad parancssor. De ha lúd, akkor legyen kövér: legyen continuous teszting (szerinte ez még a Test First-nél is jobb).

Eddig bemutattam Java-s megoldásokat. Most megmutatom, hogy mennyire egyszerű problémáról van szó, ha megfelelő eszköz van a kezünkben, ez esetben a Groovy

 1def job = {
 2  String pomPath = /-f <full path to pom>\pom.xml/
 3  String  command = /<full path to maven home>\bin\mvn.bat test /
 4  String fullCommand = command+pomPath
 5  println "Executing "+command
 6  def proc = fullCommand.execute()
 7  proc.waitForProcessOutput(System.out, System.err)
 8}
 9
10params =  [
11  closure: job,
12  sleepInterval: 1000,
13  dirPath: /<path to dir to monitor>\src/
14]
15
16import groovy.transform.Canonical;
17
18@Canonical
19class AutoRunner{
20  def closure
21  def sleepInterval = 3000
22  // running for 8 hours then stop automatically if checking every 3 seconds
23  def nrOfRepeat = 9600 
24  def dirPath = "."
25  long lastModified = 0
26
27  def autorun(){
28    println "Press CTRL+C to stop..."
29    println this
30    def to_run = {
31      while(nrOfRepeat--){
32        sleep(sleepInterval)
33        if(anyChange()){
34          closure()
35        }
36      }
37    } as Runnable
38    Thread runner = new Thread(to_run)
39    runner.start()
40  }
41  
42  def boolean anyChange(){
43    def max = lastModified
44    new File(dirPath).eachFileRecurse {
45      if(it.lastModified() > max){
46        max = it.lastModified()
47      }
48    }
49    if(max > lastModified){
50      lastModified = max
51      return true
52    }
53    return false;
54  }
55}
56
57new AutoRunner(params).autorun()

Egyszerűen nagyszerű.

A következő lépés az lehetne, hogy az anyChange() metódus helyett is closure-t használunk.

Ez az implementáció pollozást használ, ami nagyszerű kis projectek esetében, de egy nagyobb könyvtárhierarchiánál, már észrevehetően lelassúl.

Update: Mivel rengeteg helyen használom a mindennapokban kicsit letisztult. Itt egy másik implementáció, ami sokkal esszenciálisabb és persze rövidebb.

 1import groovy.io.FileType
 2long lastModified = 0
 3File dir = new File('./content')
 4while (true){
 5  long max = 0;
 6  dir.eachFileRecurse (FileType.FILES) { file ->
 7    if(max < file.lastModified())
 8      max = file.lastModified()
 9  }
10  if(lastModified < max){
11    recompile()
12    lastModified = max
13  }
14  Thread.sleep(3000)
15}
16
17def recompile(){
18  println  "recompile ${new Date()}"
19  def proc = "c:\\environment\\apps\\ruby-1.9.3\\bin\\nanoc.bat  compile".execute()
20  println proc.text
21}

Directory Watcher
Feb 23, 2016
comments powered by Disqus

Links

Cool

RSS