-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformatMonitor.rb
executable file
·72 lines (53 loc) · 1.49 KB
/
formatMonitor.rb
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
require 'digest'
CONFIG_PATH = Dir.glob( File.expand_path("~/*Sett*/uncrustify/uncrustify.cfg") )[0]
if(!CONFIG_PATH) then abort("Cannot find uncrustify configuration") end
class Application
def initialize
@files = {}
end
def files ; return @files ; end
def files= value ; @files = value ; end
def run
i = -1;
# Get the file filters from CLI or default
fileFilters = (ARGV[0] || "*.*pp,*.h*").split(",")
# Format all the file filters
#EX: -name "*.*pp" -or -name "*.h"
fileFilters.each_index { |i| fileFilters[i] = %{-name "#{fileFilters[i]}"} }
# Join the filters with -or for `find`
filters = fileFilters.join(" -or ")
while(true) do
i = (i + 1) % 15
# Every 15, get a new file listing
if(i == 0) then
files = `find . -maxdepth 20 #{filters}`.split("\n")
files.each { |path| touchFile(path) }
end
files.each { |path|
mtime = File.mtime(path)
if(mtime != @files[path][:mtime]) then
if(md5(path) != @files[path][:md5]) then
`uncrustify -c #{CONFIG_PATH} -f "#{path}" -o "#{path}.formatted"`
if(md5(path) != md5("#{path}.formatted")) then
`cat "#{path}.formatted" > "#{path}"`
end
touchFile(path)
File.delete("#{path}.formatted")
end
end
}
sleep 1
end
end
def md5 path
return Digest::MD5.file(path).hexdigest
end
def touchFile path
@files[path] = {
:mtime => File.mtime(path) ,
:md5 => md5(path) ,
}
end
end
Application.new.run