class Nanoc::CLI::Commands::Edit

Constants

DEFAULT_ASSETS_LOCATION
DEFAULT_ASSETS_PATH_PREFIX
DEFAULT_BACKEND_PATH_PREFIX
DEFAULT_DATA_SOURCE_INDEX
DEFAULT_HANDLER_NAME

Public Instance Methods

run() click to toggle source
# File lib/nanoc-edit.rb, line 31
def run
  # require_site
  site = nil
  if Nanoc::Site.cwd_is_nanoc_site?
    site = Nanoc::Site.new('.')
  else
    raise ::Nanoc::Errors::GenericTrivial, 'The current working directory does not seem to be a nanoc site.'
  end

  require 'rack'
  require 'rack/polly'
  require 'adsf'

  # Get handler
  if options.key?(:handler)
    handler = Rack::Handler.get(options[:handler])
  else
    begin
      handler = Rack::Handler.get(DEFAULT_HANDLER_NAME)
    rescue LoadError
      handler = Rack::Handler::WEBrick
    end
  end

  polly_config = site.config[:polly] ? site.config[:polly] : {}
  with_assets = options[:assets] ? true : false
  assets_location = polly_config[:assets_location] ? polly_config[:assets_location] : DEFAULT_ASSETS_LOCATION
  if with_assets && !File.directory?(File.expand_path(assets_location))
    raise ArgumentError, "assets_location '#{assets_location}' is not a directory."
  end

  # Set options
  options_for_rack = {
    Port: (options[:port] || 3000).to_i,
    Host: (options[:host] || '0.0.0.0')
  }

  # Build app
  require 'nanoc/polly/backend'

  rack_polly_options = {}

  # set required options
  data_source_index = polly_config[:data_source_index] ? polly_config[:data_source_index] : DEFAULT_DATA_SOURCE_INDEX
  backend_path_prefix = polly_config[:backend_path_prefix] ? polly_config[:backend_path_prefix] : DEFAULT_BACKEND_PATH_PREFIX
  assets_path_prefix = polly_config[:assets_path_prefix] ? polly_config[:assets_path_prefix] : DEFAULT_ASSETS_PATH_PREFIX
  rack_polly_options = rack_polly_options.merge({
    assets_path_prefix: assets_path_prefix,
    backend_path_prefix: backend_path_prefix
  })

  # set image storage options
  image_storage = polly_config[:image_storage] ? polly_config[:image_storage] : false
  if image_storage && image_storage.to_s == 'uploadcare'
    raise ArgumentError, "uploadcare_api_key not configured" unless polly_config[:uploadcare_api_key]
    rack_polly_options = rack_polly_options.merge({
      image_storage: image_storage,
      uploadcare_api_key: polly_config[:uploadcare_api_key]
    })
  end

  app = Rack::Builder.new do
    map assets_path_prefix do
      run Rack::File.new(assets_location)
    end if with_assets
    map '/' do
      use Rack::CommonLogger
      use Rack::ShowExceptions
      use Rack::Lint
      use Rack::Head
      use Adsf::Rack::IndexFileFinder, root: site.config[:output_dir]
      use Rack::Polly, rack_polly_options
      run Rack::File.new(site.config[:output_dir])
    end
    map backend_path_prefix do
      Nanoc::Polly::Config.data_source_index = data_source_index
      run Nanoc::Polly::Backend.new
    end
  end.to_app

  # Run autocompiler
  handler.run(app, options_for_rack)
end