Module: Yap::World::Addons

Defined in:
lib/yap/world/addons.rb

Defined Under Namespace

Classes: RcFile

Class Method Summary collapse

Class Method Details

.load_directories(search_paths) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/yap/world/addons.rb', line 82

def self.load_directories(search_paths)
  Treefell['shell'].puts %|searching for addons in:\n  * #{search_paths.join("\n  * ")}|
  search_paths.map do |directory|
    Dir["#{directory}/*"].map do |d|
      if File.directory?(d)
        Treefell['shell'].puts %|addon found: #{d}|
        load_directory(d).map(&:new)
      else
        Treefell['shell'].puts %|file found in add-on search path, skipping.|
        nil
      end
    end
  end.flatten.compact
end

.load_directory(directory) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/yap/world/addons.rb', line 110

def self.load_directory(directory)
  directory = File.expand_path(directory)
  Treefell['shell'].puts "loading addon from directory: #{directory}"
  namespace = File.basename(directory).
    split(/[_-]/).
    map(&:capitalize).join
  namespace = "#{namespace}Addon"

  if Yap::World::UserAddons.const_defined?(namespace)
    raise LoadError, "#{namespace} is already defined! Failed loading #{file}"
  end

  # Create a wrapper module for every add-on. This is to eliminate
  # namespace collision.
  addon_module = Module.new do
    extend Namespace
    extend AddonMethods::ClassMethods
    const_set :Addon, Addon
  end

  Yap::World::UserAddons.const_set namespace, addon_module
  Treefell['shell'].puts "creating addon namespace: Yap::World::UserAddons::#{namespace}"

  lib_path = File.join directory, "lib"
  Treefell['shell'].puts "prepending addon path to $LOAD_PATH: #{lib_path}"
  $LOAD_PATH.unshift lib_path

  gemfiles = Dir["#{directory}/Gemfile"]
  Treefell['shell'].puts "looking for Gemfile in #{namespace} addon directory: #{directory}"
  if gemfiles.any?
    gemfiles.each do |gemfile|
      Treefell['shell'].puts "loading #{gemfile} for addon"
      eval File.read(gemfile)
    end
  else
    Treefell['shell'].puts "No Gemfile found for #{namespace} addon"
  end

  Dir["#{directory}/*.rb"].map do |addon_file|
    load_file(addon_file, namespace:namespace, dir:directory, addon_module:addon_module)
  end
ensure
  if lib_path
    Treefell['shell'].puts "Removing addon #{lib_path} path from $LOAD_PATH"
    $LOAD_PATH.delete(lib_path)
  end
end

.load_file(file, dir:, namespace:, addon_module:) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/yap/world/addons.rb', line 158

def self.load_file(file, dir:, namespace:, addon_module:)
  Treefell['shell'].puts "loading #{namespace} addon file: #{file}"
  klass_name = file.sub(dir, "").
    sub(/^#{Regexp.escape(File::Separator)}/, "").
    sub(File.extname(file.to_s), "").
    split(File::Separator).
    map{ |m| m.split(/[_-]/).map(&:capitalize).join }.
    join("::")

  addon_module.module_eval IO.read(file), file, lineno=1

  klass_name.split("::").reduce(addon_module) do |ns,name|
    if ns.const_defined?(name)
      ns.const_get(name)
    else
      raise("Did not find #{klass_name} in #{file}")
    end
  end.tap do |loaded_addon|
    Treefell['shell'].puts "loaded #{File.dirname(file)} as #{loaded_addon.inspect}"
  end
end

.load_rcfiles(files) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yap/world/addons.rb', line 70

def self.load_rcfiles(files)
  Treefell['shell'].puts %|searching for rcfiles:\n  * #{files.join("\n  * ")}|
  files.map do |file|
    if File.exists?(file)
      Treefell['shell'].puts "rcfile #{file} found, loading."
      RcFile.new file
    else
      Treefell['shell'].puts "rcfile #{file} not found, skipping."
    end
  end.flatten.compact
end

.syntax_ok?(file) ⇒ Boolean



63
64
65
66
67
68
# File 'lib/yap/world/addons.rb', line 63

def self.syntax_ok?(file)
  `ruby -c #{file}`
  ($?.exitstatus == 0).tap do |result|
    Treefell['shell'].puts "is syntax ok?(#{file.inspect}) #{result}"
  end
end