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.

Defined Under Namespace

Classes: FileTime

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, #copy, #create, #delete, #exist?, #intersects?, #map, #rebase, #roots, #to_paths, #to_s, #touch, #with

Constructor Details

#initialize(files) ⇒ State

Initialize file state tracking.

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
# File 'lib/build/files/state.rb', line 48

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.



60
61
62
# File 'lib/build/files/state.rb', line 60

def added
  @added
end

#changedObject (readonly)

Returns the value of attribute changed.



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

def changed
  @changed
end

#filesObject (readonly)

Returns the value of attribute files.



58
59
60
# File 'lib/build/files/state.rb', line 58

def files
  @files
end

#missingObject (readonly)

Returns the value of attribute missing.



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

def missing
  @missing
end

#newest_timeObject (readonly)

Returns the value of attribute newest_time.



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

def newest_time
  @newest_time
end

#oldest_timeObject (readonly)

Returns the value of attribute oldest_time.



124
125
126
# File 'lib/build/files/state.rb', line 124

def oldest_time
  @oldest_time
end

#removedObject (readonly)

Returns the value of attribute removed.



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

def removed
  @removed
end

#timesObject (readonly)

Returns the value of attribute times.



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

def times
  @times
end

Class Method Details

.dirty?(inputs, outputs) ⇒ Boolean

Check if outputs are dirty with respect to inputs.

Returns:

  • (Boolean)


175
176
177
# File 'lib/build/files/state.rb', line 175

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)


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

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

Check if the state is empty.

Returns:

  • (Boolean)


135
136
137
# File 'lib/build/files/state.rb', line 135

def empty?
	@times.empty?
end

#inspectObject

Generate a string representation for debugging.



141
142
143
# File 'lib/build/files/state.rb', line 141

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

#missing?Boolean

Check if any files are missing.

Returns:

  • (Boolean)


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

def missing?
	!@missing.empty?
end

#update!Object

Update the state by checking all files for changes.



71
72
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
# File 'lib/build/files/state.rb', line 71

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