Skip to content

Commit

Permalink
Perf work and Parallel builds
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Jan 22, 2016
1 parent b8c0fd3 commit c7a4618
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@ master
* Fix asset_host in combination with Google Analytics snippet. #1751
* Show an error message when git CLI is not available. #1765
* Correctly show file names of GZIP'ed assets. #1364
* Build file output is not parallel-ized! Use `middleman build --no-parallel` to disable.

# 4.0.0

Expand Down
2 changes: 1 addition & 1 deletion middleman-cli/bin/middleman
Expand Up @@ -4,7 +4,7 @@ require 'middleman-core/profiling'
if ARGV.include? '--profile'
Middleman::Profiling.profiler = Middleman::Profiling::RubyProfProfiler.new
end
Middleman::Profiling.start
# Middleman::Profiling.start

require "middleman-core/load_paths"
Middleman.setup_load_paths
Expand Down
4 changes: 4 additions & 0 deletions middleman-cli/lib/middleman-cli/build.rb
Expand Up @@ -14,6 +14,10 @@ class Build < Thor::Group
type: :boolean,
default: true,
desc: 'Remove orphaned files from build (--no-clean to disable)'
class_option :parallel,
type: :boolean,
default: true,
desc: 'Output files in parallel (--no-parallel to disable)'
class_option :glob,
type: :string,
aliases: '-g',
Expand Down
4 changes: 2 additions & 2 deletions middleman-core/lib/middleman-core/application.rb
Expand Up @@ -184,7 +184,7 @@ def root_path
end
}, 'Callbacks that can exclude paths from the sitemap'

define_setting :skip_build_clean, proc { |p| [/\.git/].any? { |r| r.match(p) } }, 'Whether some paths should not be removed during a clean build.'
define_setting :skip_build_clean, proc { |p| [/\.git/].any? { |r| p =~ r } }, 'Whether some paths should not be removed during a clean build.'

define_setting :watcher_disable, false, 'If the Listen watcher should not run'
define_setting :watcher_force_polling, false, 'If the Listen watcher should run in polling mode'
Expand Down Expand Up @@ -314,7 +314,7 @@ def evaluate_configuration!

# Clean up missing Tilt exts
def prune_tilt_templates!
::Tilt.mappings.each do |key, _|
::Tilt.mappings.each_key do |key|
begin
::Tilt[".#{key}"]
rescue LoadError, NameError
Expand Down
32 changes: 24 additions & 8 deletions middleman-core/lib/middleman-core/builder.rb
@@ -1,6 +1,7 @@
require 'pathname'
require 'fileutils'
require 'tempfile'
require 'parallel'
require 'middleman-core/rack'
require 'middleman-core/callback_manager'
require 'middleman-core/contracts'
Expand Down Expand Up @@ -36,6 +37,7 @@ def initialize(app, opts={})

@glob = opts.fetch(:glob)
@cleaning = opts.fetch(:clean)
@parallel = opts.fetch(:parallel, true)

rack_app = ::Middleman::Rack.new(@app).to_app
@rack = ::Rack::MockRequest.new(rack_app)
Expand Down Expand Up @@ -63,16 +65,18 @@ def run!
prerender_css
end

::Middleman::Profiling.start

::Middleman::Util.instrument 'builder.output' do
output_files
end

::Middleman::Profiling.report('build')

::Middleman::Util.instrument 'builder.clean' do
clean! if @cleaning
end

::Middleman::Profiling.report('build')

::Middleman::Util.instrument 'builder.after' do
@app.execute_callbacks(:after_build, [self])
end
Expand All @@ -87,9 +91,8 @@ def prerender_css
logger.debug '== Prerendering CSS'

css_files = ::Middleman::Util.instrument 'builder.prerender.output' do
@app.sitemap.resources
.select { |resource| resource.ext == '.css' }
.each(&method(:output_resource))
resources = @app.sitemap.resources.select { |resource| resource.ext == '.css' }
output_resources(resources)
end

::Middleman::Util.instrument 'builder.prerender.check-files' do
Expand Down Expand Up @@ -117,7 +120,20 @@ def output_files
resources = resources.select { |resource| File.fnmatch(@glob, resource.destination_path) }
end

resources.each(&method(:output_resource))
output_resources(resources)
end

