class Middleman::Cli::Build

The CLI Build class

def binary_encode(string)

def binary_encode(string)
  if string.respond_to?(:force_encoding)
    string.force_encoding("ascii-8bit")
  end
  string
end

def build

Returns:
  • (void) -
def build
  if !ENV["MM_ROOT"]
    raise Thor::Error, "Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
  end
  # Use Rack::Test for inspecting a running server for output
  require "rack"
  require "rack/test"
  require 'find'
  @debugging = Middleman::Cli::Base.respond_to?(:debugging) && Middleman::Cli::Base.debugging
  @had_errors = false
  self.class.shared_instance(options["verbose"], options["instrument"])
  self.class.shared_rack
  opts = {}
  opts[:glob]  = options["glob"]  if options.has_key?("glob")
  opts[:clean] = options["clean"] if options.has_key?("clean")
  action GlobAction.new(self, opts)
  if @had_errors && !@debugging
    cmd = "middleman build --verbose"
    cmd = "bundle exec '#{cmd}'" if defined?(Bundler)
    self.shell.say "There were errors during this build, re-run with `#{cmd}` to see the full exception."
  end
  exit(1) if @had_errors
  self.class.shared_instance.run_hook :after_build, self
end

def exit_on_failure?

def exit_on_failure?
  true
end

def handle_error(file_name, response, e=Thor::Error.new(response))

def handle_error(file_name, response, e=Thor::Error.new(response))
  @had_errors = true
  say_status :error, file_name, :red
  if self.debugging
    raise e
    exit(1)
  elsif options["verbose"]
    self.shell.error(response)
  end
end

def render_to_file(resource)

Returns:
  • (String) - The full path of the file that was written

Parameters:
  • resource (Middleman::Sitemap::Resource) --
def render_to_file(resource)
  build_dir = self.class.shared_instance.build_dir
  output_file = File.join(build_dir, resource.destination_path)
  if resource.binary?
    if !File.exists?(output_file)
      say_status :create, output_file, :green
    elsif FileUtils.compare_file(resource.source_file, output_file)
      say_status :identical, output_file, :blue
      return output_file
    else
      say_status :update, output_file, :yellow
    end
    FileUtils.mkdir_p(File.dirname(output_file))
    FileUtils.cp(resource.source_file, output_file)
  else
    begin
      response = self.class.shared_rack.get(URI.escape(resource.destination_path))
      if response.status == 200
        create_file(output_file, binary_encode(response.body))
      else
        handle_error(output_file, response.body)
      end
    rescue => e
      handle_error(output_file, "#{e}\n#{e.backtrace.join("\n")}", e)
    end
  end
  output_file
end

def shared_instance(verbose=false, instrument=false)

Returns:
  • (Middleman::Application) -
def shared_instance(verbose=false, instrument=false)
  @_shared_instance ||= ::Middleman::Application.server.inst do
    set :environment, :build
    logger(verbose ? 0 : 1, instrument)
  end
end

def shared_rack

Returns:
  • (Rack::Test::Session) -
def shared_rack
  @_shared_rack ||= ::Rack::Test::Session.new(shared_server.to_rack_app)
end

def shared_server

Returns:
  • (Middleman::Application) -
def shared_server
  @_shared_server ||= shared_instance.class
end

def source_root

Set the root path to the Middleman::Application's root
def source_root
  shared_instance.root
end