Module: Hairballs::LibraryHelpers

Included in:
Plugin, Theme
Defined in:
lib/hairballs/library_helpers.rb

Overview

Helpers specifying and requiring dependencies for Themes and Plugins.

Instance Method Summary collapse

Instance Method Details

#do_bundler_extendingObject

Add all gems in the global gemset to the $LOAD_PATH so they can be used even in places like ‘rails console’.

TODO: Use #find_latest_gem for each of #libraries.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hairballs/library_helpers.rb', line 51

def do_bundler_extending
  vputs "[**:#{@name}] #do_bundler_extending: #{@libraries}"

  if defined?(::Bundler)
    vputs "[**:#{@name}] #do_bundler_extending: Libraries: #{@libraries}"

    all_global_gem_paths = Dir.glob("#{Gem.dir}/gems/*")

    all_global_gem_paths.each do |p|
      next unless @libraries.any? { |l| p.include?(l) }
      gem_path = "#{p}/lib"
      # rubocop:disable Metrics/LineLength
      vputs "[**:#{@name}] #do_bundler_extending: Adding to $LOAD_PATH: #{gem_path}"
      # rubocop:enable
      $LOAD_PATH.unshift(gem_path)
    end
  else
    vputs %([**:#{@name}] #do_bundler_extending: Bundler not defined. Skipping.)
  end
end

#find_latest_gem(gem_name) ⇒ String

Path to the highest version of the gem with the given gem.

Parameters:

  • gem_name (String)

Returns:

  • (String)

    The path to the latest install of gem_name.



41
42
43
44
45
# File 'lib/hairballs/library_helpers.rb', line 41

def find_latest_gem(gem_name)
  the_gem = Dir.glob("#{Gem.dir}/gems/#{gem_name}-*")

  the_gem.empty? ? nil : the_gem.sort.last
end

#libraries(libs = nil) ⇒ Object

Parameters:

  • libs (Array<String>) (defaults to: nil)


9
10
11
12
13
# File 'lib/hairballs/library_helpers.rb', line 9

def libraries(libs=nil)
  return @libraries if @libraries && libs.nil?

  @libraries = libs ? libs : yield([])
end

#require_librariesObject

Requires #libraries on load. If they’re not installed, install them. If it can’t be installed, move on to the next.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hairballs/library_helpers.rb', line 17

def require_libraries
  return if @libraries.nil?

  missing_dependencies = []

  @libraries.each do |lib|
    begin
      vrequire(lib)
    rescue LoadError
      # rubocop:disable Metrics/LineLength
      vputs "[**:#{@name}](#{lib}) Unable to require; adding to install list..."
      # rubocop:enable Metrics/LineLength
      missing_dependencies << lib
    end
  end

  requirer = new_dependency_requirer(dependency_installer)
  install_missing_dependencies(missing_dependencies, requirer).resume
end

#undo_bundler_extendingObject

Undo the stuff that was done in #do_bundler_extending.

TODO: Use #find_latest_gem for each of #libraries.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hairballs/library_helpers.rb', line 75

def undo_bundler_extending
  if defined?(::Bundler)
    all_global_gem_paths = Dir.glob("#{Gem.dir}/gems/*")

    all_global_gem_paths.each do |p|
      gem_path = "#{p}/lib"
      $LOAD_PATH.delete(gem_path)
    end
  else
    vputs %([**:#{@name}] Bundler not defined.  Skipping.)
  end
end