Contract ResourceList => ResourceList
def output_resources(resources)
cleaned_paths = if @parallel
::Parallel.map(resources, &method(:output_resource))
else
resources.map(&method(:output_resource))
end

cleaned_paths.each { |p| @to_clean.delete(p) } if @cleaning

resources
end

# Figure out the correct event mode.
Expand Down Expand Up @@ -181,7 +197,7 @@ def export_file!(output_file, source)
# Try to output a resource and capture errors.
# @param [Middleman::Sitemap::Resource] resource The resource.
# @return [void]
Contract IsA['Middleman::Sitemap::Resource'] => Any
Contract IsA['Middleman::Sitemap::Resource'] => Maybe[Pathname]
def output_resource(resource)
output_file = nil

Expand Down Expand Up @@ -218,7 +234,7 @@ def output_resource(resource)
output_file
end

@to_clean.delete(Pathname(cleaned_name))
Pathname(cleaned_name)
end

# Get a list of all the paths in the destination folder and save them
Expand Down
4 changes: 2 additions & 2 deletions middleman-core/lib/middleman-core/core_extensions/data.rb
Expand Up @@ -203,11 +203,11 @@ def key?(key)
def to_h
data = {}

store.each do |k, _|
store.each_key do |k|
data[k] = data_for_path(k)
end

callbacks.each do |k, _|
callbacks.each_key do |k|
data[k] = data_for_path(k)
end

Expand Down
Expand Up @@ -113,7 +113,7 @@ def stylesheet_link_tag(*sources)
path_options = {}
path_options[:relative] = options.delete(:relative) if options.key?(:relative)

sources.flatten.inject(::ActiveSupport::SafeBuffer.new) do |all, source|
sources.flatten.reduce(::ActiveSupport::SafeBuffer.new) do |all, source|
all << tag(:link, {
href: asset_path(:css, source, path_options)
}.update(options))
Expand All @@ -127,7 +127,7 @@ def javascript_include_tag(*sources)
path_options = {}
path_options[:relative] = options.delete(:relative) if options.key?(:relative)

