devdocs/lib/docs.rb

102 lines
2.4 KiB
Ruby
Raw Normal View History

2013-10-24 20:25:52 +02:00
require 'bundler/setup'
2014-01-26 21:34:49 +01:00
Bundler.require :docs
2013-10-24 20:25:52 +02:00
require 'active_support'
2013-10-24 20:25:52 +02:00
require 'active_support/core_ext'
2013-12-07 11:46:07 +01:00
I18n.enforce_available_locales = true
2013-10-24 20:25:52 +02:00
module Docs
require 'docs/core/autoload_helper'
extend AutoloadHelper
mattr_reader :root_path
@@root_path = File.expand_path '..', __FILE__
autoload :URL, 'docs/core/url'
autoload_all 'docs/core'
autoload_all 'docs/filters/core', 'filter'
autoload_all 'docs/scrapers'
autoload_all 'docs/storage'
autoload_all 'docs/subscribers'
mattr_accessor :store_class
self.store_class = FileStore
mattr_accessor :store_path
self.store_path = File.expand_path '../public/docs', @@root_path
mattr_accessor :rescue_errors
self.rescue_errors = false
2013-10-24 20:25:52 +02:00
class DocNotFound < NameError; end
def self.all
Dir["#{root_path}/docs/scrapers/**/*.rb"].
map { |file| File.basename(file, '.rb') }.
sort!.
2016-01-17 19:30:46 +01:00
map { |name| const_get(name.camelize) }.
2013-10-24 20:25:52 +02:00
reject(&:abstract)
end
2016-01-17 19:30:46 +01:00
def self.all_versions
all.flat_map(&:versions)
end
def self.defaults
%w(css dom dom_events html http javascript).map(&method(:find))
end
def self.installed
Dir["#{store_path}/**/index.json"].
map { |file| file[%r{/([^/]*)/index\.json\z}, 1] }.
sort!.
map { |path| all_versions.find { |doc| doc.path == path } }.
compact
end
def self.find(name, version = nil)
2013-10-24 20:25:52 +02:00
const = name.camelize
2016-01-17 19:30:46 +01:00
doc = const_get(const)
if version.present?
doc = doc.versions.find { |klass| klass.version == version || klass.version_slug == version }
2016-01-17 19:30:46 +01:00
raise DocNotFound.new(%(could not find version "#{version}" for doc "#{name}"), name) unless doc
2016-04-03 15:54:29 +02:00
elsif version != false
2016-01-17 19:30:46 +01:00
doc = doc.versions.first
end
doc
2013-10-24 20:25:52 +02:00
rescue NameError => error
if error.name.to_s == const
2016-01-17 19:30:46 +01:00
raise DocNotFound.new(%(could not find doc "#{name}"), name)
2013-10-24 20:25:52 +02:00
else
raise error
end
end
2016-01-17 19:30:46 +01:00
def self.generate_page(name, version, page_id)
find(name, version).store_page(store, page_id)
2013-10-24 20:25:52 +02:00
end
def self.generate(doc, version = nil)
doc = find(doc, version) unless doc.respond_to?(:store_pages)
doc.store_pages(store)
2013-10-24 20:25:52 +02:00
end
def self.generate_manifest
2016-01-17 19:30:46 +01:00
Manifest.new(store, all_versions).store
2013-10-24 20:25:52 +02:00
end
def self.store
store_class.new(store_path)
end
extend Instrumentable
def self.install_report(*names)
names.each do |name|
const_get("#{name}_subscriber".camelize).subscribe_to(self)
end
end
end