Class: Webgen::CLI::ShowBundlesCommand

Inherits:
CmdParse::Command
  • Object
show all
Defined in:
lib/webgen/cli/commands/show_bundles.rb

Overview

The CLI command for showing extension bundles.

Instance Method Summary collapse

Constructor Details

#initializeShowBundlesCommand

:nodoc:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/webgen/cli/commands/show_bundles.rb', line 11

def initialize # :nodoc:
  super('bundles', takes_commands: false)
  short_desc('Show extension bundles')
  long_desc(<<DESC)
Shows all loaded, available and installable bundles.

Loaded bundles are already used by the website, available ones are installed but
not used and installable bundles can be installed if needed.

Hint: The global verbosity option enables additional output.
DESC
  options.on("-r", "--[no-]remote", "Use remote server for listing installable bundles") do |remote|
    @remote = remote
  end
  @remote = false
end

Instance Method Details

#executeObject

:nodoc:



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/webgen/cli/commands/show_bundles.rb', line 28

def execute # :nodoc:
  bundles = command_parser.website.ext.bundle_infos.bundles.dup
  bundles.each {|n,d| d[:state] = :loaded}

  populate_hash = lambda do |file|
    bundle_name = File.basename(File.dirname(file))
    info_file = File.join(File.dirname(file), 'info.yaml')
    bundles[bundle_name] ||= (File.file?(info_file) ? YAML.load(File.read(info_file)) : {})
    bundles[bundle_name][:state] ||= :available
    bundles[bundle_name]
  end

  $LOAD_PATH.each do |path|
    Dir.glob(File.join(path, 'webgen/bundle', '*', 'init.rb')).each do |file|
      populate_hash.call(file)
    end
  end

  Gem::Specification.map do |spec|
    [spec.name, spec.matches_for_glob("webgen/bundle/*/init.rb")]
  end.select do |name, files|
    !files.empty?
  end.each do |name, files|
    files.each do |file|
      hash = populate_hash.call(file)
      hash[:gem] = name
    end
  end

  if @remote
    Gem::SpecFetcher.fetcher.detect(:latest) do |name_tuple|
      next unless name_tuple.name =~ /webgen-(.*)-bundle/
      bundle_name = $1
      if !bundles.has_key?(bundle_name)
        bundles[bundle_name] = {:state => :installable, :gem => name_tuple.name}
      end
    end
  end

  bundles.sort do |a, b|
    if a.last[:state] == b.last[:state]
      a.first <=> b.first
    elsif a.last[:state] == :loaded
      -1
    elsif b.last[:state] == :loaded
      1
    elsif a.last[:state] == :available
      -1
    else
      1
    end
  end.each do |name, data|
    format_bundle_info(name, data)
  end
end