-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.rb
More file actions
94 lines (73 loc) · 2.04 KB
/
Copy pathwatch.rb
File metadata and controls
94 lines (73 loc) · 2.04 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'digest'
class FileVersion
attr_accessor :path, :hash
def initialize(path, hash)
@path = path
@hash = hash
end
def to_s
"#{@path} #{@hash}"
end
end
def get_files(regexes)
[] if regexes == nil || regexes.empty?
regexes.collect {|regex| Dir.glob(regex)}.flatten.compact
end
def get_olds(file)
return [] if File.size?(file) == nil
File.foreach(file).collect { |line|
space_index = line.rindex(' ')
FileVersion.new(line[0, space_index], line[space_index + 1, line.size - 1])
}
end
def get_news(files)
files.collect { |file|
FileVersion.new(file, Digest::MD5.file(file).hexdigest)
}
end
def persist_file_hash(output, files)
File.open(output, 'w') do |of|
files.each do |file|
of.write(file.to_s + "\n")
end
end
end
def changed
$build.changed false
end
def join_instruction_text(fvs)
fvs.collect{|n| n.path}.join(' ')
end
class Build
# Gets all files changed since last run of changed.
def changed(add = true)
files = get_files($build.config.watching)
olds = get_olds($build.config.file_hash_file)
news = get_news(files)
persist_file_hash($build.config.file_hash_file, news) # save the current version before we screw with the list of ones.
changeds = []
news.each do |n| # O(N^2)
oindex = olds.index {|o| o.path == n.path}
if oindex == nil || olds[oindex].hash.strip != n.hash.strip
changeds << n
end
end
@instructions << BuildInstruction.new(16, join_instruction_text(changeds)) unless !add # the input files have spaces between them
changeds # return what was actually changed.
end
def watch(what)
@config.watching.push(what)
end
def watch_recursive(what)
watch("./**/" + what)
end
def watch_only(what)
@config.watching.clear
watch what
end
def objects(add = true)
objs = get_news(Dir.glob(config.obj_dir + "/*." + config.obj_file_ext))
@instructions << BuildInstruction.new(16, join_instruction_text(objs)) unless !add # the input files have spaces between them
objs
end
end