Module: FFWD::PluginLoader

Includes:
Logging
Defined in:
lib/ffwd/plugin_loader.rb

Overview

Some crazy code to load modules from a specific directory structure.

Constant Summary collapse

MODULE_NAME =
'ffwd'

Class Method Summary collapse

Methods included from Logging

included, #log

Class Method Details

.discover_plugins(dir) ⇒ Object

Discover plugins in the specified directory that are prefixed with ‘ffwd-’.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ffwd/plugin_loader.rb', line 34

def self.discover_plugins dir
  return [] unless File.directory? dir

  Dir.foreach(dir).map do |entity|
    next if entity.start_with? "."
    next unless entity.start_with? "#{MODULE_NAME}-"
    full_path = File.join dir, entity, 'lib'
    next unless File.directory? full_path
    yield full_path
  end
end

.list_modules(module_category, blacklist, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ffwd/plugin_loader.rb', line 85

def self.list_modules module_category, blacklist, &block
  load_paths.each do |source, path|
    dir = File.join(path, MODULE_NAME, module_category)

    next unless File.directory? dir

    Dir.foreach dir do |entity|
      next if entity.start_with? "."
      next unless entity.end_with? ".rb"
      next unless File.file? File.join(dir, entity)

      base = entity.slice(0, entity.size - 3)

      if blacklist.include? base
        log.warning "Ignoring blacklisted module: #{base}"
        next
      end

      path = [MODULE_NAME, module_category, base].join('/')
      yield source, path
    end
  end
end

.load(mod, blacklist) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ffwd/plugin_loader.rb', line 109

def self.load mod, blacklist
  self.list_modules(mod.category, blacklist) do |source, m|
    begin
      require m
    rescue LoadError => e
      log.error "Failed to require '#{m}'", e
    end

    # Initialize all newly discovered plugins.
    mod.load_discovered source
  end
end

.load_pathsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ffwd/plugin_loader.rb', line 60

def self.load_paths
  return @load_paths if @load_paths

  @load_paths = []

  Gem::Specification.latest_specs(true).collect do |spec|
    @load_paths << ["from gem: #{spec.full_name}",
                    File.join(spec.full_gem_path, 'lib')]
  end

  plugin_paths.each do |path|
    @load_paths << ["from plugin directory: #{path}", path]
  end

  $LOAD_PATH.each do |path|
    @load_paths << ["from $LOAD_PATH: #{path}", path]
  end

  unless plugin_paths.empty?
    $LOAD_PATH.concat plugin_paths
  end

  return @load_paths
end

.plugin_directoriesObject



29
30
31
# File 'lib/ffwd/plugin_loader.rb', line 29

def self.plugin_directories
  @plugin_directories || []
end

.plugin_directories=(directories) ⇒ Object



25
26
27
# File 'lib/ffwd/plugin_loader.rb', line 25

def self.plugin_directories= directories
  @plugin_directories = directories
end

.plugin_pathsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ffwd/plugin_loader.rb', line 46

def self.plugin_paths
  return @plugin_paths if @plugin_paths

  @plugin_paths = []

  plugin_directories.map do |dir|
    discover_plugins(dir) do |path|
      @plugin_paths << path
    end
  end

  return @plugin_paths
end