Class: Middleman::Builder
- Inherits:
-
Object
- Object
- Middleman::Builder
- Extended by:
- Forwardable
- Includes:
- Contracts
- Defined in:
- lib/middleman-core/builder.rb
Constant Summary collapse
- SORT_ORDER =
Sort order, images, fonts, js/css and finally everything else.
%w(.png .jpeg .jpg .gif .bmp .svg .svgz .webp .ico .woff .woff2 .otf .ttf .eot .js .css)
Constants included from Contracts
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Make app & events available to
after_build
callbacks. -
#events ⇒ Object
readonly
Make app & events available to
after_build
callbacks. -
#thor ⇒ Object
Reference to the Thor class.
Instance Method Summary collapse
-
#Any
Get a list of all the paths in the destination folder and save them for comparison against the files we build in this cycle.
- #binary_encode(string) ⇒ Object
-
#Bool ⇒ Boolean
Run the build phase.
- #clean ⇒ Object
- #export_file!(output_file, source) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Builder
constructor
Create a new Builder instance.
- #output_files ⇒ Object
- #output_resource(resource) ⇒ Object
-
#Pathname
Actually export the file.
- #prerender_css ⇒ Object
- #queue_current_paths ⇒ Object
-
#ResourceList ⇒ Array<Resource>
Find all the files we need to output and do so.
- #run! ⇒ Object
- #trigger(event_type, target, extra = nil) ⇒ Object
- #which_mode(output_file, source) ⇒ Object
- #write_tempfile(output_file, contents) ⇒ Object
Methods included from Contracts
Constructor Details
#initialize(app, opts = {}) ⇒ Builder
Create a new Builder instance.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/middleman-core/builder.rb', line 28 def initialize(app, opts={}) @app = app @source_dir = Pathname(File.join(@app.root, @app.config[:source])) @build_dir = Pathname(@app.config[:build_dir]) if @build_dir..relative_path_from(@source_dir).to_s =~ /\A[.\/]+\Z/ raise ":build_dir (#{@build_dir}) cannot be a parent of :source_dir (#{@source_dir})" end @glob = opts.fetch(:glob) @cleaning = opts.fetch(:clean) rack_app = ::Middleman::Rack.new(@app).to_app @rack = ::Rack::MockRequest.new(rack_app) @callbacks = ::Middleman::CallbackManager.new @callbacks.install_methods!(self, [:on_build_event]) end |
Instance Attribute Details
#app ⇒ Object (readonly)
Make app & events available to after_build
callbacks.
14 15 16 |
# File 'lib/middleman-core/builder.rb', line 14 def app @app end |
#events ⇒ Object (readonly)
Make app & events available to after_build
callbacks.
14 15 16 |
# File 'lib/middleman-core/builder.rb', line 14 def events @events end |
#thor ⇒ Object
Reference to the Thor class.
17 18 19 |
# File 'lib/middleman-core/builder.rb', line 17 def thor @thor end |
Instance Method Details
#Any
This method returns an undefined value.
Get a list of all the paths in the destination folder and save them for comparison against the files we build in this cycle
196 |
# File 'lib/middleman-core/builder.rb', line 196 Contract Any |
#binary_encode(string) ⇒ Object
230 231 232 233 |
# File 'lib/middleman-core/builder.rb', line 230 def binary_encode(string) string.force_encoding('ascii-8bit') if string.respond_to?(:force_encoding) string end |
#Bool ⇒ Boolean
Run the build phase.
49 |
# File 'lib/middleman-core/builder.rb', line 49 Contract Bool |
#clean ⇒ Object
222 223 224 225 226 227 |
# File 'lib/middleman-core/builder.rb', line 222 def clean @to_clean.each do |f| FileUtils.rm(f) trigger(:deleted, f) end end |
#export_file!(output_file, source) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/middleman-core/builder.rb', line 133 def export_file!(output_file, source) source = write_tempfile(output_file, source.to_s) if source.is_a? String method, source_path = if source.is_a? Tempfile [FileUtils.method(:mv), source.path] else [FileUtils.method(:cp), source.to_s] end mode = which_mode(output_file, source_path) if mode == :created || mode == :updated FileUtils.mkdir_p(output_file.dirname) method.call(source_path, output_file.to_s) end source.unlink if source.is_a? Tempfile trigger(mode, output_file) end |
#output_files ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/middleman-core/builder.rb', line 88 def output_files logger.debug '== Building files' @app.sitemap.resources .sort_by { |resource| SORT_ORDER.index(resource.ext) || 100 } .reject { |resource| resource.ext == '.css' } .select { |resource| !@glob || File.fnmatch(@glob, resource.destination_path) } .each(&method(:output_resource)) end |
#output_resource(resource) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/middleman-core/builder.rb', line 158 def output_resource(resource) output_file = @build_dir + resource.destination_path.gsub('%20', ' ') begin if resource.binary? export_file!(output_file, resource.file_descriptor[:full_path]) else response = @rack.get(URI.escape(resource.request_path)) # If we get a response, save it to a tempfile. if response.status == 200 export_file!(output_file, binary_encode(response.body)) else @has_error = true trigger(:error, output_file, response.body) end end rescue => e @has_error = true trigger(:error, output_file, "#{e}\n#{e.backtrace.join("\n")}") end return unless @cleaning return unless output_file.exist? # handle UTF-8-MAC filename on MacOS cleaned_name = if RUBY_PLATFORM =~ /darwin/ output_file.to_s.encode('UTF-8', 'UTF-8-MAC') else output_file end @to_clean.delete(Pathname(cleaned_name)) end |
#Pathname
This method returns an undefined value.
Actually export the file.
102 |
# File 'lib/middleman-core/builder.rb', line 102 Contract Pathname, String => Symbol |
#prerender_css ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/middleman-core/builder.rb', line 73 def prerender_css logger.debug '== Prerendering CSS' css_files = @app.sitemap.resources.select do |resource| resource.ext == '.css' end.each(&method(:output_resource)) logger.debug '== Checking for Compass sprites' # Double-check for compass sprites @app.files.find_new_files! @app.sitemap.ensure_resource_list_updated! css_files end |
#queue_current_paths ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/middleman-core/builder.rb', line 197 def queue_current_paths @to_clean = [] return unless File.exist?(@app.config[:build_dir]) paths = ::Middleman::Util.all_files_under(@app.config[:build_dir]).map do |path| Pathname(path) end @to_clean = paths.select do |path| path.realpath.relative_path_from(@build_dir.realpath).to_s !~ /\/\./ || path.to_s =~ /\.(htaccess|htpasswd)/ end # handle UTF-8-MAC filename on MacOS @to_clean = @to_clean.map do |path| if RUBY_PLATFORM =~ /darwin/ Pathname(path.to_s.encode('UTF-8', 'UTF-8-MAC')) else Pathname(path) end end end |
#ResourceList ⇒ Array<Resource>
Find all the files we need to output and do so.
72 |
# File 'lib/middleman-core/builder.rb', line 72 Contract ResourceList |
#run! ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/middleman-core/builder.rb', line 50 def run! @has_error = false @events = {} @app.execute_callbacks(:before_build, [self]) queue_current_paths if @cleaning prerender_css output_files clean if @cleaning ::Middleman::Profiling.report('build') @app.execute_callbacks(:after_build, [self]) !@has_error end |
#trigger(event_type, target, extra = nil) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/middleman-core/builder.rb', line 236 def trigger(event_type, target, extra=nil) @events[event_type] ||= [] @events[event_type] << target execute_callbacks(:on_build_event, [event_type, target, extra]) end |
#which_mode(output_file, source) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/middleman-core/builder.rb', line 103 def which_mode(output_file, source) if !output_file.exist? :created else FileUtils.compare_file(source.to_s, output_file.to_s) ? :identical : :updated end end |
#write_tempfile(output_file, contents) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/middleman-core/builder.rb', line 116 def write_tempfile(output_file, contents) file = Tempfile.new([ File.basename(output_file), File.extname(output_file) ]) file.binmode file.write(contents) file.close File.chmod(0644, file) file end |