docs.thor 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. class DocsCLI < Thor
  2. include Thor::Actions
  3. def self.to_s
  4. 'Docs'
  5. end
  6. def initialize(*args)
  7. require 'docs'
  8. trap('INT') { puts; exit! } # hide backtrace on ^C
  9. super
  10. end
  11. desc 'list', 'List available documentations'
  12. option :packaged, type: :boolean
  13. def list
  14. if options[:packaged]
  15. slugs = Dir[File.join(Docs.store_path, '*.tar.gz')].map { |f| File.basename(f, '.tar.gz') }
  16. names = find_docs_by_slugs(slugs).map do |doc|
  17. name = if doc.version?
  18. "#{doc.superclass.to_s.demodulize.underscore}@#{doc.version}"
  19. else
  20. doc.to_s.demodulize.underscore
  21. end
  22. end
  23. else
  24. names = Docs.all.flat_map do |doc|
  25. name = doc.to_s.demodulize.underscore
  26. if doc.versioned?
  27. doc.versions.map { |_doc| "#{name}@#{_doc.version}" }
  28. else
  29. name
  30. end
  31. end
  32. end
  33. output = names.join("\n")
  34. require 'tty-pager'
  35. TTY::Pager.new.page(output)
  36. end
  37. desc 'page (<doc> | <doc@version>) [path] [--verbose] [--debug]', 'Generate a page (no indexing)'
  38. option :verbose, type: :boolean
  39. option :debug, type: :boolean
  40. def page(name, path = '')
  41. unless path.empty? || path.start_with?('/')
  42. return puts 'ERROR: [path] must be an absolute path.'
  43. end
  44. Docs.install_report :image
  45. Docs.install_report :store if options[:verbose]
  46. if options[:debug]
  47. GC.disable
  48. Docs.install_report :filter, :request, :doc
  49. end
  50. name, version = name.split(/@|~/)
  51. if Docs.generate_page(name, version, path)
  52. puts 'Done'
  53. else
  54. puts "Failed!#{' (try running with --debug for more information)' unless options[:debug]}"
  55. end
  56. rescue Docs::DocNotFound => error
  57. handle_doc_not_found_error(error)
  58. end
  59. desc 'generate (<doc> | <doc@version>) [--verbose] [--debug] [--force] [--package]', 'Generate a documentation'
  60. option :all, type: :boolean
  61. option :verbose, type: :boolean
  62. option :debug, type: :boolean
  63. option :force, type: :boolean
  64. option :package, type: :boolean
  65. def generate(name)
  66. Docs.rescue_errors = true
  67. Docs.install_report :store if options[:verbose]
  68. Docs.install_report :scraper if options[:debug]
  69. Docs.install_report :progress_bar, :doc, :image, :requester if $stdout.tty?
  70. require 'unix_utils' if options[:package]
  71. doc = find_doc(name)
  72. if doc < Docs::UrlScraper && !options[:force]
  73. puts <<-TEXT.strip_heredoc
  74. /!\\ WARNING /!\\
  75. Some scrapers send thousands of HTTP requests in a short period of time,
  76. which can slow down the source site and trouble its maintainers.
  77. Please scrape responsibly. Don't do it unless you're modifying the code.
  78. To download the latest tested version of this documentation, run:
  79. thor docs:download #{name}\n
  80. TEXT
  81. return unless yes? 'Proceed? (y/n)'
  82. end
  83. result = if doc.version && options[:all]
  84. doc.superclass.versions.all? do |_doc|
  85. puts "==> #{_doc.version}"
  86. generate_doc(_doc, package: options[:package]).tap { puts "\n" }
  87. end
  88. else
  89. generate_doc(doc, package: options[:package])
  90. end
  91. generate_manifest if result
  92. rescue Docs::DocNotFound => error
  93. handle_doc_not_found_error(error)
  94. ensure
  95. Docs.rescue_errors = false
  96. end
  97. desc 'manifest', 'Create the manifest'
  98. def manifest
  99. generate_manifest
  100. puts 'Done'
  101. end
  102. desc 'download (<doc> <doc@version>... | --default | --installed | --all)', 'Download documentation packages'
  103. option :default, type: :boolean
  104. option :installed, type: :boolean
  105. option :all, type: :boolean
  106. def download(*names)
  107. require 'unix_utils'
  108. docs = if options[:default]
  109. Docs.defaults
  110. elsif options[:installed]
  111. Docs.installed
  112. elsif options[:all]
  113. Docs.all_versions
  114. else
  115. find_docs(names)
  116. end
  117. assert_docs(docs)
  118. download_docs(docs)
  119. generate_manifest
  120. puts 'Done'
  121. rescue Docs::DocNotFound => error
  122. handle_doc_not_found_error(error)
  123. end
  124. desc 'package <doc> <doc@version>...', 'Create documentation packages'
  125. def package(*names)
  126. require 'unix_utils'
  127. docs = find_docs(names)
  128. assert_docs(docs)
  129. docs.each(&method(:package_doc))
  130. puts 'Done'
  131. rescue Docs::DocNotFound => error
  132. handle_doc_not_found_error(error)
  133. end
  134. desc 'clean', 'Delete documentation packages'
  135. def clean
  136. File.delete(*Dir[File.join Docs.store_path, '*.tar.gz'])
  137. puts 'Done'
  138. end
  139. desc 'upload', '[private]'
  140. option :dryrun, type: :boolean
  141. option :packaged, type: :boolean
  142. def upload(*names)
  143. require 'net/sftp'
  144. if options[:packaged]
  145. slugs = Dir[File.join(Docs.store_path, '*.tar.gz')].map { |f| File.basename(f, '.tar.gz') }
  146. docs = find_docs_by_slugs(slugs)
  147. else
  148. docs = find_docs(names)
  149. end
  150. assert_docs(docs)
  151. # Verify files are present
  152. docs.each do |doc|
  153. unless Dir.exist?(File.join(Docs.store_path, doc.path))
  154. puts "ERROR: directory #{File.join(Docs.store_path, doc.path)} not found."
  155. return
  156. end
  157. unless File.exist?(File.join(Docs.store_path, "#{doc.path}.tar.gz"))
  158. puts "ERROR: package for '#{doc.slug}' documentation not found. Run 'thor docs:package #{doc.slug}' to create it."
  159. return
  160. end
  161. end
  162. # Sync files with S3 (used by the web app)
  163. puts '[S3] Begin syncing.'
  164. docs.each do |doc|
  165. puts "[S3] Syncing #{doc.path}..."
  166. cmd = "aws s3 sync #{File.join(Docs.store_path, doc.path)} s3://devdocs-documents/#{doc.path} --delete --profile devdocs"
  167. cmd << ' --dryrun' if options[:dryrun]
  168. system(cmd)
  169. end
  170. puts '[S3] Done syncing.'
  171. # Upload packages to downloads.devdocs.io (used by the "thor docs:download" command)
  172. puts '[S3 bundle] Begin uploading.'
  173. docs.each do |doc|
  174. filename = "#{doc.path}.tar.gz"
  175. puts "[S3 bundle] Uploading #{filename}..."
  176. cmd = "aws s3 cp #{File.join(Docs.store_path, filename)} s3://devdocs-downloads/#{filename} --profile devdocs"
  177. cmd << ' --dryrun' if options[:dryrun]
  178. system(cmd)
  179. end
  180. puts '[S3 bundle] Done uploading.'
  181. end
  182. desc 'commit', '[private]'
  183. option :message, type: :string
  184. option :amend, type: :boolean
  185. def commit(name)
  186. doc = Docs.find(name, false)
  187. message = options[:message] || "Update #{doc.name} documentation (#{doc.versions.first.release})"
  188. amend = " --amend" if options[:amend]
  189. system("git add assets/ *#{name}*") && system("git commit -m '#{message}'#{amend}")
  190. rescue Docs::DocNotFound => error
  191. handle_doc_not_found_error(error)
  192. end
  193. desc 'prepare_deploy', 'Internal task executed before deployment'
  194. def prepare_deploy
  195. puts 'Docs -- BEGIN'
  196. require 'open-uri'
  197. require 'thread'
  198. docs = Docs.all_versions
  199. time = Time.now.to_i
  200. mutex = Mutex.new
  201. (1..6).map do
  202. Thread.new do
  203. while doc = docs.shift
  204. dir = File.join(Docs.store_path, doc.path)
  205. FileUtils.mkpath(dir)
  206. ['index.json', 'meta.json'].each do |filename|
  207. json = "https://documents.devdocs.io/#{doc.path}/#{filename}?#{time}"
  208. begin
  209. open(json) do |file|
  210. mutex.synchronize do
  211. path = File.join(dir, filename)
  212. File.write(path, file.read)
  213. end
  214. end
  215. rescue => e
  216. puts "Docs -- Failed to download #{json}!"
  217. throw e
  218. end
  219. end
  220. puts "Docs -- Downloaded #{doc.slug}"
  221. end
  222. end
  223. end.map(&:join)
  224. puts 'Docs -- Generating manifest...'
  225. generate_manifest
  226. puts 'Docs -- DONE'
  227. end
  228. private
  229. def find_doc(name)
  230. name, version = name.split(/@|~/)
  231. if version == 'all'
  232. Docs.find(name, false).versions
  233. else
  234. Docs.find(name, version)
  235. end
  236. end
  237. def find_docs(names)
  238. names.flat_map {|name| find_doc(name)}
  239. end
  240. def find_docs_by_slugs(slugs)
  241. slugs.flat_map do |slug|
  242. slug, version = slug.split(/~/)
  243. Docs.find_by_slug(slug, version)
  244. end
  245. end
  246. def assert_docs(docs)
  247. if docs.empty?
  248. puts 'ERROR: called with no arguments.'
  249. puts 'Run "thor list" for usage patterns.'
  250. exit
  251. end
  252. end
  253. def handle_doc_not_found_error(error)
  254. puts %(ERROR: #{error}.)
  255. puts 'Run "thor docs:list" to see the list of docs and versions.'
  256. end
  257. def generate_doc(doc, package: nil)
  258. if Docs.generate(doc)
  259. package_doc(doc) if package
  260. puts 'Done'
  261. true
  262. else
  263. puts "Failed!#{' (try running with --debug for more information)' unless options[:debug]}"
  264. false
  265. end
  266. end
  267. def download_docs(docs)
  268. # Don't allow downloaded files to be created as StringIO
  269. require 'open-uri'
  270. OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax')
  271. OpenURI::Buffer.const_set 'StringMax', 0
  272. require 'thread'
  273. length = docs.length
  274. mutex = Mutex.new
  275. i = 0
  276. (1..4).map do
  277. Thread.new do
  278. while doc = docs.shift
  279. status = begin
  280. download_doc(doc)
  281. 'OK'
  282. rescue => e
  283. "FAILED (#{e.class}: #{e.message})"
  284. end
  285. mutex.synchronize { puts "(#{i += 1}/#{length}) #{doc.name}#{ " #{doc.version}" if doc.version} #{status}" }
  286. end
  287. end
  288. end.map(&:join)
  289. end
  290. def download_doc(doc)
  291. target_path = File.join(Docs.store_path, doc.path)
  292. URI.open "https://downloads.devdocs.io/#{doc.path}.tar.gz" do |file|
  293. FileUtils.mkpath(target_path)
  294. file.close
  295. tar = UnixUtils.gunzip(file.path)
  296. dir = UnixUtils.untar(tar)
  297. FileUtils.rm_rf(target_path)
  298. FileUtils.mv(dir, target_path)
  299. FileUtils.rm(file.path)
  300. end
  301. end
  302. def package_doc(doc)
  303. path = File.join Docs.store_path, doc.path
  304. if File.exist?(path)
  305. tar = UnixUtils.tar(path)
  306. gzip = UnixUtils.gzip(tar)
  307. FileUtils.mv(gzip, "#{path}.tar.gz")
  308. FileUtils.rm(tar)
  309. else
  310. puts %(ERROR: can't find "#{doc.name}" documentation files.)
  311. end
  312. end
  313. def generate_manifest
  314. Docs.generate_manifest
  315. end
  316. end