class Middleman::Cli::CDN

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/middleman-cdn/commands.rb, line 21
def self.exit_on_failure?
  true
end
say_status(cdn, status, newline: true, header: true, wait_enter: false) click to toggle source
# File lib/middleman-cdn/commands.rb, line 67
def self.say_status(cdn, status, newline: true, header: true, wait_enter: false)
  message = ""
  message << "#{ANSI.green { :cdn.to_s.rjust(12)} }  #{ANSI.yellow{ cdn } unless cdn.nil? }" if header
  message << " " if header && cdn
  message << status if status
  print message
  STDIN.noecho(&:gets) if wait_enter
  puts "" if newline
end

Public Instance Methods

cdn_invalidate(*args) click to toggle source
# File lib/middleman-cdn/commands.rb, line 25
def cdn_invalidate(*args)
  begin
    options, files = if args.first && args.first.respond_to?(:filter)
      [args.first, args.drop(1)]
    else
      [Middleman::CDN::Extension.options, args]
    end

    if options.nil?
      self.class.say_status(nil, ANSI.red{ "Error: You need to activate the cdn extension in config.rb.\n#{example_configuration}" })
      return
    end
    options.filter ||= /.*/

    if cdns.all? { |cdn| options.public_send(cdn.key.to_sym).nil? }
      self.class.say_status(nil, ANSI.red{ "Error: You must specify a config for one of the supported CDNs.\n#{example_configuration}" })
      raise
    end

    unless files.empty?
      files = normalize_files(files)
      message = "Invalidating #{files.count} files:"
    else
      files = normalize_files(list_files(options.filter))
      message = "Invalidating #{files.count} files with filter: #{options.filter.source}"
    end
    self.class.say_status(nil, message)
    files.each { |file| self.class.say_status(nil, " • #{file}") }

    return if files.empty?

    invalidate_all = does_filter_match_all(options.filter)

    cdns_keyed.each do |cdn_key, cdn|
      cdn_options = options.public_send(cdn_key.to_sym)
      cdn.new.invalidate(cdn_options, files, all: invalidate_all) unless cdn_options.nil?
    end
  rescue SystemExit, Interrupt
    self.class.say_status(nil, nil, header: false)
  end
end

Private Instance Methods

cdns() click to toggle source
# File lib/middleman-cdn/commands.rb, line 79
def cdns
  [
    CloudFlareCDN,
    CloudFrontCDN,
    FastlyCDN,
    MaxCDN,
    RackspaceCDN
  ]
end
cdns_keyed() click to toggle source
# File lib/middleman-cdn/commands.rb, line 89
def cdns_keyed
  Hash[cdns.map { |cdn| [cdn.key, cdn] }]
end
does_filter_match_all(filter) click to toggle source
# File lib/middleman-cdn/commands.rb, line 105
def does_filter_match_all(filter)
  [".*", ".+"].include?(filter.source)
end
example_configuration() click to toggle source
# File lib/middleman-cdn/commands.rb, line 93
      def example_configuration
        <<-TEXT

Example configuration:
activate :cdn do |cdn|
#{cdns.map(&:example_configuration).join}
  cdn.filter            = /\.html/i  # default /.*/
  cdn.after_build       = true  # default is false
end
        TEXT
      end
list_files(filter) click to toggle source
# File lib/middleman-cdn/commands.rb, line 109
def list_files(filter)
  Dir.chdir('build/') do
    Dir.glob('**/*', File::FNM_DOTMATCH).tap do |files|
      # Remove directories
      files.reject! { |f| File.directory?(f) }

      # Remove files that do not match filter
      files.reject! { |f| f !~ filter }
    end
  end
end
normalize_files(files) click to toggle source
# File lib/middleman-cdn/commands.rb, line 121
def normalize_files(files)
  normalized_files = []

  # Add directories of index.html files since they have to be
  # invalidated as well if :directory_indexes is active
  files.each do |file|
    normalized_files << file

    # For /dir/index.html add /dir/
    file_dir = file.sub(/\bindex\.html\z/, '')
    normalized_files << file_dir if file_dir != file

    # For /dir/index.html add /dir
    file_dir_no_slash = file.sub(/\/index\.html\z/, '')
    normalized_files << file_dir_no_slash if file_dir_no_slash != file
  end

  # Add leading slash
  normalized_files.map { |f| f.start_with?('/') ? f : "/#{f}" }
end