Class: Middleman::Cli::CDN

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/middleman-cdn/commands.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


21
22
23
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
# 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

Instance Method Details

#cdn_invalidate(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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