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

Returns a new instance of State.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
# File 'lib/build/files/state.rb', line 37

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.



49
50
51
# File 'lib/build/files/state.rb', line 49

def added
  @added
end

#changedObject (readonly)

Returns the value of attribute changed.



51
52
53
# File 'lib/build/files/state.rb', line 51

def changed
  @changed
end

#filesObject (readonly)

Returns the value of attribute files.



47
48
49
# File 'lib/build/files/state.rb', line 47

def files
  @files
end

#missingObject (readonly)

Returns the value of attribute missing.



52
53
54
# File 'lib/build/files/state.rb', line 52

def missing
  @missing
end

#newest_timeObject (readonly)

Returns the value of attribute newest_time.



112
113
114
# File 'lib/build/files/state.rb', line 112

def newest_time
  @newest_time
end

#oldest_timeObject (readonly)

Returns the value of attribute oldest_time.



111
112
113
# File 'lib/build/files/state.rb', line 111

def oldest_time
  @oldest_time
end

#removedObject (readonly)

Returns the value of attribute removed.



50
51
52
# File 'lib/build/files/state.rb', line 50

def removed
  @removed
end

#timesObject (readonly)

Returns the value of attribute times.



54
55
56
# File 'lib/build/files/state.rb', line 54

def times
  @times
end

Class Method Details

.dirty?(inputs, outputs) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/build/files/state.rb', line 152

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)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/build/files/state.rb', line 127

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)


118
119
120
# File 'lib/build/files/state.rb', line 118

def empty?
	@times.empty?
end

#inspectObject



122
123
124
# File 'lib/build/files/state.rb', line 122

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

#missing?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/build/files/state.rb', line 114

def missing?
	!@missing.empty?
end

#update!Object



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/build/files/state.rb', line 58

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