sources.flatten.inject(::ActiveSupport::SafeBuffer.new) do |all, source|
sources.flatten.reduce(::ActiveSupport::SafeBuffer.new) do |all, source|
all << content_tag(:script, nil, {
src: asset_path(:js, source, path_options)
}.update(options))
Expand All @@ -152,7 +152,7 @@ def auto_tag(asset_ext, asset_dir=nil)
# If the basename of the request as no extension, assume we are serving a
# directory and join index_file to the path.
path = File.join(asset_dir, current_resource.path)
path = path.sub(/#{Regexp.escape(File.extname(path))}$/, ".#{asset_ext}")
path = path[0..-(File.extname(path).length + 1)] + ".#{asset_ext}"

yield path if sitemap.find_resource_by_path(path)
end
Expand Down Expand Up @@ -191,7 +191,7 @@ def page_classes(path=current_path.dup, options={})
# @param [Hash] options Data to pass through.
# @return [String]
def asset_path(kind, source, options={})
options_with_resource = options.merge(current_resource: current_resource)
options_with_resource = {}.merge!(options).merge!(current_resource: current_resource)
::Middleman::Util.asset_path(app, kind, source, options_with_resource)
end

Expand All @@ -202,15 +202,15 @@ def asset_path(kind, source, options={})
# @param [Hash] options Additional options.
# @return [String] The fully qualified asset url
def asset_url(path, prefix='', options={})
options_with_resource = options.merge(current_resource: current_resource)
options_with_resource = {}.merge!(options).merge!(current_resource: current_resource)
::Middleman::Util.asset_url(app, path, prefix, options_with_resource)
end

# Given a source path (referenced either absolutely or relatively)
# or a Resource, this will produce the nice URL configured for that
# path, respecting :relative_links, directory indexes, etc.
def url_for(path_or_resource, options={})
options_with_resource = options.merge(current_resource: current_resource)
options_with_resource = {}.merge!(options).merge!(current_resource: current_resource)
::Middleman::Util.url_for(app, path_or_resource, options_with_resource)
end

Expand Down
2 changes: 1 addition & 1 deletion middleman-core/lib/middleman-core/core_extensions/i18n.rb
Expand Up @@ -203,7 +203,7 @@ def path_root(locale)
if (options[:mount_at_root] == locale) || (options[:mount_at_root].nil? && locales[0] == locale)
'/'
else
replacement = options[:locale_map].fetch(locale, locale)
replacement = options[:locale_map][locale] || locale
options[:path].sub(':locale', replacement.to_s).sub(':lang', replacement.to_s) # Backward compat
end
end
Expand Down
Expand Up @@ -108,7 +108,7 @@ def call(env)
ignore = rewriter.fetch(:ignore)
next if ignore.any? { |r| should_ignore?(r, full_asset_path) }

rewrite_ignore = Array(rewriter.fetch(:rewrite_ignore, []))
rewrite_ignore = Array(rewriter[:rewrite_ignore] || [])
next if rewrite_ignore.any? { |i| ::Middleman::Util.path_match(i, path) }

proc = rewriter.fetch(:proc)
Expand All @@ -132,7 +132,7 @@ def call(env)
def should_ignore?(validator, value)
if validator.is_a? Regexp
# Treat as Regexp
!value.match(validator).nil?
!!(value =~ validator)
elsif validator.respond_to? :call
# Treat as proc
validator.call(value)
Expand Down
2 changes: 1 addition & 1 deletion middleman-core/lib/middleman-core/extension.rb
Expand Up @@ -432,7 +432,7 @@ def generate_resources(resources)
{}
end

sum.merge(resource_definitions)
sum.merge!(resource_definitions)
end

resources + generator_defs.map do |path, g|
Expand Down
2 changes: 1 addition & 1 deletion middleman-core/lib/middleman-core/extensions.rb
Expand Up @@ -106,7 +106,7 @@ def load(name)
# A flattened list of all extensions which are automatically activated
# @return [Array<Symbol>] A list of extension names which are automatically activated.
def auto_activated
@auto_activate.values.map(&:to_a).flatten.map(&:name)
@auto_activate.values.map(&:to_a).flat_map(&:name)
end

# @api private
Expand Down
Expand Up @@ -19,7 +19,7 @@ def image_tag(path, params={})
real_path = path.dup
real_path = File.join(config[:images_dir], real_path) unless real_path.start_with?('/')

file = app.files.find(:source, real_path) || app.files.find(:source, real_path.gsub(/^\//, ''))
file = app.files.find(:source, real_path) || app.files.find(:source, real_path.sub(/^\//, ''))

if file && file[:full_path].exist?
begin
Expand Down
2 changes: 1 addition & 1 deletion middleman-core/lib/middleman-core/extensions/minify_css.rb
Expand Up @@ -23,7 +23,7 @@ def ready
class SassCompressor
def self.compress(style, options={})
root_node = ::Sass::SCSS::CssParser.new(style, 'middleman-css-input', 1).parse
root_node.options = options.merge(style: :compressed)
root_node.options = {}.merge!(options).merge!(style: :compressed)
root_node.render.strip
end
end
Expand Down
2 changes: 1 addition & 1 deletion middleman-core/lib/middleman-core/file_renderer.rb
Expand Up @@ -51,7 +51,7 @@ def render(locs, opts, context, &block)

# Merge per-extension options from config
extension = File.extname(path)
options = opts.merge(options_for_ext(extension))
options = {}.merge!(opts).merge!(options_for_ext(extension))
options[:outvar] ||= '@_out_buf'
options[:context] = context
options.delete(:layout)
Expand Down
2 changes: 1 addition & 1 deletion middleman-core/lib/middleman-core/renderers/haml.rb
Expand Up @@ -30,7 +30,7 @@ def prepare
end

def evaluate(scope, locals, &block)
options = @options.merge(filename: eval_file, line: line, context: @context || scope)
options = {}.merge!(@options).merge!(filename: eval_file, line: line, context: @context || scope)
@engine = ::Haml::Engine.new(data, options)
output = @engine.render(scope, locals, &block)

Expand Down
2 changes: 1 addition & 1 deletion middleman-core/lib/middleman-core/renderers/less.rb
Expand Up @@ -26,7 +26,7 @@ def prepare
if ::Less.const_defined? :Engine
@engine = ::Less::Engine.new(data)
else
parser = ::Less::Parser.new(options.merge(filename: eval_file, line: line, paths: ['.', File.dirname(eval_file)]))
parser = ::Less::Parser.new({}.merge!(options).merge!(filename: eval_file, line: line, paths: ['.', File.dirname(eval_file)]))
@engine = parser.parse(data)
end
end
Expand Down
4 changes: 2 additions & 2 deletions middleman-core/lib/middleman-core/renderers/sass.rb
Expand Up @@ -81,7 +81,7 @@ def sass_options
filename: eval_file,
line: line,
syntax: syntax,
custom: (options[:custom] || {}).merge(
custom: {}.merge!(options[:custom] || {}).merge!(
middleman_context: ctx.app,
current_resource: ctx.current_resource
)
Expand All @@ -97,7 +97,7 @@ def sass_options
more_opts[:css_filename] = file.sub(/\.s[ac]ss$/, '')
end

options.merge(more_opts)
{}.merge!(options).merge!(more_opts)
end
end

Expand Down
11 changes: 4 additions & 7 deletions middleman-core/lib/middleman-core/sources.rb
Expand Up @@ -189,6 +189,7 @@ def find(types, path, glob=false)
array_of_types = Array(types)

watchers
.lazy
.select { |d| array_of_types.include?(d.type) }
.map { |d| d.find(path, glob) }
.reject(&:nil?)
Expand All @@ -202,9 +203,7 @@ def find(types, path, glob=false)
# @return [Boolean]
Contract Or[Symbol, ArrayOf[Symbol], SetOf[Symbol]], String => Bool
def exists?(types, path)
watchers
.select { |d| Array(types).include?(d.type) }
.any? { |d| d.exists?(path) }
watchers.any? { |d| Array(types).include?(d.type) && d.exists?(path) }
end

# Check if a file for a given type exists.
Expand All @@ -214,9 +213,7 @@ def exists?(types, path)
# @return [Boolean]
Contract Or[Symbol, ArrayOf[Symbol], SetOf[Symbol]], String => Maybe[HANDLER]
def watcher_for_path(types, path)
watchers
.select { |d| Array(types).include?(d.type) }
.find { |d| d.exists?(path) }
watchers.detect { |d| Array(types).include?(d.type) && d.exists?(path) }
end

# Manually check for new files
Expand Down Expand Up @@ -317,7 +314,7 @@ def ignored?(path)
def matches?(validator, file)
path = file[:relative_path]
if validator.is_a? Regexp
!!validator.match(path.to_s)
!!(path.to_s =~ validator)
else
!!validator.call(path, @app)
end
Expand Down
Expand Up @@ -322,7 +322,7 @@ def valid?(file)
if @only.empty?
!@ignored.call(file)
else
@only.any? { |reg| reg.match(file[:relative_path].to_s) }
@only.any? { |reg| file[:relative_path].to_s =~ reg }
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions middleman-core/lib/middleman-core/util.rb
Expand Up @@ -79,7 +79,7 @@ def path_match(matcher, path)
path == matcher
end
when matcher.respond_to?(:match)
!matcher.match(path).nil?
!!(path =~ matcher)
when matcher.respond_to?(:call)
matcher.call(path)
else
Expand Down Expand Up @@ -357,7 +357,7 @@ def rewrite_paths(body, _path, exts, &_block)
begin
uri = ::Addressable::URI.parse(asset_path)

if uri.relative? && uri.host.nil? && !asset_path.match(/^[^\/].*[a-z]+\.[a-z]+\/.*/) && (result = yield(asset_path))
if uri.relative? && uri.host.nil? && !(asset_path =~ /^[^\/].*[a-z]+\.[a-z]+\/.*/) && (result = yield(asset_path))
"#{opening_character}#{result}"
else
match
Expand Down Expand Up @@ -456,10 +456,11 @@ def relative_path_from_resource(curr_resource, resource_url, relative)
Contract String => String
def step_through_extensions(path)
while ::Tilt[path]
yield File.extname(path) if block_given?
ext = File.extname(path)
yield ext if block_given?

# Strip templating extensions as long as Tilt knows them
path = path.sub(/#{::Regexp.escape(File.extname(path))}$/, '')
path = path[0..-(ext.length + 1)]
end

yield File.extname(path) if block_given?
Expand Down
1 change: 1 addition & 0 deletions middleman-core/middleman-core.gemspec
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |s|
s.add_dependency('tilt', ['~> 1.4.1'])
s.add_dependency('erubis')
s.add_dependency('fast_blank')
s.add_dependency('parallel')

# Helpers
s.add_dependency('activesupport', ['~> 4.2'])
Expand Down

0 comments on commit c7a4618

Please sign in to comment.