Class: Plumr::File
- Inherits:
-
Object
- Object
- Plumr::File
- Includes:
- Fitting
- Defined in:
- lib/plumr.rb,
lib/plumr/rake.rb
Overview
A fitting which constructs a specific file
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
any Files this file depends on, directly or indirectly.
-
#filename ⇒ Object
readonly
the output filename.
-
#source ⇒ Object
readonly
the plumr fitting which generates the data.
Class Method Summary collapse
Instance Method Summary collapse
-
#build ⇒ Object
Generates the file which this Plumr::File object represents, if it is not already up-to-date.
-
#cleanup ⇒ Object
Deletes the file and any dependencies which we know how to recreate.
- #cleanup_temp(filename) ⇒ Object
-
#generate(output_file) ⇒ Object
See Fixture#generate.
- #generate_temp(base_name, index) ⇒ Object
-
#initialize(filename, source = nil, *dependencies) ⇒ File
constructor
Creates a new Plumr::File object to generate the file
filename
;source
is the fixture which provides the file content;dependencies
are any other fixtures which must be up-to-date before the file can be generated and are not part of the dependencies ofsource
. - #isolate ⇒ Object
- #older_than?(other_timestamp) ⇒ Boolean
- #rakeify ⇒ Object
- #up_to_date? ⇒ Boolean
Constructor Details
#initialize(filename, source = nil, *dependencies) ⇒ File
Creates a new Plumr::File object to generate the file filename
; source
is the fixture which provides the file content; dependencies
are any other fixtures which must be up-to-date before the file can be generated and are not part of the dependencies of source
. Explicitly specifying dependencies is not required.
107 108 109 110 111 112 113 |
# File 'lib/plumr.rb', line 107 def initialize(filename, source=nil, *dependencies) @lock = Mutex.new @filename = filename.dup.freeze @source = source dependencies.unshift source if source @dependencies = dependencies.to_set.freeze end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
any Files this file depends on, directly or indirectly
99 100 101 |
# File 'lib/plumr.rb', line 99 def dependencies @dependencies end |
#filename ⇒ Object (readonly)
the output filename
96 97 98 |
# File 'lib/plumr.rb', line 96 def filename @filename end |
#source ⇒ Object (readonly)
the plumr fitting which generates the data
97 98 99 |
# File 'lib/plumr.rb', line 97 def source @source end |
Class Method Details
.rake_tasks ⇒ Object
38 |
# File 'lib/plumr/rake.rb', line 38 def self.rake_tasks ; @rake_tasks ; end |
Instance Method Details
#build ⇒ Object
Generates the file which this Plumr::File object represents, if it is not already up-to-date
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/plumr.rb', line 124 def build @dependencies.each { |d| d.build if d != @source } @lock.synchronize do unless up_to_date? if @source FileUtils.mkdir_p(::File.dirname(@filename)) @source.generate(@filename) else raise RuntimeError, "Don't know how to generate #{@filename}" end end end self end |
#cleanup ⇒ Object
Deletes the file and any dependencies which we know how to recreate. See Fixture#cleanup.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/plumr.rb', line 141 def cleanup # delete only files we know how to recreate return self unless @source begin ::File.unlink(@filename) @dependencies.each { |d| d.cleanup } rescue Errno::ENOENT end self end |
#cleanup_temp(filename) ⇒ Object
157 158 159 |
# File 'lib/plumr.rb', line 157 def cleanup_temp(filename) self end |
#generate(output_file) ⇒ Object
See Fixture#generate
116 117 118 119 120 |
# File 'lib/plumr.rb', line 116 def generate(output_file) build FileUtils.cp(@filename, output_file) if output_file != @filename self end |
#generate_temp(base_name, index) ⇒ Object
152 153 154 155 |
# File 'lib/plumr.rb', line 152 def generate_temp(base_name, index) build @filename end |
#isolate ⇒ Object
179 180 181 |
# File 'lib/plumr.rb', line 179 def isolate [ @filename, Set[self] ] end |
#older_than?(other_timestamp) ⇒ Boolean
170 171 172 173 174 175 176 177 |
# File 'lib/plumr.rb', line 170 def older_than?() begin = ::File.mtime @filename rescue Errno::ENOENT return false end < end |
#rakeify ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/plumr/rake.rb', line 40 def rakeify rake_tasks = File.rake_tasks return self if rake_tasks.include? @filename if @source rake_tasks.add @filename source, dependencies = @source.isolate dependencies.each { |d| d.rakeify } dep_files = dependencies.map { |d| d.filename } case source when String Rake::FileTask.define_task(@filename, *dep_files) do cp source, @filename end else Rake::FileTask.define_task(@filename, *dep_files) do source.generate @filename end end end self end |
#up_to_date? ⇒ Boolean
161 162 163 164 165 166 167 168 |
# File 'lib/plumr.rb', line 161 def up_to_date? begin = ::File.mtime @filename rescue Errno::ENOENT return false end @dependencies.all? { |d| d.older_than? } end |