1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
# frozen_string_literal: true
require "yard"
require "webrick"
namespace :apidocs do
desc "Fetch a gem version from RubyGems, build the docs"
task :gen_version, [:version] do |t, args|
# GITHUB_REF comes from GitHub Actions
version = args[:version] || ENV["GITHUB_REF"] || raise("A version is required")
puts "Building docs for #{version}"
# GitHub Actions gives the full tag name
if version.start_with?("refs/tags/")
version = version[10..-1]
end
if version.start_with?("v")
version = version[1..-1]
end
Dir.mktmpdir do
puts "Fetching graphql-#{version}"
system("gem fetch graphql --version=#{version}")
system("gem unpack graphql-#{version}.gem")
system("rm graphql-#{version}.gem")
Dir.chdir("graphql-#{version}") do
# Copy it into gh-pages for publishing
# and locally for previewing
push_dest = File.expand_path("../gh-pages/api-doc/#{version}")
local_dest = File.expand_path("../guides/_site/api-doc/#{version}")
puts "Creating directories: #{push_dest.inspect}, #{local_dest.inspect}"
FileUtils.mkdir_p(push_dest)
FileUtils.mkdir_p(local_dest)
system("yardoc")
puts "Copying from #{Dir.pwd}/doc to #{push_dest}"
copy_entry "doc", push_dest
puts "Copying from #{Dir.pwd}/doc to #{local_dest}"
copy_entry "doc", local_dest
end
end
puts "Successfully generated docs for #{version}"
end
end
namespace :site do
desc "View the documentation site locally"
task serve: [] do # if you need api docs, add `:build_doc` to the list of dependencies
require "jekyll"
options = {
"source" => File.expand_path("guides"),
"destination" => File.expand_path("guides/_site"),
"watch" => true,
"serving" => true
}
# Generate the site in server mode.
puts "Running Jekyll..."
Jekyll::Commands::Build.process(options)
Jekyll::Commands::Serve.process(options)
end
desc "Get the gh-pages branch locally, make sure it's up-to-date"
task :fetch_latest do
# Ensure the gh-pages dir exists so we can generate into it.
puts "Checking for gh-pages dir..."
unless File.exist?("./gh-pages")
puts "Creating gh-pages dir..."
sh "git clone git@github.com:rmosolgo/graphql-ruby gh-pages"
end
# Ensure latest gh-pages branch history.
Dir.chdir("gh-pages") do
sh "git checkout gh-pages"
sh "git pull origin gh-pages"
end
end
desc "Remove all generated HTML (making space to re-generate)"
task :clean_html do
# Proceed to purge all files in case we removed a file in this release.
puts "Cleaning gh-pages directory..."
purge_exclude = [
'gh-pages/.',
'gh-pages/..',
'gh-pages/.git',
'gh-pages/.gitignore',
'gh-pages/api-doc',
]
FileList["gh-pages/{*,.*}"].exclude(*purge_exclude).each do |path|
sh "rm -rf #{path}"
end
end
desc "Build guides/ into gh-pages/ with Jekyll"
task :build_html do
# Copy site to gh-pages dir.
puts "Building site into gh-pages branch..."
ENV['JEKYLL_ENV'] = 'production'
require "jekyll"
Jekyll::Commands::Build.process({
"source" => File.expand_path("guides"),
"destination" => File.expand_path("gh-pages"),
"sass" => { "style" => "compressed" }
})
File.write('gh-pages/.nojekyll', "Prevent GitHub from running Jekyll")
end
desc "Commit new docs"
task :commit_changes do
puts "Committing and pushing to GitHub Pages..."
sha = `git rev-parse HEAD`.strip
Dir.chdir('gh-pages') do
system "git status"
system "git add ."
system "git status"
system "git commit --allow-empty -m 'Updating to #{sha}.'"
end
end
desc "Push docs to gh-pages branch"
task :push_commit do
Dir.chdir('gh-pages') do
sh "git push origin gh-pages"
end
end
desc "Commit the local site to the gh-pages branch and publish to GitHub Pages"
task publish: [:build_doc, :update_search_index, :fetch_latest, :clean_html, :build_html, :commit_changes, :push_commit]
YARD::Rake::YardocTask.new(:prepare_yardoc)
task build_doc: :prepare_yardoc do
require_relative "../../lib/graphql/version"
def to_rubydoc_url(path)
"/api-doc/#{GraphQL::VERSION}/" + path
.gsub("::", "/") # namespaces
.sub(/#(.+)$/, "#\\1-instance_method") # instance methods
.sub(/\.(.+)$/, "#\\1-class_method") # class methods
end
DOC_TEMPLATE = <<-PAGE
---
layout: doc_stub
search: true
title: %{title}
url: %{url}
rubydoc_url: %{url}
doc_stub: true
---
%{documentation}
PAGE
puts "Preparing YARD docs @ v#{GraphQL::VERSION} for search index..."
registry = YARD::Registry.load!(".yardoc")
files_target = "guides/yardoc"
FileUtils.rm_rf(files_target)
FileUtils.mkdir_p(files_target)
# Get docs for all classes and modules
docs = registry.all(:class, :module)
docs.each do |code_object|
begin
# Skip private classes and modules
if code_object.visibility == :private
next
end
rubydoc_url = to_rubydoc_url(code_object.path)
page_content = DOC_TEMPLATE % {
title: code_object.path,
url: rubydoc_url,
documentation: code_object.format.gsub(/-{2,}/, " ").gsub(/^\s+/, ""),
}
filename = code_object.path.gsub(/\W+/, "_")
filepath = "guides/yardoc/#{filename}.md"
File.write(filepath, page_content)
rescue StandardError => err
puts "Error on: #{code_object.path}"
puts err
puts err.backtrace
end
end
puts "Wrote #{docs.size} YARD docs to #{files_target}."
end
desc "Update the Algolia search index used for graphql-ruby.org"
task :update_search_index do
if !ENV["ALGOLIA_API_KEY"]
warn("Can't update search index without ALGOLIA_API_KEY; Search will be out-of-date.")
else
system("bundle exec jekyll algolia push --source=./guides")
end
end
end
|