Class: Build::Files::State

Inherits:
List
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/build/files/state.rb

Overview

A stateful list of files captured at a specific time, which can then be checked for changes.

Constant Summary

Constants inherited from List

List::NONE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from List

#+, #-, #==, coerce, #create, #delete, #exist?, #intersects?, #map, #rebase, #roots, #to_paths, #to_s, #touch, #with

Constructor Details

#initialize(files) ⇒ State

Returns a new instance of State.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
# File 'lib/build/files/state.rb', line 52

def initialize(files)
	raise ArgumentError.new("Invalid files list: #{files}") unless Files::List === files
	
	@files = files
	
	@times = {}
	
	update!
end

Instance Attribute Details

#addedObject (readonly)

Returns the value of attribute added.



64
65
66
# File 'lib/build/files/state.rb', line 64

def added
  @added
end

#changedObject (readonly)

Returns the value of attribute changed.



66
67
68
# File 'lib/build/files/state.rb', line 66

def changed
  @changed
end

#filesObject (readonly)

Returns the value of attribute files.



62
63
64
# File 'lib/build/files/state.rb', line 62

def files
  @files
end

#missingObject (readonly)

Returns the value of attribute missing.



67
68
69
# File 'lib/build/files/state.rb', line 67

def missing
  @missing
end

#newest_timeObject (readonly)

Returns the value of attribute newest_time.



127
128
129
# File 'lib/build/files/state.rb', line 127

def newest_time
  @newest_time
end

#oldest_timeObject (readonly)

Returns the value of attribute oldest_time.



126
127
128
# File 'lib/build/files/state.rb', line 126

def oldest_time
  @oldest_time
end

#removedObject (readonly)

Returns the value of attribute removed.



65
66
67
# File 'lib/build/files/state.rb', line 65

def removed
  @removed
end

#timesObject (readonly)

Returns the value of attribute times.



69
70
71
# File 'lib/build/files/state.rb', line 69

def times
  @times
end

Class Method Details

.dirty?(inputs, outputs) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/build/files/state.rb', line 167

def self.dirty?(inputs, outputs)
	outputs.dirty?(inputs)
end

Instance Method Details

#dirty?(inputs) ⇒ Boolean

Are these (output) files dirty with respect to the given inputs?

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/build/files/state.rb', line 142

def dirty?(inputs)
	if self.missing?
		return true
	end
	
	# If there are no inputs or no outputs, we are always clean:
	if inputs.empty? or self.empty?
		return false
	end
	
	oldest_output_time = self.oldest_time
	newest_input_time = inputs.newest_time
	
	if newest_input_time and oldest_output_time
		# We are dirty if any inputs are newer (bigger) than any outputs:
		if newest_input_time > oldest_output_time
			return true
		else
			return false
		end
	end
	
	return true
end

#empty?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/build/files/state.rb', line 133

def empty?
	@times.empty?
end

#inspectObject



137
138
139
# File 'lib/build/files/state.rb', line 137

def inspect
	"<State Added:#{@added} Removed:#{@removed} Changed:#{@changed} Missing:#{@missing}>"
end

#missing?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/build/files/state.rb', line 129

def missing?
	!@missing.empty?
end

#update!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/build/files/state.rb', line 73

def update!
	last_times = @times
	@times = {}
	
	@added = []
	@removed = []
	@changed = []
	@missing = []
	
	file_times = []
	
	@files.each do |path|
		# When processing the same path twice (perhaps by accident), we should skip it otherwise it might cause issues when being deleted from last_times multuple times.
		next if @times.include? path
		
		if File.exist?(path)
			modified_time = File.mtime(path)
		
			if last_time = last_times.delete(path)
				# Path was valid last update:
				if modified_time != last_time
					@changed << path
					
					# puts "Changed: #{path}"
				end
			else
				# Path didn't exist before:
				@added << path
				
				# puts "Added: #{path}"
			end
		
			@times[path] = modified_time
		
			unless File.directory?(path)
				file_times << FileTime.new(path, modified_time)
			end
		else
			@missing << path
			
			# puts "Missing: #{path}"
		end
	end
	
	@removed = last_times.keys
	# puts "Removed: #{@removed.inspect}" if @removed.size > 0
	
	@oldest_time = file_times.min
	@newest_time = file_times.max
	
	return @added.size > 0 || @changed.size > 0 || @removed.size > 0 || @missing.size > 0
end