docs.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. require 'bundler/setup'
  2. Bundler.require :docs
  3. require 'active_support'
  4. require 'active_support/core_ext'
  5. I18n.enforce_available_locales = true
  6. module Docs
  7. require 'docs/core/autoload_helper'
  8. extend AutoloadHelper
  9. mattr_reader :root_path
  10. @@root_path = File.expand_path '..', __FILE__
  11. autoload :URL, 'docs/core/url'
  12. autoload_all 'docs/core'
  13. autoload_all 'docs/filters/core', 'filter'
  14. autoload_all 'docs/scrapers'
  15. autoload_all 'docs/storage'
  16. autoload_all 'docs/subscribers'
  17. mattr_accessor :store_class
  18. self.store_class = FileStore
  19. mattr_accessor :store_path
  20. self.store_path = File.expand_path '../public/docs', @@root_path
  21. class DocNotFound < NameError; end
  22. def self.all
  23. Dir["#{root_path}/docs/scrapers/**/*.rb"].
  24. map { |file| File.basename(file, '.rb') }.
  25. sort!.
  26. map { |name| const_get(name.camelize) }.
  27. reject(&:abstract)
  28. end
  29. def self.all_versions
  30. all.flat_map(&:versions)
  31. end
  32. def self.defaults
  33. %w(css dom dom_events html http javascript).map(&method(:find))
  34. end
  35. def self.installed
  36. Dir["#{store_path}/**/index.json"].
  37. map { |file| file[%r{/([^/]*)/index\.json\z}, 1] }.
  38. sort!.
  39. map { |path| all_versions.find { |doc| doc.path == path } }.
  40. compact
  41. end
  42. def self.find(name, version = nil)
  43. const = name.camelize
  44. doc = const_get(const)
  45. if version.present?
  46. doc = doc.versions.find { |klass| klass.version == version }
  47. raise DocNotFound.new(%(could not find version "#{version}" for doc "#{name}"), name) unless doc
  48. else
  49. doc = doc.versions.first
  50. end
  51. doc
  52. rescue NameError => error
  53. if error.name.to_s == const
  54. raise DocNotFound.new(%(could not find doc "#{name}"), name)
  55. else
  56. raise error
  57. end
  58. end
  59. def self.generate_page(name, version, page_id)
  60. find(name, version).store_page(store, page_id)
  61. end
  62. def self.generate(name, version)
  63. find(name, version).store_pages(store)
  64. end
  65. def self.generate_manifest
  66. Manifest.new(store, all_versions).store
  67. end
  68. def self.store
  69. store_class.new(store_path)
  70. end
  71. extend Instrumentable
  72. def self.install_report(*names)
  73. names.each do |name|
  74. const_get("#{name}_subscriber".camelize).subscribe_to(self)
  75. end
  76. end
  77. end