Add preview_docs rake task for generating API and Guides static site

This commit is contained in:
zzak 2024-02-13 12:53:39 +09:00
parent 0f9aaa5ca9
commit d850686939
No known key found for this signature in database
GPG Key ID: 213927DFCF4FF102
4 changed files with 217 additions and 0 deletions

1
.gitignore vendored

@ -9,6 +9,7 @@
/dist/
/doc/
/guides/output/
/preview/
Brewfile.lock.json
debug.log*
node_modules/

@ -5,6 +5,7 @@ require "net/http"
$:.unshift __dir__
require "tasks/release"
require "railties/lib/rails/api/task"
require "tools/preview_docs"
desc "Build gem files for all projects"
task build: "all:build"
@ -50,6 +51,20 @@ else
Rails::API::StableTask.new("rdoc")
end
desc "Generate documentation for previewing"
task :preview_docs do
FileUtils.mkdir_p("preview")
PreviewDocs.new.render("preview")
require "guides/rails_guides"
Rake::Task[:rdoc].invoke
FileUtils.cp_r("doc/rdoc", "preview/api")
FileUtils.cp_r("guides/output", "preview/guides")
system("tar -czf preview.tar.gz preview")
end
desc "Bump all versions to match RAILS_VERSION"
task update_versions: "all:update_versions"

92
tools/preview_docs.rb Normal file

@ -0,0 +1,92 @@
# frozen_string_literal: true
require "erb"
require "cgi"
puts "required tools/preview_docs"
# How to test:
#
# export BUILDKITE_COMMIT="c8b601a225"
# export BUILDKITE_BUILD_CREATOR="zzak"
# export BUILDKITE_REPO="https://github.com/rails/rails.git"
# export BUILDKITE_BUILD_NUMBER="60"
# export BUILDKITE_BUILD_URL="https://buildkite.com/rails/docs-preview/builds/60"
# export BUILDKITE_BRANCH="preview_docs"
# export BUILDKITE_MESSAGE="commit message"
# export BUILDKITE_PULL_REQUEST="42"
# bundle exec rake preview_docs
# open preview/index.html
class PreviewDocs
attr_reader :commit, :author, :build, :repo, :branch
def initialize
@commit = link_to(EnvVars.sha[0, 7], "#{EnvVars.repo}/commit/#{EnvVars.sha}")
@author = EnvVars.actor
@build = link_to(EnvVars.build_number, EnvVars.build_url)
@repo = link_to(EnvVars.repo_slug, "#{EnvVars.repo}")
@branch = link_to(EnvVars.branch, "#{EnvVars.repo}/tree/#{EnvVars.branch}")
@message = EnvVars.message || "n/a"
@pull_request = EnvVars.pull_request ? link_to("##{EnvVars.pull_request}", "#{EnvVars.repo}/pull/#{EnvVars.pull_request}") : "n/a"
end
def render(outdir)
template = File.open("tools/preview_docs/index.html.erb").read
result = ERB.new(template).result(binding)
File.open("#{outdir}/index.html", "w") do |f|
f.write result
end
end
def link_to(name, url)
"<a href=\"#{escape(url)}\">#{escape(name)}</a>"
end
def escape(str)
CGI.escapeHTML(str)
end
end
module EnvVars
def self.sha
fetch "BUILDKITE_COMMIT"
end
def self.actor
fetch "BUILDKITE_BUILD_CREATOR"
end
def self.repo
fetch("BUILDKITE_REPO").gsub(".git", "")
end
def self.repo_slug
repo.slice(/\w+\/\w+\Z/)
end
def self.build_number
fetch "BUILDKITE_BUILD_NUMBER"
end
def self.build_url
fetch "BUILDKITE_BUILD_URL"
end
def self.branch
fetch "BUILDKITE_BRANCH"
end
def self.message
ENV.fetch "BUILDKITE_MESSAGE"
end
def self.pull_request
pr = ENV.fetch("BUILDKITE_PULL_REQUEST")
pr == "false" ? false : pr
end
private
def self.fetch(env)
ENV.fetch(env) { raise "#{env} env var undefined!" }
end
end

@ -0,0 +1,109 @@
<!doctype html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8" />
<style>
body {
font-family: monospace;
& a {
color: #D30001;
}
}
.layout {
width: 500px;
margin: 0 auto;
}
.metadata table {
width: 500px;
border-bottom: 1px dotted #000;
padding-bottom: 15px;
margin-bottom: 15px;
& th {
text-align: left;
}
}
.links {
& .left {
float: left;
width: 40%;
margin: 2px 3%;
}
& .card {
padding: 0px 5px;
border-left: 3px solid #cbcb;
}
& h1, h2 {
margin: 5px;
}
}
</style>
</head>
<body>
<div class="layout">
<div class="container">
<div class="metadata">
<table>
<tr>
<th>Author</th>
<th>Build</th>
<th>Commit</th>
<th>PR</th>
</tr>
<tr>
<td><%= @author %></td>
<td><%= @build %></td>
<td><%= @commit %></td>
<td><%= @pull_request %></td>
<tr>
<th>Repo</th>
<th colspan=2>Branch</th>
</tr>
<tr>
<td><%= @repo %></td>
<td colspan=2><%= @branch %></td>
</tr>
<tr>
<th colspan=3>Message</th>
</tr>
<tr>
<td colspan=3><%= @message %></td>
</tr>
</table>
</div>
</div>
<div class="container">
<div class="links">
<div class="left">
<div class="card">
<h1><a href="api">/api</a></h1>
<h2>Reference documentation</h2>
</div>
</div>
<div class="left">
<div class="card">
<h1><a href="guides">/guides</a></h1>
<h2>In-depth tutorials</h2>
</div>
</div>
</div>
</div>
</div>
</body>
</html>